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