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 <linux/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 */
68 __u8 shadowLCR; /* last LCR value received */
69 __u8 shadowMCR; /* last MCR value received */
70 __u8 shadowMSR; /* last MSR value received */
72 struct async_icount icount;
73 struct usb_serial_port *port; /* loop back to the owner */
74 struct urb *write_urb_pool[NUM_URBS];
77 /* This structure holds all of the individual serial device information */
78 struct moschip_serial {
79 int interrupt_started;
84 #define USB_VENDOR_ID_MOSCHIP 0x9710
85 #define MOSCHIP_DEVICE_ID_7720 0x7720
86 #define MOSCHIP_DEVICE_ID_7715 0x7715
88 static struct usb_device_id moschip_port_id_table[] = {
89 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
90 { } /* terminating entry */
92 MODULE_DEVICE_TABLE(usb, moschip_port_id_table);
96 * mos7720_interrupt_callback
97 * this is the callback function for when we have received data on the
100 static void mos7720_interrupt_callback(struct urb *urb)
104 int status = urb->status;
109 dbg("%s", " : Entering\n");
118 /* this urb is terminated, clean up */
119 dbg("%s - urb shutting down with status: %d", __func__,
123 dbg("%s - nonzero urb status received: %d", __func__,
128 length = urb->actual_length;
129 data = urb->transfer_buffer;
131 /* Moschip get 4 bytes
132 * Byte 1 IIR Port 1 (port.number is 0)
133 * Byte 2 IIR Port 2 (port.number is 1)
134 * Byte 3 --------------
135 * Byte 4 FIFO status for both */
137 /* the above description is inverted
138 * oneukum 2007-03-14 */
140 if (unlikely(length != 4)) {
141 dbg("Wrong data !!!");
148 if ((sp1 | sp2) & 0x01) {
149 /* No Interrupt Pending in both the ports */
150 dbg("No Interrupt !!!");
152 switch (sp1 & 0x0f) {
154 dbg("Serial Port 1: Receiver status error or address "
155 "bit detected in 9-bit mode\n");
158 dbg("Serial Port 1: Receiver time out");
161 dbg("Serial Port 1: Modem status change");
165 switch (sp2 & 0x0f) {
167 dbg("Serial Port 2: Receiver status error or address "
168 "bit detected in 9-bit mode");
171 dbg("Serial Port 2: Receiver time out");
174 dbg("Serial Port 2: Modem status change");
180 result = usb_submit_urb(urb, GFP_ATOMIC);
182 dev_err(&urb->dev->dev,
183 "%s - Error %d submitting control urb\n",
189 * mos7720_bulk_in_callback
190 * this is the callback function for when we have received data on the
193 static void mos7720_bulk_in_callback(struct urb *urb)
196 unsigned char *data ;
197 struct usb_serial_port *port;
198 struct moschip_port *mos7720_port;
199 struct tty_struct *tty;
200 int status = urb->status;
203 dbg("nonzero read bulk status received: %d", status);
207 mos7720_port = urb->context;
209 dbg("%s", "NULL mos7720_port pointer \n");
213 port = mos7720_port->port;
215 dbg("Entering...%s", __func__);
217 data = urb->transfer_buffer;
219 tty = tty_port_tty_get(&port->port);
220 if (tty && urb->actual_length) {
221 tty_buffer_request_room(tty, urb->actual_length);
222 tty_insert_flip_string(tty, data, urb->actual_length);
223 tty_flip_buffer_push(tty);
227 if (!port->read_urb) {
228 dbg("URB KILLED !!!");
232 if (port->read_urb->status != -EINPROGRESS) {
233 port->read_urb->dev = port->serial->dev;
235 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
237 dbg("usb_submit_urb(read bulk) failed, retval = %d",
243 * mos7720_bulk_out_data_callback
244 * this is the callback function for when we have finished sending serial
245 * data on the bulk out endpoint.
247 static void mos7720_bulk_out_data_callback(struct urb *urb)
249 struct moschip_port *mos7720_port;
250 struct tty_struct *tty;
251 int status = urb->status;
254 dbg("nonzero write bulk status received:%d", status);
258 mos7720_port = urb->context;
260 dbg("NULL mos7720_port pointer");
264 dbg("Entering .........");
266 tty = tty_port_tty_get(&mos7720_port->port->port);
268 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;
293 if ((product == MOSCHIP_DEVICE_ID_7715) &&
295 dbg("serial->product== MOSCHIP_DEVICE_ID_7715");
300 if (request == MOS_WRITE) {
301 request = (__u8)MOS_WRITE;
302 requesttype = (__u8)0x40;
303 value = value + (__u16)*((unsigned char *)data);
305 pipe = usb_sndctrlpipe(serial->dev, 0);
307 request = (__u8)MOS_READ;
308 requesttype = (__u8)0xC0;
310 pipe = usb_rcvctrlpipe(serial->dev, 0);
313 status = usb_control_msg(serial->dev, pipe, request, requesttype,
314 value, index, data, size, MOS_WDR_TIMEOUT);
317 dbg("Command Write failed Value %x index %x\n", value, index);
322 static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
324 struct usb_serial *serial;
325 struct usb_serial_port *port0;
327 struct moschip_serial *mos7720_serial;
328 struct moschip_port *mos7720_port;
332 int allocated_urbs = 0;
335 serial = port->serial;
337 mos7720_port = usb_get_serial_port_data(port);
338 if (mos7720_port == NULL)
341 port0 = serial->port[0];
343 mos7720_serial = usb_get_serial_data(serial);
345 if (mos7720_serial == NULL || port0 == NULL)
348 usb_clear_halt(serial->dev, port->write_urb->pipe);
349 usb_clear_halt(serial->dev, port->read_urb->pipe);
351 /* Initialising the write urb pool */
352 for (j = 0; j < NUM_URBS; ++j) {
353 urb = usb_alloc_urb(0, GFP_KERNEL);
354 mos7720_port->write_urb_pool[j] = urb;
357 dev_err(&port->dev, "No more urbs???\n");
361 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
363 if (!urb->transfer_buffer) {
365 "%s-out of memory for urb buffers.\n",
367 usb_free_urb(mos7720_port->write_urb_pool[j]);
368 mos7720_port->write_urb_pool[j] = NULL;
377 /* 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,
429 port_number + 1, &data);
432 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
434 data = data | (port->number - port->serial->minor + 1);
435 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
438 mos7720_port->shadowLCR = data;
439 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
441 send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
443 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
445 mos7720_port->shadowLCR = data;
446 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
448 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
450 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
452 /* see if we've set up our endpoint info yet *
453 * (can't set it up in mos7720_startup as the *
454 * structures were not set up at that time.) */
455 if (!mos7720_serial->interrupt_started) {
456 dbg("Interrupt buffer NULL !!!");
458 /* not set up yet, so do it now */
459 mos7720_serial->interrupt_started = 1;
461 dbg("To Submit URB !!!");
463 /* set up our interrupt urb */
464 usb_fill_int_urb(port0->interrupt_in_urb, serial->dev,
465 usb_rcvintpipe(serial->dev,
466 port->interrupt_in_endpointAddress),
467 port0->interrupt_in_buffer,
468 port0->interrupt_in_urb->transfer_buffer_length,
469 mos7720_interrupt_callback, mos7720_port,
470 port0->interrupt_in_urb->interval);
472 /* start interrupt read for this mos7720 this interrupt *
473 * will continue as long as the mos7720 is connected */
474 dbg("Submit URB over !!!");
475 response = usb_submit_urb(port0->interrupt_in_urb, GFP_KERNEL);
478 "%s - Error %d submitting control urb\n",
482 /* set up our bulk in urb */
483 usb_fill_bulk_urb(port->read_urb, serial->dev,
484 usb_rcvbulkpipe(serial->dev,
485 port->bulk_in_endpointAddress),
486 port->bulk_in_buffer,
487 port->read_urb->transfer_buffer_length,
488 mos7720_bulk_in_callback, mos7720_port);
489 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
491 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
494 /* initialize our icount structure */
495 memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
497 /* initialize our port settings */
498 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
500 /* send a open port command */
501 mos7720_port->open = 1;
507 * mos7720_chars_in_buffer
508 * this function is called by the tty driver when it wants to know how many
509 * bytes of data we currently have outstanding in the port (data that has
510 * been written, but hasn't made it out the port yet)
511 * If successful, we return the number of bytes left to be written in the
513 * Otherwise we return a negative error number.
515 static int mos7720_chars_in_buffer(struct tty_struct *tty)
517 struct usb_serial_port *port = tty->driver_data;
520 struct moschip_port *mos7720_port;
522 dbg("%s:entering ...........", __func__);
524 mos7720_port = usb_get_serial_port_data(port);
525 if (mos7720_port == NULL) {
526 dbg("%s:leaving ...........", __func__);
530 for (i = 0; i < NUM_URBS; ++i) {
531 if (mos7720_port->write_urb_pool[i] &&
532 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
533 chars += URB_TRANSFER_BUFFER_SIZE;
535 dbg("%s - returns %d", __func__, chars);
539 static void mos7720_close(struct usb_serial_port *port)
541 struct usb_serial *serial;
542 struct moschip_port *mos7720_port;
546 dbg("mos7720_close:entering...");
548 serial = port->serial;
550 mos7720_port = usb_get_serial_port_data(port);
551 if (mos7720_port == NULL)
554 for (j = 0; j < NUM_URBS; ++j)
555 usb_kill_urb(mos7720_port->write_urb_pool[j]);
557 /* Freeing Write URBs */
558 for (j = 0; j < NUM_URBS; ++j) {
559 if (mos7720_port->write_urb_pool[j]) {
560 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
561 usb_free_urb(mos7720_port->write_urb_pool[j]);
565 /* While closing port, shutdown all bulk read, write *
566 * and interrupt read if they exists, otherwise nop */
567 dbg("Shutdown bulk write");
568 usb_kill_urb(port->write_urb);
569 dbg("Shutdown bulk read");
570 usb_kill_urb(port->read_urb);
572 mutex_lock(&serial->disc_mutex);
573 /* these commands must not be issued if the device has
574 * been disconnected */
575 if (!serial->disconnected) {
577 send_mos_cmd(serial, MOS_WRITE,
578 port->number - port->serial->minor, 0x04, &data);
581 send_mos_cmd(serial, MOS_WRITE,
582 port->number - port->serial->minor, 0x01, &data);
584 mutex_unlock(&serial->disc_mutex);
585 mos7720_port->open = 0;
587 dbg("Leaving %s", __func__);
590 static void mos7720_break(struct tty_struct *tty, int break_state)
592 struct usb_serial_port *port = tty->driver_data;
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 tty_struct *tty)
626 struct usb_serial_port *port = tty->driver_data;
627 struct moschip_port *mos7720_port;
631 dbg("%s:entering ...........", __func__);
633 mos7720_port = usb_get_serial_port_data(port);
634 if (mos7720_port == NULL) {
635 dbg("%s:leaving ...........", __func__);
640 for (i = 0; i < NUM_URBS; ++i) {
641 if (mos7720_port->write_urb_pool[i] &&
642 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
643 room += URB_TRANSFER_BUFFER_SIZE;
646 dbg("%s - returns %d", __func__, room);
650 static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
651 const unsigned char *data, int count)
658 struct moschip_port *mos7720_port;
659 struct usb_serial *serial;
661 const unsigned char *current_position = data;
663 dbg("%s:entering ...........", __func__);
665 serial = port->serial;
667 mos7720_port = usb_get_serial_port_data(port);
668 if (mos7720_port == NULL) {
669 dbg("mos7720_port is NULL");
673 /* try to find a free urb in the list */
676 for (i = 0; i < NUM_URBS; ++i) {
677 if (mos7720_port->write_urb_pool[i] &&
678 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
679 urb = mos7720_port->write_urb_pool[i];
686 dbg("%s - no more free urbs", __func__);
690 if (urb->transfer_buffer == NULL) {
691 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
693 if (urb->transfer_buffer == NULL) {
694 dev_err(&port->dev, "%s no more kernel memory...\n",
699 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
701 memcpy(urb->transfer_buffer, current_position, transfer_size);
702 usb_serial_debug_data(debug, &port->dev, __func__, transfer_size,
703 urb->transfer_buffer);
705 /* fill urb with data and submit */
706 usb_fill_bulk_urb(urb, serial->dev,
707 usb_sndbulkpipe(serial->dev,
708 port->bulk_out_endpointAddress),
709 urb->transfer_buffer, transfer_size,
710 mos7720_bulk_out_data_callback, mos7720_port);
712 /* send it down the pipe */
713 status = usb_submit_urb(urb, GFP_ATOMIC);
715 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
716 "with status = %d\n", __func__, status);
720 bytes_sent = transfer_size;
726 static void mos7720_throttle(struct tty_struct *tty)
728 struct usb_serial_port *port = tty->driver_data;
729 struct moschip_port *mos7720_port;
732 dbg("%s- port %d\n", __func__, port->number);
734 mos7720_port = usb_get_serial_port_data(port);
736 if (mos7720_port == NULL)
739 if (!mos7720_port->open) {
740 dbg("port not opened");
744 dbg("%s: Entering ..........", __func__);
746 /* if we are implementing XON/XOFF, send the stop character */
748 unsigned char stop_char = STOP_CHAR(tty);
749 status = mos7720_write(tty, port, &stop_char, 1);
754 /* if we are implementing RTS/CTS, toggle that line */
755 if (tty->termios->c_cflag & CRTSCTS) {
756 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
757 status = send_mos_cmd(port->serial, MOS_WRITE,
758 port->number - port->serial->minor,
759 UART_MCR, &mos7720_port->shadowMCR);
765 static void mos7720_unthrottle(struct tty_struct *tty)
767 struct usb_serial_port *port = tty->driver_data;
768 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
771 if (mos7720_port == NULL)
774 if (!mos7720_port->open) {
775 dbg("%s - port not opened", __func__);
779 dbg("%s: Entering ..........", __func__);
781 /* if we are implementing XON/XOFF, send the start character */
783 unsigned char start_char = START_CHAR(tty);
784 status = mos7720_write(tty, port, &start_char, 1);
789 /* if we are implementing RTS/CTS, toggle that line */
790 if (tty->termios->c_cflag & CRTSCTS) {
791 mos7720_port->shadowMCR |= UART_MCR_RTS;
792 status = send_mos_cmd(port->serial, MOS_WRITE,
793 port->number - port->serial->minor,
794 UART_MCR, &mos7720_port->shadowMCR);
800 static int set_higher_rates(struct moschip_port *mos7720_port,
804 struct usb_serial_port *port;
805 struct usb_serial *serial;
808 if (mos7720_port == NULL)
811 port = mos7720_port->port;
812 serial = port->serial;
814 /***********************************************
815 * Init Sequence for higher rates
816 ***********************************************/
817 dbg("Sending Setting Commands ..........");
818 port_number = port->number - port->serial->minor;
821 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
823 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
825 send_mos_cmd(serial, MOS_WRITE, port->number, 0x02, &data);
827 mos7720_port->shadowMCR = data;
828 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
830 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
833 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
835 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
838 /***********************************************
839 * Set for higher rates *
840 ***********************************************/
843 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
846 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
848 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
851 mos7720_port->shadowMCR = data;
852 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
854 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
856 /***********************************************
858 ***********************************************/
860 data = mos7720_port->shadowLCR | UART_LCR_DLAB;
861 mos7720_port->shadowLCR = data;
862 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
864 data = 0x001; /* DLL */
865 send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
866 data = 0x000; /* DLM */
867 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
869 data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
870 mos7720_port->shadowLCR = data;
871 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
876 /* baud rate information */
877 struct divisor_table_entry {
882 /* Define table of divisors for moschip 7720 hardware *
883 * These assume a 3.6864MHz crystal, the standard /16, and *
885 static struct divisor_table_entry divisor_table[] = {
887 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
888 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
904 /*****************************************************************************
905 * calc_baud_rate_divisor
906 * this function calculates the proper baud rate divisor for the specified
908 *****************************************************************************/
909 static int calc_baud_rate_divisor(int baudrate, int *divisor)
917 dbg("%s - %d", __func__, baudrate);
919 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
920 if (divisor_table[i].baudrate == baudrate) {
921 *divisor = divisor_table[i].divisor;
926 /* After trying for all the standard baud rates *
927 * Try calculating the divisor for this baud rate */
928 if (baudrate > 75 && baudrate < 230400) {
929 /* get the divisor */
930 custom = (__u16)(230400L / baudrate);
932 /* Check for round off */
933 round1 = (__u16)(2304000L / baudrate);
934 round = (__u16)(round1 - (custom * 10));
939 dbg("Baud %d = %d", baudrate, custom);
943 dbg("Baud calculation Failed...");
948 * send_cmd_write_baud_rate
949 * this function sends the proper command to change the baud rate of the
952 static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
955 struct usb_serial_port *port;
956 struct usb_serial *serial;
960 unsigned char number;
962 if (mos7720_port == NULL)
965 port = mos7720_port->port;
966 serial = port->serial;
968 dbg("%s: Entering ..........", __func__);
970 number = port->number - port->serial->minor;
971 dbg("%s - port = %d, baud = %d", __func__, port->number, baudrate);
973 /* Calculate the Divisor */
974 status = calc_baud_rate_divisor(baudrate, &divisor);
976 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
980 /* Enable access to divisor latch */
981 data = mos7720_port->shadowLCR | UART_LCR_DLAB;
982 mos7720_port->shadowLCR = data;
983 send_mos_cmd(serial, MOS_WRITE, number, UART_LCR, &data);
985 /* Write the divisor */
986 data = ((unsigned char)(divisor & 0xff));
987 send_mos_cmd(serial, MOS_WRITE, number, 0x00, &data);
989 data = ((unsigned char)((divisor & 0xff00) >> 8));
990 send_mos_cmd(serial, MOS_WRITE, number, 0x01, &data);
992 /* Disable access to divisor latch */
993 data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
994 mos7720_port->shadowLCR = data;
995 send_mos_cmd(serial, MOS_WRITE, number, 0x03, &data);
1001 * change_port_settings
1002 * This routine is called to set the UART on the device to match
1003 * the specified new settings.
1005 static void change_port_settings(struct tty_struct *tty,
1006 struct moschip_port *mos7720_port,
1007 struct ktermios *old_termios)
1009 struct usb_serial_port *port;
1010 struct usb_serial *serial;
1022 if (mos7720_port == NULL)
1025 port = mos7720_port->port;
1026 serial = port->serial;
1027 port_number = port->number - port->serial->minor;
1029 dbg("%s - port %d", __func__, port->number);
1031 if (!mos7720_port->open) {
1032 dbg("%s - port not opened", __func__);
1036 dbg("%s: Entering ..........", __func__);
1038 lData = UART_LCR_WLEN8;
1039 lStop = 0x00; /* 1 stop bit */
1040 lParity = 0x00; /* No parity */
1042 cflag = tty->termios->c_cflag;
1043 iflag = tty->termios->c_iflag;
1045 /* Change the number of bits */
1046 switch (cflag & CSIZE) {
1048 lData = UART_LCR_WLEN5;
1053 lData = UART_LCR_WLEN6;
1058 lData = UART_LCR_WLEN7;
1063 lData = UART_LCR_WLEN8;
1067 /* Change the Parity bit */
1068 if (cflag & PARENB) {
1069 if (cflag & PARODD) {
1070 lParity = UART_LCR_PARITY;
1071 dbg("%s - parity = odd", __func__);
1073 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
1074 dbg("%s - parity = even", __func__);
1078 dbg("%s - parity = none", __func__);
1082 lParity = lParity | 0x20;
1084 /* Change the Stop bit */
1085 if (cflag & CSTOPB) {
1086 lStop = UART_LCR_STOP;
1087 dbg("%s - stop bits = 2", __func__);
1090 dbg("%s - stop bits = 1", __func__);
1093 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1094 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1095 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
1097 /* Update the LCR with the correct value */
1098 mos7720_port->shadowLCR &=
1099 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1100 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1103 /* Disable Interrupts */
1105 send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
1109 send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
1112 send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
1114 /* Send the updated LCR value to the mos7720 */
1115 data = mos7720_port->shadowLCR;
1116 send_mos_cmd(serial, MOS_WRITE, port_number, UART_LCR, &data);
1119 mos7720_port->shadowMCR = data;
1120 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
1122 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
1124 /* set up the MCR register and send it to the mos7720 */
1125 mos7720_port->shadowMCR = UART_MCR_OUT2;
1127 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1129 if (cflag & CRTSCTS) {
1130 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
1131 /* To set hardware flow control to the specified *
1132 * serial port, in SP1/2_CONTROL_REG */
1135 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1139 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1143 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1146 data = mos7720_port->shadowMCR;
1147 send_mos_cmd(serial, MOS_WRITE, port_number, UART_MCR, &data);
1149 /* Determine divisor based on baud rate */
1150 baud = tty_get_baud_rate(tty);
1152 /* pick a default, any default... */
1153 dbg("Picked default baud...");
1157 if (baud >= 230400) {
1158 set_higher_rates(mos7720_port, baud);
1159 /* Enable Interrupts */
1161 send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1165 dbg("%s - baud rate = %d", __func__, baud);
1166 status = send_cmd_write_baud_rate(mos7720_port, baud);
1167 /* FIXME: needs to write actual resulting baud back not just
1170 tty_encode_baud_rate(tty, baud, baud);
1171 /* Enable Interrupts */
1173 send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1175 if (port->read_urb->status != -EINPROGRESS) {
1176 port->read_urb->dev = serial->dev;
1178 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1180 dbg("usb_submit_urb(read bulk) failed, status = %d",
1187 * mos7720_set_termios
1188 * this function is called by the tty driver when it wants to change the
1189 * termios structure.
1191 static void mos7720_set_termios(struct tty_struct *tty,
1192 struct usb_serial_port *port, struct ktermios *old_termios)
1196 struct usb_serial *serial;
1197 struct moschip_port *mos7720_port;
1199 serial = port->serial;
1201 mos7720_port = usb_get_serial_port_data(port);
1203 if (mos7720_port == NULL)
1206 if (!mos7720_port->open) {
1207 dbg("%s - port not opened", __func__);
1211 dbg("%s\n", "setting termios - ASPIRE");
1213 cflag = tty->termios->c_cflag;
1215 dbg("%s - cflag %08x iflag %08x", __func__,
1216 tty->termios->c_cflag,
1217 RELEVANT_IFLAG(tty->termios->c_iflag));
1219 dbg("%s - old cflag %08x old iflag %08x", __func__,
1220 old_termios->c_cflag,
1221 RELEVANT_IFLAG(old_termios->c_iflag));
1223 dbg("%s - port %d", __func__, port->number);
1225 /* change the port settings to the new ones specified */
1226 change_port_settings(tty, mos7720_port, old_termios);
1228 if (!port->read_urb) {
1229 dbg("%s", "URB KILLED !!!!!\n");
1233 if (port->read_urb->status != -EINPROGRESS) {
1234 port->read_urb->dev = serial->dev;
1235 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1237 dbg("usb_submit_urb(read bulk) failed, status = %d",
1244 * get_lsr_info - get line status register info
1246 * Purpose: Let user call ioctl() to get info when the UART physically
1247 * is emptied. On bus types like RS485, the transmitter must
1248 * release the bus after transmitting. This must be done when
1249 * the transmit shift register is empty, not be done when the
1250 * transmit holding register is empty. This functionality
1251 * allows an RS485 driver to be written in user space.
1253 static int get_lsr_info(struct tty_struct *tty,
1254 struct moschip_port *mos7720_port, unsigned int __user *value)
1256 struct usb_serial_port *port = tty->driver_data;
1257 unsigned int result = 0;
1258 unsigned char data = 0;
1259 int port_number = port->number - port->serial->minor;
1262 count = mos7720_chars_in_buffer(tty);
1264 send_mos_cmd(port->serial, MOS_READ, port_number,
1266 if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1267 == (UART_LSR_TEMT | UART_LSR_THRE)) {
1268 dbg("%s -- Empty", __func__);
1269 result = TIOCSER_TEMT;
1272 if (copy_to_user(value, &result, sizeof(int)))
1277 static int mos7720_tiocmget(struct tty_struct *tty, struct file *file)
1279 struct usb_serial_port *port = tty->driver_data;
1280 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1281 unsigned int result = 0;
1285 dbg("%s - port %d", __func__, port->number);
1287 mcr = mos7720_port->shadowMCR;
1288 msr = mos7720_port->shadowMSR;
1290 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1291 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1292 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1293 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
1294 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1295 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
1297 dbg("%s -- %x", __func__, result);
1302 static int mos7720_tiocmset(struct tty_struct *tty, struct file *file,
1303 unsigned int set, unsigned int clear)
1305 struct usb_serial_port *port = tty->driver_data;
1306 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1310 dbg("%s - port %d", __func__, port->number);
1311 dbg("he was at tiocmget");
1313 mcr = mos7720_port->shadowMCR;
1315 if (set & TIOCM_RTS)
1316 mcr |= UART_MCR_RTS;
1317 if (set & TIOCM_DTR)
1318 mcr |= UART_MCR_DTR;
1319 if (set & TIOCM_LOOP)
1320 mcr |= UART_MCR_LOOP;
1322 if (clear & TIOCM_RTS)
1323 mcr &= ~UART_MCR_RTS;
1324 if (clear & TIOCM_DTR)
1325 mcr &= ~UART_MCR_DTR;
1326 if (clear & TIOCM_LOOP)
1327 mcr &= ~UART_MCR_LOOP;
1329 mos7720_port->shadowMCR = mcr;
1330 lmcr = mos7720_port->shadowMCR;
1332 send_mos_cmd(port->serial, MOS_WRITE,
1333 port->number - port->serial->minor, UART_MCR, &lmcr);
1338 static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1339 unsigned int __user *value)
1345 struct usb_serial_port *port;
1347 if (mos7720_port == NULL)
1350 port = (struct usb_serial_port *)mos7720_port->port;
1351 mcr = mos7720_port->shadowMCR;
1353 if (copy_from_user(&arg, value, sizeof(int)))
1358 if (arg & TIOCM_RTS)
1359 mcr |= UART_MCR_RTS;
1360 if (arg & TIOCM_DTR)
1361 mcr |= UART_MCR_RTS;
1362 if (arg & TIOCM_LOOP)
1363 mcr |= UART_MCR_LOOP;
1367 if (arg & TIOCM_RTS)
1368 mcr &= ~UART_MCR_RTS;
1369 if (arg & TIOCM_DTR)
1370 mcr &= ~UART_MCR_RTS;
1371 if (arg & TIOCM_LOOP)
1372 mcr &= ~UART_MCR_LOOP;
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_serial_info(struct moschip_port *mos7720_port,
1387 struct serial_struct __user *retinfo)
1389 struct serial_struct tmp;
1394 memset(&tmp, 0, sizeof(tmp));
1396 tmp.type = PORT_16550A;
1397 tmp.line = mos7720_port->port->serial->minor;
1398 tmp.port = mos7720_port->port->number;
1400 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1401 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1402 tmp.baud_base = 9600;
1403 tmp.close_delay = 5*HZ;
1404 tmp.closing_wait = 30*HZ;
1406 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1411 static int mos7720_ioctl(struct tty_struct *tty, struct file *file,
1412 unsigned int cmd, unsigned long arg)
1414 struct usb_serial_port *port = tty->driver_data;
1415 struct moschip_port *mos7720_port;
1416 struct async_icount cnow;
1417 struct async_icount cprev;
1418 struct serial_icounter_struct icount;
1420 mos7720_port = usb_get_serial_port_data(port);
1421 if (mos7720_port == NULL)
1424 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
1428 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
1429 return get_lsr_info(tty, mos7720_port,
1430 (unsigned int __user *)arg);
1433 /* FIXME: These should be using the mode methods */
1436 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET",
1437 __func__, port->number);
1438 return set_modem_info(mos7720_port, cmd,
1439 (unsigned int __user *)arg);
1442 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
1443 return get_serial_info(mos7720_port,
1444 (struct serial_struct __user *)arg);
1447 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
1448 cprev = mos7720_port->icount;
1450 if (signal_pending(current))
1451 return -ERESTARTSYS;
1452 cnow = mos7720_port->icount;
1453 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1454 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1455 return -EIO; /* no change => error */
1456 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1457 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1458 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1459 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1468 cnow = mos7720_port->icount;
1470 memset(&icount, 0, sizeof(struct serial_icounter_struct));
1472 icount.cts = cnow.cts;
1473 icount.dsr = cnow.dsr;
1474 icount.rng = cnow.rng;
1475 icount.dcd = cnow.dcd;
1476 icount.rx = cnow.rx;
1477 icount.tx = cnow.tx;
1478 icount.frame = cnow.frame;
1479 icount.overrun = cnow.overrun;
1480 icount.parity = cnow.parity;
1481 icount.brk = cnow.brk;
1482 icount.buf_overrun = cnow.buf_overrun;
1484 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
1485 port->number, icount.rx, icount.tx);
1486 if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
1491 return -ENOIOCTLCMD;
1494 static int mos7720_startup(struct usb_serial *serial)
1496 struct moschip_serial *mos7720_serial;
1497 struct moschip_port *mos7720_port;
1498 struct usb_device *dev;
1502 dbg("%s: Entering ..........", __func__);
1505 dbg("Invalid Handler");
1511 /* create our private serial structure */
1512 mos7720_serial = kzalloc(sizeof(struct moschip_serial), GFP_KERNEL);
1513 if (mos7720_serial == NULL) {
1514 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
1518 usb_set_serial_data(serial, mos7720_serial);
1520 /* we set up the pointers to the endpoints in the mos7720_open *
1521 * function, as the structures aren't created yet. */
1523 /* set up port private structures */
1524 for (i = 0; i < serial->num_ports; ++i) {
1525 mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
1526 if (mos7720_port == NULL) {
1527 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
1528 usb_set_serial_data(serial, NULL);
1529 kfree(mos7720_serial);
1533 /* Initialize all port interrupt end point to port 0 int
1534 * endpoint. Our device has only one interrupt endpoint
1535 * comman to all ports */
1536 serial->port[i]->interrupt_in_endpointAddress =
1537 serial->port[0]->interrupt_in_endpointAddress;
1539 mos7720_port->port = serial->port[i];
1540 usb_set_serial_port_data(serial->port[i], mos7720_port);
1542 dbg("port number is %d", serial->port[i]->number);
1543 dbg("serial number is %d", serial->minor);
1547 /* setting configuration feature to one */
1548 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1549 (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5*HZ);
1551 /* LSR For Port 1 */
1552 send_mos_cmd(serial, MOS_READ, 0x00, UART_LSR, &data);
1553 dbg("LSR:%x", data);
1555 /* LSR For Port 2 */
1556 send_mos_cmd(serial, MOS_READ, 0x01, UART_LSR, &data);
1557 dbg("LSR:%x", data);
1562 static void mos7720_release(struct usb_serial *serial)
1566 /* free private structure allocated for serial port */
1567 for (i = 0; i < serial->num_ports; ++i)
1568 kfree(usb_get_serial_port_data(serial->port[i]));
1570 /* free private structure allocated for serial device */
1571 kfree(usb_get_serial_data(serial));
1574 static struct usb_driver usb_driver = {
1575 .name = "moschip7720",
1576 .probe = usb_serial_probe,
1577 .disconnect = usb_serial_disconnect,
1578 .id_table = moschip_port_id_table,
1582 static struct usb_serial_driver moschip7720_2port_driver = {
1584 .owner = THIS_MODULE,
1585 .name = "moschip7720",
1587 .description = "Moschip 2 port adapter",
1588 .usb_driver = &usb_driver,
1589 .id_table = moschip_port_id_table,
1591 .open = mos7720_open,
1592 .close = mos7720_close,
1593 .throttle = mos7720_throttle,
1594 .unthrottle = mos7720_unthrottle,
1595 .attach = mos7720_startup,
1596 .release = mos7720_release,
1597 .ioctl = mos7720_ioctl,
1598 .tiocmget = mos7720_tiocmget,
1599 .tiocmset = mos7720_tiocmset,
1600 .set_termios = mos7720_set_termios,
1601 .write = mos7720_write,
1602 .write_room = mos7720_write_room,
1603 .chars_in_buffer = mos7720_chars_in_buffer,
1604 .break_ctl = mos7720_break,
1605 .read_bulk_callback = mos7720_bulk_in_callback,
1606 .read_int_callback = mos7720_interrupt_callback,
1609 static int __init moschip7720_init(void)
1613 dbg("%s: Entering ..........", __func__);
1615 /* Register with the usb serial */
1616 retval = usb_serial_register(&moschip7720_2port_driver);
1618 goto failed_port_device_register;
1620 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1623 /* Register with the usb */
1624 retval = usb_register(&usb_driver);
1626 goto failed_usb_register;
1630 failed_usb_register:
1631 usb_serial_deregister(&moschip7720_2port_driver);
1633 failed_port_device_register:
1637 static void __exit moschip7720_exit(void)
1639 usb_deregister(&usb_driver);
1640 usb_serial_deregister(&moschip7720_2port_driver);
1643 module_init(moschip7720_init);
1644 module_exit(moschip7720_exit);
1646 /* Module information */
1647 MODULE_AUTHOR(DRIVER_AUTHOR);
1648 MODULE_DESCRIPTION(DRIVER_DESC);
1649 MODULE_LICENSE("GPL");
1651 module_param(debug, bool, S_IRUGO | S_IWUSR);
1652 MODULE_PARM_DESC(debug, "Debug enabled or not");