Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / usb / serial / mos7840.c
blob190af7d42aa7fe1bee7e46c2381a37934b37a343
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.
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.
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
16 * Clean ups from Moschip version and a few ioctl implementations by:
17 * Paul B Schroeder <pschroeder "at" uplogix "dot" com>
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>
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>
39 * Version Information
41 #define DRIVER_VERSION "1.3.1"
42 #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
45 * 16C50 UART register defines
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 */
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 */
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 */
66 #define LCR_SET_BREAK 0x40 /* Set Break condition */
67 #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
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 */
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 */
82 * Defines used for sending commands to port
85 #define WAIT_FOR_EVER (HZ * 0 ) /* timeout urb is wait for ever */
86 #define MOS_WDR_TIMEOUT (HZ * 5 ) /* default urb timeout */
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
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)
103 #define MAX_NAME_LEN 64
105 #define ZLP_REG1 0x3A //Zero_Flag_Reg1 58
106 #define ZLP_REG5 0x3E //Zero_Flag_Reg5 62
108 /* For higher baud Rates use TIOCEXBAUD */
109 #define TIOCEXBAUD 0x5462
111 /* vendor id and device id defines */
113 <<<<<<< HEAD:drivers/usb/serial/mos7840.c
114 =======
115 /* The native mos7840/7820 component */
116 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/usb/serial/mos7840.c
117 #define USB_VENDOR_ID_MOSCHIP 0x9710
118 #define MOSCHIP_DEVICE_ID_7840 0x7840
119 #define MOSCHIP_DEVICE_ID_7820 0x7820
120 <<<<<<< HEAD:drivers/usb/serial/mos7840.c
121 =======
122 /* The native component can have its vendor/device id's overridden
123 * in vendor-specific implementations. Such devices can be handled
124 * by making a change here, in moschip_port_id_table, and in
125 * moschip_id_table_combined
127 #define USB_VENDOR_ID_BANDB 0x0856
128 #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
129 #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
130 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/usb/serial/mos7840.c
132 <<<<<<< HEAD:drivers/usb/serial/mos7840.c
133 /* Interrupt Rotinue Defines */
134 =======
135 /* Interrupt Routine Defines */
136 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/usb/serial/mos7840.c
138 #define SERIAL_IIR_RLS 0x06
139 #define SERIAL_IIR_MS 0x00
142 * Emulation of the bit mask on the LINE STATUS REGISTER.
144 #define SERIAL_LSR_DR 0x0001
145 #define SERIAL_LSR_OE 0x0002
146 #define SERIAL_LSR_PE 0x0004
147 #define SERIAL_LSR_FE 0x0008
148 #define SERIAL_LSR_BI 0x0010
150 #define MOS_MSR_DELTA_CTS 0x10
151 #define MOS_MSR_DELTA_DSR 0x20
152 #define MOS_MSR_DELTA_RI 0x40
153 #define MOS_MSR_DELTA_CD 0x80
155 // Serial Port register Address
156 #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
157 #define FIFO_CONTROL_REGISTER ((__u16)(0x02))
158 #define LINE_CONTROL_REGISTER ((__u16)(0x03))
159 #define MODEM_CONTROL_REGISTER ((__u16)(0x04))
160 #define LINE_STATUS_REGISTER ((__u16)(0x05))
161 #define MODEM_STATUS_REGISTER ((__u16)(0x06))
162 #define SCRATCH_PAD_REGISTER ((__u16)(0x07))
163 #define DIVISOR_LATCH_LSB ((__u16)(0x00))
164 #define DIVISOR_LATCH_MSB ((__u16)(0x01))
166 #define CLK_MULTI_REGISTER ((__u16)(0x02))
167 #define CLK_START_VALUE_REGISTER ((__u16)(0x03))
169 #define SERIAL_LCR_DLAB ((__u16)(0x0080))
172 * URB POOL related defines
174 #define NUM_URBS 16 /* URB Count */
175 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
178 static struct usb_device_id moschip_port_id_table[] = {
179 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
180 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
181 <<<<<<< HEAD:drivers/usb/serial/mos7840.c
182 =======
183 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
184 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
185 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/usb/serial/mos7840.c
186 {} /* terminating entry */
189 static __devinitdata struct usb_device_id moschip_id_table_combined[] = {
190 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
191 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
192 <<<<<<< HEAD:drivers/usb/serial/mos7840.c
193 =======
194 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
195 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
196 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/usb/serial/mos7840.c
197 {} /* terminating entry */
200 MODULE_DEVICE_TABLE(usb, moschip_id_table_combined);
202 /* This structure holds all of the local port information */
204 struct moschip_port {
205 int port_num; /*Actual port number in the device(1,2,etc) */
206 struct urb *write_urb; /* write URB for this port */
207 struct urb *read_urb; /* read URB for this port */
208 struct urb *int_urb;
209 __u8 shadowLCR; /* last LCR value received */
210 __u8 shadowMCR; /* last MCR value received */
211 char open;
212 char open_ports;
213 char zombie;
214 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
215 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
216 int delta_msr_cond;
217 struct async_icount icount;
218 struct usb_serial_port *port; /* loop back to the owner of this object */
220 /*Offsets */
221 __u8 SpRegOffset;
222 __u8 ControlRegOffset;
223 __u8 DcrRegOffset;
224 //for processing control URBS in interrupt context
225 struct urb *control_urb;
226 struct usb_ctrlrequest *dr;
227 char *ctrl_buf;
228 int MsrLsr;
230 spinlock_t pool_lock;
231 struct urb *write_urb_pool[NUM_URBS];
232 char busy[NUM_URBS];
236 static int debug;
239 * mos7840_set_reg_sync
240 * To set the Control register by calling usb_fill_control_urb function
241 * by passing usb_sndctrlpipe function as parameter.
244 static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
245 __u16 val)
247 struct usb_device *dev = port->serial->dev;
248 val = val & 0x00ff;
249 dbg("mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
251 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
252 MCS_WR_RTYPE, val, reg, NULL, 0,
253 MOS_WDR_TIMEOUT);
257 * mos7840_get_reg_sync
258 * To set the Uart register by calling usb_fill_control_urb function by
259 * passing usb_rcvctrlpipe function as parameter.
262 static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
263 __u16 * val)
265 struct usb_device *dev = port->serial->dev;
266 int ret = 0;
268 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
269 MCS_RD_RTYPE, 0, reg, val, VENDOR_READ_LENGTH,
270 MOS_WDR_TIMEOUT);
271 dbg("mos7840_get_reg_sync offset is %x, return val %x\n", reg, *val);
272 *val = (*val) & 0x00ff;
273 return ret;
277 * mos7840_set_uart_reg
278 * To set the Uart register by calling usb_fill_control_urb function by
279 * passing usb_sndctrlpipe function as parameter.
282 static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
283 __u16 val)
286 struct usb_device *dev = port->serial->dev;
287 val = val & 0x00ff;
288 // For the UART control registers, the application number need to be Or'ed
289 if (port->serial->num_ports == 4) {
290 val |=
291 (((__u16) port->number - (__u16) (port->serial->minor)) +
292 1) << 8;
293 dbg("mos7840_set_uart_reg application number is %x\n", val);
294 } else {
295 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
296 val |=
297 (((__u16) port->number -
298 (__u16) (port->serial->minor)) + 1) << 8;
299 dbg("mos7840_set_uart_reg application number is %x\n",
300 val);
301 } else {
302 val |=
303 (((__u16) port->number -
304 (__u16) (port->serial->minor)) + 2) << 8;
305 dbg("mos7840_set_uart_reg application number is %x\n",
306 val);
309 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
310 MCS_WR_RTYPE, val, reg, NULL, 0,
311 MOS_WDR_TIMEOUT);
316 * mos7840_get_uart_reg
317 * To set the Control register by calling usb_fill_control_urb function
318 * by passing usb_rcvctrlpipe function as parameter.
320 static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
321 __u16 * val)
323 struct usb_device *dev = port->serial->dev;
324 int ret = 0;
325 __u16 Wval;
327 //dbg("application number is %4x \n",(((__u16)port->number - (__u16)(port->serial->minor))+1)<<8);
328 /*Wval is same as application number */
329 if (port->serial->num_ports == 4) {
330 Wval =
331 (((__u16) port->number - (__u16) (port->serial->minor)) +
332 1) << 8;
333 dbg("mos7840_get_uart_reg application number is %x\n", Wval);
334 } else {
335 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
336 Wval =
337 (((__u16) port->number -
338 (__u16) (port->serial->minor)) + 1) << 8;
339 dbg("mos7840_get_uart_reg application number is %x\n",
340 Wval);
341 } else {
342 Wval =
343 (((__u16) port->number -
344 (__u16) (port->serial->minor)) + 2) << 8;
345 dbg("mos7840_get_uart_reg application number is %x\n",
346 Wval);
349 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
350 MCS_RD_RTYPE, Wval, reg, val, VENDOR_READ_LENGTH,
351 MOS_WDR_TIMEOUT);
352 *val = (*val) & 0x00ff;
353 return ret;
356 static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
359 dbg("***************************************\n");
360 dbg("SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
361 dbg("ControlRegOffset is %2x \n", mos7840_port->ControlRegOffset);
362 dbg("DCRRegOffset is %2x \n", mos7840_port->DcrRegOffset);
363 dbg("***************************************\n");
367 /************************************************************************/
368 /************************************************************************/
369 /* I N T E R F A C E F U N C T I O N S */
370 /* I N T E R F A C E F U N C T I O N S */
371 /************************************************************************/
372 /************************************************************************/
374 static inline void mos7840_set_port_private(struct usb_serial_port *port,
375 struct moschip_port *data)
377 usb_set_serial_port_data(port, (void *)data);
380 static inline struct moschip_port *mos7840_get_port_private(struct
381 usb_serial_port
382 *port)
384 return (struct moschip_port *)usb_get_serial_port_data(port);
387 static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
389 struct moschip_port *mos7840_port;
390 struct async_icount *icount;
391 mos7840_port = port;
392 icount = &mos7840_port->icount;
393 if (new_msr &
394 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
395 MOS_MSR_DELTA_CD)) {
396 icount = &mos7840_port->icount;
398 /* update input line counters */
399 if (new_msr & MOS_MSR_DELTA_CTS) {
400 icount->cts++;
401 smp_wmb();
403 if (new_msr & MOS_MSR_DELTA_DSR) {
404 icount->dsr++;
405 smp_wmb();
407 if (new_msr & MOS_MSR_DELTA_CD) {
408 icount->dcd++;
409 smp_wmb();
411 if (new_msr & MOS_MSR_DELTA_RI) {
412 icount->rng++;
413 smp_wmb();
418 static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
420 struct async_icount *icount;
422 dbg("%s - %02x", __FUNCTION__, new_lsr);
424 if (new_lsr & SERIAL_LSR_BI) {
426 // Parity and Framing errors only count if they
427 // occur exclusive of a break being
428 // received.
430 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
433 /* update input line counters */
434 icount = &port->icount;
435 if (new_lsr & SERIAL_LSR_BI) {
436 icount->brk++;
437 smp_wmb();
439 if (new_lsr & SERIAL_LSR_OE) {
440 icount->overrun++;
441 smp_wmb();
443 if (new_lsr & SERIAL_LSR_PE) {
444 icount->parity++;
445 smp_wmb();
447 if (new_lsr & SERIAL_LSR_FE) {
448 icount->frame++;
449 smp_wmb();
453 /************************************************************************/
454 /************************************************************************/
455 /* U S B C A L L B A C K F U N C T I O N S */
456 /* U S B C A L L B A C K F U N C T I O N S */
457 /************************************************************************/
458 /************************************************************************/
460 static void mos7840_control_callback(struct urb *urb)
462 unsigned char *data;
463 struct moschip_port *mos7840_port;
464 __u8 regval = 0x0;
465 int result = 0;
466 int status = urb->status;
468 mos7840_port = (struct moschip_port *)urb->context;
470 switch (status) {
471 case 0:
472 /* success */
473 break;
474 case -ECONNRESET:
475 case -ENOENT:
476 case -ESHUTDOWN:
477 /* this urb is terminated, clean up */
478 dbg("%s - urb shutting down with status: %d", __FUNCTION__,
479 status);
480 return;
481 default:
482 dbg("%s - nonzero urb status received: %d", __FUNCTION__,
483 status);
484 goto exit;
487 dbg("%s urb buffer size is %d\n", __FUNCTION__, urb->actual_length);
488 dbg("%s mos7840_port->MsrLsr is %d port %d\n", __FUNCTION__,
489 mos7840_port->MsrLsr, mos7840_port->port_num);
490 data = urb->transfer_buffer;
491 regval = (__u8) data[0];
492 dbg("%s data is %x\n", __FUNCTION__, regval);
493 if (mos7840_port->MsrLsr == 0)
494 mos7840_handle_new_msr(mos7840_port, regval);
495 else if (mos7840_port->MsrLsr == 1)
496 mos7840_handle_new_lsr(mos7840_port, regval);
498 exit:
499 spin_lock(&mos7840_port->pool_lock);
500 if (!mos7840_port->zombie)
501 result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
502 spin_unlock(&mos7840_port->pool_lock);
503 if (result) {
504 dev_err(&urb->dev->dev,
505 "%s - Error %d submitting interrupt urb\n",
506 __FUNCTION__, result);
510 static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
511 __u16 * val)
513 struct usb_device *dev = mcs->port->serial->dev;
514 struct usb_ctrlrequest *dr = mcs->dr;
515 unsigned char *buffer = mcs->ctrl_buf;
516 int ret;
518 dr->bRequestType = MCS_RD_RTYPE;
519 dr->bRequest = MCS_RDREQ;
520 dr->wValue = cpu_to_le16(Wval); //0;
521 dr->wIndex = cpu_to_le16(reg);
522 dr->wLength = cpu_to_le16(2);
524 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
525 (unsigned char *)dr, buffer, 2,
526 mos7840_control_callback, mcs);
527 mcs->control_urb->transfer_buffer_length = 2;
528 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
529 return ret;
532 /*****************************************************************************
533 * mos7840_interrupt_callback
534 * this is the callback function for when we have received data on the
535 * interrupt endpoint.
536 *****************************************************************************/
538 static void mos7840_interrupt_callback(struct urb *urb)
540 int result;
541 int length;
542 struct moschip_port *mos7840_port;
543 struct usb_serial *serial;
544 __u16 Data;
545 unsigned char *data;
546 __u8 sp[5], st;
547 int i, rv = 0;
548 __u16 wval, wreg = 0;
549 int status = urb->status;
551 dbg("%s", " : Entering\n");
553 switch (status) {
554 case 0:
555 /* success */
556 break;
557 case -ECONNRESET:
558 case -ENOENT:
559 case -ESHUTDOWN:
560 /* this urb is terminated, clean up */
561 dbg("%s - urb shutting down with status: %d", __FUNCTION__,
562 status);
563 return;
564 default:
565 dbg("%s - nonzero urb status received: %d", __FUNCTION__,
566 status);
567 goto exit;
570 length = urb->actual_length;
571 data = urb->transfer_buffer;
573 serial = (struct usb_serial *)urb->context;
575 /* Moschip get 5 bytes
576 * Byte 1 IIR Port 1 (port.number is 0)
577 * Byte 2 IIR Port 2 (port.number is 1)
578 * Byte 3 IIR Port 3 (port.number is 2)
579 * Byte 4 IIR Port 4 (port.number is 3)
580 * Byte 5 FIFO status for both */
582 if (length && length > 5) {
583 dbg("%s \n", "Wrong data !!!");
584 return;
587 sp[0] = (__u8) data[0];
588 sp[1] = (__u8) data[1];
589 sp[2] = (__u8) data[2];
590 sp[3] = (__u8) data[3];
591 st = (__u8) data[4];
593 for (i = 0; i < serial->num_ports; i++) {
594 mos7840_port = mos7840_get_port_private(serial->port[i]);
595 wval =
596 (((__u16) serial->port[i]->number -
597 (__u16) (serial->minor)) + 1) << 8;
598 if (mos7840_port->open) {
599 if (sp[i] & 0x01) {
600 dbg("SP%d No Interrupt !!!\n", i);
601 } else {
602 switch (sp[i] & 0x0f) {
603 case SERIAL_IIR_RLS:
604 dbg("Serial Port %d: Receiver status error or ", i);
605 dbg("address bit detected in 9-bit mode\n");
606 mos7840_port->MsrLsr = 1;
607 wreg = LINE_STATUS_REGISTER;
608 break;
609 case SERIAL_IIR_MS:
610 dbg("Serial Port %d: Modem status change\n", i);
611 mos7840_port->MsrLsr = 0;
612 wreg = MODEM_STATUS_REGISTER;
613 break;
615 spin_lock(&mos7840_port->pool_lock);
616 if (!mos7840_port->zombie) {
617 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
618 } else {
619 spin_unlock(&mos7840_port->pool_lock);
620 return;
622 spin_unlock(&mos7840_port->pool_lock);
626 if (!(rv < 0)) /* the completion handler for the control urb will resubmit */
627 return;
628 exit:
629 result = usb_submit_urb(urb, GFP_ATOMIC);
630 if (result) {
631 dev_err(&urb->dev->dev,
632 "%s - Error %d submitting interrupt urb\n",
633 __FUNCTION__, result);
637 static int mos7840_port_paranoia_check(struct usb_serial_port *port,
638 const char *function)
640 if (!port) {
641 dbg("%s - port == NULL", function);
642 return -1;
644 if (!port->serial) {
645 dbg("%s - port->serial == NULL", function);
646 return -1;
649 return 0;
652 /* Inline functions to check the sanity of a pointer that is passed to us */
653 static int mos7840_serial_paranoia_check(struct usb_serial *serial,
654 const char *function)
656 if (!serial) {
657 dbg("%s - serial == NULL", function);
658 return -1;
660 if (!serial->type) {
661 dbg("%s - serial->type == NULL!", function);
662 return -1;
665 return 0;
668 static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
669 const char *function)
671 /* if no port was specified, or it fails a paranoia check */
672 if (!port ||
673 mos7840_port_paranoia_check(port, function) ||
674 mos7840_serial_paranoia_check(port->serial, function)) {
675 /* then say that we don't have a valid usb_serial thing, which will
676 * end up genrating -ENODEV return values */
677 return NULL;
680 return port->serial;
683 /*****************************************************************************
684 * mos7840_bulk_in_callback
685 * this is the callback function for when we have received data on the
686 * bulk in endpoint.
687 *****************************************************************************/
689 static void mos7840_bulk_in_callback(struct urb *urb)
691 int retval;
692 unsigned char *data;
693 struct usb_serial *serial;
694 struct usb_serial_port *port;
695 struct moschip_port *mos7840_port;
696 struct tty_struct *tty;
697 int status = urb->status;
699 if (status) {
700 dbg("nonzero read bulk status received: %d", status);
701 return;
704 mos7840_port = (struct moschip_port *)urb->context;
705 if (!mos7840_port) {
706 dbg("%s", "NULL mos7840_port pointer \n");
707 return;
710 port = (struct usb_serial_port *)mos7840_port->port;
711 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
712 dbg("%s", "Port Paranoia failed \n");
713 return;
716 serial = mos7840_get_usb_serial(port, __FUNCTION__);
717 if (!serial) {
718 dbg("%s\n", "Bad serial pointer ");
719 return;
722 dbg("%s\n", "Entering... \n");
724 data = urb->transfer_buffer;
726 dbg("%s", "Entering ........... \n");
728 if (urb->actual_length) {
729 tty = mos7840_port->port->tty;
730 if (tty) {
731 tty_buffer_request_room(tty, urb->actual_length);
732 tty_insert_flip_string(tty, data, urb->actual_length);
733 dbg(" %s \n", data);
734 tty_flip_buffer_push(tty);
736 mos7840_port->icount.rx += urb->actual_length;
737 smp_wmb();
738 dbg("mos7840_port->icount.rx is %d:\n",
739 mos7840_port->icount.rx);
742 if (!mos7840_port->read_urb) {
743 dbg("%s", "URB KILLED !!!\n");
744 return;
748 mos7840_port->read_urb->dev = serial->dev;
750 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
752 if (retval) {
753 dbg(" usb_submit_urb(read bulk) failed, retval = %d",
754 retval);
758 /*****************************************************************************
759 * mos7840_bulk_out_data_callback
760 * this is the callback function for when we have finished sending serial data
761 * on the bulk out endpoint.
762 *****************************************************************************/
764 static void mos7840_bulk_out_data_callback(struct urb *urb)
766 struct moschip_port *mos7840_port;
767 struct tty_struct *tty;
768 int status = urb->status;
769 int i;
771 mos7840_port = (struct moschip_port *)urb->context;
772 spin_lock(&mos7840_port->pool_lock);
773 for (i = 0; i < NUM_URBS; i++) {
774 if (urb == mos7840_port->write_urb_pool[i]) {
775 mos7840_port->busy[i] = 0;
776 break;
779 spin_unlock(&mos7840_port->pool_lock);
781 if (status) {
782 dbg("nonzero write bulk status received:%d\n", status);
783 return;
786 if (mos7840_port_paranoia_check(mos7840_port->port, __FUNCTION__)) {
787 dbg("%s", "Port Paranoia failed \n");
788 return;
791 dbg("%s \n", "Entering .........");
793 tty = mos7840_port->port->tty;
795 if (tty && mos7840_port->open)
796 tty_wakeup(tty);
800 /************************************************************************/
801 /* 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 */
802 /************************************************************************/
803 #ifdef MCSSerialProbe
804 static int mos7840_serial_probe(struct usb_serial *serial,
805 const struct usb_device_id *id)
808 /*need to implement the mode_reg reading and updating\
809 structures usb_serial_ device_type\
810 (i.e num_ports, num_bulkin,bulkout etc) */
811 /* Also we can update the changes attach */
812 return 1;
814 #endif
816 /*****************************************************************************
817 * mos7840_open
818 * this function is called by the tty driver when a port is opened
819 * If successful, we return 0
820 * Otherwise we return a negative error number.
821 *****************************************************************************/
823 static int mos7840_open(struct usb_serial_port *port, struct file *filp)
825 int response;
826 int j;
827 struct usb_serial *serial;
828 struct urb *urb;
829 __u16 Data;
830 int status;
831 struct moschip_port *mos7840_port;
832 struct moschip_port *port0;
834 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
835 dbg("%s", "Port Paranoia failed \n");
836 return -ENODEV;
839 serial = port->serial;
841 if (mos7840_serial_paranoia_check(serial, __FUNCTION__)) {
842 dbg("%s", "Serial Paranoia failed \n");
843 return -ENODEV;
846 mos7840_port = mos7840_get_port_private(port);
847 port0 = mos7840_get_port_private(serial->port[0]);
849 if (mos7840_port == NULL || port0 == NULL)
850 return -ENODEV;
852 usb_clear_halt(serial->dev, port->write_urb->pipe);
853 usb_clear_halt(serial->dev, port->read_urb->pipe);
854 port0->open_ports++;
856 /* Initialising the write urb pool */
857 for (j = 0; j < NUM_URBS; ++j) {
858 urb = usb_alloc_urb(0, GFP_KERNEL);
859 mos7840_port->write_urb_pool[j] = urb;
861 if (urb == NULL) {
862 err("No more urbs???");
863 continue;
866 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
867 if (!urb->transfer_buffer) {
868 usb_free_urb(urb);
869 mos7840_port->write_urb_pool[j] = NULL;
870 err("%s-out of memory for urb buffers.", __FUNCTION__);
871 continue;
875 /*****************************************************************************
876 * Initialize MCS7840 -- Write Init values to corresponding Registers
878 * Register Index
879 * 1 : IER
880 * 2 : FCR
881 * 3 : LCR
882 * 4 : MCR
884 * 0x08 : SP1/2 Control Reg
885 *****************************************************************************/
887 //NEED to check the following Block
889 status = 0;
890 Data = 0x0;
891 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
892 if (status < 0) {
893 dbg("Reading Spreg failed\n");
894 return -1;
896 Data |= 0x80;
897 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
898 if (status < 0) {
899 dbg("writing Spreg failed\n");
900 return -1;
903 Data &= ~0x80;
904 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
905 if (status < 0) {
906 dbg("writing Spreg failed\n");
907 return -1;
909 //End of block to be checked
911 status = 0;
912 Data = 0x0;
913 status =
914 mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
915 if (status < 0) {
916 dbg("Reading Controlreg failed\n");
917 return -1;
919 Data |= 0x08; //Driver done bit
920 Data |= 0x20; //rx_disable
921 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
922 if (status < 0) {
923 dbg("writing Controlreg failed\n");
924 return -1;
926 //do register settings here
927 // Set all regs to the device default values.
928 ////////////////////////////////////
929 // First Disable all interrupts.
930 ////////////////////////////////////
932 Data = 0x00;
933 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
934 if (status < 0) {
935 dbg("disableing interrupts failed\n");
936 return -1;
938 // Set FIFO_CONTROL_REGISTER to the default value
939 Data = 0x00;
940 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
941 if (status < 0) {
942 dbg("Writing FIFO_CONTROL_REGISTER failed\n");
943 return -1;
946 Data = 0xcf;
947 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
948 if (status < 0) {
949 dbg("Writing FIFO_CONTROL_REGISTER failed\n");
950 return -1;
953 Data = 0x03;
954 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
955 mos7840_port->shadowLCR = Data;
957 Data = 0x0b;
958 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
959 mos7840_port->shadowMCR = Data;
961 Data = 0x00;
962 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
963 mos7840_port->shadowLCR = Data;
965 Data |= SERIAL_LCR_DLAB; //data latch enable in LCR 0x80
966 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
968 Data = 0x0c;
969 status = 0;
970 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
972 Data = 0x0;
973 status = 0;
974 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
976 Data = 0x00;
977 status = 0;
978 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
980 Data = Data & ~SERIAL_LCR_DLAB;
981 status = 0;
982 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
983 mos7840_port->shadowLCR = Data;
985 //clearing Bulkin and Bulkout Fifo
986 Data = 0x0;
987 status = 0;
988 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
990 Data = Data | 0x0c;
991 status = 0;
992 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
994 Data = Data & ~0x0c;
995 status = 0;
996 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
997 //Finally enable all interrupts
998 Data = 0x0;
999 Data = 0x0c;
1000 status = 0;
1001 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1003 //clearing rx_disable
1004 Data = 0x0;
1005 status = 0;
1006 status =
1007 mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
1008 Data = Data & ~0x20;
1009 status = 0;
1010 status =
1011 mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
1013 // rx_negate
1014 Data = 0x0;
1015 status = 0;
1016 status =
1017 mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
1018 Data = Data | 0x10;
1019 status = 0;
1020 status =
1021 mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
1023 /* force low_latency on so that our tty_push actually forces *
1024 * the data through,otherwise it is scheduled, and with *
1025 * high data rates (like with OHCI) data can get lost. */
1027 if (port->tty)
1028 port->tty->low_latency = 1;
1029 /* Check to see if we've set up our endpoint info yet *
1030 * (can't set it up in mos7840_startup as the structures *
1031 * were not set up at that time.) */
1032 if (port0->open_ports == 1) {
1033 if (serial->port[0]->interrupt_in_buffer == NULL) {
1035 /* set up interrupt urb */
1037 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
1038 serial->dev,
1039 usb_rcvintpipe(serial->dev,
1040 serial->port[0]->
1041 interrupt_in_endpointAddress),
1042 serial->port[0]->interrupt_in_buffer,
1043 serial->port[0]->interrupt_in_urb->
1044 transfer_buffer_length,
1045 mos7840_interrupt_callback,
1046 serial,
1047 serial->port[0]->interrupt_in_urb->
1048 interval);
1050 /* start interrupt read for mos7840 *
1051 * will continue as long as mos7840 is connected */
1053 response =
1054 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1055 GFP_KERNEL);
1056 if (response) {
1057 err("%s - Error %d submitting interrupt urb",
1058 __FUNCTION__, response);
1065 /* see if we've set up our endpoint info yet *
1066 * (can't set it up in mos7840_startup as the *
1067 * structures were not set up at that time.) */
1069 dbg("port number is %d \n", port->number);
1070 dbg("serial number is %d \n", port->serial->minor);
1071 dbg("Bulkin endpoint is %d \n", port->bulk_in_endpointAddress);
1072 dbg("BulkOut endpoint is %d \n", port->bulk_out_endpointAddress);
1073 dbg("Interrupt endpoint is %d \n", port->interrupt_in_endpointAddress);
1074 dbg("port's number in the device is %d\n", mos7840_port->port_num);
1075 mos7840_port->read_urb = port->read_urb;
1077 /* set up our bulk in urb */
1079 usb_fill_bulk_urb(mos7840_port->read_urb,
1080 serial->dev,
1081 usb_rcvbulkpipe(serial->dev,
1082 port->bulk_in_endpointAddress),
1083 port->bulk_in_buffer,
1084 mos7840_port->read_urb->transfer_buffer_length,
1085 mos7840_bulk_in_callback, mos7840_port);
1087 dbg("mos7840_open: bulkin endpoint is %d\n",
1088 port->bulk_in_endpointAddress);
1089 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1090 if (response) {
1091 err("%s - Error %d submitting control urb", __FUNCTION__,
1092 response);
1095 /* initialize our wait queues */
1096 init_waitqueue_head(&mos7840_port->wait_chase);
1097 init_waitqueue_head(&mos7840_port->delta_msr_wait);
1099 /* initialize our icount structure */
1100 memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
1102 /* initialize our port settings */
1103 mos7840_port->shadowMCR = MCR_MASTER_IE; /* Must set to enable ints! */
1104 /* send a open port command */
1105 mos7840_port->open = 1;
1106 //mos7840_change_port_settings(mos7840_port,old_termios);
1107 mos7840_port->icount.tx = 0;
1108 mos7840_port->icount.rx = 0;
1110 dbg("\n\nusb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p\n\n", serial, mos7840_port, port);
1112 return 0;
1116 /*****************************************************************************
1117 * mos7840_chars_in_buffer
1118 * this function is called by the tty driver when it wants to know how many
1119 * bytes of data we currently have outstanding in the port (data that has
1120 * been written, but hasn't made it out the port yet)
1121 * If successful, we return the number of bytes left to be written in the
1122 * system,
1123 * Otherwise we return a negative error number.
1124 *****************************************************************************/
1126 static int mos7840_chars_in_buffer(struct usb_serial_port *port)
1128 int i;
1129 int chars = 0;
1130 unsigned long flags;
1131 struct moschip_port *mos7840_port;
1133 dbg("%s \n", " mos7840_chars_in_buffer:entering ...........");
1135 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1136 dbg("%s", "Invalid port \n");
1137 return -1;
1140 mos7840_port = mos7840_get_port_private(port);
1141 if (mos7840_port == NULL) {
1142 dbg("%s \n", "mos7840_break:leaving ...........");
1143 return -1;
1146 spin_lock_irqsave(&mos7840_port->pool_lock,flags);
1147 for (i = 0; i < NUM_URBS; ++i) {
1148 if (mos7840_port->busy[i]) {
1149 chars += URB_TRANSFER_BUFFER_SIZE;
1152 spin_unlock_irqrestore(&mos7840_port->pool_lock,flags);
1153 dbg("%s - returns %d", __FUNCTION__, chars);
1154 return chars;
1158 /************************************************************************
1160 * mos7840_block_until_tx_empty
1162 * This function will block the close until one of the following:
1163 * 1. TX count are 0
1164 * 2. The mos7840 has stopped
1165 * 3. A timeout of 3 seconds without activity has expired
1167 ************************************************************************/
1168 static void mos7840_block_until_tx_empty(struct moschip_port *mos7840_port)
1170 int timeout = HZ / 10;
1171 int wait = 30;
1172 int count;
1174 while (1) {
1176 count = mos7840_chars_in_buffer(mos7840_port->port);
1178 /* Check for Buffer status */
1179 if (count <= 0) {
1180 return;
1183 /* Block the thread for a while */
1184 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1185 timeout);
1187 /* No activity.. count down section */
1188 wait--;
1189 if (wait == 0) {
1190 dbg("%s - TIMEOUT", __FUNCTION__);
1191 return;
1192 } else {
1193 /* Reset timeout value back to seconds */
1194 wait = 30;
1199 /*****************************************************************************
1200 * mos7840_close
1201 * this function is called by the tty driver when a port is closed
1202 *****************************************************************************/
1204 static void mos7840_close(struct usb_serial_port *port, struct file *filp)
1206 struct usb_serial *serial;
1207 struct moschip_port *mos7840_port;
1208 struct moschip_port *port0;
1209 int j;
1210 __u16 Data;
1212 dbg("%s\n", "mos7840_close:entering...");
1214 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1215 dbg("%s", "Port Paranoia failed \n");
1216 return;
1219 serial = mos7840_get_usb_serial(port, __FUNCTION__);
1220 if (!serial) {
1221 dbg("%s", "Serial Paranoia failed \n");
1222 return;
1225 mos7840_port = mos7840_get_port_private(port);
1226 port0 = mos7840_get_port_private(serial->port[0]);
1228 if (mos7840_port == NULL || port0 == NULL)
1229 return;
1231 for (j = 0; j < NUM_URBS; ++j)
1232 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1234 /* Freeing Write URBs */
1235 for (j = 0; j < NUM_URBS; ++j) {
1236 if (mos7840_port->write_urb_pool[j]) {
1237 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1238 kfree(mos7840_port->write_urb_pool[j]->
1239 transfer_buffer);
1241 usb_free_urb(mos7840_port->write_urb_pool[j]);
1245 if (serial->dev) {
1246 /* flush and block until tx is empty */
1247 mos7840_block_until_tx_empty(mos7840_port);
1250 /* While closing port, shutdown all bulk read, write *
1251 * and interrupt read if they exists */
1252 if (serial->dev) {
1254 if (mos7840_port->write_urb) {
1255 dbg("%s", "Shutdown bulk write\n");
1256 usb_kill_urb(mos7840_port->write_urb);
1259 if (mos7840_port->read_urb) {
1260 dbg("%s", "Shutdown bulk read\n");
1261 usb_kill_urb(mos7840_port->read_urb);
1263 if ((&mos7840_port->control_urb)) {
1264 dbg("%s", "Shutdown control read\n");
1265 // usb_kill_urb (mos7840_port->control_urb);
1269 // if(mos7840_port->ctrl_buf != NULL)
1270 // kfree(mos7840_port->ctrl_buf);
1271 port0->open_ports--;
1272 dbg("mos7840_num_open_ports in close%d:in port%d\n",
1273 port0->open_ports, port->number);
1274 if (port0->open_ports == 0) {
1275 if (serial->port[0]->interrupt_in_urb) {
1276 dbg("%s", "Shutdown interrupt_in_urb\n");
1277 usb_kill_urb(serial->port[0]->interrupt_in_urb);
1281 if (mos7840_port->write_urb) {
1282 /* if this urb had a transfer buffer already (old tx) free it */
1284 if (mos7840_port->write_urb->transfer_buffer != NULL) {
1285 kfree(mos7840_port->write_urb->transfer_buffer);
1287 usb_free_urb(mos7840_port->write_urb);
1290 Data = 0x0;
1291 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1293 Data = 0x00;
1294 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1296 mos7840_port->open = 0;
1298 dbg("%s \n", "Leaving ............");
1301 /************************************************************************
1303 * mos7840_block_until_chase_response
1305 * This function will block the close until one of the following:
1306 * 1. Response to our Chase comes from mos7840
1307 * 2. A timeout of 10 seconds without activity has expired
1308 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1310 ************************************************************************/
1312 static void mos7840_block_until_chase_response(struct moschip_port
1313 *mos7840_port)
1315 int timeout = 1 * HZ;
1316 int wait = 10;
1317 int count;
1319 while (1) {
1320 count = mos7840_chars_in_buffer(mos7840_port->port);
1322 /* Check for Buffer status */
1323 if (count <= 0) {
1324 return;
1327 /* Block the thread for a while */
1328 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1329 timeout);
1330 /* No activity.. count down section */
1331 wait--;
1332 if (wait == 0) {
1333 dbg("%s - TIMEOUT", __FUNCTION__);
1334 return;
1335 } else {
1336 /* Reset timeout value back to seconds */
1337 wait = 10;
1343 /*****************************************************************************
1344 * mos7840_break
1345 * this function sends a break to the port
1346 *****************************************************************************/
1347 static void mos7840_break(struct usb_serial_port *port, int break_state)
1349 unsigned char data;
1350 struct usb_serial *serial;
1351 struct moschip_port *mos7840_port;
1353 dbg("%s \n", "Entering ...........");
1354 dbg("mos7840_break: Start\n");
1356 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1357 dbg("%s", "Port Paranoia failed \n");
1358 return;
1361 serial = mos7840_get_usb_serial(port, __FUNCTION__);
1362 if (!serial) {
1363 dbg("%s", "Serial Paranoia failed \n");
1364 return;
1367 mos7840_port = mos7840_get_port_private(port);
1369 if (mos7840_port == NULL) {
1370 return;
1373 if (serial->dev) {
1375 /* flush and block until tx is empty */
1376 mos7840_block_until_chase_response(mos7840_port);
1379 if (break_state == -1) {
1380 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
1381 } else {
1382 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
1385 mos7840_port->shadowLCR = data;
1386 dbg("mcs7840_break mos7840_port->shadowLCR is %x\n",
1387 mos7840_port->shadowLCR);
1388 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1389 mos7840_port->shadowLCR);
1391 return;
1394 /*****************************************************************************
1395 * mos7840_write_room
1396 * this function is called by the tty driver when it wants to know how many
1397 * bytes of data we can accept for a specific port.
1398 * If successful, we return the amount of room that we have for this port
1399 * Otherwise we return a negative error number.
1400 *****************************************************************************/
1402 static int mos7840_write_room(struct usb_serial_port *port)
1404 int i;
1405 int room = 0;
1406 unsigned long flags;
1407 struct moschip_port *mos7840_port;
1409 dbg("%s \n", " mos7840_write_room:entering ...........");
1411 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1412 dbg("%s", "Invalid port \n");
1413 dbg("%s \n", " mos7840_write_room:leaving ...........");
1414 return -1;
1417 mos7840_port = mos7840_get_port_private(port);
1418 if (mos7840_port == NULL) {
1419 dbg("%s \n", "mos7840_break:leaving ...........");
1420 return -1;
1423 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1424 for (i = 0; i < NUM_URBS; ++i) {
1425 if (!mos7840_port->busy[i]) {
1426 room += URB_TRANSFER_BUFFER_SIZE;
1429 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1431 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
1432 dbg("%s - returns %d", __FUNCTION__, room);
1433 return room;
1437 /*****************************************************************************
1438 * mos7840_write
1439 * this function is called by the tty driver when data should be written to
1440 * the port.
1441 * If successful, we return the number of bytes written, otherwise we
1442 * return a negative error number.
1443 *****************************************************************************/
1445 static int mos7840_write(struct usb_serial_port *port,
1446 const unsigned char *data, int count)
1448 int status;
1449 int i;
1450 int bytes_sent = 0;
1451 int transfer_size;
1452 unsigned long flags;
1454 struct moschip_port *mos7840_port;
1455 struct usb_serial *serial;
1456 struct urb *urb;
1457 //__u16 Data;
1458 const unsigned char *current_position = data;
1459 unsigned char *data1;
1460 dbg("%s \n", "entering ...........");
1461 //dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",mos7840_port->shadowLCR);
1463 #ifdef NOTMOS7840
1464 Data = 0x00;
1465 status = 0;
1466 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1467 mos7840_port->shadowLCR = Data;
1468 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x\n", Data);
1469 dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
1470 mos7840_port->shadowLCR);
1472 //Data = 0x03;
1473 //status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data);
1474 //mos7840_port->shadowLCR=Data;//Need to add later
1476 Data |= SERIAL_LCR_DLAB; //data latch enable in LCR 0x80
1477 status = 0;
1478 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1480 //Data = 0x0c;
1481 //status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data);
1482 Data = 0x00;
1483 status = 0;
1484 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
1485 dbg("mos7840_write:DLL value is %x\n", Data);
1487 Data = 0x0;
1488 status = 0;
1489 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
1490 dbg("mos7840_write:DLM value is %x\n", Data);
1492 Data = Data & ~SERIAL_LCR_DLAB;
1493 dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
1494 mos7840_port->shadowLCR);
1495 status = 0;
1496 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1497 #endif
1499 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1500 dbg("%s", "Port Paranoia failed \n");
1501 return -1;
1504 serial = port->serial;
1505 if (mos7840_serial_paranoia_check(serial, __FUNCTION__)) {
1506 dbg("%s", "Serial Paranoia failed \n");
1507 return -1;
1510 mos7840_port = mos7840_get_port_private(port);
1511 if (mos7840_port == NULL) {
1512 dbg("%s", "mos7840_port is NULL\n");
1513 return -1;
1516 /* try to find a free urb in the list */
1517 urb = NULL;
1519 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1520 for (i = 0; i < NUM_URBS; ++i) {
1521 if (!mos7840_port->busy[i]) {
1522 mos7840_port->busy[i] = 1;
1523 urb = mos7840_port->write_urb_pool[i];
1524 dbg("\nURB:%d", i);
1525 break;
1528 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1530 if (urb == NULL) {
1531 dbg("%s - no more free urbs", __FUNCTION__);
1532 goto exit;
1535 if (urb->transfer_buffer == NULL) {
1536 urb->transfer_buffer =
1537 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1539 if (urb->transfer_buffer == NULL) {
1540 err("%s no more kernel memory...", __FUNCTION__);
1541 goto exit;
1544 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1546 memcpy(urb->transfer_buffer, current_position, transfer_size);
1548 /* fill urb with data and submit */
1549 usb_fill_bulk_urb(urb,
1550 serial->dev,
1551 usb_sndbulkpipe(serial->dev,
1552 port->bulk_out_endpointAddress),
1553 urb->transfer_buffer,
1554 transfer_size,
1555 mos7840_bulk_out_data_callback, mos7840_port);
1557 data1 = urb->transfer_buffer;
1558 dbg("\nbulkout endpoint is %d", port->bulk_out_endpointAddress);
1560 /* send it down the pipe */
1561 status = usb_submit_urb(urb, GFP_ATOMIC);
1563 if (status) {
1564 mos7840_port->busy[i] = 0;
1565 err("%s - usb_submit_urb(write bulk) failed with status = %d",
1566 __FUNCTION__, status);
1567 bytes_sent = status;
1568 goto exit;
1570 bytes_sent = transfer_size;
1571 mos7840_port->icount.tx += transfer_size;
1572 smp_wmb();
1573 dbg("mos7840_port->icount.tx is %d:\n", mos7840_port->icount.tx);
1574 exit:
1576 return bytes_sent;
1580 /*****************************************************************************
1581 * mos7840_throttle
1582 * this function is called by the tty driver when it wants to stop the data
1583 * being read from the port.
1584 *****************************************************************************/
1586 static void mos7840_throttle(struct usb_serial_port *port)
1588 struct moschip_port *mos7840_port;
1589 struct tty_struct *tty;
1590 int status;
1592 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1593 dbg("%s", "Invalid port \n");
1594 return;
1597 dbg("- port %d\n", port->number);
1599 mos7840_port = mos7840_get_port_private(port);
1601 if (mos7840_port == NULL)
1602 return;
1604 if (!mos7840_port->open) {
1605 dbg("%s\n", "port not opened");
1606 return;
1609 dbg("%s", "Entering .......... \n");
1611 tty = port->tty;
1612 if (!tty) {
1613 dbg("%s - no tty available", __FUNCTION__);
1614 return;
1617 /* if we are implementing XON/XOFF, send the stop character */
1618 if (I_IXOFF(tty)) {
1619 unsigned char stop_char = STOP_CHAR(tty);
1620 status = mos7840_write(port, &stop_char, 1);
1621 if (status <= 0) {
1622 return;
1626 /* if we are implementing RTS/CTS, toggle that line */
1627 if (tty->termios->c_cflag & CRTSCTS) {
1628 mos7840_port->shadowMCR &= ~MCR_RTS;
1629 status = 0;
1630 status =
1631 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1632 mos7840_port->shadowMCR);
1634 if (status < 0) {
1635 return;
1639 return;
1642 /*****************************************************************************
1643 * mos7840_unthrottle
1644 * this function is called by the tty driver when it wants to resume the data
1645 * being read from the port (called after SerialThrottle is called)
1646 *****************************************************************************/
1647 static void mos7840_unthrottle(struct usb_serial_port *port)
1649 struct tty_struct *tty;
1650 int status;
1651 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1653 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1654 dbg("%s", "Invalid port \n");
1655 return;
1658 if (mos7840_port == NULL)
1659 return;
1661 if (!mos7840_port->open) {
1662 dbg("%s - port not opened", __FUNCTION__);
1663 return;
1666 dbg("%s", "Entering .......... \n");
1668 tty = port->tty;
1669 if (!tty) {
1670 dbg("%s - no tty available", __FUNCTION__);
1671 return;
1674 /* if we are implementing XON/XOFF, send the start character */
1675 if (I_IXOFF(tty)) {
1676 unsigned char start_char = START_CHAR(tty);
1677 status = mos7840_write(port, &start_char, 1);
1678 if (status <= 0) {
1679 return;
1683 /* if we are implementing RTS/CTS, toggle that line */
1684 if (tty->termios->c_cflag & CRTSCTS) {
1685 mos7840_port->shadowMCR |= MCR_RTS;
1686 status = 0;
1687 status =
1688 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1689 mos7840_port->shadowMCR);
1690 if (status < 0) {
1691 return;
1695 return;
1698 static int mos7840_tiocmget(struct usb_serial_port *port, struct file *file)
1700 struct moschip_port *mos7840_port;
1701 unsigned int result;
1702 __u16 msr;
1703 __u16 mcr;
1704 int status = 0;
1705 mos7840_port = mos7840_get_port_private(port);
1707 dbg("%s - port %d", __FUNCTION__, port->number);
1709 if (mos7840_port == NULL)
1710 return -ENODEV;
1712 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1713 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1714 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1715 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1716 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1717 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1718 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1719 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1720 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1722 dbg("%s - 0x%04X", __FUNCTION__, result);
1724 return result;
1727 static int mos7840_tiocmset(struct usb_serial_port *port, struct file *file,
1728 unsigned int set, unsigned int clear)
1730 struct moschip_port *mos7840_port;
1731 unsigned int mcr;
1732 unsigned int status;
1734 dbg("%s - port %d", __FUNCTION__, port->number);
1736 mos7840_port = mos7840_get_port_private(port);
1738 if (mos7840_port == NULL)
1739 return -ENODEV;
1741 mcr = mos7840_port->shadowMCR;
1742 if (clear & TIOCM_RTS)
1743 mcr &= ~MCR_RTS;
1744 if (clear & TIOCM_DTR)
1745 mcr &= ~MCR_DTR;
1746 if (clear & TIOCM_LOOP)
1747 mcr &= ~MCR_LOOPBACK;
1749 if (set & TIOCM_RTS)
1750 mcr |= MCR_RTS;
1751 if (set & TIOCM_DTR)
1752 mcr |= MCR_DTR;
1753 if (set & TIOCM_LOOP)
1754 mcr |= MCR_LOOPBACK;
1756 mos7840_port->shadowMCR = mcr;
1758 status = 0;
1759 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1760 if (status < 0) {
1761 dbg("setting MODEM_CONTROL_REGISTER Failed\n");
1762 return -1;
1765 return 0;
1768 /*****************************************************************************
1769 * mos7840_calc_baud_rate_divisor
1770 * this function calculates the proper baud rate divisor for the specified
1771 * baud rate.
1772 *****************************************************************************/
1773 static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
1774 __u16 * clk_sel_val)
1777 dbg("%s - %d", __FUNCTION__, baudRate);
1779 if (baudRate <= 115200) {
1780 *divisor = 115200 / baudRate;
1781 *clk_sel_val = 0x0;
1783 if ((baudRate > 115200) && (baudRate <= 230400)) {
1784 *divisor = 230400 / baudRate;
1785 *clk_sel_val = 0x10;
1786 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1787 *divisor = 403200 / baudRate;
1788 *clk_sel_val = 0x20;
1789 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1790 *divisor = 460800 / baudRate;
1791 *clk_sel_val = 0x30;
1792 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1793 *divisor = 806400 / baudRate;
1794 *clk_sel_val = 0x40;
1795 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1796 *divisor = 921600 / baudRate;
1797 *clk_sel_val = 0x50;
1798 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1799 *divisor = 1572864 / baudRate;
1800 *clk_sel_val = 0x60;
1801 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1802 *divisor = 3145728 / baudRate;
1803 *clk_sel_val = 0x70;
1805 return 0;
1807 #ifdef NOTMCS7840
1809 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1810 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1811 *divisor = mos7840_divisor_table[i].Divisor;
1812 return 0;
1816 /* After trying for all the standard baud rates *
1817 * Try calculating the divisor for this baud rate */
1819 if (baudrate > 75 && baudrate < 230400) {
1820 /* get the divisor */
1821 custom = (__u16) (230400L / baudrate);
1823 /* Check for round off */
1824 round1 = (__u16) (2304000L / baudrate);
1825 round = (__u16) (round1 - (custom * 10));
1826 if (round > 4) {
1827 custom++;
1829 *divisor = custom;
1831 dbg(" Baud %d = %d\n", baudrate, custom);
1832 return 0;
1835 dbg("%s\n", " Baud calculation Failed...");
1836 return -1;
1837 #endif
1840 /*****************************************************************************
1841 * mos7840_send_cmd_write_baud_rate
1842 * this function sends the proper command to change the baud rate of the
1843 * specified port.
1844 *****************************************************************************/
1846 static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1847 int baudRate)
1849 int divisor = 0;
1850 int status;
1851 __u16 Data;
1852 unsigned char number;
1853 __u16 clk_sel_val;
1854 struct usb_serial_port *port;
1856 if (mos7840_port == NULL)
1857 return -1;
1859 port = (struct usb_serial_port *)mos7840_port->port;
1860 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1861 dbg("%s", "Invalid port \n");
1862 return -1;
1865 if (mos7840_serial_paranoia_check(port->serial, __FUNCTION__)) {
1866 dbg("%s", "Invalid Serial \n");
1867 return -1;
1870 dbg("%s", "Entering .......... \n");
1872 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1874 dbg("%s - port = %d, baud = %d", __FUNCTION__,
1875 mos7840_port->port->number, baudRate);
1876 //reset clk_uart_sel in spregOffset
1877 if (baudRate > 115200) {
1878 #ifdef HW_flow_control
1879 //NOTE: need to see the pther register to modify
1880 //setting h/w flow control bit to 1;
1881 status = 0;
1882 Data = 0x2b;
1883 mos7840_port->shadowMCR = Data;
1884 status =
1885 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1886 if (status < 0) {
1887 dbg("Writing spreg failed in set_serial_baud\n");
1888 return -1;
1890 #endif
1892 } else {
1893 #ifdef HW_flow_control
1894 //setting h/w flow control bit to 0;
1895 status = 0;
1896 Data = 0xb;
1897 mos7840_port->shadowMCR = Data;
1898 status =
1899 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1900 if (status < 0) {
1901 dbg("Writing spreg failed in set_serial_baud\n");
1902 return -1;
1904 #endif
1908 if (1) //baudRate <= 115200)
1910 clk_sel_val = 0x0;
1911 Data = 0x0;
1912 status = 0;
1913 status =
1914 mos7840_calc_baud_rate_divisor(baudRate, &divisor,
1915 &clk_sel_val);
1916 status =
1917 mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1918 &Data);
1919 if (status < 0) {
1920 dbg("reading spreg failed in set_serial_baud\n");
1921 return -1;
1923 Data = (Data & 0x8f) | clk_sel_val;
1924 status = 0;
1925 status =
1926 mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1927 if (status < 0) {
1928 dbg("Writing spreg failed in set_serial_baud\n");
1929 return -1;
1931 /* Calculate the Divisor */
1933 if (status) {
1934 err("%s - bad baud rate", __FUNCTION__);
1935 dbg("%s\n", "bad baud rate");
1936 return status;
1938 /* Enable access to divisor latch */
1939 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1940 mos7840_port->shadowLCR = Data;
1941 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1943 /* Write the divisor */
1944 Data = (unsigned char)(divisor & 0xff);
1945 dbg("set_serial_baud Value to write DLL is %x\n", Data);
1946 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1948 Data = (unsigned char)((divisor & 0xff00) >> 8);
1949 dbg("set_serial_baud Value to write DLM is %x\n", Data);
1950 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1952 /* Disable access to divisor latch */
1953 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1954 mos7840_port->shadowLCR = Data;
1955 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1959 return status;
1962 /*****************************************************************************
1963 * mos7840_change_port_settings
1964 * This routine is called to set the UART on the device to match
1965 * the specified new settings.
1966 *****************************************************************************/
1968 static void mos7840_change_port_settings(struct moschip_port *mos7840_port,
1969 struct ktermios *old_termios)
1971 struct tty_struct *tty;
1972 int baud;
1973 unsigned cflag;
1974 unsigned iflag;
1975 __u8 lData;
1976 __u8 lParity;
1977 __u8 lStop;
1978 int status;
1979 __u16 Data;
1980 struct usb_serial_port *port;
1981 struct usb_serial *serial;
1983 if (mos7840_port == NULL)
1984 return;
1986 port = (struct usb_serial_port *)mos7840_port->port;
1988 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1989 dbg("%s", "Invalid port \n");
1990 return;
1993 if (mos7840_serial_paranoia_check(port->serial, __FUNCTION__)) {
1994 dbg("%s", "Invalid Serial \n");
1995 return;
1998 serial = port->serial;
2000 dbg("%s - port %d", __FUNCTION__, mos7840_port->port->number);
2002 if (!mos7840_port->open) {
2003 dbg("%s - port not opened", __FUNCTION__);
2004 return;
2007 tty = mos7840_port->port->tty;
2009 dbg("%s", "Entering .......... \n");
2011 lData = LCR_BITS_8;
2012 lStop = LCR_STOP_1;
2013 lParity = LCR_PAR_NONE;
2015 cflag = tty->termios->c_cflag;
2016 iflag = tty->termios->c_iflag;
2018 /* Change the number of bits */
2019 if (cflag & CSIZE) {
2020 switch (cflag & CSIZE) {
2021 case CS5:
2022 lData = LCR_BITS_5;
2023 break;
2025 case CS6:
2026 lData = LCR_BITS_6;
2027 break;
2029 case CS7:
2030 lData = LCR_BITS_7;
2031 break;
2032 default:
2033 case CS8:
2034 lData = LCR_BITS_8;
2035 break;
2038 /* Change the Parity bit */
2039 if (cflag & PARENB) {
2040 if (cflag & PARODD) {
2041 lParity = LCR_PAR_ODD;
2042 dbg("%s - parity = odd", __FUNCTION__);
2043 } else {
2044 lParity = LCR_PAR_EVEN;
2045 dbg("%s - parity = even", __FUNCTION__);
2048 } else {
2049 dbg("%s - parity = none", __FUNCTION__);
2052 if (cflag & CMSPAR) {
2053 lParity = lParity | 0x20;
2056 /* Change the Stop bit */
2057 if (cflag & CSTOPB) {
2058 lStop = LCR_STOP_2;
2059 dbg("%s - stop bits = 2", __FUNCTION__);
2060 } else {
2061 lStop = LCR_STOP_1;
2062 dbg("%s - stop bits = 1", __FUNCTION__);
2065 /* Update the LCR with the correct value */
2066 mos7840_port->shadowLCR &=
2067 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2068 mos7840_port->shadowLCR |= (lData | lParity | lStop);
2070 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x\n",
2071 mos7840_port->shadowLCR);
2072 /* Disable Interrupts */
2073 Data = 0x00;
2074 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2076 Data = 0x00;
2077 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2079 Data = 0xcf;
2080 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2082 /* Send the updated LCR value to the mos7840 */
2083 Data = mos7840_port->shadowLCR;
2085 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
2087 Data = 0x00b;
2088 mos7840_port->shadowMCR = Data;
2089 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2090 Data = 0x00b;
2091 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2093 /* set up the MCR register and send it to the mos7840 */
2095 mos7840_port->shadowMCR = MCR_MASTER_IE;
2096 if (cflag & CBAUD) {
2097 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2100 if (cflag & CRTSCTS) {
2101 mos7840_port->shadowMCR |= (MCR_XON_ANY);
2103 } else {
2104 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
2107 Data = mos7840_port->shadowMCR;
2108 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2110 /* Determine divisor based on baud rate */
2111 baud = tty_get_baud_rate(tty);
2113 if (!baud) {
2114 /* pick a default, any default... */
2115 dbg("%s\n", "Picked default baud...");
2116 baud = 9600;
2119 dbg("%s - baud rate = %d", __FUNCTION__, baud);
2120 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
2122 /* Enable Interrupts */
2123 Data = 0x0c;
2124 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2126 if (mos7840_port->read_urb->status != -EINPROGRESS) {
2127 mos7840_port->read_urb->dev = serial->dev;
2129 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2131 if (status) {
2132 dbg(" usb_submit_urb(read bulk) failed, status = %d",
2133 status);
2136 wake_up(&mos7840_port->delta_msr_wait);
2137 mos7840_port->delta_msr_cond = 1;
2138 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x\n",
2139 mos7840_port->shadowLCR);
2141 return;
2144 /*****************************************************************************
2145 * mos7840_set_termios
2146 * this function is called by the tty driver when it wants to change
2147 * the termios structure
2148 *****************************************************************************/
2150 static void mos7840_set_termios(struct usb_serial_port *port,
2151 struct ktermios *old_termios)
2153 int status;
2154 unsigned int cflag;
2155 struct usb_serial *serial;
2156 struct moschip_port *mos7840_port;
2157 struct tty_struct *tty;
2158 dbg("mos7840_set_termios: START\n");
2159 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
2160 dbg("%s", "Invalid port \n");
2161 return;
2164 serial = port->serial;
2166 if (mos7840_serial_paranoia_check(serial, __FUNCTION__)) {
2167 dbg("%s", "Invalid Serial \n");
2168 return;
2171 mos7840_port = mos7840_get_port_private(port);
2173 if (mos7840_port == NULL)
2174 return;
2176 tty = port->tty;
2178 if (!mos7840_port->open) {
2179 dbg("%s - port not opened", __FUNCTION__);
2180 return;
2183 dbg("%s\n", "setting termios - ");
2185 cflag = tty->termios->c_cflag;
2187 dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
2188 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
2189 dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
2190 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
2191 dbg("%s - port %d", __FUNCTION__, port->number);
2193 /* change the port settings to the new ones specified */
2195 mos7840_change_port_settings(mos7840_port, old_termios);
2197 if (!mos7840_port->read_urb) {
2198 dbg("%s", "URB KILLED !!!!!\n");
2199 return;
2202 if (mos7840_port->read_urb->status != -EINPROGRESS) {
2203 mos7840_port->read_urb->dev = serial->dev;
2204 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2205 if (status) {
2206 dbg(" usb_submit_urb(read bulk) failed, status = %d",
2207 status);
2210 return;
2213 /*****************************************************************************
2214 * mos7840_get_lsr_info - get line status register info
2216 * Purpose: Let user call ioctl() to get info when the UART physically
2217 * is emptied. On bus types like RS485, the transmitter must
2218 * release the bus after transmitting. This must be done when
2219 * the transmit shift register is empty, not be done when the
2220 * transmit holding register is empty. This functionality
2221 * allows an RS485 driver to be written in user space.
2222 *****************************************************************************/
2224 static int mos7840_get_lsr_info(struct moschip_port *mos7840_port,
2225 unsigned int __user *value)
2227 int count;
2228 unsigned int result = 0;
2230 count = mos7840_chars_in_buffer(mos7840_port->port);
2231 if (count == 0) {
2232 dbg("%s -- Empty", __FUNCTION__);
2233 result = TIOCSER_TEMT;
2236 if (copy_to_user(value, &result, sizeof(int)))
2237 return -EFAULT;
2238 return 0;
2241 /*****************************************************************************
2242 * mos7840_set_modem_info
2243 * function to set modem info
2244 *****************************************************************************/
2246 static int mos7840_set_modem_info(struct moschip_port *mos7840_port,
2247 unsigned int cmd, unsigned int __user *value)
2249 unsigned int mcr;
2250 unsigned int arg;
2251 __u16 Data;
2252 int status;
2253 struct usb_serial_port *port;
2255 if (mos7840_port == NULL)
2256 return -1;
2258 port = (struct usb_serial_port *)mos7840_port->port;
2259 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
2260 dbg("%s", "Invalid port \n");
2261 return -1;
2264 mcr = mos7840_port->shadowMCR;
2266 if (copy_from_user(&arg, value, sizeof(int)))
2267 return -EFAULT;
2269 switch (cmd) {
2270 case TIOCMBIS:
2271 if (arg & TIOCM_RTS)
2272 mcr |= MCR_RTS;
2273 if (arg & TIOCM_DTR)
2274 mcr |= MCR_RTS;
2275 if (arg & TIOCM_LOOP)
2276 mcr |= MCR_LOOPBACK;
2277 break;
2279 case TIOCMBIC:
2280 if (arg & TIOCM_RTS)
2281 mcr &= ~MCR_RTS;
2282 if (arg & TIOCM_DTR)
2283 mcr &= ~MCR_RTS;
2284 if (arg & TIOCM_LOOP)
2285 mcr &= ~MCR_LOOPBACK;
2286 break;
2288 case TIOCMSET:
2289 /* turn off the RTS and DTR and LOOPBACK
2290 * and then only turn on what was asked to */
2291 mcr &= ~(MCR_RTS | MCR_DTR | MCR_LOOPBACK);
2292 mcr |= ((arg & TIOCM_RTS) ? MCR_RTS : 0);
2293 mcr |= ((arg & TIOCM_DTR) ? MCR_DTR : 0);
2294 mcr |= ((arg & TIOCM_LOOP) ? MCR_LOOPBACK : 0);
2295 break;
2298 mos7840_port->shadowMCR = mcr;
2300 Data = mos7840_port->shadowMCR;
2301 status = 0;
2302 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2303 if (status < 0) {
2304 dbg("setting MODEM_CONTROL_REGISTER Failed\n");
2305 return -1;
2308 return 0;
2311 /*****************************************************************************
2312 * mos7840_get_modem_info
2313 * function to get modem info
2314 *****************************************************************************/
2316 static int mos7840_get_modem_info(struct moschip_port *mos7840_port,
2317 unsigned int __user *value)
2319 unsigned int result = 0;
2320 __u16 msr;
2321 unsigned int mcr = mos7840_port->shadowMCR;
2322 int status = 0;
2323 status =
2324 mos7840_get_uart_reg(mos7840_port->port, MODEM_STATUS_REGISTER,
2325 &msr);
2326 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
2327 |((mcr & MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
2328 |((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
2329 |((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0) /* 0x040 */
2330 |((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
2331 |((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
2333 dbg("%s -- %x", __FUNCTION__, result);
2335 if (copy_to_user(value, &result, sizeof(int)))
2336 return -EFAULT;
2337 return 0;
2340 /*****************************************************************************
2341 * mos7840_get_serial_info
2342 * function to get information about serial port
2343 *****************************************************************************/
2345 static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
2346 struct serial_struct __user *retinfo)
2348 struct serial_struct tmp;
2350 if (mos7840_port == NULL)
2351 return -1;
2353 if (!retinfo)
2354 return -EFAULT;
2356 memset(&tmp, 0, sizeof(tmp));
2358 tmp.type = PORT_16550A;
2359 tmp.line = mos7840_port->port->serial->minor;
2360 tmp.port = mos7840_port->port->number;
2361 tmp.irq = 0;
2362 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2363 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2364 tmp.baud_base = 9600;
2365 tmp.close_delay = 5 * HZ;
2366 tmp.closing_wait = 30 * HZ;
2368 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2369 return -EFAULT;
2370 return 0;
2373 /*****************************************************************************
2374 * SerialIoctl
2375 * this function handles any ioctl calls to the driver
2376 *****************************************************************************/
2378 static int mos7840_ioctl(struct usb_serial_port *port, struct file *file,
2379 unsigned int cmd, unsigned long arg)
2381 void __user *argp = (void __user *)arg;
2382 struct moschip_port *mos7840_port;
2383 struct tty_struct *tty;
2385 struct async_icount cnow;
2386 struct async_icount cprev;
2387 struct serial_icounter_struct icount;
2388 int mosret = 0;
2390 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
2391 dbg("%s", "Invalid port \n");
2392 return -1;
2395 mos7840_port = mos7840_get_port_private(port);
2397 if (mos7840_port == NULL)
2398 return -1;
2400 tty = mos7840_port->port->tty;
2402 dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
2404 switch (cmd) {
2405 /* return number of bytes available */
2407 case TIOCSERGETLSR:
2408 dbg("%s (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
2409 return mos7840_get_lsr_info(mos7840_port, argp);
2410 return 0;
2412 case TIOCMBIS:
2413 case TIOCMBIC:
2414 case TIOCMSET:
2415 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __FUNCTION__,
2416 port->number);
2417 mosret =
2418 mos7840_set_modem_info(mos7840_port, cmd, argp);
2419 return mosret;
2421 case TIOCMGET:
2422 dbg("%s (%d) TIOCMGET", __FUNCTION__, port->number);
2423 return mos7840_get_modem_info(mos7840_port, argp);
2425 case TIOCGSERIAL:
2426 dbg("%s (%d) TIOCGSERIAL", __FUNCTION__, port->number);
2427 return mos7840_get_serial_info(mos7840_port, argp);
2429 case TIOCSSERIAL:
2430 dbg("%s (%d) TIOCSSERIAL", __FUNCTION__, port->number);
2431 break;
2433 case TIOCMIWAIT:
2434 dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
2435 cprev = mos7840_port->icount;
2436 while (1) {
2437 //interruptible_sleep_on(&mos7840_port->delta_msr_wait);
2438 mos7840_port->delta_msr_cond = 0;
2439 wait_event_interruptible(mos7840_port->delta_msr_wait,
2440 (mos7840_port->
2441 delta_msr_cond == 1));
2443 /* see if a signal did it */
2444 if (signal_pending(current))
2445 return -ERESTARTSYS;
2446 cnow = mos7840_port->icount;
2447 smp_rmb();
2448 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2449 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2450 return -EIO; /* no change => error */
2451 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2452 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2453 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2454 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2455 return 0;
2457 cprev = cnow;
2459 /* NOTREACHED */
2460 break;
2462 case TIOCGICOUNT:
2463 cnow = mos7840_port->icount;
2464 smp_rmb();
2465 icount.cts = cnow.cts;
2466 icount.dsr = cnow.dsr;
2467 icount.rng = cnow.rng;
2468 icount.dcd = cnow.dcd;
2469 icount.rx = cnow.rx;
2470 icount.tx = cnow.tx;
2471 icount.frame = cnow.frame;
2472 icount.overrun = cnow.overrun;
2473 icount.parity = cnow.parity;
2474 icount.brk = cnow.brk;
2475 icount.buf_overrun = cnow.buf_overrun;
2477 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__,
2478 port->number, icount.rx, icount.tx);
2479 if (copy_to_user(argp, &icount, sizeof(icount)))
2480 return -EFAULT;
2481 return 0;
2483 case TIOCEXBAUD:
2484 return 0;
2485 default:
2486 break;
2489 return -ENOIOCTLCMD;
2492 static int mos7840_calc_num_ports(struct usb_serial *serial)
2494 int mos7840_num_ports = 0;
2496 dbg("numberofendpoints: %d \n",
2497 (int)serial->interface->cur_altsetting->desc.bNumEndpoints);
2498 dbg("numberofendpoints: %d \n",
2499 (int)serial->interface->altsetting->desc.bNumEndpoints);
2500 if (serial->interface->cur_altsetting->desc.bNumEndpoints == 5) {
2501 mos7840_num_ports = serial->num_ports = 2;
2502 } else if (serial->interface->cur_altsetting->desc.bNumEndpoints == 9) {
2503 serial->num_bulk_in = 4;
2504 serial->num_bulk_out = 4;
2505 mos7840_num_ports = serial->num_ports = 4;
2508 return mos7840_num_ports;
2511 /****************************************************************************
2512 * mos7840_startup
2513 ****************************************************************************/
2515 static int mos7840_startup(struct usb_serial *serial)
2517 struct moschip_port *mos7840_port;
2518 struct usb_device *dev;
2519 int i, status;
2521 __u16 Data;
2522 dbg("%s \n", " mos7840_startup :entering..........");
2524 if (!serial) {
2525 dbg("%s\n", "Invalid Handler");
2526 return -1;
2529 dev = serial->dev;
2531 dbg("%s\n", "Entering...");
2533 /* we set up the pointers to the endpoints in the mos7840_open *
2534 * function, as the structures aren't created yet. */
2536 /* set up port private structures */
2537 for (i = 0; i < serial->num_ports; ++i) {
2538 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2539 if (mos7840_port == NULL) {
2540 err("%s - Out of memory", __FUNCTION__);
2541 status = -ENOMEM;
2542 i--; /* don't follow NULL pointer cleaning up */
2543 goto error;
2546 /* Initialize all port interrupt end point to port 0 int endpoint *
2547 * Our device has only one interrupt end point comman to all port */
2549 mos7840_port->port = serial->port[i];
2550 mos7840_set_port_private(serial->port[i], mos7840_port);
2551 spin_lock_init(&mos7840_port->pool_lock);
2553 mos7840_port->port_num = ((serial->port[i]->number -
2554 (serial->port[i]->serial->minor)) +
2557 if (mos7840_port->port_num == 1) {
2558 mos7840_port->SpRegOffset = 0x0;
2559 mos7840_port->ControlRegOffset = 0x1;
2560 mos7840_port->DcrRegOffset = 0x4;
2561 } else if ((mos7840_port->port_num == 2)
2562 && (serial->num_ports == 4)) {
2563 mos7840_port->SpRegOffset = 0x8;
2564 mos7840_port->ControlRegOffset = 0x9;
2565 mos7840_port->DcrRegOffset = 0x16;
2566 } else if ((mos7840_port->port_num == 2)
2567 && (serial->num_ports == 2)) {
2568 mos7840_port->SpRegOffset = 0xa;
2569 mos7840_port->ControlRegOffset = 0xb;
2570 mos7840_port->DcrRegOffset = 0x19;
2571 } else if ((mos7840_port->port_num == 3)
2572 && (serial->num_ports == 4)) {
2573 mos7840_port->SpRegOffset = 0xa;
2574 mos7840_port->ControlRegOffset = 0xb;
2575 mos7840_port->DcrRegOffset = 0x19;
2576 } else if ((mos7840_port->port_num == 4)
2577 && (serial->num_ports == 4)) {
2578 mos7840_port->SpRegOffset = 0xc;
2579 mos7840_port->ControlRegOffset = 0xd;
2580 mos7840_port->DcrRegOffset = 0x1c;
2582 mos7840_dump_serial_port(mos7840_port);
2584 mos7840_set_port_private(serial->port[i], mos7840_port);
2586 //enable rx_disable bit in control register
2588 status =
2589 mos7840_get_reg_sync(serial->port[i],
2590 mos7840_port->ControlRegOffset, &Data);
2591 if (status < 0) {
2592 dbg("Reading ControlReg failed status-0x%x\n", status);
2593 break;
2594 } else
2595 dbg("ControlReg Reading success val is %x, status%d\n",
2596 Data, status);
2597 Data |= 0x08; //setting driver done bit
2598 Data |= 0x04; //sp1_bit to have cts change reflect in modem status reg
2600 //Data |= 0x20; //rx_disable bit
2601 status = 0;
2602 status =
2603 mos7840_set_reg_sync(serial->port[i],
2604 mos7840_port->ControlRegOffset, Data);
2605 if (status < 0) {
2606 dbg("Writing ControlReg failed(rx_disable) status-0x%x\n", status);
2607 break;
2608 } else
2609 dbg("ControlReg Writing success(rx_disable) status%d\n",
2610 status);
2612 //Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2 and 0x24 in DCR3
2613 Data = 0x01;
2614 status = 0;
2615 status =
2616 mos7840_set_reg_sync(serial->port[i],
2617 (__u16) (mos7840_port->DcrRegOffset +
2618 0), Data);
2619 if (status < 0) {
2620 dbg("Writing DCR0 failed status-0x%x\n", status);
2621 break;
2622 } else
2623 dbg("DCR0 Writing success status%d\n", status);
2625 Data = 0x05;
2626 status = 0;
2627 status =
2628 mos7840_set_reg_sync(serial->port[i],
2629 (__u16) (mos7840_port->DcrRegOffset +
2630 1), Data);
2631 if (status < 0) {
2632 dbg("Writing DCR1 failed status-0x%x\n", status);
2633 break;
2634 } else
2635 dbg("DCR1 Writing success status%d\n", status);
2637 Data = 0x24;
2638 status = 0;
2639 status =
2640 mos7840_set_reg_sync(serial->port[i],
2641 (__u16) (mos7840_port->DcrRegOffset +
2642 2), Data);
2643 if (status < 0) {
2644 dbg("Writing DCR2 failed status-0x%x\n", status);
2645 break;
2646 } else
2647 dbg("DCR2 Writing success status%d\n", status);
2649 // write values in clkstart0x0 and clkmulti 0x20
2650 Data = 0x0;
2651 status = 0;
2652 status =
2653 mos7840_set_reg_sync(serial->port[i],
2654 CLK_START_VALUE_REGISTER, Data);
2655 if (status < 0) {
2656 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
2657 break;
2658 } else
2659 dbg("CLK_START_VALUE_REGISTER Writing success status%d\n", status);
2661 Data = 0x20;
2662 status =
2663 mos7840_set_reg_sync(serial->port[i], CLK_MULTI_REGISTER,
2664 Data);
2665 if (status < 0) {
2666 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x\n",
2667 status);
2668 goto error;
2669 } else
2670 dbg("CLK_MULTI_REGISTER Writing success status%d\n",
2671 status);
2673 //write value 0x0 to scratchpad register
2674 Data = 0x00;
2675 status =
2676 mos7840_set_uart_reg(serial->port[i], SCRATCH_PAD_REGISTER,
2677 Data);
2678 if (status < 0) {
2679 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x\n",
2680 status);
2681 break;
2682 } else
2683 dbg("SCRATCH_PAD_REGISTER Writing success status%d\n",
2684 status);
2686 //Zero Length flag register
2687 if ((mos7840_port->port_num != 1)
2688 && (serial->num_ports == 2)) {
2690 Data = 0xff;
2691 status = 0;
2692 status = mos7840_set_reg_sync(serial->port[i],
2693 (__u16) (ZLP_REG1 +
2694 ((__u16)
2695 mos7840_port->
2696 port_num)),
2697 Data);
2698 dbg("ZLIP offset%x\n",
2699 (__u16) (ZLP_REG1 +
2700 ((__u16) mos7840_port->port_num)));
2701 if (status < 0) {
2702 dbg("Writing ZLP_REG%d failed status-0x%x\n",
2703 i + 2, status);
2704 break;
2705 } else
2706 dbg("ZLP_REG%d Writing success status%d\n",
2707 i + 2, status);
2708 } else {
2709 Data = 0xff;
2710 status = 0;
2711 status = mos7840_set_reg_sync(serial->port[i],
2712 (__u16) (ZLP_REG1 +
2713 ((__u16)
2714 mos7840_port->
2715 port_num) -
2716 0x1), Data);
2717 dbg("ZLIP offset%x\n",
2718 (__u16) (ZLP_REG1 +
2719 ((__u16) mos7840_port->port_num) - 0x1));
2720 if (status < 0) {
2721 dbg("Writing ZLP_REG%d failed status-0x%x\n",
2722 i + 1, status);
2723 break;
2724 } else
2725 dbg("ZLP_REG%d Writing success status%d\n",
2726 i + 1, status);
2729 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
2730 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2731 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
2732 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf || !mos7840_port->dr) {
2733 status = -ENOMEM;
2734 goto error;
2738 //Zero Length flag enable
2739 Data = 0x0f;
2740 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2741 if (status < 0) {
2742 dbg("Writing ZLP_REG5 failed status-0x%x\n", status);
2743 goto error;
2744 } else
2745 dbg("ZLP_REG5 Writing success status%d\n", status);
2747 /* setting configuration feature to one */
2748 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2749 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
2750 return 0;
2751 error:
2752 for (/* nothing */; i >= 0; i--) {
2753 mos7840_port = mos7840_get_port_private(serial->port[i]);
2755 kfree(mos7840_port->dr);
2756 kfree(mos7840_port->ctrl_buf);
2757 usb_free_urb(mos7840_port->control_urb);
2758 kfree(mos7840_port);
2759 serial->port[i] = NULL;
2761 return status;
2764 /****************************************************************************
2765 * mos7840_shutdown
2766 * This function is called whenever the device is removed from the usb bus.
2767 ****************************************************************************/
2769 static void mos7840_shutdown(struct usb_serial *serial)
2771 int i;
2772 unsigned long flags;
2773 struct moschip_port *mos7840_port;
2774 dbg("%s \n", " shutdown :entering..........");
2776 if (!serial) {
2777 dbg("%s", "Invalid Handler \n");
2778 return;
2781 /* check for the ports to be closed,close the ports and disconnect */
2783 /* free private structure allocated for serial port *
2784 * stop reads and writes on all ports */
2786 for (i = 0; i < serial->num_ports; ++i) {
2787 mos7840_port = mos7840_get_port_private(serial->port[i]);
2788 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
2789 mos7840_port->zombie = 1;
2790 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
2791 usb_kill_urb(mos7840_port->control_urb);
2792 kfree(mos7840_port->ctrl_buf);
2793 kfree(mos7840_port->dr);
2794 kfree(mos7840_port);
2795 mos7840_set_port_private(serial->port[i], NULL);
2798 dbg("%s\n", "Thank u :: ");
2802 static struct usb_driver io_driver = {
2803 .name = "mos7840",
2804 .probe = usb_serial_probe,
2805 .disconnect = usb_serial_disconnect,
2806 .id_table = moschip_id_table_combined,
2807 .no_dynamic_id = 1,
2810 static struct usb_serial_driver moschip7840_4port_device = {
2811 .driver = {
2812 .owner = THIS_MODULE,
2813 .name = "mos7840",
2815 .description = DRIVER_DESC,
2816 .usb_driver = &io_driver,
2817 .id_table = moschip_port_id_table,
2818 .num_interrupt_in = 1, //NUM_DONT_CARE,//1,
2819 #ifdef check
2820 .num_bulk_in = 4,
2821 .num_bulk_out = 4,
2822 .num_ports = 4,
2823 #endif
2824 .open = mos7840_open,
2825 .close = mos7840_close,
2826 .write = mos7840_write,
2827 .write_room = mos7840_write_room,
2828 .chars_in_buffer = mos7840_chars_in_buffer,
2829 .throttle = mos7840_throttle,
2830 .unthrottle = mos7840_unthrottle,
2831 .calc_num_ports = mos7840_calc_num_ports,
2832 #ifdef MCSSerialProbe
2833 .probe = mos7840_serial_probe,
2834 #endif
2835 .ioctl = mos7840_ioctl,
2836 .set_termios = mos7840_set_termios,
2837 .break_ctl = mos7840_break,
2838 .tiocmget = mos7840_tiocmget,
2839 .tiocmset = mos7840_tiocmset,
2840 .attach = mos7840_startup,
2841 .shutdown = mos7840_shutdown,
2842 .read_bulk_callback = mos7840_bulk_in_callback,
2843 .read_int_callback = mos7840_interrupt_callback,
2846 /****************************************************************************
2847 * moschip7840_init
2848 * This is called by the module subsystem, or on startup to initialize us
2849 ****************************************************************************/
2850 static int __init moschip7840_init(void)
2852 int retval;
2854 dbg("%s \n", " mos7840_init :entering..........");
2856 /* Register with the usb serial */
2857 retval = usb_serial_register(&moschip7840_4port_device);
2859 if (retval)
2860 goto failed_port_device_register;
2862 dbg("%s\n", "Entring...");
2863 info(DRIVER_DESC " " DRIVER_VERSION);
2865 /* Register with the usb */
2866 retval = usb_register(&io_driver);
2868 if (retval)
2869 goto failed_usb_register;
2871 if (retval == 0) {
2872 dbg("%s\n", "Leaving...");
2873 return 0;
2876 failed_usb_register:
2877 usb_serial_deregister(&moschip7840_4port_device);
2879 failed_port_device_register:
2881 return retval;
2884 /****************************************************************************
2885 * moschip7840_exit
2886 * Called when the driver is about to be unloaded.
2887 ****************************************************************************/
2888 static void __exit moschip7840_exit(void)
2891 dbg("%s \n", " mos7840_exit :entering..........");
2893 usb_deregister(&io_driver);
2895 usb_serial_deregister(&moschip7840_4port_device);
2897 dbg("%s\n", "Entring...");
2900 module_init(moschip7840_init);
2901 module_exit(moschip7840_exit);
2903 /* Module information */
2904 MODULE_DESCRIPTION(DRIVER_DESC);
2905 MODULE_LICENSE("GPL");
2907 module_param(debug, bool, S_IRUGO | S_IWUSR);
2908 MODULE_PARM_DESC(debug, "Debug enabled or not");