3 * Controls the Moschip 7720 usb to dual port serial convertor
5 * Copyright 2006 Moschip Semiconductor Tech. Ltd.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, version 2 of the License.
12 * Vijaya Kumar <vijaykumar.gn@gmail.com>
13 * Ajay Kumar <naanuajay@yahoo.com>
14 * Gurudeva <ngurudeva@yahoo.com>
16 * Cleaned up from the original by:
17 * Greg Kroah-Hartman <gregkh@suse.de>
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>
23 #include <linux/kernel.h>
24 #include <linux/errno.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/tty.h>
28 #include <linux/tty_driver.h>
29 #include <linux/tty_flip.h>
30 #include <linux/module.h>
31 #include <linux/spinlock.h>
32 #include <linux/serial.h>
33 #include <linux/serial_reg.h>
34 #include <linux/usb.h>
35 #include <linux/usb/serial.h>
36 #include <asm/uaccess.h>
42 #define DRIVER_VERSION "1.0.0.4F"
43 #define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
44 #define DRIVER_DESC "Moschip USB Serial Driver"
46 /* default urb timeout */
47 #define MOS_WDR_TIMEOUT (HZ * 5)
49 #define MOS_PORT1 0x0200
50 #define MOS_PORT2 0x0300
51 #define MOS_VENREG 0x0000
52 #define MOS_MAX_PORT 0x02
53 #define MOS_WRITE 0x0E
56 /* Interrupt Rotinue Defines */
57 #define SERIAL_IIR_RLS 0x06
58 #define SERIAL_IIR_RDA 0x04
59 #define SERIAL_IIR_CTI 0x0c
60 #define SERIAL_IIR_THR 0x02
61 #define SERIAL_IIR_MS 0x00
63 #define NUM_URBS 16 /* URB Count */
64 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
66 /* This structure holds all of the local port information */
69 __u8 shadowLCR; /* last LCR value received */
70 __u8 shadowMCR; /* last MCR value received */
71 __u8 shadowMSR; /* last MSR value received */
73 struct async_icount icount;
74 struct usb_serial_port *port; /* loop back to the owner */
75 struct urb *write_urb_pool[NUM_URBS];
78 /* This structure holds all of the individual serial device information */
81 int interrupt_started;
86 #define USB_VENDOR_ID_MOSCHIP 0x9710
87 #define MOSCHIP_DEVICE_ID_7720 0x7720
88 #define MOSCHIP_DEVICE_ID_7715 0x7715
90 static struct usb_device_id moschip_port_id_table [] = {
91 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP,MOSCHIP_DEVICE_ID_7720) },
92 { } /* terminating entry */
94 MODULE_DEVICE_TABLE(usb, moschip_port_id_table);
98 * mos7720_interrupt_callback
99 * this is the callback function for when we have received data on the
100 * interrupt endpoint.
102 static void mos7720_interrupt_callback(struct urb *urb)
106 int status = urb->status;
111 dbg("%s"," : Entering\n");
120 /* this urb is terminated, clean up */
121 dbg("%s - urb shutting down with status: %d", __func__,
125 dbg("%s - nonzero urb status received: %d", __func__,
130 length = urb->actual_length;
131 data = urb->transfer_buffer;
133 /* Moschip get 4 bytes
134 * Byte 1 IIR Port 1 (port.number is 0)
135 * Byte 2 IIR Port 2 (port.number is 1)
136 * Byte 3 --------------
137 * Byte 4 FIFO status for both */
139 /* the above description is inverted
140 * oneukum 2007-03-14 */
142 if (unlikely(length != 4)) {
143 dbg("Wrong data !!!");
150 if ((sp1 | sp2) & 0x01) {
151 /* No Interrupt Pending in both the ports */
152 dbg("No Interrupt !!!");
154 switch (sp1 & 0x0f) {
156 dbg("Serial Port 1: Receiver status error or address "
157 "bit detected in 9-bit mode\n");
160 dbg("Serial Port 1: Receiver time out");
163 dbg("Serial Port 1: Modem status change");
167 switch (sp2 & 0x0f) {
169 dbg("Serial Port 2: Receiver status error or address "
170 "bit detected in 9-bit mode");
173 dbg("Serial Port 2: Receiver time out");
176 dbg("Serial Port 2: Modem status change");
182 result = usb_submit_urb(urb, GFP_ATOMIC);
184 dev_err(&urb->dev->dev,
185 "%s - Error %d submitting control urb\n",
191 * mos7720_bulk_in_callback
192 * this is the callback function for when we have received data on the
195 static void mos7720_bulk_in_callback(struct urb *urb)
198 unsigned char *data ;
199 struct usb_serial_port *port;
200 struct moschip_port *mos7720_port;
201 struct tty_struct *tty;
202 int status = urb->status;
205 dbg("nonzero read bulk status received: %d", status);
209 mos7720_port = urb->context;
211 dbg("%s","NULL mos7720_port pointer \n");
215 port = mos7720_port->port;
217 dbg("Entering...%s", __func__);
219 data = urb->transfer_buffer;
222 if (tty && urb->actual_length) {
223 tty_buffer_request_room(tty, urb->actual_length);
224 tty_insert_flip_string(tty, data, urb->actual_length);
225 tty_flip_buffer_push(tty);
228 if (!port->read_urb) {
229 dbg("URB KILLED !!!");
233 if (port->read_urb->status != -EINPROGRESS) {
234 port->read_urb->dev = port->serial->dev;
236 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
238 dbg("usb_submit_urb(read bulk) failed, retval = %d",
244 * mos7720_bulk_out_data_callback
245 * this is the callback function for when we have finished sending serial
246 * data on the bulk out endpoint.
248 static void mos7720_bulk_out_data_callback(struct urb *urb)
250 struct moschip_port *mos7720_port;
251 struct tty_struct *tty;
252 int status = urb->status;
255 dbg("nonzero write bulk status received:%d", status);
259 mos7720_port = urb->context;
261 dbg("NULL mos7720_port pointer");
265 dbg("Entering .........");
267 tty = mos7720_port->port->tty;
269 if (tty && mos7720_port->open)
275 * this function will be used for sending command to device
277 static int send_mos_cmd(struct usb_serial *serial, __u8 request, __u16 value,
278 __u16 index, void *data)
282 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
286 if (value < MOS_MAX_PORT) {
287 if (product == MOSCHIP_DEVICE_ID_7715) {
288 value = value*0x100+0x100;
290 value = value*0x100+0x200;
294 if ((product == MOSCHIP_DEVICE_ID_7715) &&
296 dbg("serial->product== MOSCHIP_DEVICE_ID_7715");
301 if (request == MOS_WRITE) {
302 request = (__u8)MOS_WRITE;
303 requesttype = (__u8)0x40;
304 value = value + (__u16)*((unsigned char *)data);
306 pipe = usb_sndctrlpipe(serial->dev, 0);
308 request = (__u8)MOS_READ;
309 requesttype = (__u8)0xC0;
311 pipe = usb_rcvctrlpipe(serial->dev,0);
314 status = usb_control_msg(serial->dev, pipe, request, requesttype,
315 value, index, data, size, MOS_WDR_TIMEOUT);
318 dbg("Command Write failed Value %x index %x\n",value,index);
323 static int mos7720_open(struct usb_serial_port *port, struct file * filp)
325 struct usb_serial *serial;
326 struct usb_serial_port *port0;
328 struct moschip_serial *mos7720_serial;
329 struct moschip_port *mos7720_port;
333 int allocated_urbs = 0;
336 serial = port->serial;
338 mos7720_port = usb_get_serial_port_data(port);
339 if (mos7720_port == NULL)
342 port0 = serial->port[0];
344 mos7720_serial = usb_get_serial_data(serial);
346 if (mos7720_serial == NULL || port0 == NULL)
349 usb_clear_halt(serial->dev, port->write_urb->pipe);
350 usb_clear_halt(serial->dev, port->read_urb->pipe);
352 /* Initialising the write urb pool */
353 for (j = 0; j < NUM_URBS; ++j) {
354 urb = usb_alloc_urb(0,GFP_KERNEL);
355 mos7720_port->write_urb_pool[j] = urb;
358 err("No more urbs???");
362 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
364 if (!urb->transfer_buffer) {
365 err("%s-out of memory for urb buffers.", __func__);
366 usb_free_urb(mos7720_port->write_urb_pool[j]);
367 mos7720_port->write_urb_pool[j] = NULL;
376 /* Initialize MCS7720 -- Write Init values to corresponding Registers
384 * 0x08 : SP1/2 Control Reg
386 port_number = port->number - port->serial->minor;
387 send_mos_cmd(port->serial, MOS_READ, port_number, UART_LSR, &data);
388 dbg("SS::%p LSR:%x\n",mos7720_port, data);
390 dbg("Check:Sending Command ..........");
393 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x01, &data);
395 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x02, &data);
398 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
400 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
403 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
405 mos7720_port->shadowLCR = data;
406 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
408 mos7720_port->shadowMCR = data;
409 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
411 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
414 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
416 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
419 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, port_number + 1, &data);
421 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
423 send_mos_cmd(port->serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
426 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
428 data = data | (port->number - port->serial->minor + 1);
429 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
432 mos7720_port->shadowLCR = data;
433 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
435 send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
437 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
439 mos7720_port->shadowLCR = data;
440 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
442 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
444 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
448 /* force low_latency on so that our tty_push actually forces *
449 * the data through,otherwise it is scheduled, and with *
450 * high data rates (like with OHCI) data can get lost. */
453 port->tty->low_latency = 1;
455 /* see if we've set up our endpoint info yet *
456 * (can't set it up in mos7720_startup as the *
457 * structures were not set up at that time.) */
458 if (!mos7720_serial->interrupt_started) {
459 dbg("Interrupt buffer NULL !!!");
461 /* not set up yet, so do it now */
462 mos7720_serial->interrupt_started = 1;
464 dbg("To Submit URB !!!");
466 /* set up our interrupt urb */
467 usb_fill_int_urb(port0->interrupt_in_urb, serial->dev,
468 usb_rcvintpipe(serial->dev,
469 port->interrupt_in_endpointAddress),
470 port0->interrupt_in_buffer,
471 port0->interrupt_in_urb->transfer_buffer_length,
472 mos7720_interrupt_callback, mos7720_port,
473 port0->interrupt_in_urb->interval);
475 /* start interrupt read for this mos7720 this interrupt *
476 * will continue as long as the mos7720 is connected */
477 dbg("Submit URB over !!!");
478 response = usb_submit_urb(port0->interrupt_in_urb, GFP_KERNEL);
481 "%s - Error %d submitting control urb\n",
485 /* set up our bulk in urb */
486 usb_fill_bulk_urb(port->read_urb, serial->dev,
487 usb_rcvbulkpipe(serial->dev,
488 port->bulk_in_endpointAddress),
489 port->bulk_in_buffer,
490 port->read_urb->transfer_buffer_length,
491 mos7720_bulk_in_callback, mos7720_port);
492 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
495 "%s - Error %d submitting read urb\n", __func__, response);
497 /* initialize our icount structure */
498 memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
500 /* initialize our port settings */
501 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
503 /* send a open port command */
504 mos7720_port->open = 1;
510 * mos7720_chars_in_buffer
511 * this function is called by the tty driver when it wants to know how many
512 * bytes of data we currently have outstanding in the port (data that has
513 * been written, but hasn't made it out the port yet)
514 * If successful, we return the number of bytes left to be written in the
516 * Otherwise we return a negative error number.
518 static int mos7720_chars_in_buffer(struct usb_serial_port *port)
522 struct moschip_port *mos7720_port;
524 dbg("%s:entering ...........", __func__);
526 mos7720_port = usb_get_serial_port_data(port);
527 if (mos7720_port == NULL) {
528 dbg("%s:leaving ...........", __func__);
532 for (i = 0; i < NUM_URBS; ++i) {
533 if (mos7720_port->write_urb_pool[i] && mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
534 chars += URB_TRANSFER_BUFFER_SIZE;
536 dbg("%s - returns %d", __func__, chars);
540 static void mos7720_close(struct usb_serial_port *port, struct file *filp)
542 struct usb_serial *serial;
543 struct moschip_port *mos7720_port;
547 dbg("mos7720_close:entering...");
549 serial = port->serial;
551 mos7720_port = usb_get_serial_port_data(port);
552 if (mos7720_port == NULL)
555 for (j = 0; j < NUM_URBS; ++j)
556 usb_kill_urb(mos7720_port->write_urb_pool[j]);
558 /* Freeing Write URBs */
559 for (j = 0; j < NUM_URBS; ++j) {
560 if (mos7720_port->write_urb_pool[j]) {
561 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
562 usb_free_urb(mos7720_port->write_urb_pool[j]);
566 /* While closing port, shutdown all bulk read, write *
567 * and interrupt read if they exists, otherwise nop */
568 dbg("Shutdown bulk write");
569 usb_kill_urb(port->write_urb);
570 dbg("Shutdown bulk read");
571 usb_kill_urb(port->read_urb);
573 mutex_lock(&serial->disc_mutex);
574 /* these commands must not be issued if the device has
575 * been disconnected */
576 if (!serial->disconnected) {
578 send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
582 send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
585 mutex_unlock(&serial->disc_mutex);
586 mos7720_port->open = 0;
588 dbg("Leaving %s", __func__);
591 static void mos7720_break(struct usb_serial_port *port, int break_state)
594 struct usb_serial *serial;
595 struct moschip_port *mos7720_port;
597 dbg("Entering %s", __func__);
599 serial = port->serial;
601 mos7720_port = usb_get_serial_port_data(port);
602 if (mos7720_port == NULL)
605 if (break_state == -1)
606 data = mos7720_port->shadowLCR | UART_LCR_SBC;
608 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
610 mos7720_port->shadowLCR = data;
611 send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
619 * this function is called by the tty driver when it wants to know how many
620 * bytes of data we can accept for a specific port.
621 * If successful, we return the amount of room that we have for this port
622 * Otherwise we return a negative error number.
624 static int mos7720_write_room(struct usb_serial_port *port)
626 struct moschip_port *mos7720_port;
630 dbg("%s:entering ...........", __func__);
632 mos7720_port = usb_get_serial_port_data(port);
633 if (mos7720_port == NULL) {
634 dbg("%s:leaving ...........", __func__);
639 for (i = 0; i < NUM_URBS; ++i) {
640 if (mos7720_port->write_urb_pool[i] && mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
641 room += URB_TRANSFER_BUFFER_SIZE;
644 dbg("%s - returns %d", __func__, room);
648 static int mos7720_write(struct usb_serial_port *port,
649 const unsigned char *data, int count)
656 struct moschip_port *mos7720_port;
657 struct usb_serial *serial;
659 const unsigned char *current_position = data;
661 dbg("%s:entering ...........", __func__);
663 serial = port->serial;
665 mos7720_port = usb_get_serial_port_data(port);
666 if (mos7720_port == NULL) {
667 dbg("mos7720_port is NULL");
671 /* try to find a free urb in the list */
674 for (i = 0; i < NUM_URBS; ++i) {
675 if (mos7720_port->write_urb_pool[i] && mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
676 urb = mos7720_port->write_urb_pool[i];
683 dbg("%s - no more free urbs", __func__);
687 if (urb->transfer_buffer == NULL) {
688 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
690 if (urb->transfer_buffer == NULL) {
691 err("%s no more kernel memory...", __func__);
695 transfer_size = min (count, URB_TRANSFER_BUFFER_SIZE);
697 memcpy(urb->transfer_buffer, current_position, transfer_size);
698 usb_serial_debug_data(debug, &port->dev, __func__, transfer_size,
699 urb->transfer_buffer);
701 /* fill urb with data and submit */
702 usb_fill_bulk_urb(urb, serial->dev,
703 usb_sndbulkpipe(serial->dev,
704 port->bulk_out_endpointAddress),
705 urb->transfer_buffer, transfer_size,
706 mos7720_bulk_out_data_callback, mos7720_port);
708 /* send it down the pipe */
709 status = usb_submit_urb(urb,GFP_ATOMIC);
711 err("%s - usb_submit_urb(write bulk) failed with status = %d",
716 bytes_sent = transfer_size;
722 static void mos7720_throttle(struct usb_serial_port *port)
724 struct moschip_port *mos7720_port;
725 struct tty_struct *tty;
728 dbg("%s- port %d\n", __func__, port->number);
730 mos7720_port = usb_get_serial_port_data(port);
732 if (mos7720_port == NULL)
735 if (!mos7720_port->open) {
736 dbg("port not opened");
740 dbg("%s: Entering ..........", __func__);
744 dbg("%s - no tty available", __func__);
748 /* if we are implementing XON/XOFF, send the stop character */
750 unsigned char stop_char = STOP_CHAR(tty);
751 status = mos7720_write(port, &stop_char, 1);
756 /* if we are implementing RTS/CTS, toggle that line */
757 if (tty->termios->c_cflag & CRTSCTS) {
758 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
759 status = send_mos_cmd(port->serial, MOS_WRITE,
760 port->number - port->serial->minor,
761 UART_MCR, &mos7720_port->shadowMCR);
767 static void mos7720_unthrottle(struct usb_serial_port *port)
769 struct tty_struct *tty;
771 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
773 if (mos7720_port == NULL)
776 if (!mos7720_port->open) {
777 dbg("%s - port not opened", __func__);
781 dbg("%s: Entering ..........", __func__);
785 dbg("%s - no tty available", __func__);
789 /* if we are implementing XON/XOFF, send the start character */
791 unsigned char start_char = START_CHAR(tty);
792 status = mos7720_write(port, &start_char, 1);
797 /* if we are implementing RTS/CTS, toggle that line */
798 if (tty->termios->c_cflag & CRTSCTS) {
799 mos7720_port->shadowMCR |= UART_MCR_RTS;
800 status = send_mos_cmd(port->serial, MOS_WRITE,
801 port->number - port->serial->minor,
802 UART_MCR, &mos7720_port->shadowMCR);
808 static int set_higher_rates(struct moschip_port *mos7720_port,
812 struct usb_serial_port *port;
813 struct usb_serial *serial;
816 if (mos7720_port == NULL)
819 port = mos7720_port->port;
820 serial = port->serial;
822 /***********************************************
823 * Init Sequence for higher rates
824 ***********************************************/
825 dbg("Sending Setting Commands ..........");
826 port_number = port->number - port->serial->minor;
829 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
831 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
833 send_mos_cmd(serial, MOS_WRITE, port->number, 0x02, &data);
835 mos7720_port->shadowMCR = data;
836 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
838 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
841 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
843 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
846 /***********************************************
847 * Set for higher rates *
848 ***********************************************/
851 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1,&data);
854 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
856 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
859 mos7720_port->shadowMCR = data;
860 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
862 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
864 /***********************************************
866 ***********************************************/
868 data = mos7720_port->shadowLCR | UART_LCR_DLAB;
869 mos7720_port->shadowLCR = data;
870 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
872 data = 0x001; /* DLL */
873 send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
874 data = 0x000; /* DLM */
875 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
877 data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
878 mos7720_port->shadowLCR = data;
879 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
884 /* baud rate information */
885 struct divisor_table_entry
891 /* Define table of divisors for moschip 7720 hardware *
892 * These assume a 3.6864MHz crystal, the standard /16, and *
894 static struct divisor_table_entry divisor_table[] = {
896 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
897 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
913 /*****************************************************************************
914 * calc_baud_rate_divisor
915 * this function calculates the proper baud rate divisor for the specified
917 *****************************************************************************/
918 static int calc_baud_rate_divisor(int baudrate, int *divisor)
926 dbg("%s - %d", __func__, baudrate);
928 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
929 if (divisor_table[i].baudrate == baudrate) {
930 *divisor = divisor_table[i].divisor;
935 /* After trying for all the standard baud rates *
936 * Try calculating the divisor for this baud rate */
937 if (baudrate > 75 && baudrate < 230400) {
938 /* get the divisor */
939 custom = (__u16)(230400L / baudrate);
941 /* Check for round off */
942 round1 = (__u16)(2304000L / baudrate);
943 round = (__u16)(round1 - (custom * 10));
948 dbg("Baud %d = %d",baudrate, custom);
952 dbg("Baud calculation Failed...");
957 * send_cmd_write_baud_rate
958 * this function sends the proper command to change the baud rate of the
961 static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
964 struct usb_serial_port *port;
965 struct usb_serial *serial;
969 unsigned char number;
971 if (mos7720_port == NULL)
974 port = mos7720_port->port;
975 serial = port->serial;
977 dbg("%s: Entering ..........", __func__);
979 number = port->number - port->serial->minor;
980 dbg("%s - port = %d, baud = %d", __func__, port->number, baudrate);
982 /* Calculate the Divisor */
983 status = calc_baud_rate_divisor(baudrate, &divisor);
985 err("%s - bad baud rate", __func__);
989 /* Enable access to divisor latch */
990 data = mos7720_port->shadowLCR | UART_LCR_DLAB;
991 mos7720_port->shadowLCR = data;
992 send_mos_cmd(serial, MOS_WRITE, number, UART_LCR, &data);
994 /* Write the divisor */
995 data = ((unsigned char)(divisor & 0xff));
996 send_mos_cmd(serial, MOS_WRITE, number, 0x00, &data);
998 data = ((unsigned char)((divisor & 0xff00) >> 8));
999 send_mos_cmd(serial, MOS_WRITE, number, 0x01, &data);
1001 /* Disable access to divisor latch */
1002 data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1003 mos7720_port->shadowLCR = data;
1004 send_mos_cmd(serial, MOS_WRITE, number, 0x03, &data);
1010 * change_port_settings
1011 * This routine is called to set the UART on the device to match
1012 * the specified new settings.
1014 static void change_port_settings(struct moschip_port *mos7720_port,
1015 struct ktermios *old_termios)
1017 struct usb_serial_port *port;
1018 struct usb_serial *serial;
1019 struct tty_struct *tty;
1031 if (mos7720_port == NULL)
1034 port = mos7720_port->port;
1035 serial = port->serial;
1036 port_number = port->number - port->serial->minor;
1038 dbg("%s - port %d", __func__, port->number);
1040 if (!mos7720_port->open) {
1041 dbg("%s - port not opened", __func__);
1045 tty = mos7720_port->port->tty;
1047 dbg("%s: Entering ..........", __func__);
1049 lData = UART_LCR_WLEN8;
1050 lStop = 0x00; /* 1 stop bit */
1051 lParity = 0x00; /* No parity */
1053 cflag = tty->termios->c_cflag;
1054 iflag = tty->termios->c_iflag;
1056 /* Change the number of bits */
1057 switch (cflag & CSIZE) {
1059 lData = UART_LCR_WLEN5;
1064 lData = UART_LCR_WLEN6;
1069 lData = UART_LCR_WLEN7;
1074 lData = UART_LCR_WLEN8;
1078 /* Change the Parity bit */
1079 if (cflag & PARENB) {
1080 if (cflag & PARODD) {
1081 lParity = UART_LCR_PARITY;
1082 dbg("%s - parity = odd", __func__);
1084 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
1085 dbg("%s - parity = even", __func__);
1089 dbg("%s - parity = none", __func__);
1093 lParity = lParity | 0x20;
1095 /* Change the Stop bit */
1096 if (cflag & CSTOPB) {
1097 lStop = UART_LCR_STOP;
1098 dbg("%s - stop bits = 2", __func__);
1101 dbg("%s - stop bits = 1", __func__);
1104 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1105 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1106 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
1108 /* Update the LCR with the correct value */
1109 mos7720_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1110 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1113 /* Disable Interrupts */
1115 send_mos_cmd(serial,MOS_WRITE,port->number - port->serial->minor, UART_IER, &data);
1118 send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
1121 send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
1123 /* Send the updated LCR value to the mos7720 */
1124 data = mos7720_port->shadowLCR;
1125 send_mos_cmd(serial, MOS_WRITE, port_number, UART_LCR, &data);
1128 mos7720_port->shadowMCR = data;
1129 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
1131 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
1133 /* set up the MCR register and send it to the mos7720 */
1134 mos7720_port->shadowMCR = UART_MCR_OUT2;
1136 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1138 if (cflag & CRTSCTS) {
1139 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
1141 /* To set hardware flow control to the specified *
1142 * serial port, in SP1/2_CONTROL_REG */
1145 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1149 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1153 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1156 data = mos7720_port->shadowMCR;
1157 send_mos_cmd(serial, MOS_WRITE, port_number, UART_MCR, &data);
1159 /* Determine divisor based on baud rate */
1160 baud = tty_get_baud_rate(tty);
1162 /* pick a default, any default... */
1163 dbg("Picked default baud...");
1167 if (baud >= 230400) {
1168 set_higher_rates(mos7720_port, baud);
1169 /* Enable Interrupts */
1171 send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1175 dbg("%s - baud rate = %d", __func__, baud);
1176 status = send_cmd_write_baud_rate(mos7720_port, baud);
1177 /* FIXME: needs to write actual resulting baud back not just
1180 tty_encode_baud_rate(tty, baud, baud);
1181 /* Enable Interrupts */
1183 send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1185 if (port->read_urb->status != -EINPROGRESS) {
1186 port->read_urb->dev = serial->dev;
1188 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1190 dbg("usb_submit_urb(read bulk) failed, status = %d",
1197 * mos7720_set_termios
1198 * this function is called by the tty driver when it wants to change the
1199 * termios structure.
1201 static void mos7720_set_termios(struct usb_serial_port *port,
1202 struct ktermios *old_termios)
1206 struct usb_serial *serial;
1207 struct moschip_port *mos7720_port;
1208 struct tty_struct *tty;
1210 serial = port->serial;
1212 mos7720_port = usb_get_serial_port_data(port);
1214 if (mos7720_port == NULL)
1220 if (!mos7720_port->open) {
1221 dbg("%s - port not opened", __func__);
1225 dbg("%s\n","setting termios - ASPIRE");
1227 cflag = tty->termios->c_cflag;
1229 dbg("%s - cflag %08x iflag %08x", __func__,
1230 tty->termios->c_cflag,
1231 RELEVANT_IFLAG(tty->termios->c_iflag));
1233 dbg("%s - old cflag %08x old iflag %08x", __func__,
1234 old_termios->c_cflag,
1235 RELEVANT_IFLAG(old_termios->c_iflag));
1237 dbg("%s - port %d", __func__, port->number);
1239 /* change the port settings to the new ones specified */
1240 change_port_settings(mos7720_port, old_termios);
1242 if(!port->read_urb) {
1243 dbg("%s","URB KILLED !!!!!\n");
1247 if(port->read_urb->status != -EINPROGRESS) {
1248 port->read_urb->dev = serial->dev;
1249 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1251 dbg("usb_submit_urb(read bulk) failed, status = %d",
1258 * get_lsr_info - get line status register info
1260 * Purpose: Let user call ioctl() to get info when the UART physically
1261 * is emptied. On bus types like RS485, the transmitter must
1262 * release the bus after transmitting. This must be done when
1263 * the transmit shift register is empty, not be done when the
1264 * transmit holding register is empty. This functionality
1265 * allows an RS485 driver to be written in user space.
1267 static int get_lsr_info(struct moschip_port *mos7720_port,
1268 unsigned int __user *value)
1271 unsigned int result = 0;
1273 count = mos7720_chars_in_buffer(mos7720_port->port);
1275 dbg("%s -- Empty", __func__);
1276 result = TIOCSER_TEMT;
1279 if (copy_to_user(value, &result, sizeof(int)))
1285 * get_number_bytes_avail - get number of bytes available
1287 * Purpose: Let user call ioctl to get the count of number of bytes available.
1289 static int get_number_bytes_avail(struct moschip_port *mos7720_port,
1290 unsigned int __user *value)
1292 unsigned int result = 0;
1293 struct tty_struct *tty = mos7720_port->port->tty;
1296 return -ENOIOCTLCMD;
1298 result = tty->read_cnt;
1300 dbg("%s(%d) = %d", __func__, mos7720_port->port->number, result);
1301 if (copy_to_user(value, &result, sizeof(int)))
1304 return -ENOIOCTLCMD;
1307 static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1308 unsigned int __user *value)
1314 struct usb_serial_port *port;
1316 if (mos7720_port == NULL)
1319 port = (struct usb_serial_port*)mos7720_port->port;
1320 mcr = mos7720_port->shadowMCR;
1322 if (copy_from_user(&arg, value, sizeof(int)))
1327 if (arg & TIOCM_RTS)
1328 mcr |= UART_MCR_RTS;
1329 if (arg & TIOCM_DTR)
1330 mcr |= UART_MCR_RTS;
1331 if (arg & TIOCM_LOOP)
1332 mcr |= UART_MCR_LOOP;
1336 if (arg & TIOCM_RTS)
1337 mcr &= ~UART_MCR_RTS;
1338 if (arg & TIOCM_DTR)
1339 mcr &= ~UART_MCR_RTS;
1340 if (arg & TIOCM_LOOP)
1341 mcr &= ~UART_MCR_LOOP;
1345 /* turn off the RTS and DTR and LOOPBACK
1346 * and then only turn on what was asked to */
1347 mcr &= ~(UART_MCR_RTS | UART_MCR_DTR | UART_MCR_LOOP);
1348 mcr |= ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0);
1349 mcr |= ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0);
1350 mcr |= ((arg & TIOCM_LOOP) ? UART_MCR_LOOP : 0);
1354 mos7720_port->shadowMCR = mcr;
1356 data = mos7720_port->shadowMCR;
1357 send_mos_cmd(port->serial, MOS_WRITE,
1358 port->number - port->serial->minor, UART_MCR, &data);
1363 static int get_modem_info(struct moschip_port *mos7720_port,
1364 unsigned int __user *value)
1366 unsigned int result = 0;
1367 unsigned int msr = mos7720_port->shadowMSR;
1368 unsigned int mcr = mos7720_port->shadowMCR;
1370 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1371 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1372 | ((msr & UART_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1373 | ((msr & UART_MSR_DCD) ? TIOCM_CAR: 0) /* 0x040 */
1374 | ((msr & UART_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1375 | ((msr & UART_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1378 dbg("%s -- %x", __func__, result);
1380 if (copy_to_user(value, &result, sizeof(int)))
1385 static int get_serial_info(struct moschip_port *mos7720_port,
1386 struct serial_struct __user *retinfo)
1388 struct serial_struct tmp;
1393 memset(&tmp, 0, sizeof(tmp));
1395 tmp.type = PORT_16550A;
1396 tmp.line = mos7720_port->port->serial->minor;
1397 tmp.port = mos7720_port->port->number;
1399 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1400 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1401 tmp.baud_base = 9600;
1402 tmp.close_delay = 5*HZ;
1403 tmp.closing_wait = 30*HZ;
1405 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1410 static int mos7720_ioctl(struct usb_serial_port *port, struct file *file,
1411 unsigned int cmd, unsigned long arg)
1413 struct moschip_port *mos7720_port;
1414 struct async_icount cnow;
1415 struct async_icount cprev;
1416 struct serial_icounter_struct icount;
1418 mos7720_port = usb_get_serial_port_data(port);
1419 if (mos7720_port == NULL)
1422 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
1426 /* return number of bytes available */
1427 dbg("%s (%d) TIOCINQ", __func__, port->number);
1428 return get_number_bytes_avail(mos7720_port,
1429 (unsigned int __user *)arg);
1433 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
1434 return get_lsr_info(mos7720_port, (unsigned int __user *)arg);
1440 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __func__,
1442 return set_modem_info(mos7720_port, cmd,
1443 (unsigned int __user *)arg);
1446 dbg("%s (%d) TIOCMGET", __func__, port->number);
1447 return get_modem_info(mos7720_port,
1448 (unsigned int __user *)arg);
1451 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
1452 return get_serial_info(mos7720_port,
1453 (struct serial_struct __user *)arg);
1456 dbg("%s (%d) TIOCSSERIAL", __func__, port->number);
1460 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
1461 cprev = mos7720_port->icount;
1463 if (signal_pending(current))
1464 return -ERESTARTSYS;
1465 cnow = mos7720_port->icount;
1466 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1467 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1468 return -EIO; /* no change => error */
1469 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1470 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1471 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1472 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1481 cnow = mos7720_port->icount;
1482 icount.cts = cnow.cts;
1483 icount.dsr = cnow.dsr;
1484 icount.rng = cnow.rng;
1485 icount.dcd = cnow.dcd;
1486 icount.rx = cnow.rx;
1487 icount.tx = cnow.tx;
1488 icount.frame = cnow.frame;
1489 icount.overrun = cnow.overrun;
1490 icount.parity = cnow.parity;
1491 icount.brk = cnow.brk;
1492 icount.buf_overrun = cnow.buf_overrun;
1494 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
1495 port->number, icount.rx, icount.tx );
1496 if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
1501 return -ENOIOCTLCMD;
1504 static int mos7720_startup(struct usb_serial *serial)
1506 struct moschip_serial *mos7720_serial;
1507 struct moschip_port *mos7720_port;
1508 struct usb_device *dev;
1512 dbg("%s: Entering ..........", __func__);
1515 dbg("Invalid Handler");
1521 /* create our private serial structure */
1522 mos7720_serial = kzalloc(sizeof(struct moschip_serial), GFP_KERNEL);
1523 if (mos7720_serial == NULL) {
1524 err("%s - Out of memory", __func__);
1528 usb_set_serial_data(serial, mos7720_serial);
1530 /* we set up the pointers to the endpoints in the mos7720_open *
1531 * function, as the structures aren't created yet. */
1533 /* set up port private structures */
1534 for (i = 0; i < serial->num_ports; ++i) {
1535 mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
1536 if (mos7720_port == NULL) {
1537 err("%s - Out of memory", __func__);
1538 usb_set_serial_data(serial, NULL);
1539 kfree(mos7720_serial);
1543 /* Initialize all port interrupt end point to port 0 int
1544 * endpoint. Our device has only one interrupt endpoint
1545 * comman to all ports */
1546 serial->port[i]->interrupt_in_endpointAddress = serial->port[0]->interrupt_in_endpointAddress;
1548 mos7720_port->port = serial->port[i];
1549 usb_set_serial_port_data(serial->port[i], mos7720_port);
1551 dbg("port number is %d", serial->port[i]->number);
1552 dbg("serial number is %d", serial->minor);
1556 /* setting configuration feature to one */
1557 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1558 (__u8)0x03, 0x00,0x01,0x00, NULL, 0x00, 5*HZ);
1560 send_mos_cmd(serial,MOS_READ,0x00, UART_LSR, &data); // LSR For Port 1
1563 send_mos_cmd(serial,MOS_READ,0x01, UART_LSR, &data); // LSR For Port 2
1569 static void mos7720_shutdown(struct usb_serial *serial)
1573 /* free private structure allocated for serial port */
1574 for (i=0; i < serial->num_ports; ++i) {
1575 kfree(usb_get_serial_port_data(serial->port[i]));
1576 usb_set_serial_port_data(serial->port[i], NULL);
1579 /* free private structure allocated for serial device */
1580 kfree(usb_get_serial_data(serial));
1581 usb_set_serial_data(serial, NULL);
1584 static struct usb_driver usb_driver = {
1585 .name = "moschip7720",
1586 .probe = usb_serial_probe,
1587 .disconnect = usb_serial_disconnect,
1588 .id_table = moschip_port_id_table,
1592 static struct usb_serial_driver moschip7720_2port_driver = {
1594 .owner = THIS_MODULE,
1595 .name = "moschip7720",
1597 .description = "Moschip 2 port adapter",
1598 .usb_driver = &usb_driver,
1599 .id_table = moschip_port_id_table,
1601 .open = mos7720_open,
1602 .close = mos7720_close,
1603 .throttle = mos7720_throttle,
1604 .unthrottle = mos7720_unthrottle,
1605 .attach = mos7720_startup,
1606 .shutdown = mos7720_shutdown,
1607 .ioctl = mos7720_ioctl,
1608 .set_termios = mos7720_set_termios,
1609 .write = mos7720_write,
1610 .write_room = mos7720_write_room,
1611 .chars_in_buffer = mos7720_chars_in_buffer,
1612 .break_ctl = mos7720_break,
1613 .read_bulk_callback = mos7720_bulk_in_callback,
1614 .read_int_callback = mos7720_interrupt_callback,
1617 static int __init moschip7720_init(void)
1621 dbg("%s: Entering ..........", __func__);
1623 /* Register with the usb serial */
1624 retval = usb_serial_register(&moschip7720_2port_driver);
1626 goto failed_port_device_register;
1628 info(DRIVER_DESC " " DRIVER_VERSION);
1630 /* Register with the usb */
1631 retval = usb_register(&usb_driver);
1633 goto failed_usb_register;
1637 failed_usb_register:
1638 usb_serial_deregister(&moschip7720_2port_driver);
1640 failed_port_device_register:
1644 static void __exit moschip7720_exit(void)
1646 usb_deregister(&usb_driver);
1647 usb_serial_deregister(&moschip7720_2port_driver);
1650 module_init(moschip7720_init);
1651 module_exit(moschip7720_exit);
1653 /* Module information */
1654 MODULE_AUTHOR( DRIVER_AUTHOR );
1655 MODULE_DESCRIPTION( DRIVER_DESC );
1656 MODULE_LICENSE("GPL");
1658 module_param(debug, bool, S_IRUGO | S_IWUSR);
1659 MODULE_PARM_DESC(debug, "Debug enabled or not");