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