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