hpt366: fix section mismatch warnings
[linux-2.6/openmoko-kernel/knife-kernel.git] / drivers / usb / serial / mos7720.c
blob40f3a0188807d95c7e69a7a23001133678dbd9d1
1 /*
2 * mos7720.c
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.
11 * Developed by:
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>
40 * Version Information
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
54 #define MOS_READ 0x0D
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 */
67 struct moschip_port
69 __u8 shadowLCR; /* last LCR value received */
70 __u8 shadowMCR; /* last MCR value received */
71 __u8 shadowMSR; /* last MSR value received */
72 char open;
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 */
79 struct moschip_serial
81 int interrupt_started;
84 static int debug;
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)
104 int result;
105 int length;
106 int status = urb->status;
107 __u8 *data;
108 __u8 sp1;
109 __u8 sp2;
111 dbg("%s"," : Entering\n");
113 switch (status) {
114 case 0:
115 /* success */
116 break;
117 case -ECONNRESET:
118 case -ENOENT:
119 case -ESHUTDOWN:
120 /* this urb is terminated, clean up */
121 dbg("%s - urb shutting down with status: %d", __FUNCTION__,
122 status);
123 return;
124 default:
125 dbg("%s - nonzero urb status received: %d", __FUNCTION__,
126 status);
127 goto exit;
130 length = urb->actual_length;
131 data = urb->transfer_buffer;
133 /* Moschip get 4 bytes
134 * Byte 1 IIR Port 1 (port.number is 0)
135 * Byte 2 IIR Port 2 (port.number is 1)
136 * Byte 3 --------------
137 * Byte 4 FIFO status for both */
139 /* the above description is inverted
140 * oneukum 2007-03-14 */
142 if (unlikely(length != 4)) {
143 dbg("Wrong data !!!");
144 return;
147 sp1 = data[3];
148 sp2 = data[2];
150 if ((sp1 | sp2) & 0x01) {
151 /* No Interrupt Pending in both the ports */
152 dbg("No Interrupt !!!");
153 } else {
154 switch (sp1 & 0x0f) {
155 case SERIAL_IIR_RLS:
156 dbg("Serial Port 1: Receiver status error or address "
157 "bit detected in 9-bit mode\n");
158 break;
159 case SERIAL_IIR_CTI:
160 dbg("Serial Port 1: Receiver time out");
161 break;
162 case SERIAL_IIR_MS:
163 dbg("Serial Port 1: Modem status change");
164 break;
167 switch (sp2 & 0x0f) {
168 case SERIAL_IIR_RLS:
169 dbg("Serial Port 2: Receiver status error or address "
170 "bit detected in 9-bit mode");
171 break;
172 case SERIAL_IIR_CTI:
173 dbg("Serial Port 2: Receiver time out");
174 break;
175 case SERIAL_IIR_MS:
176 dbg("Serial Port 2: Modem status change");
177 break;
181 exit:
182 result = usb_submit_urb(urb, GFP_ATOMIC);
183 if (result)
184 dev_err(&urb->dev->dev,
185 "%s - Error %d submitting control urb\n",
186 __FUNCTION__, result);
187 return;
191 * mos7720_bulk_in_callback
192 * this is the callback function for when we have received data on the
193 * bulk in endpoint.
195 static void mos7720_bulk_in_callback(struct urb *urb)
197 int retval;
198 unsigned char *data ;
199 struct usb_serial_port *port;
200 struct moschip_port *mos7720_port;
201 struct tty_struct *tty;
202 int status = urb->status;
204 if (status) {
205 dbg("nonzero read bulk status received: %d", status);
206 return;
209 mos7720_port = urb->context;
210 if (!mos7720_port) {
211 dbg("%s","NULL mos7720_port pointer \n");
212 return ;
215 port = mos7720_port->port;
217 dbg("Entering...%s", __FUNCTION__);
219 data = urb->transfer_buffer;
221 tty = port->tty;
222 if (tty && urb->actual_length) {
223 tty_buffer_request_room(tty, urb->actual_length);
224 tty_insert_flip_string(tty, data, urb->actual_length);
225 tty_flip_buffer_push(tty);
228 if (!port->read_urb) {
229 dbg("URB KILLED !!!");
230 return;
233 if (port->read_urb->status != -EINPROGRESS) {
234 port->read_urb->dev = port->serial->dev;
236 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
237 if (retval)
238 dbg("usb_submit_urb(read bulk) failed, retval = %d",
239 retval);
244 * mos7720_bulk_out_data_callback
245 * this is the callback function for when we have finished sending serial
246 * data on the bulk out endpoint.
248 static void mos7720_bulk_out_data_callback(struct urb *urb)
250 struct moschip_port *mos7720_port;
251 struct tty_struct *tty;
252 int status = urb->status;
254 if (status) {
255 dbg("nonzero write bulk status received:%d", status);
256 return;
259 mos7720_port = urb->context;
260 if (!mos7720_port) {
261 dbg("NULL mos7720_port pointer");
262 return ;
265 dbg("Entering .........");
267 tty = mos7720_port->port->tty;
269 if (tty && mos7720_port->open)
270 tty_wakeup(tty);
274 * send_mos_cmd
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)
280 int status;
281 unsigned int pipe;
282 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
283 __u8 requesttype;
284 __u16 size = 0x0000;
286 if (value < MOS_MAX_PORT) {
287 if (product == MOSCHIP_DEVICE_ID_7715) {
288 value = value*0x100+0x100;
289 } else {
290 value = value*0x100+0x200;
292 } else {
293 value = 0x0000;
294 if ((product == MOSCHIP_DEVICE_ID_7715) &&
295 (index != 0x08)) {
296 dbg("serial->product== MOSCHIP_DEVICE_ID_7715");
297 //index = 0x01 ;
301 if (request == MOS_WRITE) {
302 request = (__u8)MOS_WRITE;
303 requesttype = (__u8)0x40;
304 value = value + (__u16)*((unsigned char *)data);
305 data = NULL;
306 pipe = usb_sndctrlpipe(serial->dev, 0);
307 } else {
308 request = (__u8)MOS_READ;
309 requesttype = (__u8)0xC0;
310 size = 0x01;
311 pipe = usb_rcvctrlpipe(serial->dev,0);
314 status = usb_control_msg(serial->dev, pipe, request, requesttype,
315 value, index, data, size, MOS_WDR_TIMEOUT);
317 if (status < 0)
318 dbg("Command Write failed Value %x index %x\n",value,index);
320 return status;
323 static int mos7720_open(struct usb_serial_port *port, struct file * filp)
325 struct usb_serial *serial;
326 struct usb_serial_port *port0;
327 struct urb *urb;
328 struct moschip_serial *mos7720_serial;
329 struct moschip_port *mos7720_port;
330 int response;
331 int port_number;
332 char data;
333 int allocated_urbs = 0;
334 int j;
336 serial = port->serial;
338 mos7720_port = usb_get_serial_port_data(port);
339 if (mos7720_port == NULL)
340 return -ENODEV;
342 port0 = serial->port[0];
344 mos7720_serial = usb_get_serial_data(serial);
346 if (mos7720_serial == NULL || port0 == NULL)
347 return -ENODEV;
349 usb_clear_halt(serial->dev, port->write_urb->pipe);
350 usb_clear_halt(serial->dev, port->read_urb->pipe);
352 /* Initialising the write urb pool */
353 for (j = 0; j < NUM_URBS; ++j) {
354 urb = usb_alloc_urb(0,GFP_KERNEL);
355 mos7720_port->write_urb_pool[j] = urb;
357 if (urb == NULL) {
358 err("No more urbs???");
359 continue;
362 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
363 GFP_KERNEL);
364 if (!urb->transfer_buffer) {
365 err("%s-out of memory for urb buffers.", __FUNCTION__);
366 usb_free_urb(mos7720_port->write_urb_pool[j]);
367 mos7720_port->write_urb_pool[j] = NULL;
368 continue;
370 allocated_urbs++;
373 if (!allocated_urbs)
374 return -ENOMEM;
376 /* Initialize MCS7720 -- Write Init values to corresponding Registers
378 * Register Index
379 * 1 : IER
380 * 2 : FCR
381 * 3 : LCR
382 * 4 : MCR
384 * 0x08 : SP1/2 Control Reg
386 port_number = port->number - port->serial->minor;
387 send_mos_cmd(port->serial, MOS_READ, port_number, UART_LSR, &data);
388 dbg("SS::%p LSR:%x\n",mos7720_port, data);
390 dbg("Check:Sending Command ..........");
392 data = 0x02;
393 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x01, &data);
394 data = 0x02;
395 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x02, &data);
397 data = 0x00;
398 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
399 data = 0x00;
400 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
402 data = 0xCF;
403 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
404 data = 0x03;
405 mos7720_port->shadowLCR = data;
406 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
407 data = 0x0b;
408 mos7720_port->shadowMCR = data;
409 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
410 data = 0x0b;
411 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
413 data = 0x00;
414 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
415 data = 0x00;
416 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
418 /* data = 0x00;
419 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, port_number + 1, &data);
420 data = 0x03;
421 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
422 data = 0x00;
423 send_mos_cmd(port->serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
425 data = 0x00;
426 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
428 data = data | (port->number - port->serial->minor + 1);
429 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
431 data = 0x83;
432 mos7720_port->shadowLCR = data;
433 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
434 data = 0x0c;
435 send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
436 data = 0x00;
437 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
438 data = 0x03;
439 mos7720_port->shadowLCR = data;
440 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
441 data = 0x0c;
442 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
443 data = 0x0c;
444 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
446 //Matrix
448 /* force low_latency on so that our tty_push actually forces *
449 * the data through,otherwise it is scheduled, and with *
450 * high data rates (like with OHCI) data can get lost. */
452 if (port->tty)
453 port->tty->low_latency = 1;
455 /* see if we've set up our endpoint info yet *
456 * (can't set it up in mos7720_startup as the *
457 * structures were not set up at that time.) */
458 if (!mos7720_serial->interrupt_started) {
459 dbg("Interrupt buffer NULL !!!");
461 /* not set up yet, so do it now */
462 mos7720_serial->interrupt_started = 1;
464 dbg("To Submit URB !!!");
466 /* set up our interrupt urb */
467 usb_fill_int_urb(port0->interrupt_in_urb, serial->dev,
468 usb_rcvintpipe(serial->dev,
469 port->interrupt_in_endpointAddress),
470 port0->interrupt_in_buffer,
471 port0->interrupt_in_urb->transfer_buffer_length,
472 mos7720_interrupt_callback, mos7720_port,
473 port0->interrupt_in_urb->interval);
475 /* start interrupt read for this mos7720 this interrupt *
476 * will continue as long as the mos7720 is connected */
477 dbg("Submit URB over !!!");
478 response = usb_submit_urb(port0->interrupt_in_urb, GFP_KERNEL);
479 if (response)
480 dev_err(&port->dev,
481 "%s - Error %d submitting control urb\n",
482 __FUNCTION__, response);
485 /* set up our bulk in urb */
486 usb_fill_bulk_urb(port->read_urb, serial->dev,
487 usb_rcvbulkpipe(serial->dev,
488 port->bulk_in_endpointAddress),
489 port->bulk_in_buffer,
490 port->read_urb->transfer_buffer_length,
491 mos7720_bulk_in_callback, mos7720_port);
492 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
493 if (response)
494 dev_err(&port->dev,
495 "%s - Error %d submitting read urb\n", __FUNCTION__, response);
497 /* initialize our icount structure */
498 memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
500 /* initialize our port settings */
501 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
503 /* send a open port command */
504 mos7720_port->open = 1;
506 return 0;
510 * mos7720_chars_in_buffer
511 * this function is called by the tty driver when it wants to know how many
512 * bytes of data we currently have outstanding in the port (data that has
513 * been written, but hasn't made it out the port yet)
514 * If successful, we return the number of bytes left to be written in the
515 * system,
516 * Otherwise we return a negative error number.
518 static int mos7720_chars_in_buffer(struct usb_serial_port *port)
520 int i;
521 int chars = 0;
522 struct moschip_port *mos7720_port;
524 dbg("%s:entering ...........", __FUNCTION__);
526 mos7720_port = usb_get_serial_port_data(port);
527 if (mos7720_port == NULL) {
528 dbg("%s:leaving ...........", __FUNCTION__);
529 return -ENODEV;
532 for (i = 0; i < NUM_URBS; ++i) {
533 if (mos7720_port->write_urb_pool[i] && mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
534 chars += URB_TRANSFER_BUFFER_SIZE;
536 dbg("%s - returns %d", __FUNCTION__, chars);
537 return chars;
540 static void mos7720_close(struct usb_serial_port *port, struct file *filp)
542 struct usb_serial *serial;
543 struct moschip_port *mos7720_port;
544 char data;
545 int j;
547 dbg("mos7720_close:entering...");
549 serial = port->serial;
551 mos7720_port = usb_get_serial_port_data(port);
552 if (mos7720_port == NULL)
553 return;
555 for (j = 0; j < NUM_URBS; ++j)
556 usb_kill_urb(mos7720_port->write_urb_pool[j]);
558 /* Freeing Write URBs */
559 for (j = 0; j < NUM_URBS; ++j) {
560 if (mos7720_port->write_urb_pool[j]) {
561 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
562 usb_free_urb(mos7720_port->write_urb_pool[j]);
566 /* While closing port, shutdown all bulk read, write *
567 * and interrupt read if they exists, otherwise nop */
568 dbg("Shutdown bulk write");
569 usb_kill_urb(port->write_urb);
570 dbg("Shutdown bulk read");
571 usb_kill_urb(port->read_urb);
573 mutex_lock(&serial->disc_mutex);
574 /* these commands must not be issued if the device has
575 * been disconnected */
576 if (!serial->disconnected) {
577 data = 0x00;
578 send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
579 0x04, &data);
581 data = 0x00;
582 send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
583 0x01, &data);
585 mutex_unlock(&serial->disc_mutex);
586 mos7720_port->open = 0;
588 dbg("Leaving %s", __FUNCTION__);
591 static void mos7720_break(struct usb_serial_port *port, int break_state)
593 unsigned char data;
594 struct usb_serial *serial;
595 struct moschip_port *mos7720_port;
597 dbg("Entering %s", __FUNCTION__);
599 serial = port->serial;
601 mos7720_port = usb_get_serial_port_data(port);
602 if (mos7720_port == NULL)
603 return;
605 if (break_state == -1)
606 data = mos7720_port->shadowLCR | UART_LCR_SBC;
607 else
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,
612 0x03, &data);
614 return;
618 * mos7720_write_room
619 * this function is called by the tty driver when it wants to know how many
620 * bytes of data we can accept for a specific port.
621 * If successful, we return the amount of room that we have for this port
622 * Otherwise we return a negative error number.
624 static int mos7720_write_room(struct usb_serial_port *port)
626 struct moschip_port *mos7720_port;
627 int room = 0;
628 int i;
630 dbg("%s:entering ...........", __FUNCTION__);
632 mos7720_port = usb_get_serial_port_data(port);
633 if (mos7720_port == NULL) {
634 dbg("%s:leaving ...........", __FUNCTION__);
635 return -ENODEV;
638 for (i = 0; i < NUM_URBS; ++i) {
639 if (mos7720_port->write_urb_pool[i] && mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
640 room += URB_TRANSFER_BUFFER_SIZE;
643 dbg("%s - returns %d", __FUNCTION__, room);
644 return room;
647 static int mos7720_write(struct usb_serial_port *port,
648 const unsigned char *data, int count)
650 int status;
651 int i;
652 int bytes_sent = 0;
653 int transfer_size;
655 struct moschip_port *mos7720_port;
656 struct usb_serial *serial;
657 struct urb *urb;
658 const unsigned char *current_position = data;
660 dbg("%s:entering ...........", __FUNCTION__);
662 serial = port->serial;
664 mos7720_port = usb_get_serial_port_data(port);
665 if (mos7720_port == NULL) {
666 dbg("mos7720_port is NULL");
667 return -ENODEV;
670 /* try to find a free urb in the list */
671 urb = NULL;
673 for (i = 0; i < NUM_URBS; ++i) {
674 if (mos7720_port->write_urb_pool[i] && mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
675 urb = mos7720_port->write_urb_pool[i];
676 dbg("URB:%d",i);
677 break;
681 if (urb == NULL) {
682 dbg("%s - no more free urbs", __FUNCTION__);
683 goto exit;
686 if (urb->transfer_buffer == NULL) {
687 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
688 GFP_KERNEL);
689 if (urb->transfer_buffer == NULL) {
690 err("%s no more kernel memory...", __FUNCTION__);
691 goto exit;
694 transfer_size = min (count, URB_TRANSFER_BUFFER_SIZE);
696 memcpy(urb->transfer_buffer, current_position, transfer_size);
697 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, transfer_size,
698 urb->transfer_buffer);
700 /* fill urb with data and submit */
701 usb_fill_bulk_urb(urb, serial->dev,
702 usb_sndbulkpipe(serial->dev,
703 port->bulk_out_endpointAddress),
704 urb->transfer_buffer, transfer_size,
705 mos7720_bulk_out_data_callback, mos7720_port);
707 /* send it down the pipe */
708 status = usb_submit_urb(urb,GFP_ATOMIC);
709 if (status) {
710 err("%s - usb_submit_urb(write bulk) failed with status = %d",
711 __FUNCTION__, status);
712 bytes_sent = status;
713 goto exit;
715 bytes_sent = transfer_size;
717 exit:
718 return bytes_sent;
721 static void mos7720_throttle(struct usb_serial_port *port)
723 struct moschip_port *mos7720_port;
724 struct tty_struct *tty;
725 int status;
727 dbg("%s- port %d\n", __FUNCTION__, port->number);
729 mos7720_port = usb_get_serial_port_data(port);
731 if (mos7720_port == NULL)
732 return;
734 if (!mos7720_port->open) {
735 dbg("port not opened");
736 return;
739 dbg("%s: Entering ..........", __FUNCTION__);
741 tty = port->tty;
742 if (!tty) {
743 dbg("%s - no tty available", __FUNCTION__);
744 return;
747 /* if we are implementing XON/XOFF, send the stop character */
748 if (I_IXOFF(tty)) {
749 unsigned char stop_char = STOP_CHAR(tty);
750 status = mos7720_write(port, &stop_char, 1);
751 if (status <= 0)
752 return;
755 /* if we are implementing RTS/CTS, toggle that line */
756 if (tty->termios->c_cflag & CRTSCTS) {
757 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
758 status = send_mos_cmd(port->serial, MOS_WRITE,
759 port->number - port->serial->minor,
760 UART_MCR, &mos7720_port->shadowMCR);
761 if (status != 0)
762 return;
766 static void mos7720_unthrottle(struct usb_serial_port *port)
768 struct tty_struct *tty;
769 int status;
770 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
772 if (mos7720_port == NULL)
773 return;
775 if (!mos7720_port->open) {
776 dbg("%s - port not opened", __FUNCTION__);
777 return;
780 dbg("%s: Entering ..........", __FUNCTION__);
782 tty = port->tty;
783 if (!tty) {
784 dbg("%s - no tty available", __FUNCTION__);
785 return;
788 /* if we are implementing XON/XOFF, send the start character */
789 if (I_IXOFF(tty)) {
790 unsigned char start_char = START_CHAR(tty);
791 status = mos7720_write(port, &start_char, 1);
792 if (status <= 0)
793 return;
796 /* if we are implementing RTS/CTS, toggle that line */
797 if (tty->termios->c_cflag & CRTSCTS) {
798 mos7720_port->shadowMCR |= UART_MCR_RTS;
799 status = send_mos_cmd(port->serial, MOS_WRITE,
800 port->number - port->serial->minor,
801 UART_MCR, &mos7720_port->shadowMCR);
802 if (status != 0)
803 return;
807 static int set_higher_rates(struct moschip_port *mos7720_port,
808 unsigned int baud)
810 unsigned char data;
811 struct usb_serial_port *port;
812 struct usb_serial *serial;
813 int port_number;
815 if (mos7720_port == NULL)
816 return -EINVAL;
818 port = mos7720_port->port;
819 serial = port->serial;
821 /***********************************************
822 * Init Sequence for higher rates
823 ***********************************************/
824 dbg("Sending Setting Commands ..........");
825 port_number = port->number - port->serial->minor;
827 data = 0x000;
828 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
829 data = 0x000;
830 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
831 data = 0x0CF;
832 send_mos_cmd(serial, MOS_WRITE, port->number, 0x02, &data);
833 data = 0x00b;
834 mos7720_port->shadowMCR = data;
835 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
836 data = 0x00b;
837 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
839 data = 0x000;
840 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
841 data = 0x000;
842 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
845 /***********************************************
846 * Set for higher rates *
847 ***********************************************/
849 data = baud * 0x10;
850 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1,&data);
852 data = 0x003;
853 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
854 data = 0x003;
855 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
857 data = 0x02b;
858 mos7720_port->shadowMCR = data;
859 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
860 data = 0x02b;
861 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
863 /***********************************************
864 * Set DLL/DLM
865 ***********************************************/
867 data = mos7720_port->shadowLCR | UART_LCR_DLAB;
868 mos7720_port->shadowLCR = data;
869 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
871 data = 0x001; /* DLL */
872 send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
873 data = 0x000; /* DLM */
874 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
876 data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
877 mos7720_port->shadowLCR = data;
878 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
880 return 0;
883 /* baud rate information */
884 struct divisor_table_entry
886 __u32 baudrate;
887 __u16 divisor;
890 /* Define table of divisors for moschip 7720 hardware *
891 * These assume a 3.6864MHz crystal, the standard /16, and *
892 * MCR.7 = 0. */
893 static struct divisor_table_entry divisor_table[] = {
894 { 50, 2304},
895 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
896 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
897 { 150, 768},
898 { 300, 384},
899 { 600, 192},
900 { 1200, 96},
901 { 1800, 64},
902 { 2400, 48},
903 { 4800, 24},
904 { 7200, 16},
905 { 9600, 12},
906 { 19200, 6},
907 { 38400, 3},
908 { 57600, 2},
909 { 115200, 1},
912 /*****************************************************************************
913 * calc_baud_rate_divisor
914 * this function calculates the proper baud rate divisor for the specified
915 * baud rate.
916 *****************************************************************************/
917 static int calc_baud_rate_divisor(int baudrate, int *divisor)
919 int i;
920 __u16 custom;
921 __u16 round1;
922 __u16 round;
925 dbg("%s - %d", __FUNCTION__, baudrate);
927 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
928 if (divisor_table[i].baudrate == baudrate) {
929 *divisor = divisor_table[i].divisor;
930 return 0;
934 /* After trying for all the standard baud rates *
935 * Try calculating the divisor for this baud rate */
936 if (baudrate > 75 && baudrate < 230400) {
937 /* get the divisor */
938 custom = (__u16)(230400L / baudrate);
940 /* Check for round off */
941 round1 = (__u16)(2304000L / baudrate);
942 round = (__u16)(round1 - (custom * 10));
943 if (round > 4)
944 custom++;
945 *divisor = custom;
947 dbg("Baud %d = %d",baudrate, custom);
948 return 0;
951 dbg("Baud calculation Failed...");
952 return -EINVAL;
956 * send_cmd_write_baud_rate
957 * this function sends the proper command to change the baud rate of the
958 * specified port.
960 static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
961 int baudrate)
963 struct usb_serial_port *port;
964 struct usb_serial *serial;
965 int divisor;
966 int status;
967 unsigned char data;
968 unsigned char number;
970 if (mos7720_port == NULL)
971 return -1;
973 port = mos7720_port->port;
974 serial = port->serial;
976 dbg("%s: Entering ..........", __FUNCTION__);
978 number = port->number - port->serial->minor;
979 dbg("%s - port = %d, baud = %d", __FUNCTION__, port->number, baudrate);
981 /* Calculate the Divisor */
982 status = calc_baud_rate_divisor(baudrate, &divisor);
983 if (status) {
984 err("%s - bad baud rate", __FUNCTION__);
985 return status;
988 /* Enable access to divisor latch */
989 data = mos7720_port->shadowLCR | UART_LCR_DLAB;
990 mos7720_port->shadowLCR = data;
991 send_mos_cmd(serial, MOS_WRITE, number, UART_LCR, &data);
993 /* Write the divisor */
994 data = ((unsigned char)(divisor & 0xff));
995 send_mos_cmd(serial, MOS_WRITE, number, 0x00, &data);
997 data = ((unsigned char)((divisor & 0xff00) >> 8));
998 send_mos_cmd(serial, MOS_WRITE, number, 0x01, &data);
1000 /* Disable access to divisor latch */
1001 data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1002 mos7720_port->shadowLCR = data;
1003 send_mos_cmd(serial, MOS_WRITE, number, 0x03, &data);
1005 return status;
1009 * change_port_settings
1010 * This routine is called to set the UART on the device to match
1011 * the specified new settings.
1013 static void change_port_settings(struct moschip_port *mos7720_port,
1014 struct ktermios *old_termios)
1016 struct usb_serial_port *port;
1017 struct usb_serial *serial;
1018 struct tty_struct *tty;
1019 int baud;
1020 unsigned cflag;
1021 unsigned iflag;
1022 __u8 mask = 0xff;
1023 __u8 lData;
1024 __u8 lParity;
1025 __u8 lStop;
1026 int status;
1027 int port_number;
1028 char data;
1030 if (mos7720_port == NULL)
1031 return ;
1033 port = mos7720_port->port;
1034 serial = port->serial;
1035 port_number = port->number - port->serial->minor;
1037 dbg("%s - port %d", __FUNCTION__, port->number);
1039 if (!mos7720_port->open) {
1040 dbg("%s - port not opened", __FUNCTION__);
1041 return;
1044 tty = mos7720_port->port->tty;
1046 dbg("%s: Entering ..........", __FUNCTION__);
1048 lData = UART_LCR_WLEN8;
1049 lStop = 0x00; /* 1 stop bit */
1050 lParity = 0x00; /* No parity */
1052 cflag = tty->termios->c_cflag;
1053 iflag = tty->termios->c_iflag;
1055 /* Change the number of bits */
1056 switch (cflag & CSIZE) {
1057 case CS5:
1058 lData = UART_LCR_WLEN5;
1059 mask = 0x1f;
1060 break;
1062 case CS6:
1063 lData = UART_LCR_WLEN6;
1064 mask = 0x3f;
1065 break;
1067 case CS7:
1068 lData = UART_LCR_WLEN7;
1069 mask = 0x7f;
1070 break;
1071 default:
1072 case CS8:
1073 lData = UART_LCR_WLEN8;
1074 break;
1077 /* Change the Parity bit */
1078 if (cflag & PARENB) {
1079 if (cflag & PARODD) {
1080 lParity = UART_LCR_PARITY;
1081 dbg("%s - parity = odd", __FUNCTION__);
1082 } else {
1083 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
1084 dbg("%s - parity = even", __FUNCTION__);
1087 } else {
1088 dbg("%s - parity = none", __FUNCTION__);
1091 if (cflag & CMSPAR)
1092 lParity = lParity | 0x20;
1094 /* Change the Stop bit */
1095 if (cflag & CSTOPB) {
1096 lStop = UART_LCR_STOP;
1097 dbg("%s - stop bits = 2", __FUNCTION__);
1098 } else {
1099 lStop = 0x00;
1100 dbg("%s - stop bits = 1", __FUNCTION__);
1103 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1104 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1105 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
1107 /* Update the LCR with the correct value */
1108 mos7720_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1109 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1112 /* Disable Interrupts */
1113 data = 0x00;
1114 send_mos_cmd(serial,MOS_WRITE,port->number - port->serial->minor, UART_IER, &data);
1116 data = 0x00;
1117 send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
1119 data = 0xcf;
1120 send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
1122 /* Send the updated LCR value to the mos7720 */
1123 data = mos7720_port->shadowLCR;
1124 send_mos_cmd(serial, MOS_WRITE, port_number, UART_LCR, &data);
1126 data = 0x00b;
1127 mos7720_port->shadowMCR = data;
1128 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
1129 data = 0x00b;
1130 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
1132 /* set up the MCR register and send it to the mos7720 */
1133 mos7720_port->shadowMCR = UART_MCR_OUT2;
1134 if (cflag & CBAUD)
1135 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1137 if (cflag & CRTSCTS) {
1138 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
1140 /* To set hardware flow control to the specified *
1141 * serial port, in SP1/2_CONTROL_REG */
1142 if (port->number) {
1143 data = 0x001;
1144 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1145 0x08, &data);
1146 } else {
1147 data = 0x002;
1148 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1149 0x08, &data);
1151 } else {
1152 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1155 data = mos7720_port->shadowMCR;
1156 send_mos_cmd(serial, MOS_WRITE, port_number, UART_MCR, &data);
1158 /* Determine divisor based on baud rate */
1159 baud = tty_get_baud_rate(tty);
1160 if (!baud) {
1161 /* pick a default, any default... */
1162 dbg("Picked default baud...");
1163 baud = 9600;
1166 if (baud >= 230400) {
1167 set_higher_rates(mos7720_port, baud);
1168 /* Enable Interrupts */
1169 data = 0x0c;
1170 send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1171 return;
1174 dbg("%s - baud rate = %d", __FUNCTION__, baud);
1175 status = send_cmd_write_baud_rate(mos7720_port, baud);
1176 /* FIXME: needs to write actual resulting baud back not just
1177 blindly do so */
1178 if (cflag & CBAUD)
1179 tty_encode_baud_rate(tty, baud, baud);
1180 /* Enable Interrupts */
1181 data = 0x0c;
1182 send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1184 if (port->read_urb->status != -EINPROGRESS) {
1185 port->read_urb->dev = serial->dev;
1187 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1188 if (status)
1189 dbg("usb_submit_urb(read bulk) failed, status = %d",
1190 status);
1192 return;
1196 * mos7720_set_termios
1197 * this function is called by the tty driver when it wants to change the
1198 * termios structure.
1200 static void mos7720_set_termios(struct usb_serial_port *port,
1201 struct ktermios *old_termios)
1203 int status;
1204 unsigned int cflag;
1205 struct usb_serial *serial;
1206 struct moschip_port *mos7720_port;
1207 struct tty_struct *tty;
1209 serial = port->serial;
1211 mos7720_port = usb_get_serial_port_data(port);
1213 if (mos7720_port == NULL)
1214 return;
1216 tty = port->tty;
1219 if (!mos7720_port->open) {
1220 dbg("%s - port not opened", __FUNCTION__);
1221 return;
1224 dbg("%s\n","setting termios - ASPIRE");
1226 cflag = tty->termios->c_cflag;
1228 dbg("%s - cflag %08x iflag %08x", __FUNCTION__,
1229 tty->termios->c_cflag,
1230 RELEVANT_IFLAG(tty->termios->c_iflag));
1232 dbg("%s - old cflag %08x old iflag %08x", __FUNCTION__,
1233 old_termios->c_cflag,
1234 RELEVANT_IFLAG(old_termios->c_iflag));
1236 dbg("%s - port %d", __FUNCTION__, port->number);
1238 /* change the port settings to the new ones specified */
1239 change_port_settings(mos7720_port, old_termios);
1241 if(!port->read_urb) {
1242 dbg("%s","URB KILLED !!!!!\n");
1243 return;
1246 if(port->read_urb->status != -EINPROGRESS) {
1247 port->read_urb->dev = serial->dev;
1248 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1249 if (status)
1250 dbg("usb_submit_urb(read bulk) failed, status = %d",
1251 status);
1253 return;
1257 * get_lsr_info - get line status register info
1259 * Purpose: Let user call ioctl() to get info when the UART physically
1260 * is emptied. On bus types like RS485, the transmitter must
1261 * release the bus after transmitting. This must be done when
1262 * the transmit shift register is empty, not be done when the
1263 * transmit holding register is empty. This functionality
1264 * allows an RS485 driver to be written in user space.
1266 static int get_lsr_info(struct moschip_port *mos7720_port,
1267 unsigned int __user *value)
1269 int count;
1270 unsigned int result = 0;
1272 count = mos7720_chars_in_buffer(mos7720_port->port);
1273 if (count == 0) {
1274 dbg("%s -- Empty", __FUNCTION__);
1275 result = TIOCSER_TEMT;
1278 if (copy_to_user(value, &result, sizeof(int)))
1279 return -EFAULT;
1280 return 0;
1284 * get_number_bytes_avail - get number of bytes available
1286 * Purpose: Let user call ioctl to get the count of number of bytes available.
1288 static int get_number_bytes_avail(struct moschip_port *mos7720_port,
1289 unsigned int __user *value)
1291 unsigned int result = 0;
1292 struct tty_struct *tty = mos7720_port->port->tty;
1294 if (!tty)
1295 return -ENOIOCTLCMD;
1297 result = tty->read_cnt;
1299 dbg("%s(%d) = %d", __FUNCTION__, mos7720_port->port->number, result);
1300 if (copy_to_user(value, &result, sizeof(int)))
1301 return -EFAULT;
1303 return -ENOIOCTLCMD;
1306 static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1307 unsigned int __user *value)
1309 unsigned int mcr ;
1310 unsigned int arg;
1311 unsigned char data;
1313 struct usb_serial_port *port;
1315 if (mos7720_port == NULL)
1316 return -1;
1318 port = (struct usb_serial_port*)mos7720_port->port;
1319 mcr = mos7720_port->shadowMCR;
1321 if (copy_from_user(&arg, value, sizeof(int)))
1322 return -EFAULT;
1324 switch (cmd) {
1325 case TIOCMBIS:
1326 if (arg & TIOCM_RTS)
1327 mcr |= UART_MCR_RTS;
1328 if (arg & TIOCM_DTR)
1329 mcr |= UART_MCR_RTS;
1330 if (arg & TIOCM_LOOP)
1331 mcr |= UART_MCR_LOOP;
1332 break;
1334 case TIOCMBIC:
1335 if (arg & TIOCM_RTS)
1336 mcr &= ~UART_MCR_RTS;
1337 if (arg & TIOCM_DTR)
1338 mcr &= ~UART_MCR_RTS;
1339 if (arg & TIOCM_LOOP)
1340 mcr &= ~UART_MCR_LOOP;
1341 break;
1343 case TIOCMSET:
1344 /* turn off the RTS and DTR and LOOPBACK
1345 * and then only turn on what was asked to */
1346 mcr &= ~(UART_MCR_RTS | UART_MCR_DTR | UART_MCR_LOOP);
1347 mcr |= ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0);
1348 mcr |= ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0);
1349 mcr |= ((arg & TIOCM_LOOP) ? UART_MCR_LOOP : 0);
1350 break;
1353 mos7720_port->shadowMCR = mcr;
1355 data = mos7720_port->shadowMCR;
1356 send_mos_cmd(port->serial, MOS_WRITE,
1357 port->number - port->serial->minor, UART_MCR, &data);
1359 return 0;
1362 static int get_modem_info(struct moschip_port *mos7720_port,
1363 unsigned int __user *value)
1365 unsigned int result = 0;
1366 unsigned int msr = mos7720_port->shadowMSR;
1367 unsigned int mcr = mos7720_port->shadowMCR;
1369 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1370 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1371 | ((msr & UART_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1372 | ((msr & UART_MSR_DCD) ? TIOCM_CAR: 0) /* 0x040 */
1373 | ((msr & UART_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1374 | ((msr & UART_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1377 dbg("%s -- %x", __FUNCTION__, result);
1379 if (copy_to_user(value, &result, sizeof(int)))
1380 return -EFAULT;
1381 return 0;
1384 static int get_serial_info(struct moschip_port *mos7720_port,
1385 struct serial_struct __user *retinfo)
1387 struct serial_struct tmp;
1389 if (!retinfo)
1390 return -EFAULT;
1392 memset(&tmp, 0, sizeof(tmp));
1394 tmp.type = PORT_16550A;
1395 tmp.line = mos7720_port->port->serial->minor;
1396 tmp.port = mos7720_port->port->number;
1397 tmp.irq = 0;
1398 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1399 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1400 tmp.baud_base = 9600;
1401 tmp.close_delay = 5*HZ;
1402 tmp.closing_wait = 30*HZ;
1404 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1405 return -EFAULT;
1406 return 0;
1409 static int mos7720_ioctl(struct usb_serial_port *port, struct file *file,
1410 unsigned int cmd, unsigned long arg)
1412 struct moschip_port *mos7720_port;
1413 struct async_icount cnow;
1414 struct async_icount cprev;
1415 struct serial_icounter_struct icount;
1417 mos7720_port = usb_get_serial_port_data(port);
1418 if (mos7720_port == NULL)
1419 return -ENODEV;
1421 dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
1423 switch (cmd) {
1424 case TIOCINQ:
1425 /* return number of bytes available */
1426 dbg("%s (%d) TIOCINQ", __FUNCTION__, port->number);
1427 return get_number_bytes_avail(mos7720_port,
1428 (unsigned int __user *)arg);
1429 break;
1431 case TIOCSERGETLSR:
1432 dbg("%s (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
1433 return get_lsr_info(mos7720_port, (unsigned int __user *)arg);
1434 return 0;
1436 case TIOCMBIS:
1437 case TIOCMBIC:
1438 case TIOCMSET:
1439 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __FUNCTION__,
1440 port->number);
1441 return set_modem_info(mos7720_port, cmd,
1442 (unsigned int __user *)arg);
1444 case TIOCMGET:
1445 dbg("%s (%d) TIOCMGET", __FUNCTION__, port->number);
1446 return get_modem_info(mos7720_port,
1447 (unsigned int __user *)arg);
1449 case TIOCGSERIAL:
1450 dbg("%s (%d) TIOCGSERIAL", __FUNCTION__, port->number);
1451 return get_serial_info(mos7720_port,
1452 (struct serial_struct __user *)arg);
1454 case TIOCSSERIAL:
1455 dbg("%s (%d) TIOCSSERIAL", __FUNCTION__, port->number);
1456 break;
1458 case TIOCMIWAIT:
1459 dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
1460 cprev = mos7720_port->icount;
1461 while (1) {
1462 if (signal_pending(current))
1463 return -ERESTARTSYS;
1464 cnow = mos7720_port->icount;
1465 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1466 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1467 return -EIO; /* no change => error */
1468 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1469 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1470 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1471 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1472 return 0;
1474 cprev = cnow;
1476 /* NOTREACHED */
1477 break;
1479 case TIOCGICOUNT:
1480 cnow = mos7720_port->icount;
1481 icount.cts = cnow.cts;
1482 icount.dsr = cnow.dsr;
1483 icount.rng = cnow.rng;
1484 icount.dcd = cnow.dcd;
1485 icount.rx = cnow.rx;
1486 icount.tx = cnow.tx;
1487 icount.frame = cnow.frame;
1488 icount.overrun = cnow.overrun;
1489 icount.parity = cnow.parity;
1490 icount.brk = cnow.brk;
1491 icount.buf_overrun = cnow.buf_overrun;
1493 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__,
1494 port->number, icount.rx, icount.tx );
1495 if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
1496 return -EFAULT;
1497 return 0;
1500 return -ENOIOCTLCMD;
1503 static int mos7720_startup(struct usb_serial *serial)
1505 struct moschip_serial *mos7720_serial;
1506 struct moschip_port *mos7720_port;
1507 struct usb_device *dev;
1508 int i;
1509 char data;
1511 dbg("%s: Entering ..........", __FUNCTION__);
1513 if (!serial) {
1514 dbg("Invalid Handler");
1515 return -ENODEV;
1518 dev = serial->dev;
1520 /* create our private serial structure */
1521 mos7720_serial = kzalloc(sizeof(struct moschip_serial), GFP_KERNEL);
1522 if (mos7720_serial == NULL) {
1523 err("%s - Out of memory", __FUNCTION__);
1524 return -ENOMEM;
1527 usb_set_serial_data(serial, mos7720_serial);
1529 /* we set up the pointers to the endpoints in the mos7720_open *
1530 * function, as the structures aren't created yet. */
1532 /* set up port private structures */
1533 for (i = 0; i < serial->num_ports; ++i) {
1534 mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
1535 if (mos7720_port == NULL) {
1536 err("%s - Out of memory", __FUNCTION__);
1537 usb_set_serial_data(serial, NULL);
1538 kfree(mos7720_serial);
1539 return -ENOMEM;
1542 /* Initialize all port interrupt end point to port 0 int
1543 * endpoint. Our device has only one interrupt endpoint
1544 * comman to all ports */
1545 serial->port[i]->interrupt_in_endpointAddress = serial->port[0]->interrupt_in_endpointAddress;
1547 mos7720_port->port = serial->port[i];
1548 usb_set_serial_port_data(serial->port[i], mos7720_port);
1550 dbg("port number is %d", serial->port[i]->number);
1551 dbg("serial number is %d", serial->minor);
1555 /* setting configuration feature to one */
1556 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1557 (__u8)0x03, 0x00,0x01,0x00, NULL, 0x00, 5*HZ);
1559 send_mos_cmd(serial,MOS_READ,0x00, UART_LSR, &data); // LSR For Port 1
1560 dbg("LSR:%x",data);
1562 send_mos_cmd(serial,MOS_READ,0x01, UART_LSR, &data); // LSR For Port 2
1563 dbg("LSR:%x",data);
1565 return 0;
1568 static void mos7720_shutdown(struct usb_serial *serial)
1570 int i;
1572 /* free private structure allocated for serial port */
1573 for (i=0; i < serial->num_ports; ++i) {
1574 kfree(usb_get_serial_port_data(serial->port[i]));
1575 usb_set_serial_port_data(serial->port[i], NULL);
1578 /* free private structure allocated for serial device */
1579 kfree(usb_get_serial_data(serial));
1580 usb_set_serial_data(serial, NULL);
1583 static struct usb_driver usb_driver = {
1584 .name = "moschip7720",
1585 .probe = usb_serial_probe,
1586 .disconnect = usb_serial_disconnect,
1587 .id_table = moschip_port_id_table,
1588 .no_dynamic_id = 1,
1591 static struct usb_serial_driver moschip7720_2port_driver = {
1592 .driver = {
1593 .owner = THIS_MODULE,
1594 .name = "moschip7720",
1596 .description = "Moschip 2 port adapter",
1597 .usb_driver = &usb_driver,
1598 .id_table = moschip_port_id_table,
1599 .num_interrupt_in = 1,
1600 .num_bulk_in = 2,
1601 .num_bulk_out = 2,
1602 .num_ports = 2,
1603 .open = mos7720_open,
1604 .close = mos7720_close,
1605 .throttle = mos7720_throttle,
1606 .unthrottle = mos7720_unthrottle,
1607 .attach = mos7720_startup,
1608 .shutdown = mos7720_shutdown,
1609 .ioctl = mos7720_ioctl,
1610 .set_termios = mos7720_set_termios,
1611 .write = mos7720_write,
1612 .write_room = mos7720_write_room,
1613 .chars_in_buffer = mos7720_chars_in_buffer,
1614 .break_ctl = mos7720_break,
1615 .read_bulk_callback = mos7720_bulk_in_callback,
1616 .read_int_callback = mos7720_interrupt_callback,
1619 static int __init moschip7720_init(void)
1621 int retval;
1623 dbg("%s: Entering ..........", __FUNCTION__);
1625 /* Register with the usb serial */
1626 retval = usb_serial_register(&moschip7720_2port_driver);
1627 if (retval)
1628 goto failed_port_device_register;
1630 info(DRIVER_DESC " " DRIVER_VERSION);
1632 /* Register with the usb */
1633 retval = usb_register(&usb_driver);
1634 if (retval)
1635 goto failed_usb_register;
1637 return 0;
1639 failed_usb_register:
1640 usb_serial_deregister(&moschip7720_2port_driver);
1642 failed_port_device_register:
1643 return retval;
1646 static void __exit moschip7720_exit(void)
1648 usb_deregister(&usb_driver);
1649 usb_serial_deregister(&moschip7720_2port_driver);
1652 module_init(moschip7720_init);
1653 module_exit(moschip7720_exit);
1655 /* Module information */
1656 MODULE_AUTHOR( DRIVER_AUTHOR );
1657 MODULE_DESCRIPTION( DRIVER_DESC );
1658 MODULE_LICENSE("GPL");
1660 module_param(debug, bool, S_IRUGO | S_IWUSR);
1661 MODULE_PARM_DESC(debug, "Debug enabled or not");