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 * VijayaKumar.G.N. <vijaykumar@aspirecom.net>
13 * AjayKumar <ajay@aspirecom.net>
14 * Gurudeva.N. <gurudev@aspirecom.net>
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)
112 dbg("%s"," : Entering\n");
115 dbg("%s","Invalid Pointer !!!!:\n");
119 switch (urb->status) {
126 /* this urb is terminated, clean up */
127 dbg("%s - urb shutting down with status: %d", __FUNCTION__,
131 dbg("%s - nonzero urb status received: %d", __FUNCTION__,
136 length = urb->actual_length;
137 data = urb->transfer_buffer;
139 /* Moschip get 4 bytes
140 * Byte 1 IIR Port 1 (port.number is 0)
141 * Byte 2 IIR Port 2 (port.number is 1)
142 * Byte 3 --------------
143 * Byte 4 FIFO status for both */
144 if (length && length > 4) {
145 dbg("Wrong data !!!");
151 sp1 = (status & 0xff000000)>>24;
152 sp2 = (status & 0x00ff0000)>>16;
153 st = status & 0x000000ff;
155 if ((sp1 & 0x01) || (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;
209 dbg("nonzero read bulk status received: %d",urb->status);
213 mos7720_port = urb->context;
215 dbg("%s","NULL mos7720_port pointer \n");
219 port = mos7720_port->port;
221 dbg("Entering...%s", __FUNCTION__);
223 data = urb->transfer_buffer;
226 if (tty && urb->actual_length) {
227 tty_buffer_request_room(tty, urb->actual_length);
228 tty_insert_flip_string(tty, data, urb->actual_length);
229 tty_flip_buffer_push(tty);
232 if (!port->read_urb) {
233 dbg("URB KILLED !!!");
237 if (port->read_urb->status != -EINPROGRESS) {
238 port->read_urb->dev = port->serial->dev;
240 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
242 dbg("usb_submit_urb(read bulk) failed, status = %d",
248 * mos7720_bulk_out_data_callback
249 * this is the callback function for when we have finished sending serial
250 * data on the bulk out endpoint.
252 static void mos7720_bulk_out_data_callback(struct urb *urb)
254 struct moschip_port *mos7720_port;
255 struct tty_struct *tty;
258 dbg("nonzero write bulk status received:%d", urb->status);
262 mos7720_port = urb->context;
264 dbg("NULL mos7720_port pointer");
268 dbg("Entering .........");
270 tty = mos7720_port->port->tty;
272 if (tty && mos7720_port->open) {
273 /* let the tty driver wakeup if it has a special *
274 * write_wakeup function */
275 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
276 tty->ldisc.write_wakeup)
277 (tty->ldisc.write_wakeup)(tty);
279 /* tell the tty driver that something has changed */
280 wake_up_interruptible(&tty->write_wait);
283 /* schedule_work(&mos7720_port->port->work); */
288 * this function will be used for sending command to device
290 static int send_mos_cmd(struct usb_serial *serial, __u8 request, __u16 value,
291 __u16 index, void *data)
295 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
299 if (value < MOS_MAX_PORT) {
300 if (product == MOSCHIP_DEVICE_ID_7715) {
301 value = value*0x100+0x100;
303 value = value*0x100+0x200;
307 if ((product == MOSCHIP_DEVICE_ID_7715) &&
309 dbg("serial->product== MOSCHIP_DEVICE_ID_7715");
314 if (request == MOS_WRITE) {
315 request = (__u8)MOS_WRITE;
316 requesttype = (__u8)0x40;
317 value = value + (__u16)*((unsigned char *)data);
319 pipe = usb_sndctrlpipe(serial->dev, 0);
321 request = (__u8)MOS_READ;
322 requesttype = (__u8)0xC0;
324 pipe = usb_rcvctrlpipe(serial->dev,0);
327 status = usb_control_msg(serial->dev, pipe, request, requesttype,
328 value, index, data, size, MOS_WDR_TIMEOUT);
331 dbg("Command Write failed Value %x index %x\n",value,index);
336 static int mos7720_open(struct usb_serial_port *port, struct file * filp)
338 struct usb_serial *serial;
339 struct usb_serial_port *port0;
341 struct moschip_serial *mos7720_serial;
342 struct moschip_port *mos7720_port;
348 serial = port->serial;
350 mos7720_port = usb_get_serial_port_data(port);
351 if (mos7720_port == NULL)
354 port0 = serial->port[0];
356 mos7720_serial = usb_get_serial_data(serial);
358 if (mos7720_serial == NULL || port0 == NULL)
361 usb_clear_halt(serial->dev, port->write_urb->pipe);
362 usb_clear_halt(serial->dev, port->read_urb->pipe);
364 /* Initialising the write urb pool */
365 for (j = 0; j < NUM_URBS; ++j) {
366 urb = usb_alloc_urb(0,GFP_ATOMIC);
367 mos7720_port->write_urb_pool[j] = urb;
370 err("No more urbs???");
374 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
376 if (!urb->transfer_buffer) {
377 err("%s-out of memory for urb buffers.", __FUNCTION__);
382 /* Initialize MCS7720 -- Write Init values to corresponding Registers
390 * 0x08 : SP1/2 Control Reg
392 port_number = port->number - port->serial->minor;
393 send_mos_cmd(port->serial, MOS_READ, port_number, UART_LSR, &data);
394 dbg("SS::%p LSR:%x\n",mos7720_port, data);
396 dbg("Check:Sending Command ..........");
399 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x01, &data);
401 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x02, &data);
404 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
406 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
409 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
411 mos7720_port->shadowLCR = data;
412 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
414 mos7720_port->shadowMCR = data;
415 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
417 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
420 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
422 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
425 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, port_number + 1, &data);
427 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
429 send_mos_cmd(port->serial, MOS_WRITE, MOS_MAX_PORT, 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);
454 /* force low_latency on so that our tty_push actually forces *
455 * the data through,otherwise it is scheduled, and with *
456 * high data rates (like with OHCI) data can get lost. */
459 port->tty->low_latency = 1;
461 /* see if we've set up our endpoint info yet *
462 * (can't set it up in mos7720_startup as the *
463 * structures were not set up at that time.) */
464 if (!mos7720_serial->interrupt_started) {
465 dbg("Interrupt buffer NULL !!!");
467 /* not set up yet, so do it now */
468 mos7720_serial->interrupt_started = 1;
470 dbg("To Submit URB !!!");
472 /* set up our interrupt urb */
473 usb_fill_int_urb(port0->interrupt_in_urb, serial->dev,
474 usb_rcvintpipe(serial->dev,
475 port->interrupt_in_endpointAddress),
476 port0->interrupt_in_buffer,
477 port0->interrupt_in_urb->transfer_buffer_length,
478 mos7720_interrupt_callback, mos7720_port,
479 port0->interrupt_in_urb->interval);
481 /* start interrupt read for this mos7720 this interrupt *
482 * will continue as long as the mos7720 is connected */
483 dbg("Submit URB over !!!");
484 response = usb_submit_urb(port0->interrupt_in_urb, GFP_KERNEL);
487 "%s - Error %d submitting control urb",
488 __FUNCTION__, response);
491 /* set up our bulk in urb */
492 usb_fill_bulk_urb(port->read_urb, serial->dev,
493 usb_rcvbulkpipe(serial->dev,
494 port->bulk_in_endpointAddress),
495 port->bulk_in_buffer,
496 port->read_urb->transfer_buffer_length,
497 mos7720_bulk_in_callback, mos7720_port);
498 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
501 "%s - Error %d submitting read urb", __FUNCTION__, response);
503 /* initialize our icount structure */
504 memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
506 /* initialize our port settings */
507 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
509 /* send a open port command */
510 mos7720_port->open = 1;
516 * mos7720_chars_in_buffer
517 * this function is called by the tty driver when it wants to know how many
518 * bytes of data we currently have outstanding in the port (data that has
519 * been written, but hasn't made it out the port yet)
520 * If successful, we return the number of bytes left to be written in the
522 * Otherwise we return a negative error number.
524 static int mos7720_chars_in_buffer(struct usb_serial_port *port)
528 struct moschip_port *mos7720_port;
530 dbg("%s:entering ...........", __FUNCTION__);
532 mos7720_port = usb_get_serial_port_data(port);
533 if (mos7720_port == NULL) {
534 dbg("%s:leaving ...........", __FUNCTION__);
538 for (i = 0; i < NUM_URBS; ++i) {
539 if (mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
540 chars += URB_TRANSFER_BUFFER_SIZE;
542 dbg("%s - returns %d", __FUNCTION__, chars);
546 static void mos7720_close(struct usb_serial_port *port, struct file *filp)
548 struct usb_serial *serial;
549 struct moschip_port *mos7720_port;
553 dbg("mos7720_close:entering...");
555 serial = port->serial;
557 mos7720_port = usb_get_serial_port_data(port);
558 if (mos7720_port == NULL)
561 for (j = 0; j < NUM_URBS; ++j)
562 usb_kill_urb(mos7720_port->write_urb_pool[j]);
564 /* Freeing Write URBs */
565 for (j = 0; j < NUM_URBS; ++j) {
566 if (mos7720_port->write_urb_pool[j]) {
567 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
568 usb_free_urb(mos7720_port->write_urb_pool[j]);
572 /* While closing port, shutdown all bulk read, write *
573 * and interrupt read if they exists */
575 dbg("Shutdown bulk write");
576 usb_kill_urb(port->write_urb);
577 dbg("Shutdown bulk read");
578 usb_kill_urb(port->read_urb);
582 send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
586 send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
589 mos7720_port->open = 0;
591 dbg("Leaving %s", __FUNCTION__);
594 static void mos7720_break(struct usb_serial_port *port, int break_state)
597 struct usb_serial *serial;
598 struct moschip_port *mos7720_port;
600 dbg("Entering %s", __FUNCTION__);
602 serial = port->serial;
604 mos7720_port = usb_get_serial_port_data(port);
605 if (mos7720_port == NULL)
608 if (break_state == -1)
609 data = mos7720_port->shadowLCR | UART_LCR_SBC;
611 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
613 mos7720_port->shadowLCR = data;
614 send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
622 * this function is called by the tty driver when it wants to know how many
623 * bytes of data we can accept for a specific port.
624 * If successful, we return the amount of room that we have for this port
625 * Otherwise we return a negative error number.
627 static int mos7720_write_room(struct usb_serial_port *port)
629 struct moschip_port *mos7720_port;
633 dbg("%s:entering ...........", __FUNCTION__);
635 mos7720_port = usb_get_serial_port_data(port);
636 if (mos7720_port == NULL) {
637 dbg("%s:leaving ...........", __FUNCTION__);
641 for (i = 0; i < NUM_URBS; ++i) {
642 if (mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
643 room += URB_TRANSFER_BUFFER_SIZE;
646 dbg("%s - returns %d", __FUNCTION__, room);
650 static int mos7720_write(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 ...........", __FUNCTION__);
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]->status != -EINPROGRESS) {
678 urb = mos7720_port->write_urb_pool[i];
685 dbg("%s - no more free urbs", __FUNCTION__);
689 if (urb->transfer_buffer == NULL) {
690 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
692 if (urb->transfer_buffer == NULL) {
693 err("%s no more kernel memory...", __FUNCTION__);
697 transfer_size = min (count, URB_TRANSFER_BUFFER_SIZE);
699 memcpy(urb->transfer_buffer, current_position, transfer_size);
700 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, transfer_size,
701 urb->transfer_buffer);
703 /* fill urb with data and submit */
704 usb_fill_bulk_urb(urb, serial->dev,
705 usb_sndbulkpipe(serial->dev,
706 port->bulk_out_endpointAddress),
707 urb->transfer_buffer, transfer_size,
708 mos7720_bulk_out_data_callback, mos7720_port);
710 /* send it down the pipe */
711 status = usb_submit_urb(urb,GFP_ATOMIC);
713 err("%s - usb_submit_urb(write bulk) failed with status = %d",
714 __FUNCTION__, status);
718 bytes_sent = transfer_size;
724 static void mos7720_throttle(struct usb_serial_port *port)
726 struct moschip_port *mos7720_port;
727 struct tty_struct *tty;
730 dbg("%s- port %d\n", __FUNCTION__, port->number);
732 mos7720_port = usb_get_serial_port_data(port);
734 if (mos7720_port == NULL)
737 if (!mos7720_port->open) {
738 dbg("port not opened");
742 dbg("%s: Entering ..........", __FUNCTION__);
746 dbg("%s - no tty available", __FUNCTION__);
750 /* if we are implementing XON/XOFF, send the stop character */
752 unsigned char stop_char = STOP_CHAR(tty);
753 status = mos7720_write(port, &stop_char, 1);
758 /* if we are implementing RTS/CTS, toggle that line */
759 if (tty->termios->c_cflag & CRTSCTS) {
760 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
761 status = send_mos_cmd(port->serial, MOS_WRITE,
762 port->number - port->serial->minor,
763 UART_MCR, &mos7720_port->shadowMCR);
769 static void mos7720_unthrottle(struct usb_serial_port *port)
771 struct tty_struct *tty;
773 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
775 if (mos7720_port == NULL)
778 if (!mos7720_port->open) {
779 dbg("%s - port not opened", __FUNCTION__);
783 dbg("%s: Entering ..........", __FUNCTION__);
787 dbg("%s - no tty available", __FUNCTION__);
791 /* if we are implementing XON/XOFF, send the start character */
793 unsigned char start_char = START_CHAR(tty);
794 status = mos7720_write(port, &start_char, 1);
799 /* if we are implementing RTS/CTS, toggle that line */
800 if (tty->termios->c_cflag & CRTSCTS) {
801 mos7720_port->shadowMCR |= UART_MCR_RTS;
802 status = send_mos_cmd(port->serial, MOS_WRITE,
803 port->number - port->serial->minor,
804 UART_MCR, &mos7720_port->shadowMCR);
810 static int set_higher_rates(struct moschip_port *mos7720_port,
814 struct usb_serial_port *port;
815 struct usb_serial *serial;
818 if (mos7720_port == NULL)
821 port = mos7720_port->port;
822 serial = port->serial;
824 /***********************************************
825 * Init Sequence for higher rates
826 ***********************************************/
827 dbg("Sending Setting Commands ..........");
828 port_number = port->number - port->serial->minor;
831 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
833 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
835 send_mos_cmd(serial, MOS_WRITE, port->number, 0x02, &data);
837 mos7720_port->shadowMCR = data;
838 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
840 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
843 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
845 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
848 /***********************************************
849 * Set for higher rates *
850 ***********************************************/
853 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1,&data);
856 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
858 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
861 mos7720_port->shadowMCR = data;
862 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
864 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
866 /***********************************************
868 ***********************************************/
870 data = mos7720_port->shadowLCR | UART_LCR_DLAB;
871 mos7720_port->shadowLCR = data;
872 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
874 data = 0x001; /* DLL */
875 send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
876 data = 0x000; /* DLM */
877 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
879 data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
880 mos7720_port->shadowLCR = data;
881 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
886 /* baud rate information */
887 struct divisor_table_entry
893 /* Define table of divisors for moschip 7720 hardware *
894 * These assume a 3.6864MHz crystal, the standard /16, and *
896 static struct divisor_table_entry divisor_table[] = {
898 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
899 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
915 /*****************************************************************************
916 * calc_baud_rate_divisor
917 * this function calculates the proper baud rate divisor for the specified
919 *****************************************************************************/
920 static int calc_baud_rate_divisor(int baudrate, int *divisor)
928 dbg("%s - %d", __FUNCTION__, baudrate);
930 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
931 if (divisor_table[i].baudrate == baudrate) {
932 *divisor = divisor_table[i].divisor;
937 /* After trying for all the standard baud rates *
938 * Try calculating the divisor for this baud rate */
939 if (baudrate > 75 && baudrate < 230400) {
940 /* get the divisor */
941 custom = (__u16)(230400L / baudrate);
943 /* Check for round off */
944 round1 = (__u16)(2304000L / baudrate);
945 round = (__u16)(round1 - (custom * 10));
950 dbg("Baud %d = %d",baudrate, custom);
954 dbg("Baud calculation Failed...");
959 * send_cmd_write_baud_rate
960 * this function sends the proper command to change the baud rate of the
963 static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
966 struct usb_serial_port *port;
967 struct usb_serial *serial;
971 unsigned char number;
973 if (mos7720_port == NULL)
976 port = mos7720_port->port;
977 serial = port->serial;
979 dbg("%s: Entering ..........", __FUNCTION__);
981 number = port->number - port->serial->minor;
982 dbg("%s - port = %d, baud = %d", __FUNCTION__, port->number, baudrate);
984 /* Calculate the Divisor */
985 status = calc_baud_rate_divisor(baudrate, &divisor);
987 err("%s - bad baud rate", __FUNCTION__);
991 /* Enable access to divisor latch */
992 data = mos7720_port->shadowLCR | UART_LCR_DLAB;
993 mos7720_port->shadowLCR = data;
994 send_mos_cmd(serial, MOS_WRITE, number, UART_LCR, &data);
996 /* Write the divisor */
997 data = ((unsigned char)(divisor & 0xff));
998 send_mos_cmd(serial, MOS_WRITE, number, 0x00, &data);
1000 data = ((unsigned char)((divisor & 0xff00) >> 8));
1001 send_mos_cmd(serial, MOS_WRITE, number, 0x01, &data);
1003 /* Disable access to divisor latch */
1004 data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1005 mos7720_port->shadowLCR = data;
1006 send_mos_cmd(serial, MOS_WRITE, number, 0x03, &data);
1012 * change_port_settings
1013 * This routine is called to set the UART on the device to match
1014 * the specified new settings.
1016 static void change_port_settings(struct moschip_port *mos7720_port,
1017 struct ktermios *old_termios)
1019 struct usb_serial_port *port;
1020 struct usb_serial *serial;
1021 struct tty_struct *tty;
1033 if (mos7720_port == NULL)
1036 port = mos7720_port->port;
1037 serial = port->serial;
1038 port_number = port->number - port->serial->minor;
1040 dbg("%s - port %d", __FUNCTION__, port->number);
1042 if (!mos7720_port->open) {
1043 dbg("%s - port not opened", __FUNCTION__);
1047 tty = mos7720_port->port->tty;
1049 if ((!tty) || (!tty->termios)) {
1050 dbg("%s - no tty structures", __FUNCTION__);
1054 dbg("%s: Entering ..........", __FUNCTION__);
1056 lData = UART_LCR_WLEN8;
1057 lStop = 0x00; /* 1 stop bit */
1058 lParity = 0x00; /* No parity */
1060 cflag = tty->termios->c_cflag;
1061 iflag = tty->termios->c_iflag;
1063 /* Change the number of bits */
1064 switch (cflag & CSIZE) {
1066 lData = UART_LCR_WLEN5;
1071 lData = UART_LCR_WLEN6;
1076 lData = UART_LCR_WLEN7;
1081 lData = UART_LCR_WLEN8;
1085 /* Change the Parity bit */
1086 if (cflag & PARENB) {
1087 if (cflag & PARODD) {
1088 lParity = UART_LCR_PARITY;
1089 dbg("%s - parity = odd", __FUNCTION__);
1091 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
1092 dbg("%s - parity = even", __FUNCTION__);
1096 dbg("%s - parity = none", __FUNCTION__);
1100 lParity = lParity | 0x20;
1102 /* Change the Stop bit */
1103 if (cflag & CSTOPB) {
1104 lStop = UART_LCR_STOP;
1105 dbg("%s - stop bits = 2", __FUNCTION__);
1108 dbg("%s - stop bits = 1", __FUNCTION__);
1111 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1112 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1113 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
1115 /* Update the LCR with the correct value */
1116 mos7720_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1117 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1120 /* Disable Interrupts */
1122 send_mos_cmd(serial,MOS_WRITE,port->number - port->serial->minor, UART_IER, &data);
1125 send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
1128 send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
1130 /* Send the updated LCR value to the mos7720 */
1131 data = mos7720_port->shadowLCR;
1132 send_mos_cmd(serial, MOS_WRITE, port_number, UART_LCR, &data);
1135 mos7720_port->shadowMCR = data;
1136 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
1138 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
1140 /* set up the MCR register and send it to the mos7720 */
1141 mos7720_port->shadowMCR = UART_MCR_OUT2;
1143 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1145 if (cflag & CRTSCTS) {
1146 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
1148 /* To set hardware flow control to the specified *
1149 * serial port, in SP1/2_CONTROL_REG */
1152 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1156 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1160 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1163 data = mos7720_port->shadowMCR;
1164 send_mos_cmd(serial, MOS_WRITE, port_number, UART_MCR, &data);
1166 /* Determine divisor based on baud rate */
1167 baud = tty_get_baud_rate(tty);
1169 /* pick a default, any default... */
1170 dbg("Picked default baud...");
1174 if (baud >= 230400) {
1175 set_higher_rates(mos7720_port, baud);
1176 /* Enable Interrupts */
1178 send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1182 dbg("%s - baud rate = %d", __FUNCTION__, baud);
1183 status = send_cmd_write_baud_rate(mos7720_port, baud);
1185 /* Enable Interrupts */
1187 send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1189 if (port->read_urb->status != -EINPROGRESS) {
1190 port->read_urb->dev = serial->dev;
1192 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1194 dbg("usb_submit_urb(read bulk) failed, status = %d",
1201 * mos7720_set_termios
1202 * this function is called by the tty driver when it wants to change the
1203 * termios structure.
1205 static void mos7720_set_termios(struct usb_serial_port *port,
1206 struct ktermios *old_termios)
1210 struct usb_serial *serial;
1211 struct moschip_port *mos7720_port;
1212 struct tty_struct *tty;
1214 serial = port->serial;
1216 mos7720_port = usb_get_serial_port_data(port);
1218 if (mos7720_port == NULL)
1223 if (!port->tty || !port->tty->termios) {
1224 dbg("%s - no tty or termios", __FUNCTION__);
1228 if (!mos7720_port->open) {
1229 dbg("%s - port not opened", __FUNCTION__);
1233 dbg("%s\n","setting termios - ASPIRE");
1235 cflag = tty->termios->c_cflag;
1238 printk("%s %s\n",__FUNCTION__,"cflag is NULL");
1242 /* check that they really want us to change something */
1244 if ((cflag == old_termios->c_cflag) &&
1245 (RELEVANT_IFLAG(tty->termios->c_iflag) ==
1246 RELEVANT_IFLAG(old_termios->c_iflag))) {
1247 dbg("Nothing to change");
1252 dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
1253 tty->termios->c_cflag,
1254 RELEVANT_IFLAG(tty->termios->c_iflag));
1257 dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
1258 old_termios->c_cflag,
1259 RELEVANT_IFLAG(old_termios->c_iflag));
1261 dbg("%s - port %d", __FUNCTION__, port->number);
1263 /* change the port settings to the new ones specified */
1264 change_port_settings(mos7720_port, old_termios);
1266 if(!port->read_urb) {
1267 dbg("%s","URB KILLED !!!!!\n");
1271 if(port->read_urb->status != -EINPROGRESS) {
1272 port->read_urb->dev = serial->dev;
1273 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1275 dbg("usb_submit_urb(read bulk) failed, status = %d",
1282 * get_lsr_info - get line status register info
1284 * Purpose: Let user call ioctl() to get info when the UART physically
1285 * is emptied. On bus types like RS485, the transmitter must
1286 * release the bus after transmitting. This must be done when
1287 * the transmit shift register is empty, not be done when the
1288 * transmit holding register is empty. This functionality
1289 * allows an RS485 driver to be written in user space.
1291 static int get_lsr_info(struct moschip_port *mos7720_port,
1292 unsigned int __user *value)
1295 unsigned int result = 0;
1297 count = mos7720_chars_in_buffer(mos7720_port->port);
1299 dbg("%s -- Empty", __FUNCTION__);
1300 result = TIOCSER_TEMT;
1303 if (copy_to_user(value, &result, sizeof(int)))
1309 * get_number_bytes_avail - get number of bytes available
1311 * Purpose: Let user call ioctl to get the count of number of bytes available.
1313 static int get_number_bytes_avail(struct moschip_port *mos7720_port,
1314 unsigned int __user *value)
1316 unsigned int result = 0;
1317 struct tty_struct *tty = mos7720_port->port->tty;
1320 return -ENOIOCTLCMD;
1322 result = tty->read_cnt;
1324 dbg("%s(%d) = %d", __FUNCTION__, mos7720_port->port->number, result);
1325 if (copy_to_user(value, &result, sizeof(int)))
1328 return -ENOIOCTLCMD;
1331 static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1332 unsigned int __user *value)
1338 struct usb_serial_port *port;
1340 if (mos7720_port == NULL)
1343 port = (struct usb_serial_port*)mos7720_port->port;
1344 mcr = mos7720_port->shadowMCR;
1346 if (copy_from_user(&arg, value, sizeof(int)))
1351 if (arg & TIOCM_RTS)
1352 mcr |= UART_MCR_RTS;
1353 if (arg & TIOCM_DTR)
1354 mcr |= UART_MCR_RTS;
1355 if (arg & TIOCM_LOOP)
1356 mcr |= UART_MCR_LOOP;
1360 if (arg & TIOCM_RTS)
1361 mcr &= ~UART_MCR_RTS;
1362 if (arg & TIOCM_DTR)
1363 mcr &= ~UART_MCR_RTS;
1364 if (arg & TIOCM_LOOP)
1365 mcr &= ~UART_MCR_LOOP;
1369 /* turn off the RTS and DTR and LOOPBACK
1370 * and then only turn on what was asked to */
1371 mcr &= ~(UART_MCR_RTS | UART_MCR_DTR | UART_MCR_LOOP);
1372 mcr |= ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0);
1373 mcr |= ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0);
1374 mcr |= ((arg & TIOCM_LOOP) ? UART_MCR_LOOP : 0);
1378 mos7720_port->shadowMCR = mcr;
1380 data = mos7720_port->shadowMCR;
1381 send_mos_cmd(port->serial, MOS_WRITE,
1382 port->number - port->serial->minor, UART_MCR, &data);
1387 static int get_modem_info(struct moschip_port *mos7720_port,
1388 unsigned int __user *value)
1390 unsigned int result = 0;
1391 unsigned int msr = mos7720_port->shadowMSR;
1392 unsigned int mcr = mos7720_port->shadowMCR;
1394 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1395 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1396 | ((msr & UART_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1397 | ((msr & UART_MSR_DCD) ? TIOCM_CAR: 0) /* 0x040 */
1398 | ((msr & UART_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1399 | ((msr & UART_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1402 dbg("%s -- %x", __FUNCTION__, result);
1404 if (copy_to_user(value, &result, sizeof(int)))
1409 static int get_serial_info(struct moschip_port *mos7720_port,
1410 struct serial_struct __user *retinfo)
1412 struct serial_struct tmp;
1417 memset(&tmp, 0, sizeof(tmp));
1419 tmp.type = PORT_16550A;
1420 tmp.line = mos7720_port->port->serial->minor;
1421 tmp.port = mos7720_port->port->number;
1423 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1424 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1425 tmp.baud_base = 9600;
1426 tmp.close_delay = 5*HZ;
1427 tmp.closing_wait = 30*HZ;
1429 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1434 static int mos7720_ioctl(struct usb_serial_port *port, struct file *file,
1435 unsigned int cmd, unsigned long arg)
1437 struct moschip_port *mos7720_port;
1438 struct async_icount cnow;
1439 struct async_icount cprev;
1440 struct serial_icounter_struct icount;
1442 mos7720_port = usb_get_serial_port_data(port);
1443 if (mos7720_port == NULL)
1446 dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
1450 /* return number of bytes available */
1451 dbg("%s (%d) TIOCINQ", __FUNCTION__, port->number);
1452 return get_number_bytes_avail(mos7720_port,
1453 (unsigned int __user *)arg);
1457 dbg("%s (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
1458 return get_lsr_info(mos7720_port, (unsigned int __user *)arg);
1464 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __FUNCTION__,
1466 return set_modem_info(mos7720_port, cmd,
1467 (unsigned int __user *)arg);
1470 dbg("%s (%d) TIOCMGET", __FUNCTION__, port->number);
1471 return get_modem_info(mos7720_port,
1472 (unsigned int __user *)arg);
1475 dbg("%s (%d) TIOCGSERIAL", __FUNCTION__, port->number);
1476 return get_serial_info(mos7720_port,
1477 (struct serial_struct __user *)arg);
1480 dbg("%s (%d) TIOCSSERIAL", __FUNCTION__, port->number);
1484 dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
1485 cprev = mos7720_port->icount;
1487 if (signal_pending(current))
1488 return -ERESTARTSYS;
1489 cnow = mos7720_port->icount;
1490 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1491 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1492 return -EIO; /* no change => error */
1493 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1494 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1495 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1496 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1505 cnow = mos7720_port->icount;
1506 icount.cts = cnow.cts;
1507 icount.dsr = cnow.dsr;
1508 icount.rng = cnow.rng;
1509 icount.dcd = cnow.dcd;
1510 icount.rx = cnow.rx;
1511 icount.tx = cnow.tx;
1512 icount.frame = cnow.frame;
1513 icount.overrun = cnow.overrun;
1514 icount.parity = cnow.parity;
1515 icount.brk = cnow.brk;
1516 icount.buf_overrun = cnow.buf_overrun;
1518 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__,
1519 port->number, icount.rx, icount.tx );
1520 if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
1525 return -ENOIOCTLCMD;
1528 static int mos7720_startup(struct usb_serial *serial)
1530 struct moschip_serial *mos7720_serial;
1531 struct moschip_port *mos7720_port;
1532 struct usb_device *dev;
1536 dbg("%s: Entering ..........", __FUNCTION__);
1539 dbg("Invalid Handler");
1545 /* create our private serial structure */
1546 mos7720_serial = kzalloc(sizeof(struct moschip_serial), GFP_KERNEL);
1547 if (mos7720_serial == NULL) {
1548 err("%s - Out of memory", __FUNCTION__);
1552 usb_set_serial_data(serial, mos7720_serial);
1554 /* we set up the pointers to the endpoints in the mos7720_open *
1555 * function, as the structures aren't created yet. */
1557 /* set up port private structures */
1558 for (i = 0; i < serial->num_ports; ++i) {
1559 mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
1560 if (mos7720_port == NULL) {
1561 err("%s - Out of memory", __FUNCTION__);
1562 usb_set_serial_data(serial, NULL);
1563 kfree(mos7720_serial);
1567 /* Initialize all port interrupt end point to port 0 int
1568 * endpoint. Our device has only one interrupt endpoint
1569 * comman to all ports */
1570 serial->port[i]->interrupt_in_endpointAddress = serial->port[0]->interrupt_in_endpointAddress;
1572 mos7720_port->port = serial->port[i];
1573 usb_set_serial_port_data(serial->port[i], mos7720_port);
1575 dbg("port number is %d", serial->port[i]->number);
1576 dbg("serial number is %d", serial->minor);
1580 /* setting configuration feature to one */
1581 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1582 (__u8)0x03, 0x00,0x01,0x00, NULL, 0x00, 5*HZ);
1584 send_mos_cmd(serial,MOS_READ,0x00, UART_LSR, &data); // LSR For Port 1
1587 send_mos_cmd(serial,MOS_READ,0x01, UART_LSR, &data); // LSR For Port 2
1593 static void mos7720_shutdown(struct usb_serial *serial)
1597 /* free private structure allocated for serial port */
1598 for (i=0; i < serial->num_ports; ++i) {
1599 kfree(usb_get_serial_port_data(serial->port[i]));
1600 usb_set_serial_port_data(serial->port[i], NULL);
1603 /* free private structure allocated for serial device */
1604 kfree(usb_get_serial_data(serial));
1605 usb_set_serial_data(serial, NULL);
1608 static struct usb_serial_driver moschip7720_2port_driver = {
1610 .owner = THIS_MODULE,
1611 .name = "moschip7720",
1613 .description = "Moschip 2 port adapter",
1614 .id_table = moschip_port_id_table,
1615 .num_interrupt_in = 1,
1619 .open = mos7720_open,
1620 .close = mos7720_close,
1621 .throttle = mos7720_throttle,
1622 .unthrottle = mos7720_unthrottle,
1623 .attach = mos7720_startup,
1624 .shutdown = mos7720_shutdown,
1625 .ioctl = mos7720_ioctl,
1626 .set_termios = mos7720_set_termios,
1627 .write = mos7720_write,
1628 .write_room = mos7720_write_room,
1629 .chars_in_buffer = mos7720_chars_in_buffer,
1630 .break_ctl = mos7720_break,
1631 .read_bulk_callback = mos7720_bulk_in_callback,
1634 static struct usb_driver usb_driver = {
1635 .name = "moschip7720",
1636 .probe = usb_serial_probe,
1637 .disconnect = usb_serial_disconnect,
1638 .id_table = moschip_port_id_table,
1641 static int __init moschip7720_init(void)
1645 dbg("%s: Entering ..........", __FUNCTION__);
1647 /* Register with the usb serial */
1648 retval = usb_serial_register(&moschip7720_2port_driver);
1650 goto failed_port_device_register;
1652 info(DRIVER_DESC " " DRIVER_VERSION);
1654 /* Register with the usb */
1655 retval = usb_register(&usb_driver);
1657 goto failed_usb_register;
1661 failed_usb_register:
1662 usb_serial_deregister(&moschip7720_2port_driver);
1664 failed_port_device_register:
1668 static void __exit moschip7720_exit(void)
1670 usb_deregister(&usb_driver);
1671 usb_serial_deregister(&moschip7720_2port_driver);
1674 module_init(moschip7720_init);
1675 module_exit(moschip7720_exit);
1677 /* Module information */
1678 MODULE_AUTHOR( DRIVER_AUTHOR );
1679 MODULE_DESCRIPTION( DRIVER_DESC );
1680 MODULE_LICENSE("GPL");
1682 module_param(debug, bool, S_IRUGO | S_IWUSR);
1683 MODULE_PARM_DESC(debug, "Debug enabled or not");