inet: frag: enforce memory limits earlier
[linux/fpc-iii.git] / drivers / usb / serial / mos7840.c
blob03d63bad6be48e6116d8e6bdd9d9db8d356d51dc
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/slab.h>
28 #include <linux/tty.h>
29 #include <linux/tty_driver.h>
30 #include <linux/tty_flip.h>
31 #include <linux/module.h>
32 #include <linux/serial.h>
33 #include <linux/usb.h>
34 #include <linux/usb/serial.h>
35 #include <linux/uaccess.h>
37 #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
40 * 16C50 UART register defines
43 #define LCR_BITS_5 0x00 /* 5 bits/char */
44 #define LCR_BITS_6 0x01 /* 6 bits/char */
45 #define LCR_BITS_7 0x02 /* 7 bits/char */
46 #define LCR_BITS_8 0x03 /* 8 bits/char */
47 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
49 #define LCR_STOP_1 0x00 /* 1 stop bit */
50 #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
51 #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
52 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
54 #define LCR_PAR_NONE 0x00 /* No parity */
55 #define LCR_PAR_ODD 0x08 /* Odd parity */
56 #define LCR_PAR_EVEN 0x18 /* Even parity */
57 #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
58 #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
59 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
61 #define LCR_SET_BREAK 0x40 /* Set Break condition */
62 #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
64 #define MCR_DTR 0x01 /* Assert DTR */
65 #define MCR_RTS 0x02 /* Assert RTS */
66 #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
67 #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
68 #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
69 #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
71 #define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
72 #define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
73 #define MOS7840_MSR_RI 0x40 /* Current state of RI */
74 #define MOS7840_MSR_CD 0x80 /* Current state of CD */
77 * Defines used for sending commands to port
80 #define MOS_WDR_TIMEOUT 5000 /* default urb timeout */
82 #define MOS_PORT1 0x0200
83 #define MOS_PORT2 0x0300
84 #define MOS_VENREG 0x0000
85 #define MOS_MAX_PORT 0x02
86 #define MOS_WRITE 0x0E
87 #define MOS_READ 0x0D
89 /* Requests */
90 #define MCS_RD_RTYPE 0xC0
91 #define MCS_WR_RTYPE 0x40
92 #define MCS_RDREQ 0x0D
93 #define MCS_WRREQ 0x0E
94 #define MCS_CTRL_TIMEOUT 500
95 #define VENDOR_READ_LENGTH (0x01)
97 #define MAX_NAME_LEN 64
99 #define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
100 #define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
102 /* For higher baud Rates use TIOCEXBAUD */
103 #define TIOCEXBAUD 0x5462
105 /* vendor id and device id defines */
107 /* The native mos7840/7820 component */
108 #define USB_VENDOR_ID_MOSCHIP 0x9710
109 #define MOSCHIP_DEVICE_ID_7840 0x7840
110 #define MOSCHIP_DEVICE_ID_7820 0x7820
111 #define MOSCHIP_DEVICE_ID_7810 0x7810
112 /* The native component can have its vendor/device id's overridden
113 * in vendor-specific implementations. Such devices can be handled
114 * by making a change here, in id_table.
116 #define USB_VENDOR_ID_BANDB 0x0856
117 #define BANDB_DEVICE_ID_USO9ML2_2 0xAC22
118 #define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00
119 #define BANDB_DEVICE_ID_USO9ML2_4 0xAC24
120 #define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01
121 #define BANDB_DEVICE_ID_US9ML2_2 0xAC29
122 #define BANDB_DEVICE_ID_US9ML2_4 0xAC30
123 #define BANDB_DEVICE_ID_USPTL4_2 0xAC31
124 #define BANDB_DEVICE_ID_USPTL4_4 0xAC32
125 #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
126 #define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02
127 #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
128 #define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03
129 #define BANDB_DEVICE_ID_USOPTL2_4 0xAC24
131 /* This driver also supports
132 * ATEN UC2324 device using Moschip MCS7840
133 * ATEN UC2322 device using Moschip MCS7820
135 #define USB_VENDOR_ID_ATENINTL 0x0557
136 #define ATENINTL_DEVICE_ID_UC2324 0x2011
137 #define ATENINTL_DEVICE_ID_UC2322 0x7820
139 /* Interrupt Routine Defines */
141 #define SERIAL_IIR_RLS 0x06
142 #define SERIAL_IIR_MS 0x00
145 * Emulation of the bit mask on the LINE STATUS REGISTER.
147 #define SERIAL_LSR_DR 0x0001
148 #define SERIAL_LSR_OE 0x0002
149 #define SERIAL_LSR_PE 0x0004
150 #define SERIAL_LSR_FE 0x0008
151 #define SERIAL_LSR_BI 0x0010
153 #define MOS_MSR_DELTA_CTS 0x10
154 #define MOS_MSR_DELTA_DSR 0x20
155 #define MOS_MSR_DELTA_RI 0x40
156 #define MOS_MSR_DELTA_CD 0x80
158 /* Serial Port register Address */
159 #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
160 #define FIFO_CONTROL_REGISTER ((__u16)(0x02))
161 #define LINE_CONTROL_REGISTER ((__u16)(0x03))
162 #define MODEM_CONTROL_REGISTER ((__u16)(0x04))
163 #define LINE_STATUS_REGISTER ((__u16)(0x05))
164 #define MODEM_STATUS_REGISTER ((__u16)(0x06))
165 #define SCRATCH_PAD_REGISTER ((__u16)(0x07))
166 #define DIVISOR_LATCH_LSB ((__u16)(0x00))
167 #define DIVISOR_LATCH_MSB ((__u16)(0x01))
169 #define CLK_MULTI_REGISTER ((__u16)(0x02))
170 #define CLK_START_VALUE_REGISTER ((__u16)(0x03))
171 #define GPIO_REGISTER ((__u16)(0x07))
173 #define SERIAL_LCR_DLAB ((__u16)(0x0080))
176 * URB POOL related defines
178 #define NUM_URBS 16 /* URB Count */
179 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
181 /* LED on/off milliseconds*/
182 #define LED_ON_MS 500
183 #define LED_OFF_MS 500
185 enum mos7840_flag {
186 MOS7840_FLAG_CTRL_BUSY,
187 MOS7840_FLAG_LED_BUSY,
190 static const struct usb_device_id id_table[] = {
191 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
192 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
193 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)},
194 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
195 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
196 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
197 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
198 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
199 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
200 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
201 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
202 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
203 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
204 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
205 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
206 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
207 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
208 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
209 {} /* terminating entry */
211 MODULE_DEVICE_TABLE(usb, id_table);
213 /* This structure holds all of the local port information */
215 struct moschip_port {
216 int port_num; /*Actual port number in the device(1,2,etc) */
217 struct urb *write_urb; /* write URB for this port */
218 struct urb *read_urb; /* read URB for this port */
219 __u8 shadowLCR; /* last LCR value received */
220 __u8 shadowMCR; /* last MCR value received */
221 char open;
222 char open_ports;
223 struct usb_serial_port *port; /* loop back to the owner of this object */
225 /* Offsets */
226 __u8 SpRegOffset;
227 __u8 ControlRegOffset;
228 __u8 DcrRegOffset;
229 /* for processing control URBS in interrupt context */
230 struct urb *control_urb;
231 struct usb_ctrlrequest *dr;
232 char *ctrl_buf;
233 int MsrLsr;
235 spinlock_t pool_lock;
236 struct urb *write_urb_pool[NUM_URBS];
237 char busy[NUM_URBS];
238 bool read_urb_busy;
240 /* For device(s) with LED indicator */
241 bool has_led;
242 struct timer_list led_timer1; /* Timer for LED on */
243 struct timer_list led_timer2; /* Timer for LED off */
244 struct urb *led_urb;
245 struct usb_ctrlrequest *led_dr;
247 unsigned long flags;
251 * mos7840_set_reg_sync
252 * To set the Control register by calling usb_fill_control_urb function
253 * by passing usb_sndctrlpipe function as parameter.
256 static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
257 __u16 val)
259 struct usb_device *dev = port->serial->dev;
260 val = val & 0x00ff;
261 dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
263 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
264 MCS_WR_RTYPE, val, reg, NULL, 0,
265 MOS_WDR_TIMEOUT);
269 * mos7840_get_reg_sync
270 * To set the Uart register by calling usb_fill_control_urb function by
271 * passing usb_rcvctrlpipe function as parameter.
274 static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
275 __u16 *val)
277 struct usb_device *dev = port->serial->dev;
278 int ret = 0;
279 u8 *buf;
281 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
282 if (!buf)
283 return -ENOMEM;
285 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
286 MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
287 MOS_WDR_TIMEOUT);
288 if (ret < VENDOR_READ_LENGTH) {
289 if (ret >= 0)
290 ret = -EIO;
291 goto out;
294 *val = buf[0];
295 dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val);
296 out:
297 kfree(buf);
298 return ret;
302 * mos7840_set_uart_reg
303 * To set the Uart register by calling usb_fill_control_urb function by
304 * passing usb_sndctrlpipe function as parameter.
307 static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
308 __u16 val)
311 struct usb_device *dev = port->serial->dev;
312 val = val & 0x00ff;
313 /* For the UART control registers, the application number need
314 to be Or'ed */
315 if (port->serial->num_ports == 4) {
316 val |= ((__u16)port->port_number + 1) << 8;
317 } else {
318 if (port->port_number == 0) {
319 val |= ((__u16)port->port_number + 1) << 8;
320 } else {
321 val |= ((__u16)port->port_number + 2) << 8;
324 dev_dbg(&port->dev, "%s application number is %x\n", __func__, val);
325 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
326 MCS_WR_RTYPE, val, reg, NULL, 0,
327 MOS_WDR_TIMEOUT);
332 * mos7840_get_uart_reg
333 * To set the Control register by calling usb_fill_control_urb function
334 * by passing usb_rcvctrlpipe function as parameter.
336 static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
337 __u16 *val)
339 struct usb_device *dev = port->serial->dev;
340 int ret = 0;
341 __u16 Wval;
342 u8 *buf;
344 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
345 if (!buf)
346 return -ENOMEM;
348 /* Wval is same as application number */
349 if (port->serial->num_ports == 4) {
350 Wval = ((__u16)port->port_number + 1) << 8;
351 } else {
352 if (port->port_number == 0) {
353 Wval = ((__u16)port->port_number + 1) << 8;
354 } else {
355 Wval = ((__u16)port->port_number + 2) << 8;
358 dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval);
359 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
360 MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
361 MOS_WDR_TIMEOUT);
362 if (ret < VENDOR_READ_LENGTH) {
363 if (ret >= 0)
364 ret = -EIO;
365 goto out;
367 *val = buf[0];
368 out:
369 kfree(buf);
370 return ret;
373 static void mos7840_dump_serial_port(struct usb_serial_port *port,
374 struct moschip_port *mos7840_port)
377 dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
378 dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset);
379 dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset);
383 /************************************************************************/
384 /************************************************************************/
385 /* I N T E R F A C E F U N C T I O N S */
386 /* I N T E R F A C E F U N C T I O N S */
387 /************************************************************************/
388 /************************************************************************/
390 static inline void mos7840_set_port_private(struct usb_serial_port *port,
391 struct moschip_port *data)
393 usb_set_serial_port_data(port, (void *)data);
396 static inline struct moschip_port *mos7840_get_port_private(struct
397 usb_serial_port
398 *port)
400 return (struct moschip_port *)usb_get_serial_port_data(port);
403 static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
405 struct moschip_port *mos7840_port;
406 struct async_icount *icount;
407 mos7840_port = port;
408 if (new_msr &
409 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
410 MOS_MSR_DELTA_CD)) {
411 icount = &mos7840_port->port->icount;
413 /* update input line counters */
414 if (new_msr & MOS_MSR_DELTA_CTS)
415 icount->cts++;
416 if (new_msr & MOS_MSR_DELTA_DSR)
417 icount->dsr++;
418 if (new_msr & MOS_MSR_DELTA_CD)
419 icount->dcd++;
420 if (new_msr & MOS_MSR_DELTA_RI)
421 icount->rng++;
423 wake_up_interruptible(&port->port->port.delta_msr_wait);
427 static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
429 struct async_icount *icount;
431 if (new_lsr & SERIAL_LSR_BI) {
433 * Parity and Framing errors only count if they
434 * occur exclusive of a break being
435 * received.
437 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
440 /* update input line counters */
441 icount = &port->port->icount;
442 if (new_lsr & SERIAL_LSR_BI)
443 icount->brk++;
444 if (new_lsr & SERIAL_LSR_OE)
445 icount->overrun++;
446 if (new_lsr & SERIAL_LSR_PE)
447 icount->parity++;
448 if (new_lsr & SERIAL_LSR_FE)
449 icount->frame++;
452 /************************************************************************/
453 /************************************************************************/
454 /* U S B C A L L B A C K F U N C T I O N S */
455 /* U S B C A L L B A C K F U N C T I O N S */
456 /************************************************************************/
457 /************************************************************************/
459 static void mos7840_control_callback(struct urb *urb)
461 unsigned char *data;
462 struct moschip_port *mos7840_port;
463 struct device *dev = &urb->dev->dev;
464 __u8 regval = 0x0;
465 int status = urb->status;
467 mos7840_port = urb->context;
469 switch (status) {
470 case 0:
471 /* success */
472 break;
473 case -ECONNRESET:
474 case -ENOENT:
475 case -ESHUTDOWN:
476 /* this urb is terminated, clean up */
477 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
478 goto out;
479 default:
480 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
481 goto out;
484 dev_dbg(dev, "%s urb buffer size is %d\n", __func__, urb->actual_length);
485 if (urb->actual_length < 1)
486 goto out;
488 dev_dbg(dev, "%s mos7840_port->MsrLsr is %d port %d\n", __func__,
489 mos7840_port->MsrLsr, mos7840_port->port_num);
490 data = urb->transfer_buffer;
491 regval = (__u8) data[0];
492 dev_dbg(dev, "%s data is %x\n", __func__, 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);
497 out:
498 clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY, &mos7840_port->flags);
501 static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
502 __u16 *val)
504 struct usb_device *dev = mcs->port->serial->dev;
505 struct usb_ctrlrequest *dr = mcs->dr;
506 unsigned char *buffer = mcs->ctrl_buf;
507 int ret;
509 if (test_and_set_bit_lock(MOS7840_FLAG_CTRL_BUSY, &mcs->flags))
510 return -EBUSY;
512 dr->bRequestType = MCS_RD_RTYPE;
513 dr->bRequest = MCS_RDREQ;
514 dr->wValue = cpu_to_le16(Wval); /* 0 */
515 dr->wIndex = cpu_to_le16(reg);
516 dr->wLength = cpu_to_le16(2);
518 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
519 (unsigned char *)dr, buffer, 2,
520 mos7840_control_callback, mcs);
521 mcs->control_urb->transfer_buffer_length = 2;
522 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
523 if (ret)
524 clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY, &mcs->flags);
526 return ret;
529 static void mos7840_set_led_callback(struct urb *urb)
531 switch (urb->status) {
532 case 0:
533 /* Success */
534 break;
535 case -ECONNRESET:
536 case -ENOENT:
537 case -ESHUTDOWN:
538 /* This urb is terminated, clean up */
539 dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n",
540 __func__, urb->status);
541 break;
542 default:
543 dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n",
544 __func__, urb->status);
548 static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
549 __u16 reg)
551 struct usb_device *dev = mcs->port->serial->dev;
552 struct usb_ctrlrequest *dr = mcs->led_dr;
554 dr->bRequestType = MCS_WR_RTYPE;
555 dr->bRequest = MCS_WRREQ;
556 dr->wValue = cpu_to_le16(wval);
557 dr->wIndex = cpu_to_le16(reg);
558 dr->wLength = cpu_to_le16(0);
560 usb_fill_control_urb(mcs->led_urb, dev, usb_sndctrlpipe(dev, 0),
561 (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
563 usb_submit_urb(mcs->led_urb, GFP_ATOMIC);
566 static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
567 __u16 val)
569 struct usb_device *dev = port->serial->dev;
571 usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
572 val, reg, NULL, 0, MOS_WDR_TIMEOUT);
575 static void mos7840_led_off(unsigned long arg)
577 struct moschip_port *mcs = (struct moschip_port *) arg;
579 /* Turn off LED */
580 mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
581 mod_timer(&mcs->led_timer2,
582 jiffies + msecs_to_jiffies(LED_OFF_MS));
585 static void mos7840_led_flag_off(unsigned long arg)
587 struct moschip_port *mcs = (struct moschip_port *) arg;
589 clear_bit_unlock(MOS7840_FLAG_LED_BUSY, &mcs->flags);
592 static void mos7840_led_activity(struct usb_serial_port *port)
594 struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
596 if (test_and_set_bit_lock(MOS7840_FLAG_LED_BUSY, &mos7840_port->flags))
597 return;
599 mos7840_set_led_async(mos7840_port, 0x0301, MODEM_CONTROL_REGISTER);
600 mod_timer(&mos7840_port->led_timer1,
601 jiffies + msecs_to_jiffies(LED_ON_MS));
604 /*****************************************************************************
605 * mos7840_interrupt_callback
606 * this is the callback function for when we have received data on the
607 * interrupt endpoint.
608 *****************************************************************************/
610 static void mos7840_interrupt_callback(struct urb *urb)
612 int result;
613 int length;
614 struct moschip_port *mos7840_port;
615 struct usb_serial *serial;
616 __u16 Data;
617 unsigned char *data;
618 __u8 sp[5], st;
619 int i, rv = 0;
620 __u16 wval, wreg = 0;
621 int status = urb->status;
623 switch (status) {
624 case 0:
625 /* success */
626 break;
627 case -ECONNRESET:
628 case -ENOENT:
629 case -ESHUTDOWN:
630 /* this urb is terminated, clean up */
631 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
632 __func__, status);
633 return;
634 default:
635 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n",
636 __func__, status);
637 goto exit;
640 length = urb->actual_length;
641 data = urb->transfer_buffer;
643 serial = urb->context;
645 /* Moschip get 5 bytes
646 * Byte 1 IIR Port 1 (port.number is 0)
647 * Byte 2 IIR Port 2 (port.number is 1)
648 * Byte 3 IIR Port 3 (port.number is 2)
649 * Byte 4 IIR Port 4 (port.number is 3)
650 * Byte 5 FIFO status for both */
652 if (length > 5) {
653 dev_dbg(&urb->dev->dev, "%s", "Wrong data !!!\n");
654 return;
657 sp[0] = (__u8) data[0];
658 sp[1] = (__u8) data[1];
659 sp[2] = (__u8) data[2];
660 sp[3] = (__u8) data[3];
661 st = (__u8) data[4];
663 for (i = 0; i < serial->num_ports; i++) {
664 mos7840_port = mos7840_get_port_private(serial->port[i]);
665 wval = ((__u16)serial->port[i]->port_number + 1) << 8;
666 if (mos7840_port->open) {
667 if (sp[i] & 0x01) {
668 dev_dbg(&urb->dev->dev, "SP%d No Interrupt !!!\n", i);
669 } else {
670 switch (sp[i] & 0x0f) {
671 case SERIAL_IIR_RLS:
672 dev_dbg(&urb->dev->dev, "Serial Port %d: Receiver status error or \n", i);
673 dev_dbg(&urb->dev->dev, "address bit detected in 9-bit mode\n");
674 mos7840_port->MsrLsr = 1;
675 wreg = LINE_STATUS_REGISTER;
676 break;
677 case SERIAL_IIR_MS:
678 dev_dbg(&urb->dev->dev, "Serial Port %d: Modem status change\n", i);
679 mos7840_port->MsrLsr = 0;
680 wreg = MODEM_STATUS_REGISTER;
681 break;
683 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
687 if (!(rv < 0))
688 /* the completion handler for the control urb will resubmit */
689 return;
690 exit:
691 result = usb_submit_urb(urb, GFP_ATOMIC);
692 if (result) {
693 dev_err(&urb->dev->dev,
694 "%s - Error %d submitting interrupt urb\n",
695 __func__, result);
699 static int mos7840_port_paranoia_check(struct usb_serial_port *port,
700 const char *function)
702 if (!port) {
703 pr_debug("%s - port == NULL\n", function);
704 return -1;
706 if (!port->serial) {
707 pr_debug("%s - port->serial == NULL\n", function);
708 return -1;
711 return 0;
714 /* Inline functions to check the sanity of a pointer that is passed to us */
715 static int mos7840_serial_paranoia_check(struct usb_serial *serial,
716 const char *function)
718 if (!serial) {
719 pr_debug("%s - serial == NULL\n", function);
720 return -1;
722 if (!serial->type) {
723 pr_debug("%s - serial->type == NULL!\n", function);
724 return -1;
727 return 0;
730 static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
731 const char *function)
733 /* if no port was specified, or it fails a paranoia check */
734 if (!port ||
735 mos7840_port_paranoia_check(port, function) ||
736 mos7840_serial_paranoia_check(port->serial, function)) {
737 /* then say that we don't have a valid usb_serial thing,
738 * which will end up genrating -ENODEV return values */
739 return NULL;
742 return port->serial;
745 /*****************************************************************************
746 * mos7840_bulk_in_callback
747 * this is the callback function for when we have received data on the
748 * bulk in endpoint.
749 *****************************************************************************/
751 static void mos7840_bulk_in_callback(struct urb *urb)
753 int retval;
754 unsigned char *data;
755 struct usb_serial *serial;
756 struct usb_serial_port *port;
757 struct moschip_port *mos7840_port;
758 int status = urb->status;
760 mos7840_port = urb->context;
761 if (!mos7840_port)
762 return;
764 if (status) {
765 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
766 mos7840_port->read_urb_busy = false;
767 return;
770 port = mos7840_port->port;
771 if (mos7840_port_paranoia_check(port, __func__)) {
772 mos7840_port->read_urb_busy = false;
773 return;
776 serial = mos7840_get_usb_serial(port, __func__);
777 if (!serial) {
778 mos7840_port->read_urb_busy = false;
779 return;
782 data = urb->transfer_buffer;
783 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
785 if (urb->actual_length) {
786 struct tty_port *tport = &mos7840_port->port->port;
787 tty_insert_flip_string(tport, data, urb->actual_length);
788 tty_flip_buffer_push(tport);
789 port->icount.rx += urb->actual_length;
790 dev_dbg(&port->dev, "icount.rx is %d:\n", port->icount.rx);
793 if (!mos7840_port->read_urb) {
794 dev_dbg(&port->dev, "%s", "URB KILLED !!!\n");
795 mos7840_port->read_urb_busy = false;
796 return;
799 if (mos7840_port->has_led)
800 mos7840_led_activity(port);
802 mos7840_port->read_urb_busy = true;
803 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
805 if (retval) {
806 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
807 mos7840_port->read_urb_busy = false;
811 /*****************************************************************************
812 * mos7840_bulk_out_data_callback
813 * this is the callback function for when we have finished sending
814 * serial data on the bulk out endpoint.
815 *****************************************************************************/
817 static void mos7840_bulk_out_data_callback(struct urb *urb)
819 struct moschip_port *mos7840_port;
820 struct usb_serial_port *port;
821 int status = urb->status;
822 int i;
824 mos7840_port = urb->context;
825 port = mos7840_port->port;
826 spin_lock(&mos7840_port->pool_lock);
827 for (i = 0; i < NUM_URBS; i++) {
828 if (urb == mos7840_port->write_urb_pool[i]) {
829 mos7840_port->busy[i] = 0;
830 break;
833 spin_unlock(&mos7840_port->pool_lock);
835 if (status) {
836 dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status);
837 return;
840 if (mos7840_port_paranoia_check(port, __func__))
841 return;
843 if (mos7840_port->open)
844 tty_port_tty_wakeup(&port->port);
848 /************************************************************************/
849 /* 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 */
850 /************************************************************************/
852 /*****************************************************************************
853 * mos7840_open
854 * this function is called by the tty driver when a port is opened
855 * If successful, we return 0
856 * Otherwise we return a negative error number.
857 *****************************************************************************/
859 static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
861 int response;
862 int j;
863 struct usb_serial *serial;
864 struct urb *urb;
865 __u16 Data;
866 int status;
867 struct moschip_port *mos7840_port;
868 struct moschip_port *port0;
870 if (mos7840_port_paranoia_check(port, __func__))
871 return -ENODEV;
873 serial = port->serial;
875 if (mos7840_serial_paranoia_check(serial, __func__))
876 return -ENODEV;
878 mos7840_port = mos7840_get_port_private(port);
879 port0 = mos7840_get_port_private(serial->port[0]);
881 if (mos7840_port == NULL || port0 == NULL)
882 return -ENODEV;
884 usb_clear_halt(serial->dev, port->write_urb->pipe);
885 usb_clear_halt(serial->dev, port->read_urb->pipe);
886 port0->open_ports++;
888 /* Initialising the write urb pool */
889 for (j = 0; j < NUM_URBS; ++j) {
890 urb = usb_alloc_urb(0, GFP_KERNEL);
891 mos7840_port->write_urb_pool[j] = urb;
892 if (!urb)
893 continue;
895 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
896 GFP_KERNEL);
897 if (!urb->transfer_buffer) {
898 usb_free_urb(urb);
899 mos7840_port->write_urb_pool[j] = NULL;
900 continue;
904 /*****************************************************************************
905 * Initialize MCS7840 -- Write Init values to corresponding Registers
907 * Register Index
908 * 1 : IER
909 * 2 : FCR
910 * 3 : LCR
911 * 4 : MCR
913 * 0x08 : SP1/2 Control Reg
914 *****************************************************************************/
916 /* NEED to check the following Block */
918 Data = 0x0;
919 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
920 if (status < 0) {
921 dev_dbg(&port->dev, "Reading Spreg failed\n");
922 goto err;
924 Data |= 0x80;
925 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
926 if (status < 0) {
927 dev_dbg(&port->dev, "writing Spreg failed\n");
928 goto err;
931 Data &= ~0x80;
932 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
933 if (status < 0) {
934 dev_dbg(&port->dev, "writing Spreg failed\n");
935 goto err;
937 /* End of block to be checked */
939 Data = 0x0;
940 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
941 &Data);
942 if (status < 0) {
943 dev_dbg(&port->dev, "Reading Controlreg failed\n");
944 goto err;
946 Data |= 0x08; /* Driver done bit */
947 Data |= 0x20; /* rx_disable */
948 status = mos7840_set_reg_sync(port,
949 mos7840_port->ControlRegOffset, Data);
950 if (status < 0) {
951 dev_dbg(&port->dev, "writing Controlreg failed\n");
952 goto err;
954 /* do register settings here */
955 /* Set all regs to the device default values. */
956 /***********************************
957 * First Disable all interrupts.
958 ***********************************/
959 Data = 0x00;
960 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
961 if (status < 0) {
962 dev_dbg(&port->dev, "disabling interrupts failed\n");
963 goto err;
965 /* Set FIFO_CONTROL_REGISTER to the default value */
966 Data = 0x00;
967 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
968 if (status < 0) {
969 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
970 goto err;
973 Data = 0xcf;
974 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
975 if (status < 0) {
976 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
977 goto err;
980 Data = 0x03;
981 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
982 mos7840_port->shadowLCR = Data;
984 Data = 0x0b;
985 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
986 mos7840_port->shadowMCR = Data;
988 Data = 0x00;
989 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
990 mos7840_port->shadowLCR = Data;
992 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
993 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
995 Data = 0x0c;
996 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
998 Data = 0x0;
999 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1001 Data = 0x00;
1002 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1004 Data = Data & ~SERIAL_LCR_DLAB;
1005 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1006 mos7840_port->shadowLCR = Data;
1008 /* clearing Bulkin and Bulkout Fifo */
1009 Data = 0x0;
1010 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
1012 Data = Data | 0x0c;
1013 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1015 Data = Data & ~0x0c;
1016 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1017 /* Finally enable all interrupts */
1018 Data = 0x0c;
1019 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1021 /* clearing rx_disable */
1022 Data = 0x0;
1023 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1024 &Data);
1025 Data = Data & ~0x20;
1026 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1027 Data);
1029 /* rx_negate */
1030 Data = 0x0;
1031 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1032 &Data);
1033 Data = Data | 0x10;
1034 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1035 Data);
1037 /* Check to see if we've set up our endpoint info yet *
1038 * (can't set it up in mos7840_startup as the structures *
1039 * were not set up at that time.) */
1040 if (port0->open_ports == 1) {
1041 /* FIXME: Buffer never NULL, so URB is not submitted. */
1042 if (serial->port[0]->interrupt_in_buffer == NULL) {
1043 /* set up interrupt urb */
1044 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
1045 serial->dev,
1046 usb_rcvintpipe(serial->dev,
1047 serial->port[0]->interrupt_in_endpointAddress),
1048 serial->port[0]->interrupt_in_buffer,
1049 serial->port[0]->interrupt_in_urb->
1050 transfer_buffer_length,
1051 mos7840_interrupt_callback,
1052 serial,
1053 serial->port[0]->interrupt_in_urb->interval);
1055 /* start interrupt read for mos7840 *
1056 * will continue as long as mos7840 is connected */
1058 response =
1059 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1060 GFP_KERNEL);
1061 if (response) {
1062 dev_err(&port->dev, "%s - Error %d submitting "
1063 "interrupt urb\n", __func__, response);
1070 /* see if we've set up our endpoint info yet *
1071 * (can't set it up in mos7840_startup as the *
1072 * structures were not set up at that time.) */
1074 dev_dbg(&port->dev, "port number is %d\n", port->port_number);
1075 dev_dbg(&port->dev, "minor number is %d\n", port->minor);
1076 dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
1077 dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
1078 dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress);
1079 dev_dbg(&port->dev, "port's number in the device is %d\n", mos7840_port->port_num);
1080 mos7840_port->read_urb = port->read_urb;
1082 /* set up our bulk in urb */
1083 if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
1084 usb_fill_bulk_urb(mos7840_port->read_urb,
1085 serial->dev,
1086 usb_rcvbulkpipe(serial->dev,
1087 (port->bulk_in_endpointAddress) + 2),
1088 port->bulk_in_buffer,
1089 mos7840_port->read_urb->transfer_buffer_length,
1090 mos7840_bulk_in_callback, mos7840_port);
1091 } else {
1092 usb_fill_bulk_urb(mos7840_port->read_urb,
1093 serial->dev,
1094 usb_rcvbulkpipe(serial->dev,
1095 port->bulk_in_endpointAddress),
1096 port->bulk_in_buffer,
1097 mos7840_port->read_urb->transfer_buffer_length,
1098 mos7840_bulk_in_callback, mos7840_port);
1101 dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress);
1102 mos7840_port->read_urb_busy = true;
1103 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1104 if (response) {
1105 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1106 __func__, response);
1107 mos7840_port->read_urb_busy = false;
1110 /* initialize our port settings */
1111 /* Must set to enable ints! */
1112 mos7840_port->shadowMCR = MCR_MASTER_IE;
1113 /* send a open port command */
1114 mos7840_port->open = 1;
1115 /* mos7840_change_port_settings(mos7840_port,old_termios); */
1117 return 0;
1118 err:
1119 for (j = 0; j < NUM_URBS; ++j) {
1120 urb = mos7840_port->write_urb_pool[j];
1121 if (!urb)
1122 continue;
1123 kfree(urb->transfer_buffer);
1124 usb_free_urb(urb);
1126 return status;
1129 /*****************************************************************************
1130 * mos7840_chars_in_buffer
1131 * this function is called by the tty driver when it wants to know how many
1132 * bytes of data we currently have outstanding in the port (data that has
1133 * been written, but hasn't made it out the port yet)
1134 * If successful, we return the number of bytes left to be written in the
1135 * system,
1136 * Otherwise we return zero.
1137 *****************************************************************************/
1139 static int mos7840_chars_in_buffer(struct tty_struct *tty)
1141 struct usb_serial_port *port = tty->driver_data;
1142 int i;
1143 int chars = 0;
1144 unsigned long flags;
1145 struct moschip_port *mos7840_port;
1147 if (mos7840_port_paranoia_check(port, __func__))
1148 return 0;
1150 mos7840_port = mos7840_get_port_private(port);
1151 if (mos7840_port == NULL)
1152 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 struct urb *urb = mos7840_port->write_urb_pool[i];
1158 chars += urb->transfer_buffer_length;
1161 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1162 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
1163 return chars;
1167 /*****************************************************************************
1168 * mos7840_close
1169 * this function is called by the tty driver when a port is closed
1170 *****************************************************************************/
1172 static void mos7840_close(struct usb_serial_port *port)
1174 struct usb_serial *serial;
1175 struct moschip_port *mos7840_port;
1176 struct moschip_port *port0;
1177 int j;
1178 __u16 Data;
1180 if (mos7840_port_paranoia_check(port, __func__))
1181 return;
1183 serial = mos7840_get_usb_serial(port, __func__);
1184 if (!serial)
1185 return;
1187 mos7840_port = mos7840_get_port_private(port);
1188 port0 = mos7840_get_port_private(serial->port[0]);
1190 if (mos7840_port == NULL || port0 == NULL)
1191 return;
1193 for (j = 0; j < NUM_URBS; ++j)
1194 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1196 /* Freeing Write URBs */
1197 for (j = 0; j < NUM_URBS; ++j) {
1198 if (mos7840_port->write_urb_pool[j]) {
1199 kfree(mos7840_port->write_urb_pool[j]->transfer_buffer);
1200 usb_free_urb(mos7840_port->write_urb_pool[j]);
1204 usb_kill_urb(mos7840_port->write_urb);
1205 usb_kill_urb(mos7840_port->read_urb);
1206 mos7840_port->read_urb_busy = false;
1208 port0->open_ports--;
1209 dev_dbg(&port->dev, "%s in close%d\n", __func__, port0->open_ports);
1210 if (port0->open_ports == 0) {
1211 if (serial->port[0]->interrupt_in_urb) {
1212 dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n");
1213 usb_kill_urb(serial->port[0]->interrupt_in_urb);
1217 if (mos7840_port->write_urb) {
1218 /* if this urb had a transfer buffer already (old tx) free it */
1219 kfree(mos7840_port->write_urb->transfer_buffer);
1220 usb_free_urb(mos7840_port->write_urb);
1223 Data = 0x0;
1224 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1226 Data = 0x00;
1227 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1229 mos7840_port->open = 0;
1232 /*****************************************************************************
1233 * mos7840_break
1234 * this function sends a break to the port
1235 *****************************************************************************/
1236 static void mos7840_break(struct tty_struct *tty, int break_state)
1238 struct usb_serial_port *port = tty->driver_data;
1239 unsigned char data;
1240 struct usb_serial *serial;
1241 struct moschip_port *mos7840_port;
1243 if (mos7840_port_paranoia_check(port, __func__))
1244 return;
1246 serial = mos7840_get_usb_serial(port, __func__);
1247 if (!serial)
1248 return;
1250 mos7840_port = mos7840_get_port_private(port);
1252 if (mos7840_port == NULL)
1253 return;
1255 if (break_state == -1)
1256 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
1257 else
1258 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
1260 /* FIXME: no locking on shadowLCR anywhere in driver */
1261 mos7840_port->shadowLCR = data;
1262 dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
1263 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1264 mos7840_port->shadowLCR);
1267 /*****************************************************************************
1268 * mos7840_write_room
1269 * this function is called by the tty driver when it wants to know how many
1270 * bytes of data we can accept for a specific port.
1271 * If successful, we return the amount of room that we have for this port
1272 * Otherwise we return a negative error number.
1273 *****************************************************************************/
1275 static int mos7840_write_room(struct tty_struct *tty)
1277 struct usb_serial_port *port = tty->driver_data;
1278 int i;
1279 int room = 0;
1280 unsigned long flags;
1281 struct moschip_port *mos7840_port;
1283 if (mos7840_port_paranoia_check(port, __func__))
1284 return -1;
1286 mos7840_port = mos7840_get_port_private(port);
1287 if (mos7840_port == NULL)
1288 return -1;
1290 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1291 for (i = 0; i < NUM_URBS; ++i) {
1292 if (!mos7840_port->busy[i])
1293 room += URB_TRANSFER_BUFFER_SIZE;
1295 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1297 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
1298 dev_dbg(&mos7840_port->port->dev, "%s - returns %d\n", __func__, room);
1299 return room;
1303 /*****************************************************************************
1304 * mos7840_write
1305 * this function is called by the tty driver when data should be written to
1306 * the port.
1307 * If successful, we return the number of bytes written, otherwise we
1308 * return a negative error number.
1309 *****************************************************************************/
1311 static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
1312 const unsigned char *data, int count)
1314 int status;
1315 int i;
1316 int bytes_sent = 0;
1317 int transfer_size;
1318 unsigned long flags;
1320 struct moschip_port *mos7840_port;
1321 struct usb_serial *serial;
1322 struct urb *urb;
1323 /* __u16 Data; */
1324 const unsigned char *current_position = data;
1325 unsigned char *data1;
1327 if (mos7840_port_paranoia_check(port, __func__))
1328 return -1;
1330 serial = port->serial;
1331 if (mos7840_serial_paranoia_check(serial, __func__))
1332 return -1;
1334 mos7840_port = mos7840_get_port_private(port);
1335 if (mos7840_port == NULL)
1336 return -1;
1338 /* try to find a free urb in the list */
1339 urb = NULL;
1341 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1342 for (i = 0; i < NUM_URBS; ++i) {
1343 if (!mos7840_port->busy[i]) {
1344 mos7840_port->busy[i] = 1;
1345 urb = mos7840_port->write_urb_pool[i];
1346 dev_dbg(&port->dev, "URB:%d\n", i);
1347 break;
1350 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1352 if (urb == NULL) {
1353 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
1354 goto exit;
1357 if (urb->transfer_buffer == NULL) {
1358 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1359 GFP_ATOMIC);
1360 if (!urb->transfer_buffer)
1361 goto exit;
1363 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1365 memcpy(urb->transfer_buffer, current_position, transfer_size);
1367 /* fill urb with data and submit */
1368 if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
1369 usb_fill_bulk_urb(urb,
1370 serial->dev,
1371 usb_sndbulkpipe(serial->dev,
1372 (port->bulk_out_endpointAddress) + 2),
1373 urb->transfer_buffer,
1374 transfer_size,
1375 mos7840_bulk_out_data_callback, mos7840_port);
1376 } else {
1377 usb_fill_bulk_urb(urb,
1378 serial->dev,
1379 usb_sndbulkpipe(serial->dev,
1380 port->bulk_out_endpointAddress),
1381 urb->transfer_buffer,
1382 transfer_size,
1383 mos7840_bulk_out_data_callback, mos7840_port);
1386 data1 = urb->transfer_buffer;
1387 dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress);
1389 if (mos7840_port->has_led)
1390 mos7840_led_activity(port);
1392 /* send it down the pipe */
1393 status = usb_submit_urb(urb, GFP_ATOMIC);
1395 if (status) {
1396 mos7840_port->busy[i] = 0;
1397 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1398 "with status = %d\n", __func__, status);
1399 bytes_sent = status;
1400 goto exit;
1402 bytes_sent = transfer_size;
1403 port->icount.tx += transfer_size;
1404 dev_dbg(&port->dev, "icount.tx is %d:\n", port->icount.tx);
1405 exit:
1406 return bytes_sent;
1410 /*****************************************************************************
1411 * mos7840_throttle
1412 * this function is called by the tty driver when it wants to stop the data
1413 * being read from the port.
1414 *****************************************************************************/
1416 static void mos7840_throttle(struct tty_struct *tty)
1418 struct usb_serial_port *port = tty->driver_data;
1419 struct moschip_port *mos7840_port;
1420 int status;
1422 if (mos7840_port_paranoia_check(port, __func__))
1423 return;
1425 mos7840_port = mos7840_get_port_private(port);
1427 if (mos7840_port == NULL)
1428 return;
1430 if (!mos7840_port->open) {
1431 dev_dbg(&port->dev, "%s", "port not opened\n");
1432 return;
1435 /* if we are implementing XON/XOFF, send the stop character */
1436 if (I_IXOFF(tty)) {
1437 unsigned char stop_char = STOP_CHAR(tty);
1438 status = mos7840_write(tty, port, &stop_char, 1);
1439 if (status <= 0)
1440 return;
1442 /* if we are implementing RTS/CTS, toggle that line */
1443 if (C_CRTSCTS(tty)) {
1444 mos7840_port->shadowMCR &= ~MCR_RTS;
1445 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1446 mos7840_port->shadowMCR);
1447 if (status < 0)
1448 return;
1452 /*****************************************************************************
1453 * mos7840_unthrottle
1454 * this function is called by the tty driver when it wants to resume
1455 * the data being read from the port (called after mos7840_throttle is
1456 * called)
1457 *****************************************************************************/
1458 static void mos7840_unthrottle(struct tty_struct *tty)
1460 struct usb_serial_port *port = tty->driver_data;
1461 int status;
1462 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1464 if (mos7840_port_paranoia_check(port, __func__))
1465 return;
1467 if (mos7840_port == NULL)
1468 return;
1470 if (!mos7840_port->open) {
1471 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1472 return;
1475 /* if we are implementing XON/XOFF, send the start character */
1476 if (I_IXOFF(tty)) {
1477 unsigned char start_char = START_CHAR(tty);
1478 status = mos7840_write(tty, port, &start_char, 1);
1479 if (status <= 0)
1480 return;
1483 /* if we are implementing RTS/CTS, toggle that line */
1484 if (C_CRTSCTS(tty)) {
1485 mos7840_port->shadowMCR |= MCR_RTS;
1486 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1487 mos7840_port->shadowMCR);
1488 if (status < 0)
1489 return;
1493 static int mos7840_tiocmget(struct tty_struct *tty)
1495 struct usb_serial_port *port = tty->driver_data;
1496 struct moschip_port *mos7840_port;
1497 unsigned int result;
1498 __u16 msr;
1499 __u16 mcr;
1500 int status;
1501 mos7840_port = mos7840_get_port_private(port);
1503 if (mos7840_port == NULL)
1504 return -ENODEV;
1506 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1507 if (status < 0)
1508 return -EIO;
1509 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1510 if (status < 0)
1511 return -EIO;
1512 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1513 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1514 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1515 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1516 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1517 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1518 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1520 dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
1522 return result;
1525 static int mos7840_tiocmset(struct tty_struct *tty,
1526 unsigned int set, unsigned int clear)
1528 struct usb_serial_port *port = tty->driver_data;
1529 struct moschip_port *mos7840_port;
1530 unsigned int mcr;
1531 int status;
1533 mos7840_port = mos7840_get_port_private(port);
1535 if (mos7840_port == NULL)
1536 return -ENODEV;
1538 /* FIXME: What locks the port registers ? */
1539 mcr = mos7840_port->shadowMCR;
1540 if (clear & TIOCM_RTS)
1541 mcr &= ~MCR_RTS;
1542 if (clear & TIOCM_DTR)
1543 mcr &= ~MCR_DTR;
1544 if (clear & TIOCM_LOOP)
1545 mcr &= ~MCR_LOOPBACK;
1547 if (set & TIOCM_RTS)
1548 mcr |= MCR_RTS;
1549 if (set & TIOCM_DTR)
1550 mcr |= MCR_DTR;
1551 if (set & TIOCM_LOOP)
1552 mcr |= MCR_LOOPBACK;
1554 mos7840_port->shadowMCR = mcr;
1556 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1557 if (status < 0) {
1558 dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n");
1559 return status;
1562 return 0;
1565 /*****************************************************************************
1566 * mos7840_calc_baud_rate_divisor
1567 * this function calculates the proper baud rate divisor for the specified
1568 * baud rate.
1569 *****************************************************************************/
1570 static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port,
1571 int baudRate, int *divisor,
1572 __u16 *clk_sel_val)
1574 dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate);
1576 if (baudRate <= 115200) {
1577 *divisor = 115200 / baudRate;
1578 *clk_sel_val = 0x0;
1580 if ((baudRate > 115200) && (baudRate <= 230400)) {
1581 *divisor = 230400 / baudRate;
1582 *clk_sel_val = 0x10;
1583 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1584 *divisor = 403200 / baudRate;
1585 *clk_sel_val = 0x20;
1586 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1587 *divisor = 460800 / baudRate;
1588 *clk_sel_val = 0x30;
1589 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1590 *divisor = 806400 / baudRate;
1591 *clk_sel_val = 0x40;
1592 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1593 *divisor = 921600 / baudRate;
1594 *clk_sel_val = 0x50;
1595 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1596 *divisor = 1572864 / baudRate;
1597 *clk_sel_val = 0x60;
1598 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1599 *divisor = 3145728 / baudRate;
1600 *clk_sel_val = 0x70;
1602 return 0;
1605 /*****************************************************************************
1606 * mos7840_send_cmd_write_baud_rate
1607 * this function sends the proper command to change the baud rate of the
1608 * specified port.
1609 *****************************************************************************/
1611 static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1612 int baudRate)
1614 int divisor = 0;
1615 int status;
1616 __u16 Data;
1617 unsigned char number;
1618 __u16 clk_sel_val;
1619 struct usb_serial_port *port;
1621 if (mos7840_port == NULL)
1622 return -1;
1624 port = mos7840_port->port;
1625 if (mos7840_port_paranoia_check(port, __func__))
1626 return -1;
1628 if (mos7840_serial_paranoia_check(port->serial, __func__))
1629 return -1;
1631 number = mos7840_port->port->port_number;
1633 dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudRate);
1634 /* reset clk_uart_sel in spregOffset */
1635 if (baudRate > 115200) {
1636 #ifdef HW_flow_control
1637 /* NOTE: need to see the pther register to modify */
1638 /* setting h/w flow control bit to 1 */
1639 Data = 0x2b;
1640 mos7840_port->shadowMCR = Data;
1641 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1642 Data);
1643 if (status < 0) {
1644 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1645 return -1;
1647 #endif
1649 } else {
1650 #ifdef HW_flow_control
1651 /* setting h/w flow control bit to 0 */
1652 Data = 0xb;
1653 mos7840_port->shadowMCR = Data;
1654 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1655 Data);
1656 if (status < 0) {
1657 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1658 return -1;
1660 #endif
1664 if (1) { /* baudRate <= 115200) */
1665 clk_sel_val = 0x0;
1666 Data = 0x0;
1667 status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor,
1668 &clk_sel_val);
1669 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1670 &Data);
1671 if (status < 0) {
1672 dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n");
1673 return -1;
1675 Data = (Data & 0x8f) | clk_sel_val;
1676 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1677 Data);
1678 if (status < 0) {
1679 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1680 return -1;
1682 /* Calculate the Divisor */
1684 if (status) {
1685 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1686 return status;
1688 /* Enable access to divisor latch */
1689 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1690 mos7840_port->shadowLCR = Data;
1691 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1693 /* Write the divisor */
1694 Data = (unsigned char)(divisor & 0xff);
1695 dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data);
1696 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1698 Data = (unsigned char)((divisor & 0xff00) >> 8);
1699 dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data);
1700 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1702 /* Disable access to divisor latch */
1703 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1704 mos7840_port->shadowLCR = Data;
1705 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1708 return status;
1711 /*****************************************************************************
1712 * mos7840_change_port_settings
1713 * This routine is called to set the UART on the device to match
1714 * the specified new settings.
1715 *****************************************************************************/
1717 static void mos7840_change_port_settings(struct tty_struct *tty,
1718 struct moschip_port *mos7840_port, struct ktermios *old_termios)
1720 int baud;
1721 unsigned cflag;
1722 unsigned iflag;
1723 __u8 lData;
1724 __u8 lParity;
1725 __u8 lStop;
1726 int status;
1727 __u16 Data;
1728 struct usb_serial_port *port;
1729 struct usb_serial *serial;
1731 if (mos7840_port == NULL)
1732 return;
1734 port = mos7840_port->port;
1736 if (mos7840_port_paranoia_check(port, __func__))
1737 return;
1739 if (mos7840_serial_paranoia_check(port->serial, __func__))
1740 return;
1742 serial = port->serial;
1744 if (!mos7840_port->open) {
1745 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1746 return;
1749 lData = LCR_BITS_8;
1750 lStop = LCR_STOP_1;
1751 lParity = LCR_PAR_NONE;
1753 cflag = tty->termios.c_cflag;
1754 iflag = tty->termios.c_iflag;
1756 /* Change the number of bits */
1757 switch (cflag & CSIZE) {
1758 case CS5:
1759 lData = LCR_BITS_5;
1760 break;
1762 case CS6:
1763 lData = LCR_BITS_6;
1764 break;
1766 case CS7:
1767 lData = LCR_BITS_7;
1768 break;
1770 default:
1771 case CS8:
1772 lData = LCR_BITS_8;
1773 break;
1776 /* Change the Parity bit */
1777 if (cflag & PARENB) {
1778 if (cflag & PARODD) {
1779 lParity = LCR_PAR_ODD;
1780 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
1781 } else {
1782 lParity = LCR_PAR_EVEN;
1783 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
1786 } else {
1787 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
1790 if (cflag & CMSPAR)
1791 lParity = lParity | 0x20;
1793 /* Change the Stop bit */
1794 if (cflag & CSTOPB) {
1795 lStop = LCR_STOP_2;
1796 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
1797 } else {
1798 lStop = LCR_STOP_1;
1799 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
1802 /* Update the LCR with the correct value */
1803 mos7840_port->shadowLCR &=
1804 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1805 mos7840_port->shadowLCR |= (lData | lParity | lStop);
1807 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__,
1808 mos7840_port->shadowLCR);
1809 /* Disable Interrupts */
1810 Data = 0x00;
1811 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1813 Data = 0x00;
1814 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1816 Data = 0xcf;
1817 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1819 /* Send the updated LCR value to the mos7840 */
1820 Data = mos7840_port->shadowLCR;
1822 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1824 Data = 0x00b;
1825 mos7840_port->shadowMCR = Data;
1826 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1827 Data = 0x00b;
1828 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1830 /* set up the MCR register and send it to the mos7840 */
1832 mos7840_port->shadowMCR = MCR_MASTER_IE;
1833 if (cflag & CBAUD)
1834 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
1836 if (cflag & CRTSCTS)
1837 mos7840_port->shadowMCR |= (MCR_XON_ANY);
1838 else
1839 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
1841 Data = mos7840_port->shadowMCR;
1842 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1844 /* Determine divisor based on baud rate */
1845 baud = tty_get_baud_rate(tty);
1847 if (!baud) {
1848 /* pick a default, any default... */
1849 dev_dbg(&port->dev, "%s", "Picked default baud...\n");
1850 baud = 9600;
1853 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
1854 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
1856 /* Enable Interrupts */
1857 Data = 0x0c;
1858 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1860 if (!mos7840_port->read_urb_busy) {
1861 mos7840_port->read_urb_busy = true;
1862 status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1863 if (status) {
1864 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
1865 status);
1866 mos7840_port->read_urb_busy = false;
1869 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__,
1870 mos7840_port->shadowLCR);
1873 /*****************************************************************************
1874 * mos7840_set_termios
1875 * this function is called by the tty driver when it wants to change
1876 * the termios structure
1877 *****************************************************************************/
1879 static void mos7840_set_termios(struct tty_struct *tty,
1880 struct usb_serial_port *port,
1881 struct ktermios *old_termios)
1883 int status;
1884 unsigned int cflag;
1885 struct usb_serial *serial;
1886 struct moschip_port *mos7840_port;
1888 if (mos7840_port_paranoia_check(port, __func__))
1889 return;
1891 serial = port->serial;
1893 if (mos7840_serial_paranoia_check(serial, __func__))
1894 return;
1896 mos7840_port = mos7840_get_port_private(port);
1898 if (mos7840_port == NULL)
1899 return;
1901 if (!mos7840_port->open) {
1902 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1903 return;
1906 dev_dbg(&port->dev, "%s", "setting termios - \n");
1908 cflag = tty->termios.c_cflag;
1910 dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__,
1911 tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag));
1912 dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__,
1913 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
1915 /* change the port settings to the new ones specified */
1917 mos7840_change_port_settings(tty, mos7840_port, old_termios);
1919 if (!mos7840_port->read_urb) {
1920 dev_dbg(&port->dev, "%s", "URB KILLED !!!!!\n");
1921 return;
1924 if (!mos7840_port->read_urb_busy) {
1925 mos7840_port->read_urb_busy = true;
1926 status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1927 if (status) {
1928 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
1929 status);
1930 mos7840_port->read_urb_busy = false;
1935 /*****************************************************************************
1936 * mos7840_get_lsr_info - get line status register info
1938 * Purpose: Let user call ioctl() to get info when the UART physically
1939 * is emptied. On bus types like RS485, the transmitter must
1940 * release the bus after transmitting. This must be done when
1941 * the transmit shift register is empty, not be done when the
1942 * transmit holding register is empty. This functionality
1943 * allows an RS485 driver to be written in user space.
1944 *****************************************************************************/
1946 static int mos7840_get_lsr_info(struct tty_struct *tty,
1947 unsigned int __user *value)
1949 int count;
1950 unsigned int result = 0;
1952 count = mos7840_chars_in_buffer(tty);
1953 if (count == 0)
1954 result = TIOCSER_TEMT;
1956 if (copy_to_user(value, &result, sizeof(int)))
1957 return -EFAULT;
1958 return 0;
1961 /*****************************************************************************
1962 * mos7840_get_serial_info
1963 * function to get information about serial port
1964 *****************************************************************************/
1966 static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
1967 struct serial_struct __user *retinfo)
1969 struct serial_struct tmp;
1971 if (mos7840_port == NULL)
1972 return -1;
1974 if (!retinfo)
1975 return -EFAULT;
1977 memset(&tmp, 0, sizeof(tmp));
1979 tmp.type = PORT_16550A;
1980 tmp.line = mos7840_port->port->minor;
1981 tmp.port = mos7840_port->port->port_number;
1982 tmp.irq = 0;
1983 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1984 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1985 tmp.baud_base = 9600;
1986 tmp.close_delay = 5 * HZ;
1987 tmp.closing_wait = 30 * HZ;
1989 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1990 return -EFAULT;
1991 return 0;
1994 /*****************************************************************************
1995 * SerialIoctl
1996 * this function handles any ioctl calls to the driver
1997 *****************************************************************************/
1999 static int mos7840_ioctl(struct tty_struct *tty,
2000 unsigned int cmd, unsigned long arg)
2002 struct usb_serial_port *port = tty->driver_data;
2003 void __user *argp = (void __user *)arg;
2004 struct moschip_port *mos7840_port;
2006 if (mos7840_port_paranoia_check(port, __func__))
2007 return -1;
2009 mos7840_port = mos7840_get_port_private(port);
2011 if (mos7840_port == NULL)
2012 return -1;
2014 switch (cmd) {
2015 /* return number of bytes available */
2017 case TIOCSERGETLSR:
2018 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
2019 return mos7840_get_lsr_info(tty, argp);
2021 case TIOCGSERIAL:
2022 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
2023 return mos7840_get_serial_info(mos7840_port, argp);
2025 case TIOCSSERIAL:
2026 dev_dbg(&port->dev, "%s TIOCSSERIAL\n", __func__);
2027 break;
2028 default:
2029 break;
2031 return -ENOIOCTLCMD;
2034 static int mos7810_check(struct usb_serial *serial)
2036 int i, pass_count = 0;
2037 u8 *buf;
2038 __u16 data = 0, mcr_data = 0;
2039 __u16 test_pattern = 0x55AA;
2040 int res;
2042 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
2043 if (!buf)
2044 return 0; /* failed to identify 7810 */
2046 /* Store MCR setting */
2047 res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2048 MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
2049 buf, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2050 if (res == VENDOR_READ_LENGTH)
2051 mcr_data = *buf;
2053 for (i = 0; i < 16; i++) {
2054 /* Send the 1-bit test pattern out to MCS7810 test pin */
2055 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2056 MCS_WRREQ, MCS_WR_RTYPE,
2057 (0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
2058 MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
2060 /* Read the test pattern back */
2061 res = usb_control_msg(serial->dev,
2062 usb_rcvctrlpipe(serial->dev, 0), MCS_RDREQ,
2063 MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
2064 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2065 if (res == VENDOR_READ_LENGTH)
2066 data = *buf;
2068 /* If this is a MCS7810 device, both test patterns must match */
2069 if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
2070 break;
2072 pass_count++;
2075 /* Restore MCR setting */
2076 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
2077 MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
2078 0, MOS_WDR_TIMEOUT);
2080 kfree(buf);
2082 if (pass_count == 16)
2083 return 1;
2085 return 0;
2088 static int mos7840_probe(struct usb_serial *serial,
2089 const struct usb_device_id *id)
2091 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
2092 u8 *buf;
2093 int device_type;
2095 if (product == MOSCHIP_DEVICE_ID_7810 ||
2096 product == MOSCHIP_DEVICE_ID_7820) {
2097 device_type = product;
2098 goto out;
2101 buf = kzalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
2102 if (!buf)
2103 return -ENOMEM;
2105 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2106 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
2107 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2109 /* For a MCS7840 device GPIO0 must be set to 1 */
2110 if (buf[0] & 0x01)
2111 device_type = MOSCHIP_DEVICE_ID_7840;
2112 else if (mos7810_check(serial))
2113 device_type = MOSCHIP_DEVICE_ID_7810;
2114 else
2115 device_type = MOSCHIP_DEVICE_ID_7820;
2117 kfree(buf);
2118 out:
2119 usb_set_serial_data(serial, (void *)(unsigned long)device_type);
2121 return 0;
2124 static int mos7840_calc_num_ports(struct usb_serial *serial)
2126 int device_type = (unsigned long)usb_get_serial_data(serial);
2127 int mos7840_num_ports;
2129 mos7840_num_ports = (device_type >> 4) & 0x000F;
2131 return mos7840_num_ports;
2134 static int mos7840_attach(struct usb_serial *serial)
2136 if (serial->num_bulk_in < serial->num_ports ||
2137 serial->num_bulk_out < serial->num_ports ||
2138 serial->num_interrupt_in < 1) {
2139 dev_err(&serial->interface->dev, "missing endpoints\n");
2140 return -ENODEV;
2143 return 0;
2146 static int mos7840_port_probe(struct usb_serial_port *port)
2148 struct usb_serial *serial = port->serial;
2149 int device_type = (unsigned long)usb_get_serial_data(serial);
2150 struct moschip_port *mos7840_port;
2151 int status;
2152 int pnum;
2153 __u16 Data;
2155 /* we set up the pointers to the endpoints in the mos7840_open *
2156 * function, as the structures aren't created yet. */
2158 pnum = port->port_number;
2160 dev_dbg(&port->dev, "mos7840_startup: configuring port %d\n", pnum);
2161 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2162 if (!mos7840_port)
2163 return -ENOMEM;
2165 /* Initialize all port interrupt end point to port 0 int
2166 * endpoint. Our device has only one interrupt end point
2167 * common to all port */
2169 mos7840_port->port = port;
2170 mos7840_set_port_private(port, mos7840_port);
2171 spin_lock_init(&mos7840_port->pool_lock);
2173 /* minor is not initialised until later by
2174 * usb-serial.c:get_free_serial() and cannot therefore be used
2175 * to index device instances */
2176 mos7840_port->port_num = pnum + 1;
2177 dev_dbg(&port->dev, "port->minor = %d\n", port->minor);
2178 dev_dbg(&port->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num);
2180 if (mos7840_port->port_num == 1) {
2181 mos7840_port->SpRegOffset = 0x0;
2182 mos7840_port->ControlRegOffset = 0x1;
2183 mos7840_port->DcrRegOffset = 0x4;
2184 } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 4)) {
2185 mos7840_port->SpRegOffset = 0x8;
2186 mos7840_port->ControlRegOffset = 0x9;
2187 mos7840_port->DcrRegOffset = 0x16;
2188 } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 2)) {
2189 mos7840_port->SpRegOffset = 0xa;
2190 mos7840_port->ControlRegOffset = 0xb;
2191 mos7840_port->DcrRegOffset = 0x19;
2192 } else if ((mos7840_port->port_num == 3) && (serial->num_ports == 4)) {
2193 mos7840_port->SpRegOffset = 0xa;
2194 mos7840_port->ControlRegOffset = 0xb;
2195 mos7840_port->DcrRegOffset = 0x19;
2196 } else if ((mos7840_port->port_num == 4) && (serial->num_ports == 4)) {
2197 mos7840_port->SpRegOffset = 0xc;
2198 mos7840_port->ControlRegOffset = 0xd;
2199 mos7840_port->DcrRegOffset = 0x1c;
2201 mos7840_dump_serial_port(port, mos7840_port);
2202 mos7840_set_port_private(port, mos7840_port);
2204 /* enable rx_disable bit in control register */
2205 status = mos7840_get_reg_sync(port,
2206 mos7840_port->ControlRegOffset, &Data);
2207 if (status < 0) {
2208 dev_dbg(&port->dev, "Reading ControlReg failed status-0x%x\n", status);
2209 goto out;
2210 } else
2211 dev_dbg(&port->dev, "ControlReg Reading success val is %x, status%d\n", Data, status);
2212 Data |= 0x08; /* setting driver done bit */
2213 Data |= 0x04; /* sp1_bit to have cts change reflect in
2214 modem status reg */
2216 /* Data |= 0x20; //rx_disable bit */
2217 status = mos7840_set_reg_sync(port,
2218 mos7840_port->ControlRegOffset, Data);
2219 if (status < 0) {
2220 dev_dbg(&port->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status);
2221 goto out;
2222 } else
2223 dev_dbg(&port->dev, "ControlReg Writing success(rx_disable) status%d\n", status);
2225 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2226 and 0x24 in DCR3 */
2227 Data = 0x01;
2228 status = mos7840_set_reg_sync(port,
2229 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
2230 if (status < 0) {
2231 dev_dbg(&port->dev, "Writing DCR0 failed status-0x%x\n", status);
2232 goto out;
2233 } else
2234 dev_dbg(&port->dev, "DCR0 Writing success status%d\n", status);
2236 Data = 0x05;
2237 status = mos7840_set_reg_sync(port,
2238 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
2239 if (status < 0) {
2240 dev_dbg(&port->dev, "Writing DCR1 failed status-0x%x\n", status);
2241 goto out;
2242 } else
2243 dev_dbg(&port->dev, "DCR1 Writing success status%d\n", status);
2245 Data = 0x24;
2246 status = mos7840_set_reg_sync(port,
2247 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
2248 if (status < 0) {
2249 dev_dbg(&port->dev, "Writing DCR2 failed status-0x%x\n", status);
2250 goto out;
2251 } else
2252 dev_dbg(&port->dev, "DCR2 Writing success status%d\n", status);
2254 /* write values in clkstart0x0 and clkmulti 0x20 */
2255 Data = 0x0;
2256 status = mos7840_set_reg_sync(port, CLK_START_VALUE_REGISTER, Data);
2257 if (status < 0) {
2258 dev_dbg(&port->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
2259 goto out;
2260 } else
2261 dev_dbg(&port->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status);
2263 Data = 0x20;
2264 status = mos7840_set_reg_sync(port, CLK_MULTI_REGISTER, Data);
2265 if (status < 0) {
2266 dev_dbg(&port->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status);
2267 goto error;
2268 } else
2269 dev_dbg(&port->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status);
2271 /* write value 0x0 to scratchpad register */
2272 Data = 0x00;
2273 status = mos7840_set_uart_reg(port, SCRATCH_PAD_REGISTER, Data);
2274 if (status < 0) {
2275 dev_dbg(&port->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
2276 goto out;
2277 } else
2278 dev_dbg(&port->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status);
2280 /* Zero Length flag register */
2281 if ((mos7840_port->port_num != 1) && (serial->num_ports == 2)) {
2282 Data = 0xff;
2283 status = mos7840_set_reg_sync(port,
2284 (__u16) (ZLP_REG1 +
2285 ((__u16)mos7840_port->port_num)), Data);
2286 dev_dbg(&port->dev, "ZLIP offset %x\n",
2287 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num)));
2288 if (status < 0) {
2289 dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 2, status);
2290 goto out;
2291 } else
2292 dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 2, status);
2293 } else {
2294 Data = 0xff;
2295 status = mos7840_set_reg_sync(port,
2296 (__u16) (ZLP_REG1 +
2297 ((__u16)mos7840_port->port_num) - 0x1), Data);
2298 dev_dbg(&port->dev, "ZLIP offset %x\n",
2299 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1));
2300 if (status < 0) {
2301 dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 1, status);
2302 goto out;
2303 } else
2304 dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 1, status);
2307 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
2308 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2309 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2310 GFP_KERNEL);
2311 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2312 !mos7840_port->dr) {
2313 status = -ENOMEM;
2314 goto error;
2317 mos7840_port->has_led = false;
2319 /* Initialize LED timers */
2320 if (device_type == MOSCHIP_DEVICE_ID_7810) {
2321 mos7840_port->has_led = true;
2323 mos7840_port->led_urb = usb_alloc_urb(0, GFP_KERNEL);
2324 mos7840_port->led_dr = kmalloc(sizeof(*mos7840_port->led_dr),
2325 GFP_KERNEL);
2326 if (!mos7840_port->led_urb || !mos7840_port->led_dr) {
2327 status = -ENOMEM;
2328 goto error;
2331 setup_timer(&mos7840_port->led_timer1, mos7840_led_off,
2332 (unsigned long)mos7840_port);
2333 mos7840_port->led_timer1.expires =
2334 jiffies + msecs_to_jiffies(LED_ON_MS);
2335 setup_timer(&mos7840_port->led_timer2, mos7840_led_flag_off,
2336 (unsigned long)mos7840_port);
2337 mos7840_port->led_timer2.expires =
2338 jiffies + msecs_to_jiffies(LED_OFF_MS);
2340 /* Turn off LED */
2341 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
2343 out:
2344 if (pnum == serial->num_ports - 1) {
2345 /* Zero Length flag enable */
2346 Data = 0x0f;
2347 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2348 if (status < 0) {
2349 dev_dbg(&port->dev, "Writing ZLP_REG5 failed status-0x%x\n", status);
2350 goto error;
2351 } else
2352 dev_dbg(&port->dev, "ZLP_REG5 Writing success status%d\n", status);
2354 /* setting configuration feature to one */
2355 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2356 0x03, 0x00, 0x01, 0x00, NULL, 0x00,
2357 MOS_WDR_TIMEOUT);
2359 return 0;
2360 error:
2361 kfree(mos7840_port->led_dr);
2362 usb_free_urb(mos7840_port->led_urb);
2363 kfree(mos7840_port->dr);
2364 kfree(mos7840_port->ctrl_buf);
2365 usb_free_urb(mos7840_port->control_urb);
2366 kfree(mos7840_port);
2368 return status;
2371 static int mos7840_port_remove(struct usb_serial_port *port)
2373 struct moschip_port *mos7840_port;
2375 mos7840_port = mos7840_get_port_private(port);
2377 if (mos7840_port->has_led) {
2378 /* Turn off LED */
2379 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
2381 del_timer_sync(&mos7840_port->led_timer1);
2382 del_timer_sync(&mos7840_port->led_timer2);
2384 usb_kill_urb(mos7840_port->led_urb);
2385 usb_free_urb(mos7840_port->led_urb);
2386 kfree(mos7840_port->led_dr);
2388 usb_kill_urb(mos7840_port->control_urb);
2389 usb_free_urb(mos7840_port->control_urb);
2390 kfree(mos7840_port->ctrl_buf);
2391 kfree(mos7840_port->dr);
2392 kfree(mos7840_port);
2394 return 0;
2397 static struct usb_serial_driver moschip7840_4port_device = {
2398 .driver = {
2399 .owner = THIS_MODULE,
2400 .name = "mos7840",
2402 .description = DRIVER_DESC,
2403 .id_table = id_table,
2404 .num_ports = 4,
2405 .open = mos7840_open,
2406 .close = mos7840_close,
2407 .write = mos7840_write,
2408 .write_room = mos7840_write_room,
2409 .chars_in_buffer = mos7840_chars_in_buffer,
2410 .throttle = mos7840_throttle,
2411 .unthrottle = mos7840_unthrottle,
2412 .calc_num_ports = mos7840_calc_num_ports,
2413 .probe = mos7840_probe,
2414 .ioctl = mos7840_ioctl,
2415 .set_termios = mos7840_set_termios,
2416 .break_ctl = mos7840_break,
2417 .tiocmget = mos7840_tiocmget,
2418 .tiocmset = mos7840_tiocmset,
2419 .tiocmiwait = usb_serial_generic_tiocmiwait,
2420 .get_icount = usb_serial_generic_get_icount,
2421 .attach = mos7840_attach,
2422 .port_probe = mos7840_port_probe,
2423 .port_remove = mos7840_port_remove,
2424 .read_bulk_callback = mos7840_bulk_in_callback,
2425 .read_int_callback = mos7840_interrupt_callback,
2428 static struct usb_serial_driver * const serial_drivers[] = {
2429 &moschip7840_4port_device, NULL
2432 module_usb_serial_driver(serial_drivers, id_table);
2434 MODULE_DESCRIPTION(DRIVER_DESC);
2435 MODULE_LICENSE("GPL");