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