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");
114 dbg("%s","Invalid Pointer !!!!:\n");
125 /* this urb is terminated, clean up */
126 dbg("%s - urb shutting down with status: %d", __FUNCTION__,
130 dbg("%s - nonzero urb status received: %d", __FUNCTION__,
135 length = urb->actual_length;
136 data = urb->transfer_buffer;
138 /* Moschip get 4 bytes
139 * Byte 1 IIR Port 1 (port.number is 0)
140 * Byte 2 IIR Port 2 (port.number is 1)
141 * Byte 3 --------------
142 * Byte 4 FIFO status for both */
144 /* the above description is inverted
145 * oneukum 2007-03-14 */
147 if (unlikely(length != 4)) {
148 dbg("Wrong data !!!");
155 if ((sp1 | sp2) & 0x01) {
156 /* No Interrupt Pending in both the ports */
157 dbg("No Interrupt !!!");
159 switch (sp1 & 0x0f) {
161 dbg("Serial Port 1: Receiver status error or address "
162 "bit detected in 9-bit mode\n");
165 dbg("Serial Port 1: Receiver time out");
168 dbg("Serial Port 1: Modem status change");
172 switch (sp2 & 0x0f) {
174 dbg("Serial Port 2: Receiver status error or address "
175 "bit detected in 9-bit mode");
178 dbg("Serial Port 2: Receiver time out");
181 dbg("Serial Port 2: Modem status change");
187 result = usb_submit_urb(urb, GFP_ATOMIC);
189 dev_err(&urb->dev->dev,
190 "%s - Error %d submitting control urb\n",
191 __FUNCTION__, result);
196 * mos7720_bulk_in_callback
197 * this is the callback function for when we have received data on the
200 static void mos7720_bulk_in_callback(struct urb *urb)
203 unsigned char *data ;
204 struct usb_serial_port *port;
205 struct moschip_port *mos7720_port;
206 struct tty_struct *tty;
207 int status = urb->status;
210 dbg("nonzero read bulk status received: %d", status);
214 mos7720_port = urb->context;
216 dbg("%s","NULL mos7720_port pointer \n");
220 port = mos7720_port->port;
222 dbg("Entering...%s", __FUNCTION__);
224 data = urb->transfer_buffer;
227 if (tty && urb->actual_length) {
228 tty_buffer_request_room(tty, urb->actual_length);
229 tty_insert_flip_string(tty, data, urb->actual_length);
230 tty_flip_buffer_push(tty);
233 if (!port->read_urb) {
234 dbg("URB KILLED !!!");
238 if (port->read_urb->status != -EINPROGRESS) {
239 port->read_urb->dev = port->serial->dev;
241 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
243 dbg("usb_submit_urb(read bulk) failed, retval = %d",
249 * mos7720_bulk_out_data_callback
250 * this is the callback function for when we have finished sending serial
251 * data on the bulk out endpoint.
253 static void mos7720_bulk_out_data_callback(struct urb *urb)
255 struct moschip_port *mos7720_port;
256 struct tty_struct *tty;
257 int status = urb->status;
260 dbg("nonzero write bulk status received:%d", status);
264 mos7720_port = urb->context;
266 dbg("NULL mos7720_port pointer");
270 dbg("Entering .........");
272 tty = mos7720_port->port->tty;
274 if (tty && mos7720_port->open)
280 * this function will be used for sending command to device
282 static int send_mos_cmd(struct usb_serial *serial, __u8 request, __u16 value,
283 __u16 index, void *data)
287 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
291 if (value < MOS_MAX_PORT) {
292 if (product == MOSCHIP_DEVICE_ID_7715) {
293 value = value*0x100+0x100;
295 value = value*0x100+0x200;
299 if ((product == MOSCHIP_DEVICE_ID_7715) &&
301 dbg("serial->product== MOSCHIP_DEVICE_ID_7715");
306 if (request == MOS_WRITE) {
307 request = (__u8)MOS_WRITE;
308 requesttype = (__u8)0x40;
309 value = value + (__u16)*((unsigned char *)data);
311 pipe = usb_sndctrlpipe(serial->dev, 0);
313 request = (__u8)MOS_READ;
314 requesttype = (__u8)0xC0;
316 pipe = usb_rcvctrlpipe(serial->dev,0);
319 status = usb_control_msg(serial->dev, pipe, request, requesttype,
320 value, index, data, size, MOS_WDR_TIMEOUT);
323 dbg("Command Write failed Value %x index %x\n",value,index);
328 static int mos7720_open(struct usb_serial_port *port, struct file * filp)
330 struct usb_serial *serial;
331 struct usb_serial_port *port0;
333 struct moschip_serial *mos7720_serial;
334 struct moschip_port *mos7720_port;
338 int allocated_urbs = 0;
341 serial = port->serial;
343 mos7720_port = usb_get_serial_port_data(port);
344 if (mos7720_port == NULL)
347 port0 = serial->port[0];
349 mos7720_serial = usb_get_serial_data(serial);
351 if (mos7720_serial == NULL || port0 == NULL)
354 usb_clear_halt(serial->dev, port->write_urb->pipe);
355 usb_clear_halt(serial->dev, port->read_urb->pipe);
357 /* Initialising the write urb pool */
358 for (j = 0; j < NUM_URBS; ++j) {
359 urb = usb_alloc_urb(0,GFP_KERNEL);
360 mos7720_port->write_urb_pool[j] = urb;
363 err("No more urbs???");
367 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
369 if (!urb->transfer_buffer) {
370 err("%s-out of memory for urb buffers.", __FUNCTION__);
371 usb_free_urb(mos7720_port->write_urb_pool[j]);
372 mos7720_port->write_urb_pool[j] = NULL;
381 /* Initialize MCS7720 -- Write Init values to corresponding Registers
389 * 0x08 : SP1/2 Control Reg
391 port_number = port->number - port->serial->minor;
392 send_mos_cmd(port->serial, MOS_READ, port_number, UART_LSR, &data);
393 dbg("SS::%p LSR:%x\n",mos7720_port, data);
395 dbg("Check:Sending Command ..........");
398 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x01, &data);
400 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x02, &data);
403 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
405 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
408 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
410 mos7720_port->shadowLCR = data;
411 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
413 mos7720_port->shadowMCR = data;
414 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
416 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
419 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
421 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
424 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, port_number + 1, &data);
426 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
428 send_mos_cmd(port->serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
431 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
433 data = data | (port->number - port->serial->minor + 1);
434 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
437 mos7720_port->shadowLCR = data;
438 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
440 send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
442 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
444 mos7720_port->shadowLCR = data;
445 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
447 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
449 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
453 /* force low_latency on so that our tty_push actually forces *
454 * the data through,otherwise it is scheduled, and with *
455 * high data rates (like with OHCI) data can get lost. */
458 port->tty->low_latency = 1;
460 /* see if we've set up our endpoint info yet *
461 * (can't set it up in mos7720_startup as the *
462 * structures were not set up at that time.) */
463 if (!mos7720_serial->interrupt_started) {
464 dbg("Interrupt buffer NULL !!!");
466 /* not set up yet, so do it now */
467 mos7720_serial->interrupt_started = 1;
469 dbg("To Submit URB !!!");
471 /* set up our interrupt urb */
472 usb_fill_int_urb(port0->interrupt_in_urb, serial->dev,
473 usb_rcvintpipe(serial->dev,
474 port->interrupt_in_endpointAddress),
475 port0->interrupt_in_buffer,
476 port0->interrupt_in_urb->transfer_buffer_length,
477 mos7720_interrupt_callback, mos7720_port,
478 port0->interrupt_in_urb->interval);
480 /* start interrupt read for this mos7720 this interrupt *
481 * will continue as long as the mos7720 is connected */
482 dbg("Submit URB over !!!");
483 response = usb_submit_urb(port0->interrupt_in_urb, GFP_KERNEL);
486 "%s - Error %d submitting control urb",
487 __FUNCTION__, response);
490 /* set up our bulk in urb */
491 usb_fill_bulk_urb(port->read_urb, serial->dev,
492 usb_rcvbulkpipe(serial->dev,
493 port->bulk_in_endpointAddress),
494 port->bulk_in_buffer,
495 port->read_urb->transfer_buffer_length,
496 mos7720_bulk_in_callback, mos7720_port);
497 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
500 "%s - Error %d submitting read urb", __FUNCTION__, response);
502 /* initialize our icount structure */
503 memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
505 /* initialize our port settings */
506 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
508 /* send a open port command */
509 mos7720_port->open = 1;
515 * mos7720_chars_in_buffer
516 * this function is called by the tty driver when it wants to know how many
517 * bytes of data we currently have outstanding in the port (data that has
518 * been written, but hasn't made it out the port yet)
519 * If successful, we return the number of bytes left to be written in the
521 * Otherwise we return a negative error number.
523 static int mos7720_chars_in_buffer(struct usb_serial_port *port)
527 struct moschip_port *mos7720_port;
529 dbg("%s:entering ...........", __FUNCTION__);
531 mos7720_port = usb_get_serial_port_data(port);
532 if (mos7720_port == NULL) {
533 dbg("%s:leaving ...........", __FUNCTION__);
537 for (i = 0; i < NUM_URBS; ++i) {
538 if (mos7720_port->write_urb_pool[i] && mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
539 chars += URB_TRANSFER_BUFFER_SIZE;
541 dbg("%s - returns %d", __FUNCTION__, chars);
545 static void mos7720_close(struct usb_serial_port *port, struct file *filp)
547 struct usb_serial *serial;
548 struct moschip_port *mos7720_port;
552 dbg("mos7720_close:entering...");
554 serial = port->serial;
556 mos7720_port = usb_get_serial_port_data(port);
557 if (mos7720_port == NULL)
560 for (j = 0; j < NUM_URBS; ++j)
561 usb_kill_urb(mos7720_port->write_urb_pool[j]);
563 /* Freeing Write URBs */
564 for (j = 0; j < NUM_URBS; ++j) {
565 if (mos7720_port->write_urb_pool[j]) {
566 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
567 usb_free_urb(mos7720_port->write_urb_pool[j]);
571 /* While closing port, shutdown all bulk read, write *
572 * and interrupt read if they exists */
574 dbg("Shutdown bulk write");
575 usb_kill_urb(port->write_urb);
576 dbg("Shutdown bulk read");
577 usb_kill_urb(port->read_urb);
581 send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
585 send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
588 mos7720_port->open = 0;
590 dbg("Leaving %s", __FUNCTION__);
593 static void mos7720_break(struct usb_serial_port *port, int break_state)
596 struct usb_serial *serial;
597 struct moschip_port *mos7720_port;
599 dbg("Entering %s", __FUNCTION__);
601 serial = port->serial;
603 mos7720_port = usb_get_serial_port_data(port);
604 if (mos7720_port == NULL)
607 if (break_state == -1)
608 data = mos7720_port->shadowLCR | UART_LCR_SBC;
610 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
612 mos7720_port->shadowLCR = data;
613 send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
621 * this function is called by the tty driver when it wants to know how many
622 * bytes of data we can accept for a specific port.
623 * If successful, we return the amount of room that we have for this port
624 * Otherwise we return a negative error number.
626 static int mos7720_write_room(struct usb_serial_port *port)
628 struct moschip_port *mos7720_port;
632 dbg("%s:entering ...........", __FUNCTION__);
634 mos7720_port = usb_get_serial_port_data(port);
635 if (mos7720_port == NULL) {
636 dbg("%s:leaving ...........", __FUNCTION__);
640 for (i = 0; i < NUM_URBS; ++i) {
641 if (mos7720_port->write_urb_pool[i] && mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
642 room += URB_TRANSFER_BUFFER_SIZE;
645 dbg("%s - returns %d", __FUNCTION__, room);
649 static int mos7720_write(struct usb_serial_port *port,
650 const unsigned char *data, int count)
657 struct moschip_port *mos7720_port;
658 struct usb_serial *serial;
660 const unsigned char *current_position = data;
662 dbg("%s:entering ...........", __FUNCTION__);
664 serial = port->serial;
666 mos7720_port = usb_get_serial_port_data(port);
667 if (mos7720_port == NULL) {
668 dbg("mos7720_port is NULL");
672 /* try to find a free urb in the list */
675 for (i = 0; i < NUM_URBS; ++i) {
676 if (mos7720_port->write_urb_pool[i] && mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
677 urb = mos7720_port->write_urb_pool[i];
684 dbg("%s - no more free urbs", __FUNCTION__);
688 if (urb->transfer_buffer == NULL) {
689 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
691 if (urb->transfer_buffer == NULL) {
692 err("%s no more kernel memory...", __FUNCTION__);
696 transfer_size = min (count, URB_TRANSFER_BUFFER_SIZE);
698 memcpy(urb->transfer_buffer, current_position, transfer_size);
699 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, transfer_size,
700 urb->transfer_buffer);
702 /* fill urb with data and submit */
703 usb_fill_bulk_urb(urb, serial->dev,
704 usb_sndbulkpipe(serial->dev,
705 port->bulk_out_endpointAddress),
706 urb->transfer_buffer, transfer_size,
707 mos7720_bulk_out_data_callback, mos7720_port);
709 /* send it down the pipe */
710 status = usb_submit_urb(urb,GFP_ATOMIC);
712 err("%s - usb_submit_urb(write bulk) failed with status = %d",
713 __FUNCTION__, status);
717 bytes_sent = transfer_size;
723 static void mos7720_throttle(struct usb_serial_port *port)
725 struct moschip_port *mos7720_port;
726 struct tty_struct *tty;
729 dbg("%s- port %d\n", __FUNCTION__, port->number);
731 mos7720_port = usb_get_serial_port_data(port);
733 if (mos7720_port == NULL)
736 if (!mos7720_port->open) {
737 dbg("port not opened");
741 dbg("%s: Entering ..........", __FUNCTION__);
745 dbg("%s - no tty available", __FUNCTION__);
749 /* if we are implementing XON/XOFF, send the stop character */
751 unsigned char stop_char = STOP_CHAR(tty);
752 status = mos7720_write(port, &stop_char, 1);
757 /* if we are implementing RTS/CTS, toggle that line */
758 if (tty->termios->c_cflag & CRTSCTS) {
759 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
760 status = send_mos_cmd(port->serial, MOS_WRITE,
761 port->number - port->serial->minor,
762 UART_MCR, &mos7720_port->shadowMCR);
768 static void mos7720_unthrottle(struct usb_serial_port *port)
770 struct tty_struct *tty;
772 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
774 if (mos7720_port == NULL)
777 if (!mos7720_port->open) {
778 dbg("%s - port not opened", __FUNCTION__);
782 dbg("%s: Entering ..........", __FUNCTION__);
786 dbg("%s - no tty available", __FUNCTION__);
790 /* if we are implementing XON/XOFF, send the start character */
792 unsigned char start_char = START_CHAR(tty);
793 status = mos7720_write(port, &start_char, 1);
798 /* if we are implementing RTS/CTS, toggle that line */
799 if (tty->termios->c_cflag & CRTSCTS) {
800 mos7720_port->shadowMCR |= UART_MCR_RTS;
801 status = send_mos_cmd(port->serial, MOS_WRITE,
802 port->number - port->serial->minor,
803 UART_MCR, &mos7720_port->shadowMCR);
809 static int set_higher_rates(struct moschip_port *mos7720_port,
813 struct usb_serial_port *port;
814 struct usb_serial *serial;
817 if (mos7720_port == NULL)
820 port = mos7720_port->port;
821 serial = port->serial;
823 /***********************************************
824 * Init Sequence for higher rates
825 ***********************************************/
826 dbg("Sending Setting Commands ..........");
827 port_number = port->number - port->serial->minor;
830 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
832 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
834 send_mos_cmd(serial, MOS_WRITE, port->number, 0x02, &data);
836 mos7720_port->shadowMCR = data;
837 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
839 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
842 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
844 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
847 /***********************************************
848 * Set for higher rates *
849 ***********************************************/
852 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1,&data);
855 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
857 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
860 mos7720_port->shadowMCR = data;
861 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
863 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
865 /***********************************************
867 ***********************************************/
869 data = mos7720_port->shadowLCR | UART_LCR_DLAB;
870 mos7720_port->shadowLCR = data;
871 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
873 data = 0x001; /* DLL */
874 send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
875 data = 0x000; /* DLM */
876 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
878 data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
879 mos7720_port->shadowLCR = data;
880 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
885 /* baud rate information */
886 struct divisor_table_entry
892 /* Define table of divisors for moschip 7720 hardware *
893 * These assume a 3.6864MHz crystal, the standard /16, and *
895 static struct divisor_table_entry divisor_table[] = {
897 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
898 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
914 /*****************************************************************************
915 * calc_baud_rate_divisor
916 * this function calculates the proper baud rate divisor for the specified
918 *****************************************************************************/
919 static int calc_baud_rate_divisor(int baudrate, int *divisor)
927 dbg("%s - %d", __FUNCTION__, baudrate);
929 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
930 if (divisor_table[i].baudrate == baudrate) {
931 *divisor = divisor_table[i].divisor;
936 /* After trying for all the standard baud rates *
937 * Try calculating the divisor for this baud rate */
938 if (baudrate > 75 && baudrate < 230400) {
939 /* get the divisor */
940 custom = (__u16)(230400L / baudrate);
942 /* Check for round off */
943 round1 = (__u16)(2304000L / baudrate);
944 round = (__u16)(round1 - (custom * 10));
949 dbg("Baud %d = %d",baudrate, custom);
953 dbg("Baud calculation Failed...");
958 * send_cmd_write_baud_rate
959 * this function sends the proper command to change the baud rate of the
962 static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
965 struct usb_serial_port *port;
966 struct usb_serial *serial;
970 unsigned char number;
972 if (mos7720_port == NULL)
975 port = mos7720_port->port;
976 serial = port->serial;
978 dbg("%s: Entering ..........", __FUNCTION__);
980 number = port->number - port->serial->minor;
981 dbg("%s - port = %d, baud = %d", __FUNCTION__, port->number, baudrate);
983 /* Calculate the Divisor */
984 status = calc_baud_rate_divisor(baudrate, &divisor);
986 err("%s - bad baud rate", __FUNCTION__);
990 /* Enable access to divisor latch */
991 data = mos7720_port->shadowLCR | UART_LCR_DLAB;
992 mos7720_port->shadowLCR = data;
993 send_mos_cmd(serial, MOS_WRITE, number, UART_LCR, &data);
995 /* Write the divisor */
996 data = ((unsigned char)(divisor & 0xff));
997 send_mos_cmd(serial, MOS_WRITE, number, 0x00, &data);
999 data = ((unsigned char)((divisor & 0xff00) >> 8));
1000 send_mos_cmd(serial, MOS_WRITE, number, 0x01, &data);
1002 /* Disable access to divisor latch */
1003 data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1004 mos7720_port->shadowLCR = data;
1005 send_mos_cmd(serial, MOS_WRITE, number, 0x03, &data);
1011 * change_port_settings
1012 * This routine is called to set the UART on the device to match
1013 * the specified new settings.
1015 static void change_port_settings(struct moschip_port *mos7720_port,
1016 struct ktermios *old_termios)
1018 struct usb_serial_port *port;
1019 struct usb_serial *serial;
1020 struct tty_struct *tty;
1032 if (mos7720_port == NULL)
1035 port = mos7720_port->port;
1036 serial = port->serial;
1037 port_number = port->number - port->serial->minor;
1039 dbg("%s - port %d", __FUNCTION__, port->number);
1041 if (!mos7720_port->open) {
1042 dbg("%s - port not opened", __FUNCTION__);
1046 tty = mos7720_port->port->tty;
1048 if ((!tty) || (!tty->termios)) {
1049 dbg("%s - no tty structures", __FUNCTION__);
1053 dbg("%s: Entering ..........", __FUNCTION__);
1055 lData = UART_LCR_WLEN8;
1056 lStop = 0x00; /* 1 stop bit */
1057 lParity = 0x00; /* No parity */
1059 cflag = tty->termios->c_cflag;
1060 iflag = tty->termios->c_iflag;
1062 /* Change the number of bits */
1063 switch (cflag & CSIZE) {
1065 lData = UART_LCR_WLEN5;
1070 lData = UART_LCR_WLEN6;
1075 lData = UART_LCR_WLEN7;
1080 lData = UART_LCR_WLEN8;
1084 /* Change the Parity bit */
1085 if (cflag & PARENB) {
1086 if (cflag & PARODD) {
1087 lParity = UART_LCR_PARITY;
1088 dbg("%s - parity = odd", __FUNCTION__);
1090 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
1091 dbg("%s - parity = even", __FUNCTION__);
1095 dbg("%s - parity = none", __FUNCTION__);
1099 lParity = lParity | 0x20;
1101 /* Change the Stop bit */
1102 if (cflag & CSTOPB) {
1103 lStop = UART_LCR_STOP;
1104 dbg("%s - stop bits = 2", __FUNCTION__);
1107 dbg("%s - stop bits = 1", __FUNCTION__);
1110 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1111 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1112 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
1114 /* Update the LCR with the correct value */
1115 mos7720_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1116 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1119 /* Disable Interrupts */
1121 send_mos_cmd(serial,MOS_WRITE,port->number - port->serial->minor, UART_IER, &data);
1124 send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
1127 send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
1129 /* Send the updated LCR value to the mos7720 */
1130 data = mos7720_port->shadowLCR;
1131 send_mos_cmd(serial, MOS_WRITE, port_number, UART_LCR, &data);
1134 mos7720_port->shadowMCR = data;
1135 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
1137 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
1139 /* set up the MCR register and send it to the mos7720 */
1140 mos7720_port->shadowMCR = UART_MCR_OUT2;
1142 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1144 if (cflag & CRTSCTS) {
1145 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
1147 /* To set hardware flow control to the specified *
1148 * serial port, in SP1/2_CONTROL_REG */
1151 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1155 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1159 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1162 data = mos7720_port->shadowMCR;
1163 send_mos_cmd(serial, MOS_WRITE, port_number, UART_MCR, &data);
1165 /* Determine divisor based on baud rate */
1166 baud = tty_get_baud_rate(tty);
1168 /* pick a default, any default... */
1169 dbg("Picked default baud...");
1173 if (baud >= 230400) {
1174 set_higher_rates(mos7720_port, baud);
1175 /* Enable Interrupts */
1177 send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1181 dbg("%s - baud rate = %d", __FUNCTION__, baud);
1182 status = send_cmd_write_baud_rate(mos7720_port, baud);
1184 /* Enable Interrupts */
1186 send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1188 if (port->read_urb->status != -EINPROGRESS) {
1189 port->read_urb->dev = serial->dev;
1191 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1193 dbg("usb_submit_urb(read bulk) failed, status = %d",
1200 * mos7720_set_termios
1201 * this function is called by the tty driver when it wants to change the
1202 * termios structure.
1204 static void mos7720_set_termios(struct usb_serial_port *port,
1205 struct ktermios *old_termios)
1209 struct usb_serial *serial;
1210 struct moschip_port *mos7720_port;
1211 struct tty_struct *tty;
1213 serial = port->serial;
1215 mos7720_port = usb_get_serial_port_data(port);
1217 if (mos7720_port == NULL)
1222 if (!port->tty || !port->tty->termios) {
1223 dbg("%s - no tty or termios", __FUNCTION__);
1227 if (!mos7720_port->open) {
1228 dbg("%s - port not opened", __FUNCTION__);
1232 dbg("%s\n","setting termios - ASPIRE");
1234 cflag = tty->termios->c_cflag;
1237 printk("%s %s\n",__FUNCTION__,"cflag is NULL");
1241 /* check that they really want us to change something */
1243 if ((cflag == old_termios->c_cflag) &&
1244 (RELEVANT_IFLAG(tty->termios->c_iflag) ==
1245 RELEVANT_IFLAG(old_termios->c_iflag))) {
1246 dbg("Nothing to change");
1251 dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
1252 tty->termios->c_cflag,
1253 RELEVANT_IFLAG(tty->termios->c_iflag));
1256 dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
1257 old_termios->c_cflag,
1258 RELEVANT_IFLAG(old_termios->c_iflag));
1260 dbg("%s - port %d", __FUNCTION__, port->number);
1262 /* change the port settings to the new ones specified */
1263 change_port_settings(mos7720_port, old_termios);
1265 if(!port->read_urb) {
1266 dbg("%s","URB KILLED !!!!!\n");
1270 if(port->read_urb->status != -EINPROGRESS) {
1271 port->read_urb->dev = serial->dev;
1272 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1274 dbg("usb_submit_urb(read bulk) failed, status = %d",
1281 * get_lsr_info - get line status register info
1283 * Purpose: Let user call ioctl() to get info when the UART physically
1284 * is emptied. On bus types like RS485, the transmitter must
1285 * release the bus after transmitting. This must be done when
1286 * the transmit shift register is empty, not be done when the
1287 * transmit holding register is empty. This functionality
1288 * allows an RS485 driver to be written in user space.
1290 static int get_lsr_info(struct moschip_port *mos7720_port,
1291 unsigned int __user *value)
1294 unsigned int result = 0;
1296 count = mos7720_chars_in_buffer(mos7720_port->port);
1298 dbg("%s -- Empty", __FUNCTION__);
1299 result = TIOCSER_TEMT;
1302 if (copy_to_user(value, &result, sizeof(int)))
1308 * get_number_bytes_avail - get number of bytes available
1310 * Purpose: Let user call ioctl to get the count of number of bytes available.
1312 static int get_number_bytes_avail(struct moschip_port *mos7720_port,
1313 unsigned int __user *value)
1315 unsigned int result = 0;
1316 struct tty_struct *tty = mos7720_port->port->tty;
1319 return -ENOIOCTLCMD;
1321 result = tty->read_cnt;
1323 dbg("%s(%d) = %d", __FUNCTION__, mos7720_port->port->number, result);
1324 if (copy_to_user(value, &result, sizeof(int)))
1327 return -ENOIOCTLCMD;
1330 static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1331 unsigned int __user *value)
1337 struct usb_serial_port *port;
1339 if (mos7720_port == NULL)
1342 port = (struct usb_serial_port*)mos7720_port->port;
1343 mcr = mos7720_port->shadowMCR;
1345 if (copy_from_user(&arg, value, sizeof(int)))
1350 if (arg & TIOCM_RTS)
1351 mcr |= UART_MCR_RTS;
1352 if (arg & TIOCM_DTR)
1353 mcr |= UART_MCR_RTS;
1354 if (arg & TIOCM_LOOP)
1355 mcr |= UART_MCR_LOOP;
1359 if (arg & TIOCM_RTS)
1360 mcr &= ~UART_MCR_RTS;
1361 if (arg & TIOCM_DTR)
1362 mcr &= ~UART_MCR_RTS;
1363 if (arg & TIOCM_LOOP)
1364 mcr &= ~UART_MCR_LOOP;
1368 /* turn off the RTS and DTR and LOOPBACK
1369 * and then only turn on what was asked to */
1370 mcr &= ~(UART_MCR_RTS | UART_MCR_DTR | UART_MCR_LOOP);
1371 mcr |= ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0);
1372 mcr |= ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0);
1373 mcr |= ((arg & TIOCM_LOOP) ? UART_MCR_LOOP : 0);
1377 mos7720_port->shadowMCR = mcr;
1379 data = mos7720_port->shadowMCR;
1380 send_mos_cmd(port->serial, MOS_WRITE,
1381 port->number - port->serial->minor, UART_MCR, &data);
1386 static int get_modem_info(struct moschip_port *mos7720_port,
1387 unsigned int __user *value)
1389 unsigned int result = 0;
1390 unsigned int msr = mos7720_port->shadowMSR;
1391 unsigned int mcr = mos7720_port->shadowMCR;
1393 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1394 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1395 | ((msr & UART_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1396 | ((msr & UART_MSR_DCD) ? TIOCM_CAR: 0) /* 0x040 */
1397 | ((msr & UART_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1398 | ((msr & UART_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1401 dbg("%s -- %x", __FUNCTION__, result);
1403 if (copy_to_user(value, &result, sizeof(int)))
1408 static int get_serial_info(struct moschip_port *mos7720_port,
1409 struct serial_struct __user *retinfo)
1411 struct serial_struct tmp;
1416 memset(&tmp, 0, sizeof(tmp));
1418 tmp.type = PORT_16550A;
1419 tmp.line = mos7720_port->port->serial->minor;
1420 tmp.port = mos7720_port->port->number;
1422 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1423 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1424 tmp.baud_base = 9600;
1425 tmp.close_delay = 5*HZ;
1426 tmp.closing_wait = 30*HZ;
1428 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1433 static int mos7720_ioctl(struct usb_serial_port *port, struct file *file,
1434 unsigned int cmd, unsigned long arg)
1436 struct moschip_port *mos7720_port;
1437 struct async_icount cnow;
1438 struct async_icount cprev;
1439 struct serial_icounter_struct icount;
1441 mos7720_port = usb_get_serial_port_data(port);
1442 if (mos7720_port == NULL)
1445 dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
1449 /* return number of bytes available */
1450 dbg("%s (%d) TIOCINQ", __FUNCTION__, port->number);
1451 return get_number_bytes_avail(mos7720_port,
1452 (unsigned int __user *)arg);
1456 dbg("%s (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
1457 return get_lsr_info(mos7720_port, (unsigned int __user *)arg);
1463 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __FUNCTION__,
1465 return set_modem_info(mos7720_port, cmd,
1466 (unsigned int __user *)arg);
1469 dbg("%s (%d) TIOCMGET", __FUNCTION__, port->number);
1470 return get_modem_info(mos7720_port,
1471 (unsigned int __user *)arg);
1474 dbg("%s (%d) TIOCGSERIAL", __FUNCTION__, port->number);
1475 return get_serial_info(mos7720_port,
1476 (struct serial_struct __user *)arg);
1479 dbg("%s (%d) TIOCSSERIAL", __FUNCTION__, port->number);
1483 dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
1484 cprev = mos7720_port->icount;
1486 if (signal_pending(current))
1487 return -ERESTARTSYS;
1488 cnow = mos7720_port->icount;
1489 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1490 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1491 return -EIO; /* no change => error */
1492 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1493 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1494 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1495 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1504 cnow = mos7720_port->icount;
1505 icount.cts = cnow.cts;
1506 icount.dsr = cnow.dsr;
1507 icount.rng = cnow.rng;
1508 icount.dcd = cnow.dcd;
1509 icount.rx = cnow.rx;
1510 icount.tx = cnow.tx;
1511 icount.frame = cnow.frame;
1512 icount.overrun = cnow.overrun;
1513 icount.parity = cnow.parity;
1514 icount.brk = cnow.brk;
1515 icount.buf_overrun = cnow.buf_overrun;
1517 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__,
1518 port->number, icount.rx, icount.tx );
1519 if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
1524 return -ENOIOCTLCMD;
1527 static int mos7720_startup(struct usb_serial *serial)
1529 struct moschip_serial *mos7720_serial;
1530 struct moschip_port *mos7720_port;
1531 struct usb_device *dev;
1535 dbg("%s: Entering ..........", __FUNCTION__);
1538 dbg("Invalid Handler");
1544 /* create our private serial structure */
1545 mos7720_serial = kzalloc(sizeof(struct moschip_serial), GFP_KERNEL);
1546 if (mos7720_serial == NULL) {
1547 err("%s - Out of memory", __FUNCTION__);
1551 usb_set_serial_data(serial, mos7720_serial);
1553 /* we set up the pointers to the endpoints in the mos7720_open *
1554 * function, as the structures aren't created yet. */
1556 /* set up port private structures */
1557 for (i = 0; i < serial->num_ports; ++i) {
1558 mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
1559 if (mos7720_port == NULL) {
1560 err("%s - Out of memory", __FUNCTION__);
1561 usb_set_serial_data(serial, NULL);
1562 kfree(mos7720_serial);
1566 /* Initialize all port interrupt end point to port 0 int
1567 * endpoint. Our device has only one interrupt endpoint
1568 * comman to all ports */
1569 serial->port[i]->interrupt_in_endpointAddress = serial->port[0]->interrupt_in_endpointAddress;
1571 mos7720_port->port = serial->port[i];
1572 usb_set_serial_port_data(serial->port[i], mos7720_port);
1574 dbg("port number is %d", serial->port[i]->number);
1575 dbg("serial number is %d", serial->minor);
1579 /* setting configuration feature to one */
1580 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1581 (__u8)0x03, 0x00,0x01,0x00, NULL, 0x00, 5*HZ);
1583 send_mos_cmd(serial,MOS_READ,0x00, UART_LSR, &data); // LSR For Port 1
1586 send_mos_cmd(serial,MOS_READ,0x01, UART_LSR, &data); // LSR For Port 2
1592 static void mos7720_shutdown(struct usb_serial *serial)
1596 /* free private structure allocated for serial port */
1597 for (i=0; i < serial->num_ports; ++i) {
1598 kfree(usb_get_serial_port_data(serial->port[i]));
1599 usb_set_serial_port_data(serial->port[i], NULL);
1602 /* free private structure allocated for serial device */
1603 kfree(usb_get_serial_data(serial));
1604 usb_set_serial_data(serial, NULL);
1607 static struct usb_driver usb_driver = {
1608 .name = "moschip7720",
1609 .probe = usb_serial_probe,
1610 .disconnect = usb_serial_disconnect,
1611 .id_table = moschip_port_id_table,
1615 static struct usb_serial_driver moschip7720_2port_driver = {
1617 .owner = THIS_MODULE,
1618 .name = "moschip7720",
1620 .description = "Moschip 2 port adapter",
1621 .usb_driver = &usb_driver,
1622 .id_table = moschip_port_id_table,
1623 .num_interrupt_in = 1,
1627 .open = mos7720_open,
1628 .close = mos7720_close,
1629 .throttle = mos7720_throttle,
1630 .unthrottle = mos7720_unthrottle,
1631 .attach = mos7720_startup,
1632 .shutdown = mos7720_shutdown,
1633 .ioctl = mos7720_ioctl,
1634 .set_termios = mos7720_set_termios,
1635 .write = mos7720_write,
1636 .write_room = mos7720_write_room,
1637 .chars_in_buffer = mos7720_chars_in_buffer,
1638 .break_ctl = mos7720_break,
1639 .read_bulk_callback = mos7720_bulk_in_callback,
1640 .read_int_callback = mos7720_interrupt_callback,
1643 static int __init moschip7720_init(void)
1647 dbg("%s: Entering ..........", __FUNCTION__);
1649 /* Register with the usb serial */
1650 retval = usb_serial_register(&moschip7720_2port_driver);
1652 goto failed_port_device_register;
1654 info(DRIVER_DESC " " DRIVER_VERSION);
1656 /* Register with the usb */
1657 retval = usb_register(&usb_driver);
1659 goto failed_usb_register;
1663 failed_usb_register:
1664 usb_serial_deregister(&moschip7720_2port_driver);
1666 failed_port_device_register:
1670 static void __exit moschip7720_exit(void)
1672 usb_deregister(&usb_driver);
1673 usb_serial_deregister(&moschip7720_2port_driver);
1676 module_init(moschip7720_init);
1677 module_exit(moschip7720_exit);
1679 /* Module information */
1680 MODULE_AUTHOR( DRIVER_AUTHOR );
1681 MODULE_DESCRIPTION( DRIVER_DESC );
1682 MODULE_LICENSE("GPL");
1684 module_param(debug, bool, S_IRUGO | S_IWUSR);
1685 MODULE_PARM_DESC(debug, "Debug enabled or not");