Staging: strip: delete the driver
[linux/fpc-iii.git] / drivers / usb / serial / mos7720.c
blob0d47f2c4d59f21b80b0f9f1540af9fac422009cf
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 <linux/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 {
68 __u8 shadowLCR; /* last LCR value received */
69 __u8 shadowMCR; /* last MCR value received */
70 __u8 shadowMSR; /* last MSR value received */
71 char open;
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;
82 static int debug;
84 static struct usb_serial_driver moschip7720_2port_driver;
86 #define USB_VENDOR_ID_MOSCHIP 0x9710
87 #define MOSCHIP_DEVICE_ID_7720 0x7720
88 #define MOSCHIP_DEVICE_ID_7715 0x7715
90 static const struct usb_device_id moschip_port_id_table[] = {
91 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
92 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) },
93 { } /* terminating entry */
95 MODULE_DEVICE_TABLE(usb, moschip_port_id_table);
99 * mos7720_interrupt_callback
100 * this is the callback function for when we have received data on the
101 * interrupt endpoint.
103 static void mos7720_interrupt_callback(struct urb *urb)
105 int result;
106 int length;
107 int status = urb->status;
108 __u8 *data;
109 __u8 sp1;
110 __u8 sp2;
112 dbg(" : Entering");
114 switch (status) {
115 case 0:
116 /* success */
117 break;
118 case -ECONNRESET:
119 case -ENOENT:
120 case -ESHUTDOWN:
121 /* this urb is terminated, clean up */
122 dbg("%s - urb shutting down with status: %d", __func__,
123 status);
124 return;
125 default:
126 dbg("%s - nonzero urb status received: %d", __func__,
127 status);
128 goto exit;
131 length = urb->actual_length;
132 data = urb->transfer_buffer;
134 /* Moschip get 4 bytes
135 * Byte 1 IIR Port 1 (port.number is 0)
136 * Byte 2 IIR Port 2 (port.number is 1)
137 * Byte 3 --------------
138 * Byte 4 FIFO status for both */
140 /* the above description is inverted
141 * oneukum 2007-03-14 */
143 if (unlikely(length != 4)) {
144 dbg("Wrong data !!!");
145 return;
148 sp1 = data[3];
149 sp2 = data[2];
151 if ((sp1 | sp2) & 0x01) {
152 /* No Interrupt Pending in both the ports */
153 dbg("No Interrupt !!!");
154 } else {
155 switch (sp1 & 0x0f) {
156 case SERIAL_IIR_RLS:
157 dbg("Serial Port 1: Receiver status error or address "
158 "bit detected in 9-bit mode\n");
159 break;
160 case SERIAL_IIR_CTI:
161 dbg("Serial Port 1: Receiver time out");
162 break;
163 case SERIAL_IIR_MS:
164 dbg("Serial Port 1: Modem status change");
165 break;
168 switch (sp2 & 0x0f) {
169 case SERIAL_IIR_RLS:
170 dbg("Serial Port 2: Receiver status error or address "
171 "bit detected in 9-bit mode");
172 break;
173 case SERIAL_IIR_CTI:
174 dbg("Serial Port 2: Receiver time out");
175 break;
176 case SERIAL_IIR_MS:
177 dbg("Serial Port 2: Modem status change");
178 break;
182 exit:
183 result = usb_submit_urb(urb, GFP_ATOMIC);
184 if (result)
185 dev_err(&urb->dev->dev,
186 "%s - Error %d submitting control urb\n",
187 __func__, result);
188 return;
192 * mos7715_interrupt_callback
193 * this is the 7715's callback function for when we have received data on
194 * the interrupt endpoint.
196 static void mos7715_interrupt_callback(struct urb *urb)
198 int result;
199 int length;
200 int status = urb->status;
201 __u8 *data;
202 __u8 iir;
204 switch (status) {
205 case 0:
206 /* success */
207 break;
208 case -ECONNRESET:
209 case -ENOENT:
210 case -ESHUTDOWN:
211 /* this urb is terminated, clean up */
212 dbg("%s - urb shutting down with status: %d", __func__,
213 status);
214 return;
215 default:
216 dbg("%s - nonzero urb status received: %d", __func__,
217 status);
218 goto exit;
221 length = urb->actual_length;
222 data = urb->transfer_buffer;
224 /* Structure of data from 7715 device:
225 * Byte 1: IIR serial Port
226 * Byte 2: unused
227 * Byte 2: DSR parallel port
228 * Byte 4: FIFO status for both */
230 if (unlikely(length != 4)) {
231 dbg("Wrong data !!!");
232 return;
235 iir = data[0];
236 if (!(iir & 0x01)) { /* serial port interrupt pending */
237 switch (iir & 0x0f) {
238 case SERIAL_IIR_RLS:
239 dbg("Serial Port: Receiver status error or address "
240 "bit detected in 9-bit mode\n");
241 break;
242 case SERIAL_IIR_CTI:
243 dbg("Serial Port: Receiver time out");
244 break;
245 case SERIAL_IIR_MS:
246 dbg("Serial Port: Modem status change");
247 break;
251 exit:
252 result = usb_submit_urb(urb, GFP_ATOMIC);
253 if (result)
254 dev_err(&urb->dev->dev,
255 "%s - Error %d submitting control urb\n",
256 __func__, result);
257 return;
261 * mos7720_bulk_in_callback
262 * this is the callback function for when we have received data on the
263 * bulk in endpoint.
265 static void mos7720_bulk_in_callback(struct urb *urb)
267 int retval;
268 unsigned char *data ;
269 struct usb_serial_port *port;
270 struct moschip_port *mos7720_port;
271 struct tty_struct *tty;
272 int status = urb->status;
274 if (status) {
275 dbg("nonzero read bulk status received: %d", status);
276 return;
279 mos7720_port = urb->context;
280 if (!mos7720_port) {
281 dbg("NULL mos7720_port pointer");
282 return ;
285 port = mos7720_port->port;
287 dbg("Entering...%s", __func__);
289 data = urb->transfer_buffer;
291 tty = tty_port_tty_get(&port->port);
292 if (tty && urb->actual_length) {
293 tty_insert_flip_string(tty, data, urb->actual_length);
294 tty_flip_buffer_push(tty);
296 tty_kref_put(tty);
298 if (!port->read_urb) {
299 dbg("URB KILLED !!!");
300 return;
303 if (port->read_urb->status != -EINPROGRESS) {
304 port->read_urb->dev = port->serial->dev;
306 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
307 if (retval)
308 dbg("usb_submit_urb(read bulk) failed, retval = %d",
309 retval);
314 * mos7720_bulk_out_data_callback
315 * this is the callback function for when we have finished sending serial
316 * data on the bulk out endpoint.
318 static void mos7720_bulk_out_data_callback(struct urb *urb)
320 struct moschip_port *mos7720_port;
321 struct tty_struct *tty;
322 int status = urb->status;
324 if (status) {
325 dbg("nonzero write bulk status received:%d", status);
326 return;
329 mos7720_port = urb->context;
330 if (!mos7720_port) {
331 dbg("NULL mos7720_port pointer");
332 return ;
335 dbg("Entering .........");
337 tty = tty_port_tty_get(&mos7720_port->port->port);
339 if (tty && mos7720_port->open)
340 tty_wakeup(tty);
341 tty_kref_put(tty);
345 * send_mos_cmd
346 * this function will be used for sending command to device
348 static int send_mos_cmd(struct usb_serial *serial, __u8 request, __u16 value,
349 __u16 index, u8 *data)
351 int status;
352 u8 *buf;
353 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
355 if (value < MOS_MAX_PORT) {
356 if (product == MOSCHIP_DEVICE_ID_7715)
357 value = 0x0200; /* identifies the 7715's serial port */
358 else
359 value = value*0x100+0x200;
360 } else {
361 value = 0x0000;
362 if ((product == MOSCHIP_DEVICE_ID_7715) &&
363 (index != 0x08)) {
364 dbg("serial->product== MOSCHIP_DEVICE_ID_7715");
365 /* index = 0x01 ; */
369 if (request == MOS_WRITE) {
370 value = value + *data;
371 status = usb_control_msg(serial->dev,
372 usb_sndctrlpipe(serial->dev, 0), MOS_WRITE,
373 0x40, value, index, NULL, 0, MOS_WDR_TIMEOUT);
374 } else {
375 buf = kmalloc(1, GFP_KERNEL);
376 if (!buf) {
377 status = -ENOMEM;
378 goto out;
380 status = usb_control_msg(serial->dev,
381 usb_rcvctrlpipe(serial->dev, 0), MOS_READ,
382 0xc0, value, index, buf, 1, MOS_WDR_TIMEOUT);
383 *data = *buf;
384 kfree(buf);
386 out:
387 if (status < 0)
388 dbg("Command Write failed Value %x index %x", value, index);
390 return status;
395 * mos77xx_probe
396 * this function installs the appropriate read interrupt endpoint callback
397 * depending on whether the device is a 7720 or 7715, thus avoiding costly
398 * run-time checks in the high-frequency callback routine itself.
400 static int mos77xx_probe(struct usb_serial *serial,
401 const struct usb_device_id *id)
403 if (id->idProduct == MOSCHIP_DEVICE_ID_7715)
404 moschip7720_2port_driver.read_int_callback =
405 mos7715_interrupt_callback;
406 else
407 moschip7720_2port_driver.read_int_callback =
408 mos7720_interrupt_callback;
410 return 0;
413 static int mos77xx_calc_num_ports(struct usb_serial *serial)
415 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
416 if (product == MOSCHIP_DEVICE_ID_7715)
417 return 1;
419 return 2;
422 static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
424 struct usb_serial *serial;
425 struct usb_serial_port *port0;
426 struct urb *urb;
427 struct moschip_serial *mos7720_serial;
428 struct moschip_port *mos7720_port;
429 int response;
430 int port_number;
431 char data;
432 int allocated_urbs = 0;
433 int j;
435 serial = port->serial;
437 mos7720_port = usb_get_serial_port_data(port);
438 if (mos7720_port == NULL)
439 return -ENODEV;
441 port0 = serial->port[0];
443 mos7720_serial = usb_get_serial_data(serial);
445 if (mos7720_serial == NULL || port0 == NULL)
446 return -ENODEV;
448 usb_clear_halt(serial->dev, port->write_urb->pipe);
449 usb_clear_halt(serial->dev, port->read_urb->pipe);
451 /* Initialising the write urb pool */
452 for (j = 0; j < NUM_URBS; ++j) {
453 urb = usb_alloc_urb(0, GFP_KERNEL);
454 mos7720_port->write_urb_pool[j] = urb;
456 if (urb == NULL) {
457 dev_err(&port->dev, "No more urbs???\n");
458 continue;
461 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
462 GFP_KERNEL);
463 if (!urb->transfer_buffer) {
464 dev_err(&port->dev,
465 "%s-out of memory for urb buffers.\n",
466 __func__);
467 usb_free_urb(mos7720_port->write_urb_pool[j]);
468 mos7720_port->write_urb_pool[j] = NULL;
469 continue;
471 allocated_urbs++;
474 if (!allocated_urbs)
475 return -ENOMEM;
477 /* Initialize MCS7720 -- Write Init values to corresponding Registers
479 * Register Index
480 * 0 : THR/RHR
481 * 1 : IER
482 * 2 : FCR
483 * 3 : LCR
484 * 4 : MCR
485 * 5 : LSR
486 * 6 : MSR
487 * 7 : SPR
489 * 0x08 : SP1/2 Control Reg
491 port_number = port->number - port->serial->minor;
492 send_mos_cmd(port->serial, MOS_READ, port_number, UART_LSR, &data);
493 dbg("SS::%p LSR:%x", mos7720_port, data);
495 dbg("Check:Sending Command ..........");
497 data = 0x02;
498 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x01, &data);
499 data = 0x02;
500 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x02, &data);
502 data = 0x00;
503 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
504 data = 0x00;
505 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
507 data = 0xCF;
508 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
509 data = 0x03;
510 mos7720_port->shadowLCR = data;
511 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
512 data = 0x0b;
513 mos7720_port->shadowMCR = data;
514 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
515 data = 0x0b;
516 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
518 data = 0x00;
519 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
520 data = 0x00;
521 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
523 /* data = 0x00;
524 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, port_number + 1, &data);
525 data = 0x03;
526 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
527 data = 0x00;
528 send_mos_cmd(port->serial, MOS_WRITE, MOS_MAX_PORT,
529 port_number + 1, &data);
531 data = 0x00;
532 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
534 data = data | (port->number - port->serial->minor + 1);
535 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
537 data = 0x83;
538 mos7720_port->shadowLCR = data;
539 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
540 data = 0x0c;
541 send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
542 data = 0x00;
543 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
544 data = 0x03;
545 mos7720_port->shadowLCR = data;
546 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
547 data = 0x0c;
548 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
549 data = 0x0c;
550 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
552 /* see if we've set up our endpoint info yet *
553 * (can't set it up in mos7720_startup as the *
554 * structures were not set up at that time.) */
555 if (!mos7720_serial->interrupt_started) {
556 dbg("Interrupt buffer NULL !!!");
558 /* not set up yet, so do it now */
559 mos7720_serial->interrupt_started = 1;
561 dbg("To Submit URB !!!");
563 /* set up our interrupt urb */
564 usb_fill_int_urb(port0->interrupt_in_urb, serial->dev,
565 usb_rcvintpipe(serial->dev,
566 port->interrupt_in_endpointAddress),
567 port0->interrupt_in_buffer,
568 port0->interrupt_in_urb->transfer_buffer_length,
569 mos7720_interrupt_callback, mos7720_port,
570 port0->interrupt_in_urb->interval);
572 /* start interrupt read for this mos7720 this interrupt *
573 * will continue as long as the mos7720 is connected */
574 dbg("Submit URB over !!!");
575 response = usb_submit_urb(port0->interrupt_in_urb, GFP_KERNEL);
576 if (response)
577 dev_err(&port->dev,
578 "%s - Error %d submitting control urb\n",
579 __func__, response);
582 /* set up our bulk in urb */
583 usb_fill_bulk_urb(port->read_urb, serial->dev,
584 usb_rcvbulkpipe(serial->dev,
585 port->bulk_in_endpointAddress),
586 port->bulk_in_buffer,
587 port->read_urb->transfer_buffer_length,
588 mos7720_bulk_in_callback, mos7720_port);
589 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
590 if (response)
591 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
592 __func__, response);
594 /* initialize our icount structure */
595 memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
597 /* initialize our port settings */
598 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
600 /* send a open port command */
601 mos7720_port->open = 1;
603 return 0;
607 * mos7720_chars_in_buffer
608 * this function is called by the tty driver when it wants to know how many
609 * bytes of data we currently have outstanding in the port (data that has
610 * been written, but hasn't made it out the port yet)
611 * If successful, we return the number of bytes left to be written in the
612 * system,
613 * Otherwise we return a negative error number.
615 static int mos7720_chars_in_buffer(struct tty_struct *tty)
617 struct usb_serial_port *port = tty->driver_data;
618 int i;
619 int chars = 0;
620 struct moschip_port *mos7720_port;
622 dbg("%s:entering ...........", __func__);
624 mos7720_port = usb_get_serial_port_data(port);
625 if (mos7720_port == NULL) {
626 dbg("%s:leaving ...........", __func__);
627 return 0;
630 for (i = 0; i < NUM_URBS; ++i) {
631 if (mos7720_port->write_urb_pool[i] &&
632 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
633 chars += URB_TRANSFER_BUFFER_SIZE;
635 dbg("%s - returns %d", __func__, chars);
636 return chars;
639 static void mos7720_close(struct usb_serial_port *port)
641 struct usb_serial *serial;
642 struct moschip_port *mos7720_port;
643 char data;
644 int j;
646 dbg("mos7720_close:entering...");
648 serial = port->serial;
650 mos7720_port = usb_get_serial_port_data(port);
651 if (mos7720_port == NULL)
652 return;
654 for (j = 0; j < NUM_URBS; ++j)
655 usb_kill_urb(mos7720_port->write_urb_pool[j]);
657 /* Freeing Write URBs */
658 for (j = 0; j < NUM_URBS; ++j) {
659 if (mos7720_port->write_urb_pool[j]) {
660 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
661 usb_free_urb(mos7720_port->write_urb_pool[j]);
665 /* While closing port, shutdown all bulk read, write *
666 * and interrupt read if they exists, otherwise nop */
667 dbg("Shutdown bulk write");
668 usb_kill_urb(port->write_urb);
669 dbg("Shutdown bulk read");
670 usb_kill_urb(port->read_urb);
672 mutex_lock(&serial->disc_mutex);
673 /* these commands must not be issued if the device has
674 * been disconnected */
675 if (!serial->disconnected) {
676 data = 0x00;
677 send_mos_cmd(serial, MOS_WRITE,
678 port->number - port->serial->minor, 0x04, &data);
680 data = 0x00;
681 send_mos_cmd(serial, MOS_WRITE,
682 port->number - port->serial->minor, 0x01, &data);
684 mutex_unlock(&serial->disc_mutex);
685 mos7720_port->open = 0;
687 dbg("Leaving %s", __func__);
690 static void mos7720_break(struct tty_struct *tty, int break_state)
692 struct usb_serial_port *port = tty->driver_data;
693 unsigned char data;
694 struct usb_serial *serial;
695 struct moschip_port *mos7720_port;
697 dbg("Entering %s", __func__);
699 serial = port->serial;
701 mos7720_port = usb_get_serial_port_data(port);
702 if (mos7720_port == NULL)
703 return;
705 if (break_state == -1)
706 data = mos7720_port->shadowLCR | UART_LCR_SBC;
707 else
708 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
710 mos7720_port->shadowLCR = data;
711 send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
712 0x03, &data);
714 return;
718 * mos7720_write_room
719 * this function is called by the tty driver when it wants to know how many
720 * bytes of data we can accept for a specific port.
721 * If successful, we return the amount of room that we have for this port
722 * Otherwise we return a negative error number.
724 static int mos7720_write_room(struct tty_struct *tty)
726 struct usb_serial_port *port = tty->driver_data;
727 struct moschip_port *mos7720_port;
728 int room = 0;
729 int i;
731 dbg("%s:entering ...........", __func__);
733 mos7720_port = usb_get_serial_port_data(port);
734 if (mos7720_port == NULL) {
735 dbg("%s:leaving ...........", __func__);
736 return -ENODEV;
739 /* FIXME: Locking */
740 for (i = 0; i < NUM_URBS; ++i) {
741 if (mos7720_port->write_urb_pool[i] &&
742 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
743 room += URB_TRANSFER_BUFFER_SIZE;
746 dbg("%s - returns %d", __func__, room);
747 return room;
750 static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
751 const unsigned char *data, int count)
753 int status;
754 int i;
755 int bytes_sent = 0;
756 int transfer_size;
758 struct moschip_port *mos7720_port;
759 struct usb_serial *serial;
760 struct urb *urb;
761 const unsigned char *current_position = data;
763 dbg("%s:entering ...........", __func__);
765 serial = port->serial;
767 mos7720_port = usb_get_serial_port_data(port);
768 if (mos7720_port == NULL) {
769 dbg("mos7720_port is NULL");
770 return -ENODEV;
773 /* try to find a free urb in the list */
774 urb = NULL;
776 for (i = 0; i < NUM_URBS; ++i) {
777 if (mos7720_port->write_urb_pool[i] &&
778 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
779 urb = mos7720_port->write_urb_pool[i];
780 dbg("URB:%d", i);
781 break;
785 if (urb == NULL) {
786 dbg("%s - no more free urbs", __func__);
787 goto exit;
790 if (urb->transfer_buffer == NULL) {
791 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
792 GFP_KERNEL);
793 if (urb->transfer_buffer == NULL) {
794 dev_err(&port->dev, "%s no more kernel memory...\n",
795 __func__);
796 goto exit;
799 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
801 memcpy(urb->transfer_buffer, current_position, transfer_size);
802 usb_serial_debug_data(debug, &port->dev, __func__, transfer_size,
803 urb->transfer_buffer);
805 /* fill urb with data and submit */
806 usb_fill_bulk_urb(urb, serial->dev,
807 usb_sndbulkpipe(serial->dev,
808 port->bulk_out_endpointAddress),
809 urb->transfer_buffer, transfer_size,
810 mos7720_bulk_out_data_callback, mos7720_port);
812 /* send it down the pipe */
813 status = usb_submit_urb(urb, GFP_ATOMIC);
814 if (status) {
815 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
816 "with status = %d\n", __func__, status);
817 bytes_sent = status;
818 goto exit;
820 bytes_sent = transfer_size;
822 exit:
823 return bytes_sent;
826 static void mos7720_throttle(struct tty_struct *tty)
828 struct usb_serial_port *port = tty->driver_data;
829 struct moschip_port *mos7720_port;
830 int status;
832 dbg("%s- port %d", __func__, port->number);
834 mos7720_port = usb_get_serial_port_data(port);
836 if (mos7720_port == NULL)
837 return;
839 if (!mos7720_port->open) {
840 dbg("port not opened");
841 return;
844 dbg("%s: Entering ..........", __func__);
846 /* if we are implementing XON/XOFF, send the stop character */
847 if (I_IXOFF(tty)) {
848 unsigned char stop_char = STOP_CHAR(tty);
849 status = mos7720_write(tty, port, &stop_char, 1);
850 if (status <= 0)
851 return;
854 /* if we are implementing RTS/CTS, toggle that line */
855 if (tty->termios->c_cflag & CRTSCTS) {
856 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
857 status = send_mos_cmd(port->serial, MOS_WRITE,
858 port->number - port->serial->minor,
859 UART_MCR, &mos7720_port->shadowMCR);
860 if (status != 0)
861 return;
865 static void mos7720_unthrottle(struct tty_struct *tty)
867 struct usb_serial_port *port = tty->driver_data;
868 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
869 int status;
871 if (mos7720_port == NULL)
872 return;
874 if (!mos7720_port->open) {
875 dbg("%s - port not opened", __func__);
876 return;
879 dbg("%s: Entering ..........", __func__);
881 /* if we are implementing XON/XOFF, send the start character */
882 if (I_IXOFF(tty)) {
883 unsigned char start_char = START_CHAR(tty);
884 status = mos7720_write(tty, port, &start_char, 1);
885 if (status <= 0)
886 return;
889 /* if we are implementing RTS/CTS, toggle that line */
890 if (tty->termios->c_cflag & CRTSCTS) {
891 mos7720_port->shadowMCR |= UART_MCR_RTS;
892 status = send_mos_cmd(port->serial, MOS_WRITE,
893 port->number - port->serial->minor,
894 UART_MCR, &mos7720_port->shadowMCR);
895 if (status != 0)
896 return;
900 static int set_higher_rates(struct moschip_port *mos7720_port,
901 unsigned int baud)
903 unsigned char data;
904 struct usb_serial_port *port;
905 struct usb_serial *serial;
906 int port_number;
908 if (mos7720_port == NULL)
909 return -EINVAL;
911 port = mos7720_port->port;
912 serial = port->serial;
914 /***********************************************
915 * Init Sequence for higher rates
916 ***********************************************/
917 dbg("Sending Setting Commands ..........");
918 port_number = port->number - port->serial->minor;
920 data = 0x000;
921 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
922 data = 0x000;
923 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
924 data = 0x0CF;
925 send_mos_cmd(serial, MOS_WRITE, port->number, 0x02, &data);
926 data = 0x00b;
927 mos7720_port->shadowMCR = data;
928 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
929 data = 0x00b;
930 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
932 data = 0x000;
933 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
934 data = 0x000;
935 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
938 /***********************************************
939 * Set for higher rates *
940 ***********************************************/
942 data = baud * 0x10;
943 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
945 data = 0x003;
946 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
947 data = 0x003;
948 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
950 data = 0x02b;
951 mos7720_port->shadowMCR = data;
952 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
953 data = 0x02b;
954 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
956 /***********************************************
957 * Set DLL/DLM
958 ***********************************************/
960 data = mos7720_port->shadowLCR | UART_LCR_DLAB;
961 mos7720_port->shadowLCR = data;
962 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
964 data = 0x001; /* DLL */
965 send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
966 data = 0x000; /* DLM */
967 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
969 data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
970 mos7720_port->shadowLCR = data;
971 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
973 return 0;
976 /* baud rate information */
977 struct divisor_table_entry {
978 __u32 baudrate;
979 __u16 divisor;
982 /* Define table of divisors for moschip 7720 hardware *
983 * These assume a 3.6864MHz crystal, the standard /16, and *
984 * MCR.7 = 0. */
985 static struct divisor_table_entry divisor_table[] = {
986 { 50, 2304},
987 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
988 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
989 { 150, 768},
990 { 300, 384},
991 { 600, 192},
992 { 1200, 96},
993 { 1800, 64},
994 { 2400, 48},
995 { 4800, 24},
996 { 7200, 16},
997 { 9600, 12},
998 { 19200, 6},
999 { 38400, 3},
1000 { 57600, 2},
1001 { 115200, 1},
1004 /*****************************************************************************
1005 * calc_baud_rate_divisor
1006 * this function calculates the proper baud rate divisor for the specified
1007 * baud rate.
1008 *****************************************************************************/
1009 static int calc_baud_rate_divisor(int baudrate, int *divisor)
1011 int i;
1012 __u16 custom;
1013 __u16 round1;
1014 __u16 round;
1017 dbg("%s - %d", __func__, baudrate);
1019 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
1020 if (divisor_table[i].baudrate == baudrate) {
1021 *divisor = divisor_table[i].divisor;
1022 return 0;
1026 /* After trying for all the standard baud rates *
1027 * Try calculating the divisor for this baud rate */
1028 if (baudrate > 75 && baudrate < 230400) {
1029 /* get the divisor */
1030 custom = (__u16)(230400L / baudrate);
1032 /* Check for round off */
1033 round1 = (__u16)(2304000L / baudrate);
1034 round = (__u16)(round1 - (custom * 10));
1035 if (round > 4)
1036 custom++;
1037 *divisor = custom;
1039 dbg("Baud %d = %d", baudrate, custom);
1040 return 0;
1043 dbg("Baud calculation Failed...");
1044 return -EINVAL;
1048 * send_cmd_write_baud_rate
1049 * this function sends the proper command to change the baud rate of the
1050 * specified port.
1052 static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1053 int baudrate)
1055 struct usb_serial_port *port;
1056 struct usb_serial *serial;
1057 int divisor;
1058 int status;
1059 unsigned char data;
1060 unsigned char number;
1062 if (mos7720_port == NULL)
1063 return -1;
1065 port = mos7720_port->port;
1066 serial = port->serial;
1068 dbg("%s: Entering ..........", __func__);
1070 number = port->number - port->serial->minor;
1071 dbg("%s - port = %d, baud = %d", __func__, port->number, baudrate);
1073 /* Calculate the Divisor */
1074 status = calc_baud_rate_divisor(baudrate, &divisor);
1075 if (status) {
1076 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1077 return status;
1080 /* Enable access to divisor latch */
1081 data = mos7720_port->shadowLCR | UART_LCR_DLAB;
1082 mos7720_port->shadowLCR = data;
1083 send_mos_cmd(serial, MOS_WRITE, number, UART_LCR, &data);
1085 /* Write the divisor */
1086 data = ((unsigned char)(divisor & 0xff));
1087 send_mos_cmd(serial, MOS_WRITE, number, 0x00, &data);
1089 data = ((unsigned char)((divisor & 0xff00) >> 8));
1090 send_mos_cmd(serial, MOS_WRITE, number, 0x01, &data);
1092 /* Disable access to divisor latch */
1093 data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1094 mos7720_port->shadowLCR = data;
1095 send_mos_cmd(serial, MOS_WRITE, number, 0x03, &data);
1097 return status;
1101 * change_port_settings
1102 * This routine is called to set the UART on the device to match
1103 * the specified new settings.
1105 static void change_port_settings(struct tty_struct *tty,
1106 struct moschip_port *mos7720_port,
1107 struct ktermios *old_termios)
1109 struct usb_serial_port *port;
1110 struct usb_serial *serial;
1111 int baud;
1112 unsigned cflag;
1113 unsigned iflag;
1114 __u8 mask = 0xff;
1115 __u8 lData;
1116 __u8 lParity;
1117 __u8 lStop;
1118 int status;
1119 int port_number;
1120 char data;
1122 if (mos7720_port == NULL)
1123 return ;
1125 port = mos7720_port->port;
1126 serial = port->serial;
1127 port_number = port->number - port->serial->minor;
1129 dbg("%s - port %d", __func__, port->number);
1131 if (!mos7720_port->open) {
1132 dbg("%s - port not opened", __func__);
1133 return;
1136 dbg("%s: Entering ..........", __func__);
1138 lData = UART_LCR_WLEN8;
1139 lStop = 0x00; /* 1 stop bit */
1140 lParity = 0x00; /* No parity */
1142 cflag = tty->termios->c_cflag;
1143 iflag = tty->termios->c_iflag;
1145 /* Change the number of bits */
1146 switch (cflag & CSIZE) {
1147 case CS5:
1148 lData = UART_LCR_WLEN5;
1149 mask = 0x1f;
1150 break;
1152 case CS6:
1153 lData = UART_LCR_WLEN6;
1154 mask = 0x3f;
1155 break;
1157 case CS7:
1158 lData = UART_LCR_WLEN7;
1159 mask = 0x7f;
1160 break;
1161 default:
1162 case CS8:
1163 lData = UART_LCR_WLEN8;
1164 break;
1167 /* Change the Parity bit */
1168 if (cflag & PARENB) {
1169 if (cflag & PARODD) {
1170 lParity = UART_LCR_PARITY;
1171 dbg("%s - parity = odd", __func__);
1172 } else {
1173 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
1174 dbg("%s - parity = even", __func__);
1177 } else {
1178 dbg("%s - parity = none", __func__);
1181 if (cflag & CMSPAR)
1182 lParity = lParity | 0x20;
1184 /* Change the Stop bit */
1185 if (cflag & CSTOPB) {
1186 lStop = UART_LCR_STOP;
1187 dbg("%s - stop bits = 2", __func__);
1188 } else {
1189 lStop = 0x00;
1190 dbg("%s - stop bits = 1", __func__);
1193 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1194 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1195 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
1197 /* Update the LCR with the correct value */
1198 mos7720_port->shadowLCR &=
1199 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1200 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1203 /* Disable Interrupts */
1204 data = 0x00;
1205 send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
1206 UART_IER, &data);
1208 data = 0x00;
1209 send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
1211 data = 0xcf;
1212 send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
1214 /* Send the updated LCR value to the mos7720 */
1215 data = mos7720_port->shadowLCR;
1216 send_mos_cmd(serial, MOS_WRITE, port_number, UART_LCR, &data);
1218 data = 0x00b;
1219 mos7720_port->shadowMCR = data;
1220 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
1221 data = 0x00b;
1222 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
1224 /* set up the MCR register and send it to the mos7720 */
1225 mos7720_port->shadowMCR = UART_MCR_OUT2;
1226 if (cflag & CBAUD)
1227 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1229 if (cflag & CRTSCTS) {
1230 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
1231 /* To set hardware flow control to the specified *
1232 * serial port, in SP1/2_CONTROL_REG */
1233 if (port->number) {
1234 data = 0x001;
1235 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1236 0x08, &data);
1237 } else {
1238 data = 0x002;
1239 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1240 0x08, &data);
1242 } else {
1243 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1246 data = mos7720_port->shadowMCR;
1247 send_mos_cmd(serial, MOS_WRITE, port_number, UART_MCR, &data);
1249 /* Determine divisor based on baud rate */
1250 baud = tty_get_baud_rate(tty);
1251 if (!baud) {
1252 /* pick a default, any default... */
1253 dbg("Picked default baud...");
1254 baud = 9600;
1257 if (baud >= 230400) {
1258 set_higher_rates(mos7720_port, baud);
1259 /* Enable Interrupts */
1260 data = 0x0c;
1261 send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1262 return;
1265 dbg("%s - baud rate = %d", __func__, baud);
1266 status = send_cmd_write_baud_rate(mos7720_port, baud);
1267 /* FIXME: needs to write actual resulting baud back not just
1268 blindly do so */
1269 if (cflag & CBAUD)
1270 tty_encode_baud_rate(tty, baud, baud);
1271 /* Enable Interrupts */
1272 data = 0x0c;
1273 send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1275 if (port->read_urb->status != -EINPROGRESS) {
1276 port->read_urb->dev = serial->dev;
1278 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1279 if (status)
1280 dbg("usb_submit_urb(read bulk) failed, status = %d",
1281 status);
1283 return;
1287 * mos7720_set_termios
1288 * this function is called by the tty driver when it wants to change the
1289 * termios structure.
1291 static void mos7720_set_termios(struct tty_struct *tty,
1292 struct usb_serial_port *port, struct ktermios *old_termios)
1294 int status;
1295 unsigned int cflag;
1296 struct usb_serial *serial;
1297 struct moschip_port *mos7720_port;
1299 serial = port->serial;
1301 mos7720_port = usb_get_serial_port_data(port);
1303 if (mos7720_port == NULL)
1304 return;
1306 if (!mos7720_port->open) {
1307 dbg("%s - port not opened", __func__);
1308 return;
1311 dbg("setting termios - ASPIRE");
1313 cflag = tty->termios->c_cflag;
1315 dbg("%s - cflag %08x iflag %08x", __func__,
1316 tty->termios->c_cflag,
1317 RELEVANT_IFLAG(tty->termios->c_iflag));
1319 dbg("%s - old cflag %08x old iflag %08x", __func__,
1320 old_termios->c_cflag,
1321 RELEVANT_IFLAG(old_termios->c_iflag));
1323 dbg("%s - port %d", __func__, port->number);
1325 /* change the port settings to the new ones specified */
1326 change_port_settings(tty, mos7720_port, old_termios);
1328 if (!port->read_urb) {
1329 dbg("URB KILLED !!!!!");
1330 return;
1333 if (port->read_urb->status != -EINPROGRESS) {
1334 port->read_urb->dev = serial->dev;
1335 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1336 if (status)
1337 dbg("usb_submit_urb(read bulk) failed, status = %d",
1338 status);
1340 return;
1344 * get_lsr_info - get line status register info
1346 * Purpose: Let user call ioctl() to get info when the UART physically
1347 * is emptied. On bus types like RS485, the transmitter must
1348 * release the bus after transmitting. This must be done when
1349 * the transmit shift register is empty, not be done when the
1350 * transmit holding register is empty. This functionality
1351 * allows an RS485 driver to be written in user space.
1353 static int get_lsr_info(struct tty_struct *tty,
1354 struct moschip_port *mos7720_port, unsigned int __user *value)
1356 struct usb_serial_port *port = tty->driver_data;
1357 unsigned int result = 0;
1358 unsigned char data = 0;
1359 int port_number = port->number - port->serial->minor;
1360 int count;
1362 count = mos7720_chars_in_buffer(tty);
1363 if (count == 0) {
1364 send_mos_cmd(port->serial, MOS_READ, port_number,
1365 UART_LSR, &data);
1366 if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1367 == (UART_LSR_TEMT | UART_LSR_THRE)) {
1368 dbg("%s -- Empty", __func__);
1369 result = TIOCSER_TEMT;
1372 if (copy_to_user(value, &result, sizeof(int)))
1373 return -EFAULT;
1374 return 0;
1377 static int mos7720_tiocmget(struct tty_struct *tty, struct file *file)
1379 struct usb_serial_port *port = tty->driver_data;
1380 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1381 unsigned int result = 0;
1382 unsigned int mcr ;
1383 unsigned int msr ;
1385 dbg("%s - port %d", __func__, port->number);
1387 mcr = mos7720_port->shadowMCR;
1388 msr = mos7720_port->shadowMSR;
1390 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1391 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1392 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1393 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
1394 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1395 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
1397 dbg("%s -- %x", __func__, result);
1399 return result;
1402 static int mos7720_tiocmset(struct tty_struct *tty, struct file *file,
1403 unsigned int set, unsigned int clear)
1405 struct usb_serial_port *port = tty->driver_data;
1406 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1407 unsigned int mcr ;
1408 unsigned char lmcr;
1410 dbg("%s - port %d", __func__, port->number);
1411 dbg("he was at tiocmget");
1413 mcr = mos7720_port->shadowMCR;
1415 if (set & TIOCM_RTS)
1416 mcr |= UART_MCR_RTS;
1417 if (set & TIOCM_DTR)
1418 mcr |= UART_MCR_DTR;
1419 if (set & TIOCM_LOOP)
1420 mcr |= UART_MCR_LOOP;
1422 if (clear & TIOCM_RTS)
1423 mcr &= ~UART_MCR_RTS;
1424 if (clear & TIOCM_DTR)
1425 mcr &= ~UART_MCR_DTR;
1426 if (clear & TIOCM_LOOP)
1427 mcr &= ~UART_MCR_LOOP;
1429 mos7720_port->shadowMCR = mcr;
1430 lmcr = mos7720_port->shadowMCR;
1432 send_mos_cmd(port->serial, MOS_WRITE,
1433 port->number - port->serial->minor, UART_MCR, &lmcr);
1435 return 0;
1438 static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1439 unsigned int __user *value)
1441 unsigned int mcr ;
1442 unsigned int arg;
1443 unsigned char data;
1445 struct usb_serial_port *port;
1447 if (mos7720_port == NULL)
1448 return -1;
1450 port = (struct usb_serial_port *)mos7720_port->port;
1451 mcr = mos7720_port->shadowMCR;
1453 if (copy_from_user(&arg, value, sizeof(int)))
1454 return -EFAULT;
1456 switch (cmd) {
1457 case TIOCMBIS:
1458 if (arg & TIOCM_RTS)
1459 mcr |= UART_MCR_RTS;
1460 if (arg & TIOCM_DTR)
1461 mcr |= UART_MCR_RTS;
1462 if (arg & TIOCM_LOOP)
1463 mcr |= UART_MCR_LOOP;
1464 break;
1466 case TIOCMBIC:
1467 if (arg & TIOCM_RTS)
1468 mcr &= ~UART_MCR_RTS;
1469 if (arg & TIOCM_DTR)
1470 mcr &= ~UART_MCR_RTS;
1471 if (arg & TIOCM_LOOP)
1472 mcr &= ~UART_MCR_LOOP;
1473 break;
1477 mos7720_port->shadowMCR = mcr;
1479 data = mos7720_port->shadowMCR;
1480 send_mos_cmd(port->serial, MOS_WRITE,
1481 port->number - port->serial->minor, UART_MCR, &data);
1483 return 0;
1486 static int get_serial_info(struct moschip_port *mos7720_port,
1487 struct serial_struct __user *retinfo)
1489 struct serial_struct tmp;
1491 if (!retinfo)
1492 return -EFAULT;
1494 memset(&tmp, 0, sizeof(tmp));
1496 tmp.type = PORT_16550A;
1497 tmp.line = mos7720_port->port->serial->minor;
1498 tmp.port = mos7720_port->port->number;
1499 tmp.irq = 0;
1500 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1501 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1502 tmp.baud_base = 9600;
1503 tmp.close_delay = 5*HZ;
1504 tmp.closing_wait = 30*HZ;
1506 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1507 return -EFAULT;
1508 return 0;
1511 static int mos7720_ioctl(struct tty_struct *tty, struct file *file,
1512 unsigned int cmd, unsigned long arg)
1514 struct usb_serial_port *port = tty->driver_data;
1515 struct moschip_port *mos7720_port;
1516 struct async_icount cnow;
1517 struct async_icount cprev;
1518 struct serial_icounter_struct icount;
1520 mos7720_port = usb_get_serial_port_data(port);
1521 if (mos7720_port == NULL)
1522 return -ENODEV;
1524 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
1526 switch (cmd) {
1527 case TIOCSERGETLSR:
1528 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
1529 return get_lsr_info(tty, mos7720_port,
1530 (unsigned int __user *)arg);
1531 return 0;
1533 /* FIXME: These should be using the mode methods */
1534 case TIOCMBIS:
1535 case TIOCMBIC:
1536 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET",
1537 __func__, port->number);
1538 return set_modem_info(mos7720_port, cmd,
1539 (unsigned int __user *)arg);
1541 case TIOCGSERIAL:
1542 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
1543 return get_serial_info(mos7720_port,
1544 (struct serial_struct __user *)arg);
1546 case TIOCMIWAIT:
1547 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
1548 cprev = mos7720_port->icount;
1549 while (1) {
1550 if (signal_pending(current))
1551 return -ERESTARTSYS;
1552 cnow = mos7720_port->icount;
1553 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1554 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1555 return -EIO; /* no change => error */
1556 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1557 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1558 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1559 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1560 return 0;
1562 cprev = cnow;
1564 /* NOTREACHED */
1565 break;
1567 case TIOCGICOUNT:
1568 cnow = mos7720_port->icount;
1569 icount.cts = cnow.cts;
1570 icount.dsr = cnow.dsr;
1571 icount.rng = cnow.rng;
1572 icount.dcd = cnow.dcd;
1573 icount.rx = cnow.rx;
1574 icount.tx = cnow.tx;
1575 icount.frame = cnow.frame;
1576 icount.overrun = cnow.overrun;
1577 icount.parity = cnow.parity;
1578 icount.brk = cnow.brk;
1579 icount.buf_overrun = cnow.buf_overrun;
1581 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
1582 port->number, icount.rx, icount.tx);
1583 if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
1584 return -EFAULT;
1585 return 0;
1588 return -ENOIOCTLCMD;
1591 static int mos7720_startup(struct usb_serial *serial)
1593 struct moschip_serial *mos7720_serial;
1594 struct moschip_port *mos7720_port;
1595 struct usb_device *dev;
1596 int i;
1597 char data;
1598 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
1600 dbg("%s: Entering ..........", __func__);
1602 if (!serial) {
1603 dbg("Invalid Handler");
1604 return -ENODEV;
1607 dev = serial->dev;
1609 /* create our private serial structure */
1610 mos7720_serial = kzalloc(sizeof(struct moschip_serial), GFP_KERNEL);
1611 if (mos7720_serial == NULL) {
1612 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
1613 return -ENOMEM;
1616 usb_set_serial_data(serial, mos7720_serial);
1619 * The 7715 uses the first bulk in/out endpoint pair for the parallel
1620 * port, and the second for the serial port. Because the usbserial core
1621 * assumes both pairs are serial ports, we must engage in a bit of
1622 * subterfuge and swap the pointers for ports 0 and 1 in order to make
1623 * port 0 point to the serial port. However, both moschip devices use a
1624 * single interrupt-in endpoint for both ports (as mentioned a little
1625 * further down), and this endpoint was assigned to port 0. So after
1626 * the swap, we must copy the interrupt endpoint elements from port 1
1627 * (as newly assigned) to port 0, and null out port 1 pointers.
1629 if (product == MOSCHIP_DEVICE_ID_7715) {
1630 struct usb_serial_port *tmp = serial->port[0];
1631 serial->port[0] = serial->port[1];
1632 serial->port[1] = tmp;
1633 serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb;
1634 serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer;
1635 serial->port[0]->interrupt_in_endpointAddress =
1636 tmp->interrupt_in_endpointAddress;
1637 serial->port[1]->interrupt_in_urb = NULL;
1638 serial->port[1]->interrupt_in_buffer = NULL;
1641 /* we set up the pointers to the endpoints in the mos7720_open *
1642 * function, as the structures aren't created yet. */
1644 /* set up port private structures */
1645 for (i = 0; i < serial->num_ports; ++i) {
1646 mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
1647 if (mos7720_port == NULL) {
1648 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
1649 usb_set_serial_data(serial, NULL);
1650 kfree(mos7720_serial);
1651 return -ENOMEM;
1654 /* Initialize all port interrupt end point to port 0 int
1655 * endpoint. Our device has only one interrupt endpoint
1656 * common to all ports */
1657 serial->port[i]->interrupt_in_endpointAddress =
1658 serial->port[0]->interrupt_in_endpointAddress;
1660 mos7720_port->port = serial->port[i];
1661 usb_set_serial_port_data(serial->port[i], mos7720_port);
1663 dbg("port number is %d", serial->port[i]->number);
1664 dbg("serial number is %d", serial->minor);
1668 /* setting configuration feature to one */
1669 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1670 (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5*HZ);
1672 /* LSR For Port 1 */
1673 send_mos_cmd(serial, MOS_READ, 0x00, UART_LSR, &data);
1674 dbg("LSR:%x", data);
1676 /* LSR For Port 2 */
1677 send_mos_cmd(serial, MOS_READ, 0x01, UART_LSR, &data);
1678 dbg("LSR:%x", data);
1680 return 0;
1683 static void mos7720_release(struct usb_serial *serial)
1685 int i;
1687 /* free private structure allocated for serial port */
1688 for (i = 0; i < serial->num_ports; ++i)
1689 kfree(usb_get_serial_port_data(serial->port[i]));
1691 /* free private structure allocated for serial device */
1692 kfree(usb_get_serial_data(serial));
1695 static struct usb_driver usb_driver = {
1696 .name = "moschip7720",
1697 .probe = usb_serial_probe,
1698 .disconnect = usb_serial_disconnect,
1699 .id_table = moschip_port_id_table,
1700 .no_dynamic_id = 1,
1703 static struct usb_serial_driver moschip7720_2port_driver = {
1704 .driver = {
1705 .owner = THIS_MODULE,
1706 .name = "moschip7720",
1708 .description = "Moschip 2 port adapter",
1709 .usb_driver = &usb_driver,
1710 .id_table = moschip_port_id_table,
1711 .calc_num_ports = mos77xx_calc_num_ports,
1712 .open = mos7720_open,
1713 .close = mos7720_close,
1714 .throttle = mos7720_throttle,
1715 .unthrottle = mos7720_unthrottle,
1716 .probe = mos77xx_probe,
1717 .attach = mos7720_startup,
1718 .release = mos7720_release,
1719 .ioctl = mos7720_ioctl,
1720 .tiocmget = mos7720_tiocmget,
1721 .tiocmset = mos7720_tiocmset,
1722 .set_termios = mos7720_set_termios,
1723 .write = mos7720_write,
1724 .write_room = mos7720_write_room,
1725 .chars_in_buffer = mos7720_chars_in_buffer,
1726 .break_ctl = mos7720_break,
1727 .read_bulk_callback = mos7720_bulk_in_callback,
1728 .read_int_callback = NULL /* dynamically assigned in probe() */
1731 static int __init moschip7720_init(void)
1733 int retval;
1735 dbg("%s: Entering ..........", __func__);
1737 /* Register with the usb serial */
1738 retval = usb_serial_register(&moschip7720_2port_driver);
1739 if (retval)
1740 goto failed_port_device_register;
1742 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1743 DRIVER_DESC "\n");
1745 /* Register with the usb */
1746 retval = usb_register(&usb_driver);
1747 if (retval)
1748 goto failed_usb_register;
1750 return 0;
1752 failed_usb_register:
1753 usb_serial_deregister(&moschip7720_2port_driver);
1755 failed_port_device_register:
1756 return retval;
1759 static void __exit moschip7720_exit(void)
1761 usb_deregister(&usb_driver);
1762 usb_serial_deregister(&moschip7720_2port_driver);
1765 module_init(moschip7720_init);
1766 module_exit(moschip7720_exit);
1768 /* Module information */
1769 MODULE_AUTHOR(DRIVER_AUTHOR);
1770 MODULE_DESCRIPTION(DRIVER_DESC);
1771 MODULE_LICENSE("GPL");
1773 module_param(debug, bool, S_IRUGO | S_IWUSR);
1774 MODULE_PARM_DESC(debug, "Debug enabled or not");