USB: serial: add support for serial port on the moschip 7715
[pandora-kernel.git] / drivers / usb / serial / mos7720.c
1 /*
2  * mos7720.c
3  *   Controls the Moschip 7720 usb to dual port serial convertor
4  *
5  * Copyright 2006 Moschip Semiconductor Tech. Ltd.
6  *
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.
10  *
11  * Developed by:
12  *      Vijaya Kumar <vijaykumar.gn@gmail.com>
13  *      Ajay Kumar <naanuajay@yahoo.com>
14  *      Gurudeva <ngurudeva@yahoo.com>
15  *
16  * Cleaned up from the original by:
17  *      Greg Kroah-Hartman <gregkh@suse.de>
18  *
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>
22  */
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>
37
38
39 /*
40  * Version Information
41  */
42 #define DRIVER_VERSION "1.0.0.4F"
43 #define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
44 #define DRIVER_DESC "Moschip USB Serial Driver"
45
46 /* default urb timeout */
47 #define MOS_WDR_TIMEOUT (HZ * 5)
48
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
55
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
62
63 #define NUM_URBS                        16      /* URB Count */
64 #define URB_TRANSFER_BUFFER_SIZE        32      /* URB Size */
65
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];
75 };
76
77 /* This structure holds all of the individual serial device information */
78 struct moschip_serial {
79         int interrupt_started;
80 };
81
82 static int debug;
83
84 static struct usb_serial_driver moschip7720_2port_driver;
85
86 #define USB_VENDOR_ID_MOSCHIP           0x9710
87 #define MOSCHIP_DEVICE_ID_7720          0x7720
88 #define MOSCHIP_DEVICE_ID_7715          0x7715
89
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 */
94 };
95 MODULE_DEVICE_TABLE(usb, moschip_port_id_table);
96
97
98 /*
99  * mos7720_interrupt_callback
100  *      this is the callback function for when we have received data on the
101  *      interrupt endpoint.
102  */
103 static void mos7720_interrupt_callback(struct urb *urb)
104 {
105         int result;
106         int length;
107         int status = urb->status;
108         __u8 *data;
109         __u8 sp1;
110         __u8 sp2;
111
112         dbg("%s", " : Entering\n");
113
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;
129         }
130
131         length = urb->actual_length;
132         data = urb->transfer_buffer;
133
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 */
139
140         /* the above description is inverted
141          *      oneukum 2007-03-14 */
142
143         if (unlikely(length != 4)) {
144                 dbg("Wrong data !!!");
145                 return;
146         }
147
148         sp1 = data[3];
149         sp2 = data[2];
150
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;
166                 }
167
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;
179                 }
180         }
181
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;
189 }
190
191 /*
192  * mos7715_interrupt_callback
193  *      this is the 7715's callback function for when we have received data on
194  *      the interrupt endpoint.
195  */
196 static void mos7715_interrupt_callback(struct urb *urb)
197 {
198         int result;
199         int length;
200         int status = urb->status;
201         __u8 *data;
202         __u8 iir;
203
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;
219         }
220
221         length = urb->actual_length;
222         data = urb->transfer_buffer;
223
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 */
229
230         if (unlikely(length != 4)) {
231                 dbg("Wrong data !!!");
232                 return;
233         }
234
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;
248                 }
249         }
250
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;
258 }
259
260 /*
261  * mos7720_bulk_in_callback
262  *      this is the callback function for when we have received data on the
263  *      bulk in endpoint.
264  */
265 static void mos7720_bulk_in_callback(struct urb *urb)
266 {
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;
273
274         if (status) {
275                 dbg("nonzero read bulk status received: %d", status);
276                 return;
277         }
278
279         mos7720_port = urb->context;
280         if (!mos7720_port) {
281                 dbg("%s", "NULL mos7720_port pointer \n");
282                 return ;
283         }
284
285         port = mos7720_port->port;
286
287         dbg("Entering...%s", __func__);
288
289         data = urb->transfer_buffer;
290
291         tty = tty_port_tty_get(&port->port);
292         if (tty && urb->actual_length) {
293                 tty_buffer_request_room(tty, urb->actual_length);
294                 tty_insert_flip_string(tty, data, urb->actual_length);
295                 tty_flip_buffer_push(tty);
296         }
297         tty_kref_put(tty);
298
299         if (!port->read_urb) {
300                 dbg("URB KILLED !!!");
301                 return;
302         }
303
304         if (port->read_urb->status != -EINPROGRESS) {
305                 port->read_urb->dev = port->serial->dev;
306
307                 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
308                 if (retval)
309                         dbg("usb_submit_urb(read bulk) failed, retval = %d",
310                             retval);
311         }
312 }
313
314 /*
315  * mos7720_bulk_out_data_callback
316  *      this is the callback function for when we have finished sending serial
317  *      data on the bulk out endpoint.
318  */
319 static void mos7720_bulk_out_data_callback(struct urb *urb)
320 {
321         struct moschip_port *mos7720_port;
322         struct tty_struct *tty;
323         int status = urb->status;
324
325         if (status) {
326                 dbg("nonzero write bulk status received:%d", status);
327                 return;
328         }
329
330         mos7720_port = urb->context;
331         if (!mos7720_port) {
332                 dbg("NULL mos7720_port pointer");
333                 return ;
334         }
335
336         dbg("Entering .........");
337
338         tty = tty_port_tty_get(&mos7720_port->port->port);
339
340         if (tty && mos7720_port->open)
341                 tty_wakeup(tty);
342         tty_kref_put(tty);
343 }
344
345 /*
346  * send_mos_cmd
347  *      this function will be used for sending command to device
348  */
349 static int send_mos_cmd(struct usb_serial *serial, __u8 request, __u16 value,
350                         __u16 index, u8 *data)
351 {
352         int status;
353         u8 *buf;
354         u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
355
356         if (value < MOS_MAX_PORT) {
357                 if (product == MOSCHIP_DEVICE_ID_7715)
358                         value = 0x0200; /* identifies the 7715's serial port */
359                 else
360                         value = value*0x100+0x200;
361         } else {
362                 value = 0x0000;
363                 if ((product == MOSCHIP_DEVICE_ID_7715) &&
364                     (index != 0x08)) {
365                         dbg("serial->product== MOSCHIP_DEVICE_ID_7715");
366                         /* index = 0x01 ; */
367                 }
368         }
369
370         if (request == MOS_WRITE) {
371                 value = value + *data;
372                 status = usb_control_msg(serial->dev,
373                                 usb_sndctrlpipe(serial->dev, 0), MOS_WRITE,
374                                 0x40, value, index, NULL, 0, MOS_WDR_TIMEOUT);
375         } else {
376                 buf = kmalloc(1, GFP_KERNEL);
377                 if (!buf) {
378                         status = -ENOMEM;
379                         goto out;
380                 }
381                 status = usb_control_msg(serial->dev,
382                                 usb_rcvctrlpipe(serial->dev, 0), MOS_READ,
383                                 0xc0, value, index, buf, 1, MOS_WDR_TIMEOUT);
384                 *data = *buf;
385                 kfree(buf);
386         }
387 out:
388         if (status < 0)
389                 dbg("Command Write failed Value %x index %x\n", value, index);
390
391         return status;
392 }
393
394
395 /*
396  * mos77xx_probe
397  *      this function installs the appropriate read interrupt endpoint callback
398  *      depending on whether the device is a 7720 or 7715, thus avoiding costly
399  *      run-time checks in the high-frequency callback routine itself.
400  */
401 static int mos77xx_probe(struct usb_serial *serial,
402                          const struct usb_device_id *id)
403 {
404         if (id->idProduct == MOSCHIP_DEVICE_ID_7715)
405                 moschip7720_2port_driver.read_int_callback =
406                         mos7715_interrupt_callback;
407         else
408                 moschip7720_2port_driver.read_int_callback =
409                         mos7720_interrupt_callback;
410
411         return 0;
412 }
413
414 static int mos77xx_calc_num_ports(struct usb_serial *serial)
415 {
416         u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
417         if (product == MOSCHIP_DEVICE_ID_7715)
418                 return 1;
419
420         return 2;
421 }
422
423 static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
424 {
425         struct usb_serial *serial;
426         struct usb_serial_port *port0;
427         struct urb *urb;
428         struct moschip_serial *mos7720_serial;
429         struct moschip_port *mos7720_port;
430         int response;
431         int port_number;
432         char data;
433         int allocated_urbs = 0;
434         int j;
435
436         serial = port->serial;
437
438         mos7720_port = usb_get_serial_port_data(port);
439         if (mos7720_port == NULL)
440                 return -ENODEV;
441
442         port0 = serial->port[0];
443
444         mos7720_serial = usb_get_serial_data(serial);
445
446         if (mos7720_serial == NULL || port0 == NULL)
447                 return -ENODEV;
448
449         usb_clear_halt(serial->dev, port->write_urb->pipe);
450         usb_clear_halt(serial->dev, port->read_urb->pipe);
451
452         /* Initialising the write urb pool */
453         for (j = 0; j < NUM_URBS; ++j) {
454                 urb = usb_alloc_urb(0, GFP_KERNEL);
455                 mos7720_port->write_urb_pool[j] = urb;
456
457                 if (urb == NULL) {
458                         dev_err(&port->dev, "No more urbs???\n");
459                         continue;
460                 }
461
462                 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
463                                                GFP_KERNEL);
464                 if (!urb->transfer_buffer) {
465                         dev_err(&port->dev,
466                                 "%s-out of memory for urb buffers.\n",
467                                 __func__);
468                         usb_free_urb(mos7720_port->write_urb_pool[j]);
469                         mos7720_port->write_urb_pool[j] = NULL;
470                         continue;
471                 }
472                 allocated_urbs++;
473         }
474
475         if (!allocated_urbs)
476                 return -ENOMEM;
477
478          /* Initialize MCS7720 -- Write Init values to corresponding Registers
479           *
480           * Register Index
481           * 0 : THR/RHR
482           * 1 : IER
483           * 2 : FCR
484           * 3 : LCR
485           * 4 : MCR
486           * 5 : LSR
487           * 6 : MSR
488           * 7 : SPR
489           *
490           * 0x08 : SP1/2 Control Reg
491           */
492         port_number = port->number - port->serial->minor;
493         send_mos_cmd(port->serial, MOS_READ, port_number, UART_LSR, &data);
494         dbg("SS::%p LSR:%x\n", mos7720_port, data);
495
496         dbg("Check:Sending Command ..........");
497
498         data = 0x02;
499         send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x01, &data);
500         data = 0x02;
501         send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x02, &data);
502
503         data = 0x00;
504         send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
505         data = 0x00;
506         send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
507
508         data = 0xCF;
509         send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
510         data = 0x03;
511         mos7720_port->shadowLCR  = data;
512         send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
513         data = 0x0b;
514         mos7720_port->shadowMCR  = data;
515         send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
516         data = 0x0b;
517         send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
518
519         data = 0x00;
520         send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
521         data = 0x00;
522         send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
523
524 /*      data = 0x00;
525         send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, port_number + 1, &data);
526         data = 0x03;
527         send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
528         data = 0x00;
529         send_mos_cmd(port->serial, MOS_WRITE, MOS_MAX_PORT,
530                                                 port_number + 1, &data);
531 */
532         data = 0x00;
533         send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
534
535         data = data | (port->number - port->serial->minor + 1);
536         send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
537
538         data = 0x83;
539         mos7720_port->shadowLCR  = data;
540         send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
541         data = 0x0c;
542         send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
543         data = 0x00;
544         send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
545         data = 0x03;
546         mos7720_port->shadowLCR  = data;
547         send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
548         data = 0x0c;
549         send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
550         data = 0x0c;
551         send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
552
553         /* see if we've set up our endpoint info yet   *
554          * (can't set it up in mos7720_startup as the  *
555          * structures were not set up at that time.)   */
556         if (!mos7720_serial->interrupt_started) {
557                 dbg("Interrupt buffer NULL !!!");
558
559                 /* not set up yet, so do it now */
560                 mos7720_serial->interrupt_started = 1;
561
562                 dbg("To Submit URB !!!");
563
564                 /* set up our interrupt urb */
565                 usb_fill_int_urb(port0->interrupt_in_urb, serial->dev,
566                          usb_rcvintpipe(serial->dev,
567                                 port->interrupt_in_endpointAddress),
568                          port0->interrupt_in_buffer,
569                          port0->interrupt_in_urb->transfer_buffer_length,
570                          mos7720_interrupt_callback, mos7720_port,
571                          port0->interrupt_in_urb->interval);
572
573                 /* start interrupt read for this mos7720 this interrupt *
574                  * will continue as long as the mos7720 is connected    */
575                 dbg("Submit URB over !!!");
576                 response = usb_submit_urb(port0->interrupt_in_urb, GFP_KERNEL);
577                 if (response)
578                         dev_err(&port->dev,
579                                 "%s - Error %d submitting control urb\n",
580                                 __func__, response);
581         }
582
583         /* set up our bulk in urb */
584         usb_fill_bulk_urb(port->read_urb, serial->dev,
585                           usb_rcvbulkpipe(serial->dev,
586                                 port->bulk_in_endpointAddress),
587                           port->bulk_in_buffer,
588                           port->read_urb->transfer_buffer_length,
589                           mos7720_bulk_in_callback, mos7720_port);
590         response = usb_submit_urb(port->read_urb, GFP_KERNEL);
591         if (response)
592                 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
593                                                         __func__, response);
594
595         /* initialize our icount structure */
596         memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
597
598         /* initialize our port settings */
599         mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
600
601         /* send a open port command */
602         mos7720_port->open = 1;
603
604         return 0;
605 }
606
607 /*
608  * mos7720_chars_in_buffer
609  *      this function is called by the tty driver when it wants to know how many
610  *      bytes of data we currently have outstanding in the port (data that has
611  *      been written, but hasn't made it out the port yet)
612  *      If successful, we return the number of bytes left to be written in the
613  *      system,
614  *      Otherwise we return a negative error number.
615  */
616 static int mos7720_chars_in_buffer(struct tty_struct *tty)
617 {
618         struct usb_serial_port *port = tty->driver_data;
619         int i;
620         int chars = 0;
621         struct moschip_port *mos7720_port;
622
623         dbg("%s:entering ...........", __func__);
624
625         mos7720_port = usb_get_serial_port_data(port);
626         if (mos7720_port == NULL) {
627                 dbg("%s:leaving ...........", __func__);
628                 return 0;
629         }
630
631         for (i = 0; i < NUM_URBS; ++i) {
632                 if (mos7720_port->write_urb_pool[i] &&
633                     mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
634                         chars += URB_TRANSFER_BUFFER_SIZE;
635         }
636         dbg("%s - returns %d", __func__, chars);
637         return chars;
638 }
639
640 static void mos7720_close(struct usb_serial_port *port)
641 {
642         struct usb_serial *serial;
643         struct moschip_port *mos7720_port;
644         char data;
645         int j;
646
647         dbg("mos7720_close:entering...");
648
649         serial = port->serial;
650
651         mos7720_port = usb_get_serial_port_data(port);
652         if (mos7720_port == NULL)
653                 return;
654
655         for (j = 0; j < NUM_URBS; ++j)
656                 usb_kill_urb(mos7720_port->write_urb_pool[j]);
657
658         /* Freeing Write URBs */
659         for (j = 0; j < NUM_URBS; ++j) {
660                 if (mos7720_port->write_urb_pool[j]) {
661                         kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
662                         usb_free_urb(mos7720_port->write_urb_pool[j]);
663                 }
664         }
665
666         /* While closing port, shutdown all bulk read, write  *
667          * and interrupt read if they exists, otherwise nop   */
668         dbg("Shutdown bulk write");
669         usb_kill_urb(port->write_urb);
670         dbg("Shutdown bulk read");
671         usb_kill_urb(port->read_urb);
672
673         mutex_lock(&serial->disc_mutex);
674         /* these commands must not be issued if the device has
675          * been disconnected */
676         if (!serial->disconnected) {
677                 data = 0x00;
678                 send_mos_cmd(serial, MOS_WRITE,
679                         port->number - port->serial->minor, 0x04, &data);
680
681                 data = 0x00;
682                 send_mos_cmd(serial, MOS_WRITE,
683                         port->number - port->serial->minor, 0x01, &data);
684         }
685         mutex_unlock(&serial->disc_mutex);
686         mos7720_port->open = 0;
687
688         dbg("Leaving %s", __func__);
689 }
690
691 static void mos7720_break(struct tty_struct *tty, int break_state)
692 {
693         struct usb_serial_port *port = tty->driver_data;
694         unsigned char data;
695         struct usb_serial *serial;
696         struct moschip_port *mos7720_port;
697
698         dbg("Entering %s", __func__);
699
700         serial = port->serial;
701
702         mos7720_port = usb_get_serial_port_data(port);
703         if (mos7720_port == NULL)
704                 return;
705
706         if (break_state == -1)
707                 data = mos7720_port->shadowLCR | UART_LCR_SBC;
708         else
709                 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
710
711         mos7720_port->shadowLCR  = data;
712         send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
713                      0x03, &data);
714
715         return;
716 }
717
718 /*
719  * mos7720_write_room
720  *      this function is called by the tty driver when it wants to know how many
721  *      bytes of data we can accept for a specific port.
722  *      If successful, we return the amount of room that we have for this port
723  *      Otherwise we return a negative error number.
724  */
725 static int mos7720_write_room(struct tty_struct *tty)
726 {
727         struct usb_serial_port *port = tty->driver_data;
728         struct moschip_port *mos7720_port;
729         int room = 0;
730         int i;
731
732         dbg("%s:entering ...........", __func__);
733
734         mos7720_port = usb_get_serial_port_data(port);
735         if (mos7720_port == NULL) {
736                 dbg("%s:leaving ...........", __func__);
737                 return -ENODEV;
738         }
739
740         /* FIXME: Locking */
741         for (i = 0; i < NUM_URBS; ++i) {
742                 if (mos7720_port->write_urb_pool[i] &&
743                     mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
744                         room += URB_TRANSFER_BUFFER_SIZE;
745         }
746
747         dbg("%s - returns %d", __func__, room);
748         return room;
749 }
750
751 static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
752                                  const unsigned char *data, int count)
753 {
754         int status;
755         int i;
756         int bytes_sent = 0;
757         int transfer_size;
758
759         struct moschip_port *mos7720_port;
760         struct usb_serial *serial;
761         struct urb    *urb;
762         const unsigned char *current_position = data;
763
764         dbg("%s:entering ...........", __func__);
765
766         serial = port->serial;
767
768         mos7720_port = usb_get_serial_port_data(port);
769         if (mos7720_port == NULL) {
770                 dbg("mos7720_port is NULL");
771                 return -ENODEV;
772         }
773
774         /* try to find a free urb in the list */
775         urb = NULL;
776
777         for (i = 0; i < NUM_URBS; ++i) {
778                 if (mos7720_port->write_urb_pool[i] &&
779                     mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
780                         urb = mos7720_port->write_urb_pool[i];
781                         dbg("URB:%d", i);
782                         break;
783                 }
784         }
785
786         if (urb == NULL) {
787                 dbg("%s - no more free urbs", __func__);
788                 goto exit;
789         }
790
791         if (urb->transfer_buffer == NULL) {
792                 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
793                                                GFP_KERNEL);
794                 if (urb->transfer_buffer == NULL) {
795                         dev_err(&port->dev, "%s no more kernel memory...\n",
796                                 __func__);
797                         goto exit;
798                 }
799         }
800         transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
801
802         memcpy(urb->transfer_buffer, current_position, transfer_size);
803         usb_serial_debug_data(debug, &port->dev, __func__, transfer_size,
804                               urb->transfer_buffer);
805
806         /* fill urb with data and submit  */
807         usb_fill_bulk_urb(urb, serial->dev,
808                           usb_sndbulkpipe(serial->dev,
809                                         port->bulk_out_endpointAddress),
810                           urb->transfer_buffer, transfer_size,
811                           mos7720_bulk_out_data_callback, mos7720_port);
812
813         /* send it down the pipe */
814         status = usb_submit_urb(urb, GFP_ATOMIC);
815         if (status) {
816                 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
817                         "with status = %d\n", __func__, status);
818                 bytes_sent = status;
819                 goto exit;
820         }
821         bytes_sent = transfer_size;
822
823 exit:
824         return bytes_sent;
825 }
826
827 static void mos7720_throttle(struct tty_struct *tty)
828 {
829         struct usb_serial_port *port = tty->driver_data;
830         struct moschip_port *mos7720_port;
831         int status;
832
833         dbg("%s- port %d\n", __func__, port->number);
834
835         mos7720_port = usb_get_serial_port_data(port);
836
837         if (mos7720_port == NULL)
838                 return;
839
840         if (!mos7720_port->open) {
841                 dbg("port not opened");
842                 return;
843         }
844
845         dbg("%s: Entering ..........", __func__);
846
847         /* if we are implementing XON/XOFF, send the stop character */
848         if (I_IXOFF(tty)) {
849                 unsigned char stop_char = STOP_CHAR(tty);
850                 status = mos7720_write(tty, port, &stop_char, 1);
851                 if (status <= 0)
852                         return;
853         }
854
855         /* if we are implementing RTS/CTS, toggle that line */
856         if (tty->termios->c_cflag & CRTSCTS) {
857                 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
858                 status = send_mos_cmd(port->serial, MOS_WRITE,
859                                       port->number - port->serial->minor,
860                                       UART_MCR, &mos7720_port->shadowMCR);
861                 if (status != 0)
862                         return;
863         }
864 }
865
866 static void mos7720_unthrottle(struct tty_struct *tty)
867 {
868         struct usb_serial_port *port = tty->driver_data;
869         struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
870         int status;
871
872         if (mos7720_port == NULL)
873                 return;
874
875         if (!mos7720_port->open) {
876                 dbg("%s - port not opened", __func__);
877                 return;
878         }
879
880         dbg("%s: Entering ..........", __func__);
881
882         /* if we are implementing XON/XOFF, send the start character */
883         if (I_IXOFF(tty)) {
884                 unsigned char start_char = START_CHAR(tty);
885                 status = mos7720_write(tty, port, &start_char, 1);
886                 if (status <= 0)
887                         return;
888         }
889
890         /* if we are implementing RTS/CTS, toggle that line */
891         if (tty->termios->c_cflag & CRTSCTS) {
892                 mos7720_port->shadowMCR |= UART_MCR_RTS;
893                 status = send_mos_cmd(port->serial, MOS_WRITE,
894                                       port->number - port->serial->minor,
895                                       UART_MCR, &mos7720_port->shadowMCR);
896                 if (status != 0)
897                         return;
898         }
899 }
900
901 static int set_higher_rates(struct moschip_port *mos7720_port,
902                             unsigned int baud)
903 {
904         unsigned char data;
905         struct usb_serial_port *port;
906         struct usb_serial *serial;
907         int port_number;
908
909         if (mos7720_port == NULL)
910                 return -EINVAL;
911
912         port = mos7720_port->port;
913         serial = port->serial;
914
915          /***********************************************
916          *      Init Sequence for higher rates
917          ***********************************************/
918         dbg("Sending Setting Commands ..........");
919         port_number = port->number - port->serial->minor;
920
921         data = 0x000;
922         send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
923         data = 0x000;
924         send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
925         data = 0x0CF;
926         send_mos_cmd(serial, MOS_WRITE, port->number, 0x02, &data);
927         data = 0x00b;
928         mos7720_port->shadowMCR  = data;
929         send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
930         data = 0x00b;
931         send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
932
933         data = 0x000;
934         send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
935         data = 0x000;
936         send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
937
938
939         /***********************************************
940          *              Set for higher rates           *
941          ***********************************************/
942
943         data = baud * 0x10;
944         send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
945
946         data = 0x003;
947         send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
948         data = 0x003;
949         send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
950
951         data = 0x02b;
952         mos7720_port->shadowMCR  = data;
953         send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
954         data = 0x02b;
955         send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
956
957         /***********************************************
958          *              Set DLL/DLM
959          ***********************************************/
960
961         data = mos7720_port->shadowLCR | UART_LCR_DLAB;
962         mos7720_port->shadowLCR  = data;
963         send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
964
965         data =  0x001; /* DLL */
966         send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
967         data =  0x000; /* DLM */
968         send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
969
970         data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
971         mos7720_port->shadowLCR  = data;
972         send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
973
974         return 0;
975 }
976
977 /* baud rate information */
978 struct divisor_table_entry {
979         __u32  baudrate;
980         __u16  divisor;
981 };
982
983 /* Define table of divisors for moschip 7720 hardware      *
984  * These assume a 3.6864MHz crystal, the standard /16, and *
985  * MCR.7 = 0.                                              */
986 static struct divisor_table_entry divisor_table[] = {
987         {   50,         2304},
988         {   110,        1047},  /* 2094.545455 => 230450   => .0217 % over */
989         {   134,        857},   /* 1713.011152 => 230398.5 => .00065% under */
990         {   150,        768},
991         {   300,        384},
992         {   600,        192},
993         {   1200,       96},
994         {   1800,       64},
995         {   2400,       48},
996         {   4800,       24},
997         {   7200,       16},
998         {   9600,       12},
999         {   19200,      6},
1000         {   38400,      3},
1001         {   57600,      2},
1002         {   115200,     1},
1003 };
1004
1005 /*****************************************************************************
1006  * calc_baud_rate_divisor
1007  *      this function calculates the proper baud rate divisor for the specified
1008  *      baud rate.
1009  *****************************************************************************/
1010 static int calc_baud_rate_divisor(int baudrate, int *divisor)
1011 {
1012         int i;
1013         __u16 custom;
1014         __u16 round1;
1015         __u16 round;
1016
1017
1018         dbg("%s - %d", __func__, baudrate);
1019
1020         for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
1021                 if (divisor_table[i].baudrate == baudrate) {
1022                         *divisor = divisor_table[i].divisor;
1023                         return 0;
1024                 }
1025         }
1026
1027         /* After trying for all the standard baud rates    *
1028          * Try calculating the divisor for this baud rate  */
1029         if (baudrate > 75 &&  baudrate < 230400) {
1030                 /* get the divisor */
1031                 custom = (__u16)(230400L  / baudrate);
1032
1033                 /* Check for round off */
1034                 round1 = (__u16)(2304000L / baudrate);
1035                 round = (__u16)(round1 - (custom * 10));
1036                 if (round > 4)
1037                         custom++;
1038                 *divisor = custom;
1039
1040                 dbg("Baud %d = %d", baudrate, custom);
1041                 return 0;
1042         }
1043
1044         dbg("Baud calculation Failed...");
1045         return -EINVAL;
1046 }
1047
1048 /*
1049  * send_cmd_write_baud_rate
1050  *      this function sends the proper command to change the baud rate of the
1051  *      specified port.
1052  */
1053 static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1054                                     int baudrate)
1055 {
1056         struct usb_serial_port *port;
1057         struct usb_serial *serial;
1058         int divisor;
1059         int status;
1060         unsigned char data;
1061         unsigned char number;
1062
1063         if (mos7720_port == NULL)
1064                 return -1;
1065
1066         port = mos7720_port->port;
1067         serial = port->serial;
1068
1069         dbg("%s: Entering ..........", __func__);
1070
1071         number = port->number - port->serial->minor;
1072         dbg("%s - port = %d, baud = %d", __func__, port->number, baudrate);
1073
1074         /* Calculate the Divisor */
1075         status = calc_baud_rate_divisor(baudrate, &divisor);
1076         if (status) {
1077                 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1078                 return status;
1079         }
1080
1081         /* Enable access to divisor latch */
1082         data = mos7720_port->shadowLCR | UART_LCR_DLAB;
1083         mos7720_port->shadowLCR  = data;
1084         send_mos_cmd(serial, MOS_WRITE, number, UART_LCR, &data);
1085
1086         /* Write the divisor */
1087         data = ((unsigned char)(divisor & 0xff));
1088         send_mos_cmd(serial, MOS_WRITE, number, 0x00, &data);
1089
1090         data = ((unsigned char)((divisor & 0xff00) >> 8));
1091         send_mos_cmd(serial, MOS_WRITE, number, 0x01, &data);
1092
1093         /* Disable access to divisor latch */
1094         data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1095         mos7720_port->shadowLCR = data;
1096         send_mos_cmd(serial, MOS_WRITE, number, 0x03, &data);
1097
1098         return status;
1099 }
1100
1101 /*
1102  * change_port_settings
1103  *      This routine is called to set the UART on the device to match
1104  *      the specified new settings.
1105  */
1106 static void change_port_settings(struct tty_struct *tty,
1107                                  struct moschip_port *mos7720_port,
1108                                  struct ktermios *old_termios)
1109 {
1110         struct usb_serial_port *port;
1111         struct usb_serial *serial;
1112         int baud;
1113         unsigned cflag;
1114         unsigned iflag;
1115         __u8 mask = 0xff;
1116         __u8 lData;
1117         __u8 lParity;
1118         __u8 lStop;
1119         int status;
1120         int port_number;
1121         char data;
1122
1123         if (mos7720_port == NULL)
1124                 return ;
1125
1126         port = mos7720_port->port;
1127         serial = port->serial;
1128         port_number = port->number - port->serial->minor;
1129
1130         dbg("%s - port %d", __func__, port->number);
1131
1132         if (!mos7720_port->open) {
1133                 dbg("%s - port not opened", __func__);
1134                 return;
1135         }
1136
1137         dbg("%s: Entering ..........", __func__);
1138
1139         lData = UART_LCR_WLEN8;
1140         lStop = 0x00;   /* 1 stop bit */
1141         lParity = 0x00; /* No parity */
1142
1143         cflag = tty->termios->c_cflag;
1144         iflag = tty->termios->c_iflag;
1145
1146         /* Change the number of bits */
1147         switch (cflag & CSIZE) {
1148         case CS5:
1149                 lData = UART_LCR_WLEN5;
1150                 mask = 0x1f;
1151                 break;
1152
1153         case CS6:
1154                 lData = UART_LCR_WLEN6;
1155                 mask = 0x3f;
1156                 break;
1157
1158         case CS7:
1159                 lData = UART_LCR_WLEN7;
1160                 mask = 0x7f;
1161                 break;
1162         default:
1163         case CS8:
1164                 lData = UART_LCR_WLEN8;
1165                 break;
1166         }
1167
1168         /* Change the Parity bit */
1169         if (cflag & PARENB) {
1170                 if (cflag & PARODD) {
1171                         lParity = UART_LCR_PARITY;
1172                         dbg("%s - parity = odd", __func__);
1173                 } else {
1174                         lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
1175                         dbg("%s - parity = even", __func__);
1176                 }
1177
1178         } else {
1179                 dbg("%s - parity = none", __func__);
1180         }
1181
1182         if (cflag & CMSPAR)
1183                 lParity = lParity | 0x20;
1184
1185         /* Change the Stop bit */
1186         if (cflag & CSTOPB) {
1187                 lStop = UART_LCR_STOP;
1188                 dbg("%s - stop bits = 2", __func__);
1189         } else {
1190                 lStop = 0x00;
1191                 dbg("%s - stop bits = 1", __func__);
1192         }
1193
1194 #define LCR_BITS_MASK           0x03    /* Mask for bits/char field */
1195 #define LCR_STOP_MASK           0x04    /* Mask for stop bits field */
1196 #define LCR_PAR_MASK            0x38    /* Mask for parity field */
1197
1198         /* Update the LCR with the correct value */
1199         mos7720_port->shadowLCR &=
1200                         ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1201         mos7720_port->shadowLCR |= (lData | lParity | lStop);
1202
1203
1204         /* Disable Interrupts */
1205         data = 0x00;
1206         send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
1207                                                         UART_IER, &data);
1208
1209         data = 0x00;
1210         send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
1211
1212         data = 0xcf;
1213         send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
1214
1215         /* Send the updated LCR value to the mos7720 */
1216         data = mos7720_port->shadowLCR;
1217         send_mos_cmd(serial, MOS_WRITE, port_number, UART_LCR, &data);
1218
1219         data = 0x00b;
1220         mos7720_port->shadowMCR = data;
1221         send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
1222         data = 0x00b;
1223         send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
1224
1225         /* set up the MCR register and send it to the mos7720 */
1226         mos7720_port->shadowMCR = UART_MCR_OUT2;
1227         if (cflag & CBAUD)
1228                 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1229
1230         if (cflag & CRTSCTS) {
1231                 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
1232                 /* To set hardware flow control to the specified *
1233                  * serial port, in SP1/2_CONTROL_REG             */
1234                 if (port->number) {
1235                         data = 0x001;
1236                         send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1237                                      0x08, &data);
1238                 } else {
1239                         data = 0x002;
1240                         send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1241                                      0x08, &data);
1242                 }
1243         } else {
1244                 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1245         }
1246
1247         data = mos7720_port->shadowMCR;
1248         send_mos_cmd(serial, MOS_WRITE, port_number, UART_MCR, &data);
1249
1250         /* Determine divisor based on baud rate */
1251         baud = tty_get_baud_rate(tty);
1252         if (!baud) {
1253                 /* pick a default, any default... */
1254                 dbg("Picked default baud...");
1255                 baud = 9600;
1256         }
1257
1258         if (baud >= 230400) {
1259                 set_higher_rates(mos7720_port, baud);
1260                 /* Enable Interrupts */
1261                 data = 0x0c;
1262                 send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1263                 return;
1264         }
1265
1266         dbg("%s - baud rate = %d", __func__, baud);
1267         status = send_cmd_write_baud_rate(mos7720_port, baud);
1268         /* FIXME: needs to write actual resulting baud back not just
1269            blindly do so */
1270         if (cflag & CBAUD)
1271                 tty_encode_baud_rate(tty, baud, baud);
1272         /* Enable Interrupts */
1273         data = 0x0c;
1274         send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1275
1276         if (port->read_urb->status != -EINPROGRESS) {
1277                 port->read_urb->dev = serial->dev;
1278
1279                 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1280                 if (status)
1281                         dbg("usb_submit_urb(read bulk) failed, status = %d",
1282                             status);
1283         }
1284         return;
1285 }
1286
1287 /*
1288  * mos7720_set_termios
1289  *      this function is called by the tty driver when it wants to change the
1290  *      termios structure.
1291  */
1292 static void mos7720_set_termios(struct tty_struct *tty,
1293                 struct usb_serial_port *port, struct ktermios *old_termios)
1294 {
1295         int status;
1296         unsigned int cflag;
1297         struct usb_serial *serial;
1298         struct moschip_port *mos7720_port;
1299
1300         serial = port->serial;
1301
1302         mos7720_port = usb_get_serial_port_data(port);
1303
1304         if (mos7720_port == NULL)
1305                 return;
1306
1307         if (!mos7720_port->open) {
1308                 dbg("%s - port not opened", __func__);
1309                 return;
1310         }
1311
1312         dbg("%s\n", "setting termios - ASPIRE");
1313
1314         cflag = tty->termios->c_cflag;
1315
1316         dbg("%s - cflag %08x iflag %08x", __func__,
1317             tty->termios->c_cflag,
1318             RELEVANT_IFLAG(tty->termios->c_iflag));
1319
1320         dbg("%s - old cflag %08x old iflag %08x", __func__,
1321             old_termios->c_cflag,
1322             RELEVANT_IFLAG(old_termios->c_iflag));
1323
1324         dbg("%s - port %d", __func__, port->number);
1325
1326         /* change the port settings to the new ones specified */
1327         change_port_settings(tty, mos7720_port, old_termios);
1328
1329         if (!port->read_urb) {
1330                 dbg("%s", "URB KILLED !!!!!\n");
1331                 return;
1332         }
1333
1334         if (port->read_urb->status != -EINPROGRESS) {
1335                 port->read_urb->dev = serial->dev;
1336                 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1337                 if (status)
1338                         dbg("usb_submit_urb(read bulk) failed, status = %d",
1339                             status);
1340         }
1341         return;
1342 }
1343
1344 /*
1345  * get_lsr_info - get line status register info
1346  *
1347  * Purpose: Let user call ioctl() to get info when the UART physically
1348  *          is emptied.  On bus types like RS485, the transmitter must
1349  *          release the bus after transmitting. This must be done when
1350  *          the transmit shift register is empty, not be done when the
1351  *          transmit holding register is empty.  This functionality
1352  *          allows an RS485 driver to be written in user space.
1353  */
1354 static int get_lsr_info(struct tty_struct *tty,
1355                 struct moschip_port *mos7720_port, unsigned int __user *value)
1356 {
1357         struct usb_serial_port *port = tty->driver_data;
1358         unsigned int result = 0;
1359         unsigned char data = 0;
1360         int port_number = port->number - port->serial->minor;
1361         int count;
1362
1363         count = mos7720_chars_in_buffer(tty);
1364         if (count == 0) {
1365                 send_mos_cmd(port->serial, MOS_READ, port_number,
1366                                                         UART_LSR, &data);
1367                 if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1368                                         == (UART_LSR_TEMT | UART_LSR_THRE)) {
1369                         dbg("%s -- Empty", __func__);
1370                         result = TIOCSER_TEMT;
1371                 }
1372         }
1373         if (copy_to_user(value, &result, sizeof(int)))
1374                 return -EFAULT;
1375         return 0;
1376 }
1377
1378 static int mos7720_tiocmget(struct tty_struct *tty, struct file *file)
1379 {
1380         struct usb_serial_port *port = tty->driver_data;
1381         struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1382         unsigned int result = 0;
1383         unsigned int mcr ;
1384         unsigned int msr ;
1385
1386         dbg("%s - port %d", __func__, port->number);
1387
1388         mcr = mos7720_port->shadowMCR;
1389         msr = mos7720_port->shadowMSR;
1390
1391         result = ((mcr & UART_MCR_DTR)  ? TIOCM_DTR : 0)   /* 0x002 */
1392           | ((mcr & UART_MCR_RTS)   ? TIOCM_RTS : 0)   /* 0x004 */
1393           | ((msr & UART_MSR_CTS)   ? TIOCM_CTS : 0)   /* 0x020 */
1394           | ((msr & UART_MSR_DCD)   ? TIOCM_CAR : 0)   /* 0x040 */
1395           | ((msr & UART_MSR_RI)    ? TIOCM_RI :  0)   /* 0x080 */
1396           | ((msr & UART_MSR_DSR)   ? TIOCM_DSR : 0);  /* 0x100 */
1397
1398         dbg("%s -- %x", __func__, result);
1399
1400         return result;
1401 }
1402
1403 static int mos7720_tiocmset(struct tty_struct *tty, struct file *file,
1404                                         unsigned int set, unsigned int clear)
1405 {
1406         struct usb_serial_port *port = tty->driver_data;
1407         struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1408         unsigned int mcr ;
1409         unsigned char lmcr;
1410
1411         dbg("%s - port %d", __func__, port->number);
1412         dbg("he was at tiocmget");
1413
1414         mcr = mos7720_port->shadowMCR;
1415
1416         if (set & TIOCM_RTS)
1417                 mcr |= UART_MCR_RTS;
1418         if (set & TIOCM_DTR)
1419                 mcr |= UART_MCR_DTR;
1420         if (set & TIOCM_LOOP)
1421                 mcr |= UART_MCR_LOOP;
1422
1423         if (clear & TIOCM_RTS)
1424                 mcr &= ~UART_MCR_RTS;
1425         if (clear & TIOCM_DTR)
1426                 mcr &= ~UART_MCR_DTR;
1427         if (clear & TIOCM_LOOP)
1428                 mcr &= ~UART_MCR_LOOP;
1429
1430         mos7720_port->shadowMCR = mcr;
1431         lmcr = mos7720_port->shadowMCR;
1432
1433         send_mos_cmd(port->serial, MOS_WRITE,
1434                 port->number - port->serial->minor, UART_MCR, &lmcr);
1435
1436         return 0;
1437 }
1438
1439 static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1440                           unsigned int __user *value)
1441 {
1442         unsigned int mcr ;
1443         unsigned int arg;
1444         unsigned char data;
1445
1446         struct usb_serial_port *port;
1447
1448         if (mos7720_port == NULL)
1449                 return -1;
1450
1451         port = (struct usb_serial_port *)mos7720_port->port;
1452         mcr = mos7720_port->shadowMCR;
1453
1454         if (copy_from_user(&arg, value, sizeof(int)))
1455                 return -EFAULT;
1456
1457         switch (cmd) {
1458         case TIOCMBIS:
1459                 if (arg & TIOCM_RTS)
1460                         mcr |= UART_MCR_RTS;
1461                 if (arg & TIOCM_DTR)
1462                         mcr |= UART_MCR_RTS;
1463                 if (arg & TIOCM_LOOP)
1464                         mcr |= UART_MCR_LOOP;
1465                 break;
1466
1467         case TIOCMBIC:
1468                 if (arg & TIOCM_RTS)
1469                         mcr &= ~UART_MCR_RTS;
1470                 if (arg & TIOCM_DTR)
1471                         mcr &= ~UART_MCR_RTS;
1472                 if (arg & TIOCM_LOOP)
1473                         mcr &= ~UART_MCR_LOOP;
1474                 break;
1475
1476         }
1477
1478         mos7720_port->shadowMCR = mcr;
1479
1480         data = mos7720_port->shadowMCR;
1481         send_mos_cmd(port->serial, MOS_WRITE,
1482                      port->number - port->serial->minor, UART_MCR, &data);
1483
1484         return 0;
1485 }
1486
1487 static int get_serial_info(struct moschip_port *mos7720_port,
1488                            struct serial_struct __user *retinfo)
1489 {
1490         struct serial_struct tmp;
1491
1492         if (!retinfo)
1493                 return -EFAULT;
1494
1495         memset(&tmp, 0, sizeof(tmp));
1496
1497         tmp.type                = PORT_16550A;
1498         tmp.line                = mos7720_port->port->serial->minor;
1499         tmp.port                = mos7720_port->port->number;
1500         tmp.irq                 = 0;
1501         tmp.flags               = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1502         tmp.xmit_fifo_size      = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1503         tmp.baud_base           = 9600;
1504         tmp.close_delay         = 5*HZ;
1505         tmp.closing_wait        = 30*HZ;
1506
1507         if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1508                 return -EFAULT;
1509         return 0;
1510 }
1511
1512 static int mos7720_ioctl(struct tty_struct *tty, struct file *file,
1513                          unsigned int cmd, unsigned long arg)
1514 {
1515         struct usb_serial_port *port = tty->driver_data;
1516         struct moschip_port *mos7720_port;
1517         struct async_icount cnow;
1518         struct async_icount cprev;
1519         struct serial_icounter_struct icount;
1520
1521         mos7720_port = usb_get_serial_port_data(port);
1522         if (mos7720_port == NULL)
1523                 return -ENODEV;
1524
1525         dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
1526
1527         switch (cmd) {
1528         case TIOCSERGETLSR:
1529                 dbg("%s (%d) TIOCSERGETLSR", __func__,  port->number);
1530                 return get_lsr_info(tty, mos7720_port,
1531                                         (unsigned int __user *)arg);
1532                 return 0;
1533
1534         /* FIXME: These should be using the mode methods */
1535         case TIOCMBIS:
1536         case TIOCMBIC:
1537                 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET",
1538                                         __func__, port->number);
1539                 return set_modem_info(mos7720_port, cmd,
1540                                       (unsigned int __user *)arg);
1541
1542         case TIOCGSERIAL:
1543                 dbg("%s (%d) TIOCGSERIAL", __func__,  port->number);
1544                 return get_serial_info(mos7720_port,
1545                                        (struct serial_struct __user *)arg);
1546
1547         case TIOCMIWAIT:
1548                 dbg("%s (%d) TIOCMIWAIT", __func__,  port->number);
1549                 cprev = mos7720_port->icount;
1550                 while (1) {
1551                         if (signal_pending(current))
1552                                 return -ERESTARTSYS;
1553                         cnow = mos7720_port->icount;
1554                         if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1555                             cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1556                                 return -EIO; /* no change => error */
1557                         if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1558                             ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1559                             ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
1560                             ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1561                                 return 0;
1562                         }
1563                         cprev = cnow;
1564                 }
1565                 /* NOTREACHED */
1566                 break;
1567
1568         case TIOCGICOUNT:
1569                 cnow = mos7720_port->icount;
1570                 icount.cts = cnow.cts;
1571                 icount.dsr = cnow.dsr;
1572                 icount.rng = cnow.rng;
1573                 icount.dcd = cnow.dcd;
1574                 icount.rx = cnow.rx;
1575                 icount.tx = cnow.tx;
1576                 icount.frame = cnow.frame;
1577                 icount.overrun = cnow.overrun;
1578                 icount.parity = cnow.parity;
1579                 icount.brk = cnow.brk;
1580                 icount.buf_overrun = cnow.buf_overrun;
1581
1582                 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
1583                     port->number, icount.rx, icount.tx);
1584                 if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
1585                         return -EFAULT;
1586                 return 0;
1587         }
1588
1589         return -ENOIOCTLCMD;
1590 }
1591
1592 static int mos7720_startup(struct usb_serial *serial)
1593 {
1594         struct moschip_serial *mos7720_serial;
1595         struct moschip_port *mos7720_port;
1596         struct usb_device *dev;
1597         int i;
1598         char data;
1599         u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
1600
1601         dbg("%s: Entering ..........", __func__);
1602
1603         if (!serial) {
1604                 dbg("Invalid Handler");
1605                 return -ENODEV;
1606         }
1607
1608         dev = serial->dev;
1609
1610         /* create our private serial structure */
1611         mos7720_serial = kzalloc(sizeof(struct moschip_serial), GFP_KERNEL);
1612         if (mos7720_serial == NULL) {
1613                 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
1614                 return -ENOMEM;
1615         }
1616
1617         usb_set_serial_data(serial, mos7720_serial);
1618
1619         /*
1620          * The 7715 uses the first bulk in/out endpoint pair for the parallel
1621          * port, and the second for the serial port.  Because the usbserial core
1622          * assumes both pairs are serial ports, we must engage in a bit of
1623          * subterfuge and swap the pointers for ports 0 and 1 in order to make
1624          * port 0 point to the serial port.  However, both moschip devices use a
1625          * single interrupt-in endpoint for both ports (as mentioned a little
1626          * further down), and this endpoint was assigned to port 0.  So after
1627          * the swap, we must copy the interrupt endpoint elements from port 1
1628          * (as newly assigned) to port 0, and null out port 1 pointers.
1629          */
1630         if (product == MOSCHIP_DEVICE_ID_7715) {
1631                 struct usb_serial_port *tmp = serial->port[0];
1632                 serial->port[0] = serial->port[1];
1633                 serial->port[1] = tmp;
1634                 serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb;
1635                 serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer;
1636                 serial->port[0]->interrupt_in_endpointAddress =
1637                         tmp->interrupt_in_endpointAddress;
1638                 serial->port[1]->interrupt_in_urb = NULL;
1639                 serial->port[1]->interrupt_in_buffer = NULL;
1640         }
1641
1642         /* we set up the pointers to the endpoints in the mos7720_open *
1643          * function, as the structures aren't created yet.             */
1644
1645         /* set up port private structures */
1646         for (i = 0; i < serial->num_ports; ++i) {
1647                 mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
1648                 if (mos7720_port == NULL) {
1649                         dev_err(&dev->dev, "%s - Out of memory\n", __func__);
1650                         usb_set_serial_data(serial, NULL);
1651                         kfree(mos7720_serial);
1652                         return -ENOMEM;
1653                 }
1654
1655                 /* Initialize all port interrupt end point to port 0 int
1656                  * endpoint.  Our device has only one interrupt endpoint
1657                  * common to all ports */
1658                 serial->port[i]->interrupt_in_endpointAddress =
1659                                 serial->port[0]->interrupt_in_endpointAddress;
1660
1661                 mos7720_port->port = serial->port[i];
1662                 usb_set_serial_port_data(serial->port[i], mos7720_port);
1663
1664                 dbg("port number is %d", serial->port[i]->number);
1665                 dbg("serial number is %d", serial->minor);
1666         }
1667
1668
1669         /* setting configuration feature to one */
1670         usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1671                         (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5*HZ);
1672
1673         /* LSR For Port 1 */
1674         send_mos_cmd(serial, MOS_READ, 0x00, UART_LSR, &data);
1675         dbg("LSR:%x", data);
1676
1677         /* LSR For Port 2 */
1678         send_mos_cmd(serial, MOS_READ, 0x01, UART_LSR, &data);
1679         dbg("LSR:%x", data);
1680
1681         return 0;
1682 }
1683
1684 static void mos7720_release(struct usb_serial *serial)
1685 {
1686         int i;
1687
1688         /* free private structure allocated for serial port */
1689         for (i = 0; i < serial->num_ports; ++i)
1690                 kfree(usb_get_serial_port_data(serial->port[i]));
1691
1692         /* free private structure allocated for serial device */
1693         kfree(usb_get_serial_data(serial));
1694 }
1695
1696 static struct usb_driver usb_driver = {
1697         .name =         "moschip7720",
1698         .probe =        usb_serial_probe,
1699         .disconnect =   usb_serial_disconnect,
1700         .id_table =     moschip_port_id_table,
1701         .no_dynamic_id =        1,
1702 };
1703
1704 static struct usb_serial_driver moschip7720_2port_driver = {
1705         .driver = {
1706                 .owner =        THIS_MODULE,
1707                 .name =         "moschip7720",
1708         },
1709         .description            = "Moschip 2 port adapter",
1710         .usb_driver             = &usb_driver,
1711         .id_table               = moschip_port_id_table,
1712         .calc_num_ports         = mos77xx_calc_num_ports,
1713         .open                   = mos7720_open,
1714         .close                  = mos7720_close,
1715         .throttle               = mos7720_throttle,
1716         .unthrottle             = mos7720_unthrottle,
1717         .probe                  = mos77xx_probe,
1718         .attach                 = mos7720_startup,
1719         .release                = mos7720_release,
1720         .ioctl                  = mos7720_ioctl,
1721         .tiocmget               = mos7720_tiocmget,
1722         .tiocmset               = mos7720_tiocmset,
1723         .set_termios            = mos7720_set_termios,
1724         .write                  = mos7720_write,
1725         .write_room             = mos7720_write_room,
1726         .chars_in_buffer        = mos7720_chars_in_buffer,
1727         .break_ctl              = mos7720_break,
1728         .read_bulk_callback     = mos7720_bulk_in_callback,
1729         .read_int_callback      = NULL  /* dynamically assigned in probe() */
1730 };
1731
1732 static int __init moschip7720_init(void)
1733 {
1734         int retval;
1735
1736         dbg("%s: Entering ..........", __func__);
1737
1738         /* Register with the usb serial */
1739         retval = usb_serial_register(&moschip7720_2port_driver);
1740         if (retval)
1741                 goto failed_port_device_register;
1742
1743         printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1744                DRIVER_DESC "\n");
1745
1746         /* Register with the usb */
1747         retval = usb_register(&usb_driver);
1748         if (retval)
1749                 goto failed_usb_register;
1750
1751         return 0;
1752
1753 failed_usb_register:
1754         usb_serial_deregister(&moschip7720_2port_driver);
1755
1756 failed_port_device_register:
1757         return retval;
1758 }
1759
1760 static void __exit moschip7720_exit(void)
1761 {
1762         usb_deregister(&usb_driver);
1763         usb_serial_deregister(&moschip7720_2port_driver);
1764 }
1765
1766 module_init(moschip7720_init);
1767 module_exit(moschip7720_exit);
1768
1769 /* Module information */
1770 MODULE_AUTHOR(DRIVER_AUTHOR);
1771 MODULE_DESCRIPTION(DRIVER_DESC);
1772 MODULE_LICENSE("GPL");
1773
1774 module_param(debug, bool, S_IRUGO | S_IWUSR);
1775 MODULE_PARM_DESC(debug, "Debug enabled or not");