USB: serial: mos7720: fix use-after-free on probe errors
[pandora-kernel.git] / drivers / usb / serial / mos7720.c
1 /*
2  * mos7720.c
3  *   Controls the Moschip 7720 usb to dual port serial convertor
4  *
5  * Copyright 2006 Moschip Semiconductor Tech. Ltd.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, version 2 of the License.
10  *
11  * Developed by:
12  *      Vijaya Kumar <vijaykumar.gn@gmail.com>
13  *      Ajay Kumar <naanuajay@yahoo.com>
14  *      Gurudeva <ngurudeva@yahoo.com>
15  *
16  * Cleaned up from the original by:
17  *      Greg Kroah-Hartman <gregkh@suse.de>
18  *
19  * Originally based on drivers/usb/serial/io_edgeport.c which is:
20  *      Copyright (C) 2000 Inside Out Networks, All rights reserved.
21  *      Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
22  */
23 #include <linux/kernel.h>
24 #include <linux/errno.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/tty.h>
28 #include <linux/tty_driver.h>
29 #include <linux/tty_flip.h>
30 #include <linux/module.h>
31 #include <linux/spinlock.h>
32 #include <linux/serial.h>
33 #include <linux/serial_reg.h>
34 #include <linux/usb.h>
35 #include <linux/usb/serial.h>
36 #include <linux/uaccess.h>
37 #include <linux/parport.h>
38
39 /*
40  * Version Information
41  */
42 #define DRIVER_VERSION "2.1"
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 5000
48
49 #define MOS_MAX_PORT    0x02
50 #define MOS_WRITE       0x0E
51 #define MOS_READ        0x0D
52
53 /* Interrupt Rotinue Defines    */
54 #define SERIAL_IIR_RLS  0x06
55 #define SERIAL_IIR_RDA  0x04
56 #define SERIAL_IIR_CTI  0x0c
57 #define SERIAL_IIR_THR  0x02
58 #define SERIAL_IIR_MS   0x00
59
60 #define NUM_URBS                        16      /* URB Count */
61 #define URB_TRANSFER_BUFFER_SIZE        32      /* URB Size */
62
63 /* This structure holds all of the local serial port information */
64 struct moschip_port {
65         __u8    shadowLCR;              /* last LCR value received */
66         __u8    shadowMCR;              /* last MCR value received */
67         __u8    shadowMSR;              /* last MSR value received */
68         char                    open;
69         struct async_icount     icount;
70         struct usb_serial_port  *port;  /* loop back to the owner */
71         struct urb              *write_urb_pool[NUM_URBS];
72 };
73
74 static int debug;
75
76 static struct usb_serial_driver moschip7720_2port_driver;
77
78 #define USB_VENDOR_ID_MOSCHIP           0x9710
79 #define MOSCHIP_DEVICE_ID_7720          0x7720
80 #define MOSCHIP_DEVICE_ID_7715          0x7715
81
82 static const struct usb_device_id moschip_port_id_table[] = {
83         { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
84         { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) },
85         { } /* terminating entry */
86 };
87 MODULE_DEVICE_TABLE(usb, moschip_port_id_table);
88
89 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
90
91 /* initial values for parport regs */
92 #define DCR_INIT_VAL       0x0c /* SLCTIN, nINIT */
93 #define ECR_INIT_VAL       0x00 /* SPP mode */
94
95 struct urbtracker {
96         struct mos7715_parport  *mos_parport;
97         struct list_head        urblist_entry;
98         struct kref             ref_count;
99         struct urb              *urb;
100         struct usb_ctrlrequest  *setup;
101 };
102
103 enum mos7715_pp_modes {
104         SPP = 0<<5,
105         PS2 = 1<<5,      /* moschip calls this 'NIBBLE' mode */
106         PPF = 2<<5,      /* moschip calls this 'CB-FIFO mode */
107 };
108
109 struct mos7715_parport {
110         struct parport          *pp;           /* back to containing struct */
111         struct kref             ref_count;     /* to instance of this struct */
112         struct list_head        deferred_urbs; /* list deferred async urbs */
113         struct list_head        active_urbs;   /* list async urbs in flight */
114         spinlock_t              listlock;      /* protects list access */
115         bool                    msg_pending;   /* usb sync call pending */
116         struct completion       syncmsg_compl; /* usb sync call completed */
117         struct tasklet_struct   urb_tasklet;   /* for sending deferred urbs */
118         struct usb_serial       *serial;       /* back to containing struct */
119         __u8                    shadowECR;     /* parallel port regs... */
120         __u8                    shadowDCR;
121         atomic_t                shadowDSR;     /* updated in int-in callback */
122 };
123
124 /* lock guards against dereferencing NULL ptr in parport ops callbacks */
125 static DEFINE_SPINLOCK(release_lock);
126
127 #endif  /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
128
129 static const unsigned int dummy; /* for clarity in register access fns */
130
131 enum mos_regs {
132         THR,              /* serial port regs */
133         RHR,
134         IER,
135         FCR,
136         ISR,
137         LCR,
138         MCR,
139         LSR,
140         MSR,
141         SPR,
142         DLL,
143         DLM,
144         DPR,              /* parallel port regs */
145         DSR,
146         DCR,
147         ECR,
148         SP1_REG,          /* device control regs */
149         SP2_REG,          /* serial port 2 (7720 only) */
150         PP_REG,
151         SP_CONTROL_REG,
152 };
153
154 /*
155  * Return the correct value for the Windex field of the setup packet
156  * for a control endpoint message.  See the 7715 datasheet.
157  */
158 static inline __u16 get_reg_index(enum mos_regs reg)
159 {
160         static const __u16 mos7715_index_lookup_table[] = {
161                 0x00,           /* THR */
162                 0x00,           /* RHR */
163                 0x01,           /* IER */
164                 0x02,           /* FCR */
165                 0x02,           /* ISR */
166                 0x03,           /* LCR */
167                 0x04,           /* MCR */
168                 0x05,           /* LSR */
169                 0x06,           /* MSR */
170                 0x07,           /* SPR */
171                 0x00,           /* DLL */
172                 0x01,           /* DLM */
173                 0x00,           /* DPR */
174                 0x01,           /* DSR */
175                 0x02,           /* DCR */
176                 0x0a,           /* ECR */
177                 0x01,           /* SP1_REG */
178                 0x02,           /* SP2_REG (7720 only) */
179                 0x04,           /* PP_REG (7715 only) */
180                 0x08,           /* SP_CONTROL_REG */
181         };
182         return mos7715_index_lookup_table[reg];
183 }
184
185 /*
186  * Return the correct value for the upper byte of the Wvalue field of
187  * the setup packet for a control endpoint message.
188  */
189 static inline __u16 get_reg_value(enum mos_regs reg,
190                                   unsigned int serial_portnum)
191 {
192         if (reg >= SP1_REG)           /* control reg */
193                 return 0x0000;
194
195         else if (reg >= DPR)          /* parallel port reg (7715 only) */
196                 return 0x0100;
197
198         else                          /* serial port reg */
199                 return (serial_portnum + 2) << 8;
200 }
201
202 /*
203  * Write data byte to the specified device register.  The data is embedded in
204  * the value field of the setup packet. serial_portnum is ignored for registers
205  * not specific to a particular serial port.
206  */
207 static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
208                          enum mos_regs reg, __u8 data)
209 {
210         struct usb_device *usbdev = serial->dev;
211         unsigned int pipe = usb_sndctrlpipe(usbdev, 0);
212         __u8 request = (__u8)0x0e;
213         __u8 requesttype = (__u8)0x40;
214         __u16 index = get_reg_index(reg);
215         __u16 value = get_reg_value(reg, serial_portnum) + data;
216         int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
217                                      index, NULL, 0, MOS_WDR_TIMEOUT);
218         if (status < 0)
219                 dev_err(&usbdev->dev,
220                         "mos7720: usb_control_msg() failed: %d", status);
221         return status;
222 }
223
224 /*
225  * Read data byte from the specified device register.  The data returned by the
226  * device is embedded in the value field of the setup packet.  serial_portnum is
227  * ignored for registers that are not specific to a particular serial port.
228  */
229 static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
230                         enum mos_regs reg, __u8 *data)
231 {
232         struct usb_device *usbdev = serial->dev;
233         unsigned int pipe = usb_rcvctrlpipe(usbdev, 0);
234         __u8 request = (__u8)0x0d;
235         __u8 requesttype = (__u8)0xc0;
236         __u16 index = get_reg_index(reg);
237         __u16 value = get_reg_value(reg, serial_portnum);
238         u8 *buf;
239         int status;
240
241         buf = kmalloc(1, GFP_KERNEL);
242         if (!buf)
243                 return -ENOMEM;
244
245         status = usb_control_msg(usbdev, pipe, request, requesttype, value,
246                                      index, buf, 1, MOS_WDR_TIMEOUT);
247         if (status == 1)
248                 *data = *buf;
249         else if (status < 0)
250                 dev_err(&usbdev->dev,
251                         "mos7720: usb_control_msg() failed: %d", status);
252         kfree(buf);
253
254         return status;
255 }
256
257 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
258
259 static inline int mos7715_change_mode(struct mos7715_parport *mos_parport,
260                                       enum mos7715_pp_modes mode)
261 {
262         mos_parport->shadowECR = mode;
263         write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
264         return 0;
265 }
266
267 static void destroy_mos_parport(struct kref *kref)
268 {
269         struct mos7715_parport *mos_parport =
270                 container_of(kref, struct mos7715_parport, ref_count);
271
272         dbg("%s called", __func__);
273         kfree(mos_parport);
274 }
275
276 static void destroy_urbtracker(struct kref *kref)
277 {
278         struct urbtracker *urbtrack =
279                 container_of(kref, struct urbtracker, ref_count);
280         struct mos7715_parport *mos_parport = urbtrack->mos_parport;
281         dbg("%s called", __func__);
282         usb_free_urb(urbtrack->urb);
283         kfree(urbtrack->setup);
284         kfree(urbtrack);
285         kref_put(&mos_parport->ref_count, destroy_mos_parport);
286 }
287
288 /*
289  * This runs as a tasklet when sending an urb in a non-blocking parallel
290  * port callback had to be deferred because the disconnect mutex could not be
291  * obtained at the time.
292  */
293 static void send_deferred_urbs(unsigned long _mos_parport)
294 {
295         int ret_val;
296         unsigned long flags;
297         struct mos7715_parport *mos_parport = (void *)_mos_parport;
298         struct urbtracker *urbtrack;
299         struct list_head *cursor, *next;
300
301         dbg("%s called", __func__);
302
303         /* if release function ran, game over */
304         if (unlikely(mos_parport->serial == NULL))
305                 return;
306
307         /* try again to get the mutex */
308         if (!mutex_trylock(&mos_parport->serial->disc_mutex)) {
309                 dbg("%s: rescheduling tasklet", __func__);
310                 tasklet_schedule(&mos_parport->urb_tasklet);
311                 return;
312         }
313
314         /* if device disconnected, game over */
315         if (unlikely(mos_parport->serial->disconnected)) {
316                 mutex_unlock(&mos_parport->serial->disc_mutex);
317                 return;
318         }
319
320         spin_lock_irqsave(&mos_parport->listlock, flags);
321         if (list_empty(&mos_parport->deferred_urbs)) {
322                 spin_unlock_irqrestore(&mos_parport->listlock, flags);
323                 mutex_unlock(&mos_parport->serial->disc_mutex);
324                 dbg("%s: deferred_urbs list empty", __func__);
325                 return;
326         }
327
328         /* move contents of deferred_urbs list to active_urbs list and submit */
329         list_for_each_safe(cursor, next, &mos_parport->deferred_urbs)
330                 list_move_tail(cursor, &mos_parport->active_urbs);
331         list_for_each_entry(urbtrack, &mos_parport->active_urbs,
332                             urblist_entry) {
333                 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
334                 dbg("%s: urb submitted", __func__);
335                 if (ret_val) {
336                         dev_err(&mos_parport->serial->dev->dev,
337                                 "usb_submit_urb() failed: %d", ret_val);
338                         list_del(&urbtrack->urblist_entry);
339                         kref_put(&urbtrack->ref_count, destroy_urbtracker);
340                 }
341         }
342         spin_unlock_irqrestore(&mos_parport->listlock, flags);
343         mutex_unlock(&mos_parport->serial->disc_mutex);
344 }
345
346 /* callback for parallel port control urbs submitted asynchronously */
347 static void async_complete(struct urb *urb)
348 {
349         struct urbtracker *urbtrack = urb->context;
350         int status = urb->status;
351         dbg("%s called", __func__);
352         if (unlikely(status))
353                 dbg("%s - nonzero urb status received: %d", __func__, status);
354
355         /* remove the urbtracker from the active_urbs list */
356         spin_lock(&urbtrack->mos_parport->listlock);
357         list_del(&urbtrack->urblist_entry);
358         spin_unlock(&urbtrack->mos_parport->listlock);
359         kref_put(&urbtrack->ref_count, destroy_urbtracker);
360 }
361
362 static int write_parport_reg_nonblock(struct mos7715_parport *mos_parport,
363                                       enum mos_regs reg, __u8 data)
364 {
365         struct urbtracker *urbtrack;
366         int ret_val;
367         unsigned long flags;
368         struct usb_serial *serial = mos_parport->serial;
369         struct usb_device *usbdev = serial->dev;
370         dbg("%s called", __func__);
371
372         /* create and initialize the control urb and containing urbtracker */
373         urbtrack = kmalloc(sizeof(struct urbtracker), GFP_ATOMIC);
374         if (urbtrack == NULL) {
375                 dev_err(&usbdev->dev, "out of memory");
376                 return -ENOMEM;
377         }
378         kref_get(&mos_parport->ref_count);
379         urbtrack->mos_parport = mos_parport;
380         urbtrack->urb = usb_alloc_urb(0, GFP_ATOMIC);
381         if (urbtrack->urb == NULL) {
382                 dev_err(&usbdev->dev, "out of urbs");
383                 kfree(urbtrack);
384                 return -ENOMEM;
385         }
386         urbtrack->setup = kmalloc(sizeof(*urbtrack->setup), GFP_ATOMIC);
387         if (!urbtrack->setup) {
388                 usb_free_urb(urbtrack->urb);
389                 kfree(urbtrack);
390                 return -ENOMEM;
391         }
392         urbtrack->setup->bRequestType = (__u8)0x40;
393         urbtrack->setup->bRequest = (__u8)0x0e;
394         urbtrack->setup->wValue = cpu_to_le16(get_reg_value(reg, dummy));
395         urbtrack->setup->wIndex = cpu_to_le16(get_reg_index(reg));
396         urbtrack->setup->wLength = 0;
397         usb_fill_control_urb(urbtrack->urb, usbdev,
398                              usb_sndctrlpipe(usbdev, 0),
399                              (unsigned char *)urbtrack->setup,
400                              NULL, 0, async_complete, urbtrack);
401         kref_init(&urbtrack->ref_count);
402         INIT_LIST_HEAD(&urbtrack->urblist_entry);
403
404         /*
405          * get the disconnect mutex, or add tracker to the deferred_urbs list
406          * and schedule a tasklet to try again later
407          */
408         if (!mutex_trylock(&serial->disc_mutex)) {
409                 spin_lock_irqsave(&mos_parport->listlock, flags);
410                 list_add_tail(&urbtrack->urblist_entry,
411                               &mos_parport->deferred_urbs);
412                 spin_unlock_irqrestore(&mos_parport->listlock, flags);
413                 tasklet_schedule(&mos_parport->urb_tasklet);
414                 dbg("tasklet scheduled");
415                 return 0;
416         }
417
418         /* bail if device disconnected */
419         if (serial->disconnected) {
420                 kref_put(&urbtrack->ref_count, destroy_urbtracker);
421                 mutex_unlock(&serial->disc_mutex);
422                 return -ENODEV;
423         }
424
425         /* add the tracker to the active_urbs list and submit */
426         spin_lock_irqsave(&mos_parport->listlock, flags);
427         list_add_tail(&urbtrack->urblist_entry, &mos_parport->active_urbs);
428         spin_unlock_irqrestore(&mos_parport->listlock, flags);
429         ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
430         mutex_unlock(&serial->disc_mutex);
431         if (ret_val) {
432                 dev_err(&usbdev->dev,
433                         "%s: submit_urb() failed: %d", __func__, ret_val);
434                 spin_lock_irqsave(&mos_parport->listlock, flags);
435                 list_del(&urbtrack->urblist_entry);
436                 spin_unlock_irqrestore(&mos_parport->listlock, flags);
437                 kref_put(&urbtrack->ref_count, destroy_urbtracker);
438                 return ret_val;
439         }
440         return 0;
441 }
442
443 /*
444  * This is the the common top part of all parallel port callback operations that
445  * send synchronous messages to the device.  This implements convoluted locking
446  * that avoids two scenarios: (1) a port operation is called after usbserial
447  * has called our release function, at which point struct mos7715_parport has
448  * been destroyed, and (2) the device has been disconnected, but usbserial has
449  * not called the release function yet because someone has a serial port open.
450  * The shared release_lock prevents the first, and the mutex and disconnected
451  * flag maintained by usbserial covers the second.  We also use the msg_pending
452  * flag to ensure that all synchronous usb messgage calls have completed before
453  * our release function can return.
454  */
455 static int parport_prologue(struct parport *pp)
456 {
457         struct mos7715_parport *mos_parport;
458
459         spin_lock(&release_lock);
460         mos_parport = pp->private_data;
461         if (unlikely(mos_parport == NULL)) {
462                 /* release fn called, port struct destroyed */
463                 spin_unlock(&release_lock);
464                 return -1;
465         }
466         mos_parport->msg_pending = true;   /* synch usb call pending */
467         INIT_COMPLETION(mos_parport->syncmsg_compl);
468         spin_unlock(&release_lock);
469
470         mutex_lock(&mos_parport->serial->disc_mutex);
471         if (mos_parport->serial->disconnected) {
472                 /* device disconnected */
473                 mutex_unlock(&mos_parport->serial->disc_mutex);
474                 mos_parport->msg_pending = false;
475                 complete(&mos_parport->syncmsg_compl);
476                 return -1;
477         }
478
479         return 0;
480 }
481
482 /*
483  * This is the the common bottom part of all parallel port functions that send
484  * synchronous messages to the device.
485  */
486 static inline void parport_epilogue(struct parport *pp)
487 {
488         struct mos7715_parport *mos_parport = pp->private_data;
489         mutex_unlock(&mos_parport->serial->disc_mutex);
490         mos_parport->msg_pending = false;
491         complete(&mos_parport->syncmsg_compl);
492 }
493
494 static void parport_mos7715_write_data(struct parport *pp, unsigned char d)
495 {
496         struct mos7715_parport *mos_parport = pp->private_data;
497         dbg("%s called: %2.2x", __func__, d);
498         if (parport_prologue(pp) < 0)
499                 return;
500         mos7715_change_mode(mos_parport, SPP);
501         write_mos_reg(mos_parport->serial, dummy, DPR, (__u8)d);
502         parport_epilogue(pp);
503 }
504
505 static unsigned char parport_mos7715_read_data(struct parport *pp)
506 {
507         struct mos7715_parport *mos_parport = pp->private_data;
508         unsigned char d;
509         dbg("%s called", __func__);
510         if (parport_prologue(pp) < 0)
511                 return 0;
512         read_mos_reg(mos_parport->serial, dummy, DPR, &d);
513         parport_epilogue(pp);
514         return d;
515 }
516
517 static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
518 {
519         struct mos7715_parport *mos_parport = pp->private_data;
520         __u8 data;
521         dbg("%s called: %2.2x", __func__, d);
522         if (parport_prologue(pp) < 0)
523                 return;
524         data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
525         write_mos_reg(mos_parport->serial, dummy, DCR, data);
526         mos_parport->shadowDCR = data;
527         parport_epilogue(pp);
528 }
529
530 static unsigned char parport_mos7715_read_control(struct parport *pp)
531 {
532         struct mos7715_parport *mos_parport = pp->private_data;
533         __u8 dcr;
534         dbg("%s called", __func__);
535         spin_lock(&release_lock);
536         mos_parport = pp->private_data;
537         if (unlikely(mos_parport == NULL)) {
538                 spin_unlock(&release_lock);
539                 return 0;
540         }
541         dcr = mos_parport->shadowDCR & 0x0f;
542         spin_unlock(&release_lock);
543         return dcr;
544 }
545
546 static unsigned char parport_mos7715_frob_control(struct parport *pp,
547                                                   unsigned char mask,
548                                                   unsigned char val)
549 {
550         struct mos7715_parport *mos_parport = pp->private_data;
551         __u8 dcr;
552         dbg("%s called", __func__);
553         mask &= 0x0f;
554         val &= 0x0f;
555         if (parport_prologue(pp) < 0)
556                 return 0;
557         mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
558         write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
559         dcr = mos_parport->shadowDCR & 0x0f;
560         parport_epilogue(pp);
561         return dcr;
562 }
563
564 static unsigned char parport_mos7715_read_status(struct parport *pp)
565 {
566         unsigned char status;
567         struct mos7715_parport *mos_parport = pp->private_data;
568         dbg("%s called", __func__);
569         spin_lock(&release_lock);
570         mos_parport = pp->private_data;
571         if (unlikely(mos_parport == NULL)) {    /* release called */
572                 spin_unlock(&release_lock);
573                 return 0;
574         }
575         status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
576         spin_unlock(&release_lock);
577         return status;
578 }
579
580 static void parport_mos7715_enable_irq(struct parport *pp)
581 {
582         dbg("%s called", __func__);
583 }
584 static void parport_mos7715_disable_irq(struct parport *pp)
585 {
586         dbg("%s called", __func__);
587 }
588
589 static void parport_mos7715_data_forward(struct parport *pp)
590 {
591         struct mos7715_parport *mos_parport = pp->private_data;
592         dbg("%s called", __func__);
593         if (parport_prologue(pp) < 0)
594                 return;
595         mos7715_change_mode(mos_parport, PS2);
596         mos_parport->shadowDCR &=  ~0x20;
597         write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
598         parport_epilogue(pp);
599 }
600
601 static void parport_mos7715_data_reverse(struct parport *pp)
602 {
603         struct mos7715_parport *mos_parport = pp->private_data;
604         dbg("%s called", __func__);
605         if (parport_prologue(pp) < 0)
606                 return;
607         mos7715_change_mode(mos_parport, PS2);
608         mos_parport->shadowDCR |= 0x20;
609         write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
610         parport_epilogue(pp);
611 }
612
613 static void parport_mos7715_init_state(struct pardevice *dev,
614                                        struct parport_state *s)
615 {
616         dbg("%s called", __func__);
617         s->u.pc.ctr = DCR_INIT_VAL;
618         s->u.pc.ecr = ECR_INIT_VAL;
619 }
620
621 /* N.B. Parport core code requires that this function not block */
622 static void parport_mos7715_save_state(struct parport *pp,
623                                        struct parport_state *s)
624 {
625         struct mos7715_parport *mos_parport;
626         dbg("%s called", __func__);
627         spin_lock(&release_lock);
628         mos_parport = pp->private_data;
629         if (unlikely(mos_parport == NULL)) {    /* release called */
630                 spin_unlock(&release_lock);
631                 return;
632         }
633         s->u.pc.ctr = mos_parport->shadowDCR;
634         s->u.pc.ecr = mos_parport->shadowECR;
635         spin_unlock(&release_lock);
636 }
637
638 /* N.B. Parport core code requires that this function not block */
639 static void parport_mos7715_restore_state(struct parport *pp,
640                                           struct parport_state *s)
641 {
642         struct mos7715_parport *mos_parport;
643         dbg("%s called", __func__);
644         spin_lock(&release_lock);
645         mos_parport = pp->private_data;
646         if (unlikely(mos_parport == NULL)) {    /* release called */
647                 spin_unlock(&release_lock);
648                 return;
649         }
650         write_parport_reg_nonblock(mos_parport, DCR, mos_parport->shadowDCR);
651         write_parport_reg_nonblock(mos_parport, ECR, mos_parport->shadowECR);
652         spin_unlock(&release_lock);
653 }
654
655 static size_t parport_mos7715_write_compat(struct parport *pp,
656                                            const void *buffer,
657                                            size_t len, int flags)
658 {
659         int retval;
660         struct mos7715_parport *mos_parport = pp->private_data;
661         int actual_len;
662         dbg("%s called: %u chars", __func__, (unsigned int)len);
663         if (parport_prologue(pp) < 0)
664                 return 0;
665         mos7715_change_mode(mos_parport, PPF);
666         retval = usb_bulk_msg(mos_parport->serial->dev,
667                               usb_sndbulkpipe(mos_parport->serial->dev, 2),
668                               (void *)buffer, len, &actual_len,
669                               MOS_WDR_TIMEOUT);
670         parport_epilogue(pp);
671         if (retval) {
672                 dev_err(&mos_parport->serial->dev->dev,
673                         "mos7720: usb_bulk_msg() failed: %d", retval);
674                 return 0;
675         }
676         return actual_len;
677 }
678
679 static struct parport_operations parport_mos7715_ops = {
680         .owner =                THIS_MODULE,
681         .write_data =           parport_mos7715_write_data,
682         .read_data =            parport_mos7715_read_data,
683
684         .write_control =        parport_mos7715_write_control,
685         .read_control =         parport_mos7715_read_control,
686         .frob_control =         parport_mos7715_frob_control,
687
688         .read_status =          parport_mos7715_read_status,
689
690         .enable_irq =           parport_mos7715_enable_irq,
691         .disable_irq =          parport_mos7715_disable_irq,
692
693         .data_forward =         parport_mos7715_data_forward,
694         .data_reverse =         parport_mos7715_data_reverse,
695
696         .init_state =           parport_mos7715_init_state,
697         .save_state =           parport_mos7715_save_state,
698         .restore_state =        parport_mos7715_restore_state,
699
700         .compat_write_data =    parport_mos7715_write_compat,
701
702         .nibble_read_data =     parport_ieee1284_read_nibble,
703         .byte_read_data =       parport_ieee1284_read_byte,
704 };
705
706 /*
707  * Allocate and initialize parallel port control struct, initialize
708  * the parallel port hardware device, and register with the parport subsystem.
709  */
710 static int mos7715_parport_init(struct usb_serial *serial)
711 {
712         struct mos7715_parport *mos_parport;
713
714         /* allocate and initialize parallel port control struct */
715         mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
716         if (mos_parport == NULL) {
717                 dbg("mos7715_parport_init: kzalloc failed");
718                 return -ENOMEM;
719         }
720         mos_parport->msg_pending = false;
721         kref_init(&mos_parport->ref_count);
722         spin_lock_init(&mos_parport->listlock);
723         INIT_LIST_HEAD(&mos_parport->active_urbs);
724         INIT_LIST_HEAD(&mos_parport->deferred_urbs);
725         usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
726         mos_parport->serial = serial;
727         tasklet_init(&mos_parport->urb_tasklet, send_deferred_urbs,
728                      (unsigned long) mos_parport);
729         init_completion(&mos_parport->syncmsg_compl);
730
731         /* cycle parallel port reset bit */
732         write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x80);
733         write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x00);
734
735         /* initialize device registers */
736         mos_parport->shadowDCR = DCR_INIT_VAL;
737         write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
738         mos_parport->shadowECR = ECR_INIT_VAL;
739         write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
740
741         /* register with parport core */
742         mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE,
743                                                 PARPORT_DMA_NONE,
744                                                 &parport_mos7715_ops);
745         if (mos_parport->pp == NULL) {
746                 dev_err(&serial->interface->dev,
747                         "Could not register parport\n");
748                 kref_put(&mos_parport->ref_count, destroy_mos_parport);
749                 return -EIO;
750         }
751         mos_parport->pp->private_data = mos_parport;
752         mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP;
753         mos_parport->pp->dev = &serial->interface->dev;
754         parport_announce_port(mos_parport->pp);
755
756         return 0;
757 }
758 #endif  /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
759
760 /*
761  * mos7720_interrupt_callback
762  *      this is the callback function for when we have received data on the
763  *      interrupt endpoint.
764  */
765 static void mos7720_interrupt_callback(struct urb *urb)
766 {
767         int result;
768         int length;
769         int status = urb->status;
770         __u8 *data;
771         __u8 sp1;
772         __u8 sp2;
773
774         switch (status) {
775         case 0:
776                 /* success */
777                 break;
778         case -ECONNRESET:
779         case -ENOENT:
780         case -ESHUTDOWN:
781                 /* this urb is terminated, clean up */
782                 dbg("%s - urb shutting down with status: %d", __func__,
783                     status);
784                 return;
785         default:
786                 dbg("%s - nonzero urb status received: %d", __func__,
787                     status);
788                 goto exit;
789         }
790
791         length = urb->actual_length;
792         data = urb->transfer_buffer;
793
794         /* Moschip get 4 bytes
795          * Byte 1 IIR Port 1 (port.number is 0)
796          * Byte 2 IIR Port 2 (port.number is 1)
797          * Byte 3 --------------
798          * Byte 4 FIFO status for both */
799
800         /* the above description is inverted
801          *      oneukum 2007-03-14 */
802
803         if (unlikely(length != 4)) {
804                 dbg("Wrong data !!!");
805                 return;
806         }
807
808         sp1 = data[3];
809         sp2 = data[2];
810
811         if ((sp1 | sp2) & 0x01) {
812                 /* No Interrupt Pending in both the ports */
813                 dbg("No Interrupt !!!");
814         } else {
815                 switch (sp1 & 0x0f) {
816                 case SERIAL_IIR_RLS:
817                         dbg("Serial Port 1: Receiver status error or address "
818                             "bit detected in 9-bit mode\n");
819                         break;
820                 case SERIAL_IIR_CTI:
821                         dbg("Serial Port 1: Receiver time out");
822                         break;
823                 case SERIAL_IIR_MS:
824                         /* dbg("Serial Port 1: Modem status change"); */
825                         break;
826                 }
827
828                 switch (sp2 & 0x0f) {
829                 case SERIAL_IIR_RLS:
830                         dbg("Serial Port 2: Receiver status error or address "
831                             "bit detected in 9-bit mode");
832                         break;
833                 case SERIAL_IIR_CTI:
834                         dbg("Serial Port 2: Receiver time out");
835                         break;
836                 case SERIAL_IIR_MS:
837                         /* dbg("Serial Port 2: Modem status change"); */
838                         break;
839                 }
840         }
841
842 exit:
843         result = usb_submit_urb(urb, GFP_ATOMIC);
844         if (result)
845                 dev_err(&urb->dev->dev,
846                         "%s - Error %d submitting control urb\n",
847                         __func__, result);
848 }
849
850 /*
851  * mos7715_interrupt_callback
852  *      this is the 7715's callback function for when we have received data on
853  *      the interrupt endpoint.
854  */
855 static void mos7715_interrupt_callback(struct urb *urb)
856 {
857         int result;
858         int length;
859         int status = urb->status;
860         __u8 *data;
861         __u8 iir;
862
863         switch (status) {
864         case 0:
865                 /* success */
866                 break;
867         case -ECONNRESET:
868         case -ENOENT:
869         case -ESHUTDOWN:
870         case -ENODEV:
871                 /* this urb is terminated, clean up */
872                 dbg("%s - urb shutting down with status: %d", __func__,
873                     status);
874                 return;
875         default:
876                 dbg("%s - nonzero urb status received: %d", __func__,
877                     status);
878                 goto exit;
879         }
880
881         length = urb->actual_length;
882         data = urb->transfer_buffer;
883
884         /* Structure of data from 7715 device:
885          * Byte 1: IIR serial Port
886          * Byte 2: unused
887          * Byte 2: DSR parallel port
888          * Byte 4: FIFO status for both */
889
890         if (unlikely(length != 4)) {
891                 dbg("Wrong data !!!");
892                 return;
893         }
894
895         iir = data[0];
896         if (!(iir & 0x01)) {    /* serial port interrupt pending */
897                 switch (iir & 0x0f) {
898                 case SERIAL_IIR_RLS:
899                         dbg("Serial Port: Receiver status error or address "
900                             "bit detected in 9-bit mode\n");
901                         break;
902                 case SERIAL_IIR_CTI:
903                         dbg("Serial Port: Receiver time out");
904                         break;
905                 case SERIAL_IIR_MS:
906                         /* dbg("Serial Port: Modem status change"); */
907                         break;
908                 }
909         }
910
911 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
912         {       /* update local copy of DSR reg */
913                 struct usb_serial_port *port = urb->context;
914                 struct mos7715_parport *mos_parport = port->serial->private;
915                 if (unlikely(mos_parport == NULL))
916                         return;
917                 atomic_set(&mos_parport->shadowDSR, data[2]);
918         }
919 #endif
920
921 exit:
922         result = usb_submit_urb(urb, GFP_ATOMIC);
923         if (result)
924                 dev_err(&urb->dev->dev,
925                         "%s - Error %d submitting control urb\n",
926                         __func__, result);
927 }
928
929 /*
930  * mos7720_bulk_in_callback
931  *      this is the callback function for when we have received data on the
932  *      bulk in endpoint.
933  */
934 static void mos7720_bulk_in_callback(struct urb *urb)
935 {
936         int retval;
937         unsigned char *data ;
938         struct usb_serial_port *port;
939         struct tty_struct *tty;
940         int status = urb->status;
941
942         if (status) {
943                 dbg("nonzero read bulk status received: %d", status);
944                 return;
945         }
946
947         port = urb->context;
948
949         dbg("Entering...%s", __func__);
950
951         data = urb->transfer_buffer;
952
953         tty = tty_port_tty_get(&port->port);
954         if (tty && urb->actual_length) {
955                 tty_insert_flip_string(tty, data, urb->actual_length);
956                 tty_flip_buffer_push(tty);
957         }
958         tty_kref_put(tty);
959
960         if (!port->read_urb) {
961                 dbg("URB KILLED !!!");
962                 return;
963         }
964
965         if (port->read_urb->status != -EINPROGRESS) {
966                 port->read_urb->dev = port->serial->dev;
967
968                 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
969                 if (retval)
970                         dbg("usb_submit_urb(read bulk) failed, retval = %d",
971                             retval);
972         }
973 }
974
975 /*
976  * mos7720_bulk_out_data_callback
977  *      this is the callback function for when we have finished sending serial
978  *      data on the bulk out endpoint.
979  */
980 static void mos7720_bulk_out_data_callback(struct urb *urb)
981 {
982         struct moschip_port *mos7720_port;
983         struct tty_struct *tty;
984         int status = urb->status;
985
986         if (status) {
987                 dbg("nonzero write bulk status received:%d", status);
988                 return;
989         }
990
991         mos7720_port = urb->context;
992         if (!mos7720_port) {
993                 dbg("NULL mos7720_port pointer");
994                 return ;
995         }
996
997         tty = tty_port_tty_get(&mos7720_port->port->port);
998
999         if (tty && mos7720_port->open)
1000                 tty_wakeup(tty);
1001         tty_kref_put(tty);
1002 }
1003
1004 /*
1005  * mos77xx_probe
1006  *      this function installs the appropriate read interrupt endpoint callback
1007  *      depending on whether the device is a 7720 or 7715, thus avoiding costly
1008  *      run-time checks in the high-frequency callback routine itself.
1009  */
1010 static int mos77xx_probe(struct usb_serial *serial,
1011                          const struct usb_device_id *id)
1012 {
1013         if (id->idProduct == MOSCHIP_DEVICE_ID_7715)
1014                 moschip7720_2port_driver.read_int_callback =
1015                         mos7715_interrupt_callback;
1016         else
1017                 moschip7720_2port_driver.read_int_callback =
1018                         mos7720_interrupt_callback;
1019
1020         return 0;
1021 }
1022
1023 static int mos77xx_calc_num_ports(struct usb_serial *serial)
1024 {
1025         u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
1026         if (product == MOSCHIP_DEVICE_ID_7715)
1027                 return 1;
1028
1029         return 2;
1030 }
1031
1032 static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
1033 {
1034         struct usb_serial *serial;
1035         struct usb_serial_port *port0;
1036         struct urb *urb;
1037         struct moschip_port *mos7720_port;
1038         int response;
1039         int port_number;
1040         __u8 data;
1041         int allocated_urbs = 0;
1042         int j;
1043
1044         serial = port->serial;
1045
1046         mos7720_port = usb_get_serial_port_data(port);
1047         if (mos7720_port == NULL)
1048                 return -ENODEV;
1049
1050         port0 = serial->port[0];
1051
1052         usb_clear_halt(serial->dev, port->write_urb->pipe);
1053         usb_clear_halt(serial->dev, port->read_urb->pipe);
1054
1055         /* Initialising the write urb pool */
1056         for (j = 0; j < NUM_URBS; ++j) {
1057                 urb = usb_alloc_urb(0, GFP_KERNEL);
1058                 mos7720_port->write_urb_pool[j] = urb;
1059
1060                 if (urb == NULL) {
1061                         dev_err(&port->dev, "No more urbs???\n");
1062                         continue;
1063                 }
1064
1065                 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1066                                                GFP_KERNEL);
1067                 if (!urb->transfer_buffer) {
1068                         dev_err(&port->dev,
1069                                 "%s-out of memory for urb buffers.\n",
1070                                 __func__);
1071                         usb_free_urb(mos7720_port->write_urb_pool[j]);
1072                         mos7720_port->write_urb_pool[j] = NULL;
1073                         continue;
1074                 }
1075                 allocated_urbs++;
1076         }
1077
1078         if (!allocated_urbs)
1079                 return -ENOMEM;
1080
1081          /* Initialize MCS7720 -- Write Init values to corresponding Registers
1082           *
1083           * Register Index
1084           * 0 : THR/RHR
1085           * 1 : IER
1086           * 2 : FCR
1087           * 3 : LCR
1088           * 4 : MCR
1089           * 5 : LSR
1090           * 6 : MSR
1091           * 7 : SPR
1092           *
1093           * 0x08 : SP1/2 Control Reg
1094           */
1095         port_number = port->number - port->serial->minor;
1096         read_mos_reg(serial, port_number, LSR, &data);
1097
1098         dbg("SS::%p LSR:%x", mos7720_port, data);
1099
1100         dbg("Check:Sending Command ..........");
1101
1102         write_mos_reg(serial, dummy, SP1_REG, 0x02);
1103         write_mos_reg(serial, dummy, SP2_REG, 0x02);
1104
1105         write_mos_reg(serial, port_number, IER, 0x00);
1106         write_mos_reg(serial, port_number, FCR, 0x00);
1107
1108         write_mos_reg(serial, port_number, FCR, 0xcf);
1109         mos7720_port->shadowLCR = 0x03;
1110         write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1111         mos7720_port->shadowMCR = 0x0b;
1112         write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1113
1114         write_mos_reg(serial, port_number, SP_CONTROL_REG, 0x00);
1115         read_mos_reg(serial, dummy, SP_CONTROL_REG, &data);
1116         data = data | (port->number - port->serial->minor + 1);
1117         write_mos_reg(serial, dummy, SP_CONTROL_REG, data);
1118         mos7720_port->shadowLCR = 0x83;
1119         write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1120         write_mos_reg(serial, port_number, THR, 0x0c);
1121         write_mos_reg(serial, port_number, IER, 0x00);
1122         mos7720_port->shadowLCR = 0x03;
1123         write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1124         write_mos_reg(serial, port_number, IER, 0x0c);
1125
1126         response = usb_submit_urb(port->read_urb, GFP_KERNEL);
1127         if (response)
1128                 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
1129                                                         __func__, response);
1130
1131         /* initialize our icount structure */
1132         memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
1133
1134         /* initialize our port settings */
1135         mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
1136
1137         /* send a open port command */
1138         mos7720_port->open = 1;
1139
1140         return 0;
1141 }
1142
1143 /*
1144  * mos7720_chars_in_buffer
1145  *      this function is called by the tty driver when it wants to know how many
1146  *      bytes of data we currently have outstanding in the port (data that has
1147  *      been written, but hasn't made it out the port yet)
1148  *      If successful, we return the number of bytes left to be written in the
1149  *      system,
1150  *      Otherwise we return a negative error number.
1151  */
1152 static int mos7720_chars_in_buffer(struct tty_struct *tty)
1153 {
1154         struct usb_serial_port *port = tty->driver_data;
1155         int i;
1156         int chars = 0;
1157         struct moschip_port *mos7720_port;
1158
1159         dbg("%s:entering ...........", __func__);
1160
1161         mos7720_port = usb_get_serial_port_data(port);
1162         if (mos7720_port == NULL) {
1163                 dbg("%s:leaving ...........", __func__);
1164                 return 0;
1165         }
1166
1167         for (i = 0; i < NUM_URBS; ++i) {
1168                 if (mos7720_port->write_urb_pool[i] &&
1169                     mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
1170                         chars += URB_TRANSFER_BUFFER_SIZE;
1171         }
1172         dbg("%s - returns %d", __func__, chars);
1173         return chars;
1174 }
1175
1176 static void mos7720_close(struct usb_serial_port *port)
1177 {
1178         struct usb_serial *serial;
1179         struct moschip_port *mos7720_port;
1180         int j;
1181
1182         dbg("mos7720_close:entering...");
1183
1184         serial = port->serial;
1185
1186         mos7720_port = usb_get_serial_port_data(port);
1187         if (mos7720_port == NULL)
1188                 return;
1189
1190         for (j = 0; j < NUM_URBS; ++j)
1191                 usb_kill_urb(mos7720_port->write_urb_pool[j]);
1192
1193         /* Freeing Write URBs */
1194         for (j = 0; j < NUM_URBS; ++j) {
1195                 if (mos7720_port->write_urb_pool[j]) {
1196                         kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
1197                         usb_free_urb(mos7720_port->write_urb_pool[j]);
1198                 }
1199         }
1200
1201         /* While closing port, shutdown all bulk read, write  *
1202          * and interrupt read if they exists, otherwise nop   */
1203         dbg("Shutdown bulk write");
1204         usb_kill_urb(port->write_urb);
1205         dbg("Shutdown bulk read");
1206         usb_kill_urb(port->read_urb);
1207
1208         mutex_lock(&serial->disc_mutex);
1209         /* these commands must not be issued if the device has
1210          * been disconnected */
1211         if (!serial->disconnected) {
1212                 write_mos_reg(serial, port->number - port->serial->minor,
1213                               MCR, 0x00);
1214                 write_mos_reg(serial, port->number - port->serial->minor,
1215                               IER, 0x00);
1216         }
1217         mutex_unlock(&serial->disc_mutex);
1218         mos7720_port->open = 0;
1219
1220         dbg("Leaving %s", __func__);
1221 }
1222
1223 static void mos7720_break(struct tty_struct *tty, int break_state)
1224 {
1225         struct usb_serial_port *port = tty->driver_data;
1226         unsigned char data;
1227         struct usb_serial *serial;
1228         struct moschip_port *mos7720_port;
1229
1230         dbg("Entering %s", __func__);
1231
1232         serial = port->serial;
1233
1234         mos7720_port = usb_get_serial_port_data(port);
1235         if (mos7720_port == NULL)
1236                 return;
1237
1238         if (break_state == -1)
1239                 data = mos7720_port->shadowLCR | UART_LCR_SBC;
1240         else
1241                 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
1242
1243         mos7720_port->shadowLCR  = data;
1244         write_mos_reg(serial, port->number - port->serial->minor,
1245                       LCR, mos7720_port->shadowLCR);
1246 }
1247
1248 /*
1249  * mos7720_write_room
1250  *      this function is called by the tty driver when it wants to know how many
1251  *      bytes of data we can accept for a specific port.
1252  *      If successful, we return the amount of room that we have for this port
1253  *      Otherwise we return a negative error number.
1254  */
1255 static int mos7720_write_room(struct tty_struct *tty)
1256 {
1257         struct usb_serial_port *port = tty->driver_data;
1258         struct moschip_port *mos7720_port;
1259         int room = 0;
1260         int i;
1261
1262         dbg("%s:entering ...........", __func__);
1263
1264         mos7720_port = usb_get_serial_port_data(port);
1265         if (mos7720_port == NULL) {
1266                 dbg("%s:leaving ...........", __func__);
1267                 return -ENODEV;
1268         }
1269
1270         /* FIXME: Locking */
1271         for (i = 0; i < NUM_URBS; ++i) {
1272                 if (mos7720_port->write_urb_pool[i] &&
1273                     mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
1274                         room += URB_TRANSFER_BUFFER_SIZE;
1275         }
1276
1277         dbg("%s - returns %d", __func__, room);
1278         return room;
1279 }
1280
1281 static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
1282                                  const unsigned char *data, int count)
1283 {
1284         int status;
1285         int i;
1286         int bytes_sent = 0;
1287         int transfer_size;
1288
1289         struct moschip_port *mos7720_port;
1290         struct usb_serial *serial;
1291         struct urb    *urb;
1292         const unsigned char *current_position = data;
1293
1294         dbg("%s:entering ...........", __func__);
1295
1296         serial = port->serial;
1297
1298         mos7720_port = usb_get_serial_port_data(port);
1299         if (mos7720_port == NULL) {
1300                 dbg("mos7720_port is NULL");
1301                 return -ENODEV;
1302         }
1303
1304         /* try to find a free urb in the list */
1305         urb = NULL;
1306
1307         for (i = 0; i < NUM_URBS; ++i) {
1308                 if (mos7720_port->write_urb_pool[i] &&
1309                     mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
1310                         urb = mos7720_port->write_urb_pool[i];
1311                         dbg("URB:%d", i);
1312                         break;
1313                 }
1314         }
1315
1316         if (urb == NULL) {
1317                 dbg("%s - no more free urbs", __func__);
1318                 goto exit;
1319         }
1320
1321         if (urb->transfer_buffer == NULL) {
1322                 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1323                                                GFP_ATOMIC);
1324                 if (urb->transfer_buffer == NULL) {
1325                         dev_err(&port->dev, "%s no more kernel memory...\n",
1326                                 __func__);
1327                         goto exit;
1328                 }
1329         }
1330         transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1331
1332         memcpy(urb->transfer_buffer, current_position, transfer_size);
1333         usb_serial_debug_data(debug, &port->dev, __func__, transfer_size,
1334                               urb->transfer_buffer);
1335
1336         /* fill urb with data and submit  */
1337         usb_fill_bulk_urb(urb, serial->dev,
1338                           usb_sndbulkpipe(serial->dev,
1339                                         port->bulk_out_endpointAddress),
1340                           urb->transfer_buffer, transfer_size,
1341                           mos7720_bulk_out_data_callback, mos7720_port);
1342
1343         /* send it down the pipe */
1344         status = usb_submit_urb(urb, GFP_ATOMIC);
1345         if (status) {
1346                 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
1347                         "with status = %d\n", __func__, status);
1348                 bytes_sent = status;
1349                 goto exit;
1350         }
1351         bytes_sent = transfer_size;
1352
1353 exit:
1354         return bytes_sent;
1355 }
1356
1357 static void mos7720_throttle(struct tty_struct *tty)
1358 {
1359         struct usb_serial_port *port = tty->driver_data;
1360         struct moschip_port *mos7720_port;
1361         int status;
1362
1363         dbg("%s- port %d", __func__, port->number);
1364
1365         mos7720_port = usb_get_serial_port_data(port);
1366
1367         if (mos7720_port == NULL)
1368                 return;
1369
1370         if (!mos7720_port->open) {
1371                 dbg("port not opened");
1372                 return;
1373         }
1374
1375         dbg("%s: Entering ..........", __func__);
1376
1377         /* if we are implementing XON/XOFF, send the stop character */
1378         if (I_IXOFF(tty)) {
1379                 unsigned char stop_char = STOP_CHAR(tty);
1380                 status = mos7720_write(tty, port, &stop_char, 1);
1381                 if (status <= 0)
1382                         return;
1383         }
1384
1385         /* if we are implementing RTS/CTS, toggle that line */
1386         if (tty->termios->c_cflag & CRTSCTS) {
1387                 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
1388                 write_mos_reg(port->serial, port->number - port->serial->minor,
1389                               MCR, mos7720_port->shadowMCR);
1390                 if (status != 0)
1391                         return;
1392         }
1393 }
1394
1395 static void mos7720_unthrottle(struct tty_struct *tty)
1396 {
1397         struct usb_serial_port *port = tty->driver_data;
1398         struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1399         int status;
1400
1401         if (mos7720_port == NULL)
1402                 return;
1403
1404         if (!mos7720_port->open) {
1405                 dbg("%s - port not opened", __func__);
1406                 return;
1407         }
1408
1409         dbg("%s: Entering ..........", __func__);
1410
1411         /* if we are implementing XON/XOFF, send the start character */
1412         if (I_IXOFF(tty)) {
1413                 unsigned char start_char = START_CHAR(tty);
1414                 status = mos7720_write(tty, port, &start_char, 1);
1415                 if (status <= 0)
1416                         return;
1417         }
1418
1419         /* if we are implementing RTS/CTS, toggle that line */
1420         if (tty->termios->c_cflag & CRTSCTS) {
1421                 mos7720_port->shadowMCR |= UART_MCR_RTS;
1422                 write_mos_reg(port->serial, port->number - port->serial->minor,
1423                               MCR, mos7720_port->shadowMCR);
1424                 if (status != 0)
1425                         return;
1426         }
1427 }
1428
1429 /* FIXME: this function does not work */
1430 static int set_higher_rates(struct moschip_port *mos7720_port,
1431                             unsigned int baud)
1432 {
1433         struct usb_serial_port *port;
1434         struct usb_serial *serial;
1435         int port_number;
1436         enum mos_regs sp_reg;
1437         if (mos7720_port == NULL)
1438                 return -EINVAL;
1439
1440         port = mos7720_port->port;
1441         serial = port->serial;
1442
1443          /***********************************************
1444          *      Init Sequence for higher rates
1445          ***********************************************/
1446         dbg("Sending Setting Commands ..........");
1447         port_number = port->number - port->serial->minor;
1448
1449         write_mos_reg(serial, port_number, IER, 0x00);
1450         write_mos_reg(serial, port_number, FCR, 0x00);
1451         write_mos_reg(serial, port_number, FCR, 0xcf);
1452         mos7720_port->shadowMCR = 0x0b;
1453         write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1454         write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x00);
1455
1456         /***********************************************
1457          *              Set for higher rates           *
1458          ***********************************************/
1459         /* writing baud rate verbatum into uart clock field clearly not right */
1460         if (port_number == 0)
1461                 sp_reg = SP1_REG;
1462         else
1463                 sp_reg = SP2_REG;
1464         write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
1465         write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x03);
1466         mos7720_port->shadowMCR = 0x2b;
1467         write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1468
1469         /***********************************************
1470          *              Set DLL/DLM
1471          ***********************************************/
1472         mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1473         write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1474         write_mos_reg(serial, port_number, DLL, 0x01);
1475         write_mos_reg(serial, port_number, DLM, 0x00);
1476         mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1477         write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1478
1479         return 0;
1480 }
1481
1482 /* baud rate information */
1483 struct divisor_table_entry {
1484         __u32  baudrate;
1485         __u16  divisor;
1486 };
1487
1488 /* Define table of divisors for moschip 7720 hardware      *
1489  * These assume a 3.6864MHz crystal, the standard /16, and *
1490  * MCR.7 = 0.                                              */
1491 static struct divisor_table_entry divisor_table[] = {
1492         {   50,         2304},
1493         {   110,        1047},  /* 2094.545455 => 230450   => .0217 % over */
1494         {   134,        857},   /* 1713.011152 => 230398.5 => .00065% under */
1495         {   150,        768},
1496         {   300,        384},
1497         {   600,        192},
1498         {   1200,       96},
1499         {   1800,       64},
1500         {   2400,       48},
1501         {   4800,       24},
1502         {   7200,       16},
1503         {   9600,       12},
1504         {   19200,      6},
1505         {   38400,      3},
1506         {   57600,      2},
1507         {   115200,     1},
1508 };
1509
1510 /*****************************************************************************
1511  * calc_baud_rate_divisor
1512  *      this function calculates the proper baud rate divisor for the specified
1513  *      baud rate.
1514  *****************************************************************************/
1515 static int calc_baud_rate_divisor(int baudrate, int *divisor)
1516 {
1517         int i;
1518         __u16 custom;
1519         __u16 round1;
1520         __u16 round;
1521
1522
1523         dbg("%s - %d", __func__, baudrate);
1524
1525         for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
1526                 if (divisor_table[i].baudrate == baudrate) {
1527                         *divisor = divisor_table[i].divisor;
1528                         return 0;
1529                 }
1530         }
1531
1532         /* After trying for all the standard baud rates    *
1533          * Try calculating the divisor for this baud rate  */
1534         if (baudrate > 75 &&  baudrate < 230400) {
1535                 /* get the divisor */
1536                 custom = (__u16)(230400L  / baudrate);
1537
1538                 /* Check for round off */
1539                 round1 = (__u16)(2304000L / baudrate);
1540                 round = (__u16)(round1 - (custom * 10));
1541                 if (round > 4)
1542                         custom++;
1543                 *divisor = custom;
1544
1545                 dbg("Baud %d = %d", baudrate, custom);
1546                 return 0;
1547         }
1548
1549         dbg("Baud calculation Failed...");
1550         return -EINVAL;
1551 }
1552
1553 /*
1554  * send_cmd_write_baud_rate
1555  *      this function sends the proper command to change the baud rate of the
1556  *      specified port.
1557  */
1558 static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1559                                     int baudrate)
1560 {
1561         struct usb_serial_port *port;
1562         struct usb_serial *serial;
1563         int divisor;
1564         int status;
1565         unsigned char number;
1566
1567         if (mos7720_port == NULL)
1568                 return -1;
1569
1570         port = mos7720_port->port;
1571         serial = port->serial;
1572
1573         dbg("%s: Entering ..........", __func__);
1574
1575         number = port->number - port->serial->minor;
1576         dbg("%s - port = %d, baud = %d", __func__, port->number, baudrate);
1577
1578         /* Calculate the Divisor */
1579         status = calc_baud_rate_divisor(baudrate, &divisor);
1580         if (status) {
1581                 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1582                 return status;
1583         }
1584
1585         /* Enable access to divisor latch */
1586         mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1587         write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
1588
1589         /* Write the divisor */
1590         write_mos_reg(serial, number, DLL, (__u8)(divisor & 0xff));
1591         write_mos_reg(serial, number, DLM, (__u8)((divisor & 0xff00) >> 8));
1592
1593         /* Disable access to divisor latch */
1594         mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1595         write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
1596
1597         return status;
1598 }
1599
1600 /*
1601  * change_port_settings
1602  *      This routine is called to set the UART on the device to match
1603  *      the specified new settings.
1604  */
1605 static void change_port_settings(struct tty_struct *tty,
1606                                  struct moschip_port *mos7720_port,
1607                                  struct ktermios *old_termios)
1608 {
1609         struct usb_serial_port *port;
1610         struct usb_serial *serial;
1611         int baud;
1612         unsigned cflag;
1613         unsigned iflag;
1614         __u8 mask = 0xff;
1615         __u8 lData;
1616         __u8 lParity;
1617         __u8 lStop;
1618         int status;
1619         int port_number;
1620
1621         if (mos7720_port == NULL)
1622                 return ;
1623
1624         port = mos7720_port->port;
1625         serial = port->serial;
1626         port_number = port->number - port->serial->minor;
1627
1628         dbg("%s - port %d", __func__, port->number);
1629
1630         if (!mos7720_port->open) {
1631                 dbg("%s - port not opened", __func__);
1632                 return;
1633         }
1634
1635         dbg("%s: Entering ..........", __func__);
1636
1637         lData = UART_LCR_WLEN8;
1638         lStop = 0x00;   /* 1 stop bit */
1639         lParity = 0x00; /* No parity */
1640
1641         cflag = tty->termios->c_cflag;
1642         iflag = tty->termios->c_iflag;
1643
1644         /* Change the number of bits */
1645         switch (cflag & CSIZE) {
1646         case CS5:
1647                 lData = UART_LCR_WLEN5;
1648                 mask = 0x1f;
1649                 break;
1650
1651         case CS6:
1652                 lData = UART_LCR_WLEN6;
1653                 mask = 0x3f;
1654                 break;
1655
1656         case CS7:
1657                 lData = UART_LCR_WLEN7;
1658                 mask = 0x7f;
1659                 break;
1660         default:
1661         case CS8:
1662                 lData = UART_LCR_WLEN8;
1663                 break;
1664         }
1665
1666         /* Change the Parity bit */
1667         if (cflag & PARENB) {
1668                 if (cflag & PARODD) {
1669                         lParity = UART_LCR_PARITY;
1670                         dbg("%s - parity = odd", __func__);
1671                 } else {
1672                         lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
1673                         dbg("%s - parity = even", __func__);
1674                 }
1675
1676         } else {
1677                 dbg("%s - parity = none", __func__);
1678         }
1679
1680         if (cflag & CMSPAR)
1681                 lParity = lParity | 0x20;
1682
1683         /* Change the Stop bit */
1684         if (cflag & CSTOPB) {
1685                 lStop = UART_LCR_STOP;
1686                 dbg("%s - stop bits = 2", __func__);
1687         } else {
1688                 lStop = 0x00;
1689                 dbg("%s - stop bits = 1", __func__);
1690         }
1691
1692 #define LCR_BITS_MASK           0x03    /* Mask for bits/char field */
1693 #define LCR_STOP_MASK           0x04    /* Mask for stop bits field */
1694 #define LCR_PAR_MASK            0x38    /* Mask for parity field */
1695
1696         /* Update the LCR with the correct value */
1697         mos7720_port->shadowLCR &=
1698                 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1699         mos7720_port->shadowLCR |= (lData | lParity | lStop);
1700
1701
1702         /* Disable Interrupts */
1703         write_mos_reg(serial, port_number, IER, 0x00);
1704         write_mos_reg(serial, port_number, FCR, 0x00);
1705         write_mos_reg(serial, port_number, FCR, 0xcf);
1706
1707         /* Send the updated LCR value to the mos7720 */
1708         write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1709         mos7720_port->shadowMCR = 0x0b;
1710         write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1711
1712         /* set up the MCR register and send it to the mos7720 */
1713         mos7720_port->shadowMCR = UART_MCR_OUT2;
1714         if (cflag & CBAUD)
1715                 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1716
1717         if (cflag & CRTSCTS) {
1718                 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
1719                 /* To set hardware flow control to the specified *
1720                  * serial port, in SP1/2_CONTROL_REG             */
1721                 if (port_number)
1722                         write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x01);
1723                 else
1724                         write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x02);
1725
1726         } else
1727                 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1728
1729         write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1730
1731         /* Determine divisor based on baud rate */
1732         baud = tty_get_baud_rate(tty);
1733         if (!baud) {
1734                 /* pick a default, any default... */
1735                 dbg("Picked default baud...");
1736                 baud = 9600;
1737         }
1738
1739         if (baud >= 230400) {
1740                 set_higher_rates(mos7720_port, baud);
1741                 /* Enable Interrupts */
1742                 write_mos_reg(serial, port_number, IER, 0x0c);
1743                 return;
1744         }
1745
1746         dbg("%s - baud rate = %d", __func__, baud);
1747         status = send_cmd_write_baud_rate(mos7720_port, baud);
1748         /* FIXME: needs to write actual resulting baud back not just
1749            blindly do so */
1750         if (cflag & CBAUD)
1751                 tty_encode_baud_rate(tty, baud, baud);
1752         /* Enable Interrupts */
1753         write_mos_reg(serial, port_number, IER, 0x0c);
1754
1755         if (port->read_urb->status != -EINPROGRESS) {
1756                 port->read_urb->dev = serial->dev;
1757
1758                 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1759                 if (status)
1760                         dbg("usb_submit_urb(read bulk) failed, status = %d",
1761                             status);
1762         }
1763 }
1764
1765 /*
1766  * mos7720_set_termios
1767  *      this function is called by the tty driver when it wants to change the
1768  *      termios structure.
1769  */
1770 static void mos7720_set_termios(struct tty_struct *tty,
1771                 struct usb_serial_port *port, struct ktermios *old_termios)
1772 {
1773         int status;
1774         unsigned int cflag;
1775         struct usb_serial *serial;
1776         struct moschip_port *mos7720_port;
1777
1778         serial = port->serial;
1779
1780         mos7720_port = usb_get_serial_port_data(port);
1781
1782         if (mos7720_port == NULL)
1783                 return;
1784
1785         if (!mos7720_port->open) {
1786                 dbg("%s - port not opened", __func__);
1787                 return;
1788         }
1789
1790         dbg("%s\n", "setting termios - ASPIRE");
1791
1792         cflag = tty->termios->c_cflag;
1793
1794         dbg("%s - cflag %08x iflag %08x", __func__,
1795             tty->termios->c_cflag,
1796             RELEVANT_IFLAG(tty->termios->c_iflag));
1797
1798         dbg("%s - old cflag %08x old iflag %08x", __func__,
1799             old_termios->c_cflag,
1800             RELEVANT_IFLAG(old_termios->c_iflag));
1801
1802         dbg("%s - port %d", __func__, port->number);
1803
1804         /* change the port settings to the new ones specified */
1805         change_port_settings(tty, mos7720_port, old_termios);
1806
1807         if (!port->read_urb) {
1808                 dbg("%s", "URB KILLED !!!!!");
1809                 return;
1810         }
1811
1812         if (port->read_urb->status != -EINPROGRESS) {
1813                 port->read_urb->dev = serial->dev;
1814                 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1815                 if (status)
1816                         dbg("usb_submit_urb(read bulk) failed, status = %d",
1817                             status);
1818         }
1819 }
1820
1821 /*
1822  * get_lsr_info - get line status register info
1823  *
1824  * Purpose: Let user call ioctl() to get info when the UART physically
1825  *          is emptied.  On bus types like RS485, the transmitter must
1826  *          release the bus after transmitting. This must be done when
1827  *          the transmit shift register is empty, not be done when the
1828  *          transmit holding register is empty.  This functionality
1829  *          allows an RS485 driver to be written in user space.
1830  */
1831 static int get_lsr_info(struct tty_struct *tty,
1832                 struct moschip_port *mos7720_port, unsigned int __user *value)
1833 {
1834         struct usb_serial_port *port = tty->driver_data;
1835         unsigned int result = 0;
1836         unsigned char data = 0;
1837         int port_number = port->number - port->serial->minor;
1838         int count;
1839
1840         count = mos7720_chars_in_buffer(tty);
1841         if (count == 0) {
1842                 read_mos_reg(port->serial, port_number, LSR, &data);
1843                 if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1844                                         == (UART_LSR_TEMT | UART_LSR_THRE)) {
1845                         dbg("%s -- Empty", __func__);
1846                         result = TIOCSER_TEMT;
1847                 }
1848         }
1849         if (copy_to_user(value, &result, sizeof(int)))
1850                 return -EFAULT;
1851         return 0;
1852 }
1853
1854 static int mos7720_tiocmget(struct tty_struct *tty)
1855 {
1856         struct usb_serial_port *port = tty->driver_data;
1857         struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1858         unsigned int result = 0;
1859         unsigned int mcr ;
1860         unsigned int msr ;
1861
1862         dbg("%s - port %d", __func__, port->number);
1863
1864         mcr = mos7720_port->shadowMCR;
1865         msr = mos7720_port->shadowMSR;
1866
1867         result = ((mcr & UART_MCR_DTR)  ? TIOCM_DTR : 0)   /* 0x002 */
1868           | ((mcr & UART_MCR_RTS)   ? TIOCM_RTS : 0)   /* 0x004 */
1869           | ((msr & UART_MSR_CTS)   ? TIOCM_CTS : 0)   /* 0x020 */
1870           | ((msr & UART_MSR_DCD)   ? TIOCM_CAR : 0)   /* 0x040 */
1871           | ((msr & UART_MSR_RI)    ? TIOCM_RI :  0)   /* 0x080 */
1872           | ((msr & UART_MSR_DSR)   ? TIOCM_DSR : 0);  /* 0x100 */
1873
1874         dbg("%s -- %x", __func__, result);
1875
1876         return result;
1877 }
1878
1879 static int mos7720_tiocmset(struct tty_struct *tty,
1880                             unsigned int set, unsigned int clear)
1881 {
1882         struct usb_serial_port *port = tty->driver_data;
1883         struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1884         unsigned int mcr ;
1885         dbg("%s - port %d", __func__, port->number);
1886         dbg("he was at tiocmset");
1887
1888         mcr = mos7720_port->shadowMCR;
1889
1890         if (set & TIOCM_RTS)
1891                 mcr |= UART_MCR_RTS;
1892         if (set & TIOCM_DTR)
1893                 mcr |= UART_MCR_DTR;
1894         if (set & TIOCM_LOOP)
1895                 mcr |= UART_MCR_LOOP;
1896
1897         if (clear & TIOCM_RTS)
1898                 mcr &= ~UART_MCR_RTS;
1899         if (clear & TIOCM_DTR)
1900                 mcr &= ~UART_MCR_DTR;
1901         if (clear & TIOCM_LOOP)
1902                 mcr &= ~UART_MCR_LOOP;
1903
1904         mos7720_port->shadowMCR = mcr;
1905         write_mos_reg(port->serial, port->number - port->serial->minor,
1906                       MCR, mos7720_port->shadowMCR);
1907
1908         return 0;
1909 }
1910
1911 static int mos7720_get_icount(struct tty_struct *tty,
1912                                 struct serial_icounter_struct *icount)
1913 {
1914         struct usb_serial_port *port = tty->driver_data;
1915         struct moschip_port *mos7720_port;
1916         struct async_icount cnow;
1917
1918         mos7720_port = usb_get_serial_port_data(port);
1919         cnow = mos7720_port->icount;
1920
1921         icount->cts = cnow.cts;
1922         icount->dsr = cnow.dsr;
1923         icount->rng = cnow.rng;
1924         icount->dcd = cnow.dcd;
1925         icount->rx = cnow.rx;
1926         icount->tx = cnow.tx;
1927         icount->frame = cnow.frame;
1928         icount->overrun = cnow.overrun;
1929         icount->parity = cnow.parity;
1930         icount->brk = cnow.brk;
1931         icount->buf_overrun = cnow.buf_overrun;
1932
1933         dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
1934                 port->number, icount->rx, icount->tx);
1935         return 0;
1936 }
1937
1938 static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1939                           unsigned int __user *value)
1940 {
1941         unsigned int mcr;
1942         unsigned int arg;
1943
1944         struct usb_serial_port *port;
1945
1946         if (mos7720_port == NULL)
1947                 return -1;
1948
1949         port = (struct usb_serial_port *)mos7720_port->port;
1950         mcr = mos7720_port->shadowMCR;
1951
1952         if (copy_from_user(&arg, value, sizeof(int)))
1953                 return -EFAULT;
1954
1955         switch (cmd) {
1956         case TIOCMBIS:
1957                 if (arg & TIOCM_RTS)
1958                         mcr |= UART_MCR_RTS;
1959                 if (arg & TIOCM_DTR)
1960                         mcr |= UART_MCR_RTS;
1961                 if (arg & TIOCM_LOOP)
1962                         mcr |= UART_MCR_LOOP;
1963                 break;
1964
1965         case TIOCMBIC:
1966                 if (arg & TIOCM_RTS)
1967                         mcr &= ~UART_MCR_RTS;
1968                 if (arg & TIOCM_DTR)
1969                         mcr &= ~UART_MCR_RTS;
1970                 if (arg & TIOCM_LOOP)
1971                         mcr &= ~UART_MCR_LOOP;
1972                 break;
1973
1974         }
1975
1976         mos7720_port->shadowMCR = mcr;
1977         write_mos_reg(port->serial, port->number - port->serial->minor,
1978                       MCR, mos7720_port->shadowMCR);
1979
1980         return 0;
1981 }
1982
1983 static int get_serial_info(struct moschip_port *mos7720_port,
1984                            struct serial_struct __user *retinfo)
1985 {
1986         struct serial_struct tmp;
1987
1988         if (!retinfo)
1989                 return -EFAULT;
1990
1991         memset(&tmp, 0, sizeof(tmp));
1992
1993         tmp.type                = PORT_16550A;
1994         tmp.line                = mos7720_port->port->serial->minor;
1995         tmp.port                = mos7720_port->port->number;
1996         tmp.irq                 = 0;
1997         tmp.flags               = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1998         tmp.xmit_fifo_size      = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1999         tmp.baud_base           = 9600;
2000         tmp.close_delay         = 5*HZ;
2001         tmp.closing_wait        = 30*HZ;
2002
2003         if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2004                 return -EFAULT;
2005         return 0;
2006 }
2007
2008 static int mos7720_ioctl(struct tty_struct *tty,
2009                          unsigned int cmd, unsigned long arg)
2010 {
2011         struct usb_serial_port *port = tty->driver_data;
2012         struct moschip_port *mos7720_port;
2013         struct async_icount cnow;
2014         struct async_icount cprev;
2015
2016         mos7720_port = usb_get_serial_port_data(port);
2017         if (mos7720_port == NULL)
2018                 return -ENODEV;
2019
2020         dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
2021
2022         switch (cmd) {
2023         case TIOCSERGETLSR:
2024                 dbg("%s (%d) TIOCSERGETLSR", __func__,  port->number);
2025                 return get_lsr_info(tty, mos7720_port,
2026                                         (unsigned int __user *)arg);
2027
2028         /* FIXME: These should be using the mode methods */
2029         case TIOCMBIS:
2030         case TIOCMBIC:
2031                 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET",
2032                                         __func__, port->number);
2033                 return set_modem_info(mos7720_port, cmd,
2034                                       (unsigned int __user *)arg);
2035
2036         case TIOCGSERIAL:
2037                 dbg("%s (%d) TIOCGSERIAL", __func__,  port->number);
2038                 return get_serial_info(mos7720_port,
2039                                        (struct serial_struct __user *)arg);
2040
2041         case TIOCMIWAIT:
2042                 dbg("%s (%d) TIOCMIWAIT", __func__,  port->number);
2043                 cprev = mos7720_port->icount;
2044                 while (1) {
2045                         if (signal_pending(current))
2046                                 return -ERESTARTSYS;
2047                         cnow = mos7720_port->icount;
2048                         if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2049                             cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2050                                 return -EIO; /* no change => error */
2051                         if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2052                             ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2053                             ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
2054                             ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2055                                 return 0;
2056                         }
2057                         cprev = cnow;
2058                 }
2059                 /* NOTREACHED */
2060                 break;
2061         }
2062
2063         return -ENOIOCTLCMD;
2064 }
2065
2066 static int mos7720_startup(struct usb_serial *serial)
2067 {
2068         struct moschip_port *mos7720_port;
2069         struct usb_device *dev;
2070         int i;
2071         char data;
2072         u16 product;
2073         int ret_val;
2074
2075         dbg("%s: Entering ..........", __func__);
2076
2077         if (!serial) {
2078                 dbg("Invalid Handler");
2079                 return -ENODEV;
2080         }
2081
2082         if (serial->num_bulk_in < 2 || serial->num_bulk_out < 2) {
2083                 dev_err(&serial->interface->dev, "missing bulk endpoints\n");
2084                 return -ENODEV;
2085         }
2086
2087         product = le16_to_cpu(serial->dev->descriptor.idProduct);
2088         dev = serial->dev;
2089
2090         /*
2091          * The 7715 uses the first bulk in/out endpoint pair for the parallel
2092          * port, and the second for the serial port.  Because the usbserial core
2093          * assumes both pairs are serial ports, we must engage in a bit of
2094          * subterfuge and swap the pointers for ports 0 and 1 in order to make
2095          * port 0 point to the serial port.  However, both moschip devices use a
2096          * single interrupt-in endpoint for both ports (as mentioned a little
2097          * further down), and this endpoint was assigned to port 0.  So after
2098          * the swap, we must copy the interrupt endpoint elements from port 1
2099          * (as newly assigned) to port 0, and null out port 1 pointers.
2100          */
2101         if (product == MOSCHIP_DEVICE_ID_7715) {
2102                 struct usb_serial_port *tmp = serial->port[0];
2103                 serial->port[0] = serial->port[1];
2104                 serial->port[1] = tmp;
2105                 serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb;
2106                 serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer;
2107                 serial->port[0]->interrupt_in_endpointAddress =
2108                         tmp->interrupt_in_endpointAddress;
2109                 serial->port[1]->interrupt_in_urb = NULL;
2110                 serial->port[1]->interrupt_in_buffer = NULL;
2111         }
2112
2113
2114         /* set up serial port private structures */
2115         for (i = 0; i < serial->num_ports; ++i) {
2116                 mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2117                 if (mos7720_port == NULL) {
2118                         dev_err(&dev->dev, "%s - Out of memory\n", __func__);
2119                         return -ENOMEM;
2120                 }
2121
2122                 /* Initialize all port interrupt end point to port 0 int
2123                  * endpoint.  Our device has only one interrupt endpoint
2124                  * common to all ports */
2125                 serial->port[i]->interrupt_in_endpointAddress =
2126                                 serial->port[0]->interrupt_in_endpointAddress;
2127
2128                 mos7720_port->port = serial->port[i];
2129                 usb_set_serial_port_data(serial->port[i], mos7720_port);
2130
2131                 dbg("port number is %d", serial->port[i]->number);
2132                 dbg("serial number is %d", serial->minor);
2133         }
2134
2135
2136         /* setting configuration feature to one */
2137         usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2138                         (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5000);
2139
2140         /* start the interrupt urb */
2141         ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
2142         if (ret_val)
2143                 dev_err(&dev->dev,
2144                         "%s - Error %d submitting control urb\n",
2145                         __func__, ret_val);
2146
2147 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
2148         if (product == MOSCHIP_DEVICE_ID_7715) {
2149                 ret_val = mos7715_parport_init(serial);
2150                 if (ret_val < 0) {
2151                         usb_kill_urb(serial->port[0]->interrupt_in_urb);
2152                         return ret_val;
2153                 }
2154         }
2155 #endif
2156         /* LSR For Port 1 */
2157         read_mos_reg(serial, 0, LSR, &data);
2158         dbg("LSR:%x", data);
2159
2160         return 0;
2161 }
2162
2163 static void mos7720_release(struct usb_serial *serial)
2164 {
2165         int i;
2166
2167         usb_kill_urb(serial->port[0]->interrupt_in_urb);
2168
2169 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
2170         /* close the parallel port */
2171
2172         if (le16_to_cpu(serial->dev->descriptor.idProduct)
2173             == MOSCHIP_DEVICE_ID_7715) {
2174                 struct urbtracker *urbtrack;
2175                 unsigned long flags;
2176                 struct mos7715_parport *mos_parport =
2177                         usb_get_serial_data(serial);
2178
2179                 /* prevent NULL ptr dereference in port callbacks */
2180                 spin_lock(&release_lock);
2181                 mos_parport->pp->private_data = NULL;
2182                 spin_unlock(&release_lock);
2183
2184                 /* wait for synchronous usb calls to return */
2185                 if (mos_parport->msg_pending)
2186                         wait_for_completion_timeout(&mos_parport->syncmsg_compl,
2187                                             msecs_to_jiffies(MOS_WDR_TIMEOUT));
2188
2189                 parport_remove_port(mos_parport->pp);
2190                 usb_set_serial_data(serial, NULL);
2191                 mos_parport->serial = NULL;
2192
2193                 /* if tasklet currently scheduled, wait for it to complete */
2194                 tasklet_kill(&mos_parport->urb_tasklet);
2195
2196                 /* unlink any urbs sent by the tasklet  */
2197                 spin_lock_irqsave(&mos_parport->listlock, flags);
2198                 list_for_each_entry(urbtrack,
2199                                     &mos_parport->active_urbs,
2200                                     urblist_entry)
2201                         usb_unlink_urb(urbtrack->urb);
2202                 spin_unlock_irqrestore(&mos_parport->listlock, flags);
2203
2204                 kref_put(&mos_parport->ref_count, destroy_mos_parport);
2205         }
2206 #endif
2207         /* free private structure allocated for serial port */
2208         for (i = 0; i < serial->num_ports; ++i)
2209                 kfree(usb_get_serial_port_data(serial->port[i]));
2210 }
2211
2212 static struct usb_driver usb_driver = {
2213         .name =         "moschip7720",
2214         .probe =        usb_serial_probe,
2215         .disconnect =   usb_serial_disconnect,
2216         .id_table =     moschip_port_id_table,
2217         .no_dynamic_id =        1,
2218 };
2219
2220 static struct usb_serial_driver moschip7720_2port_driver = {
2221         .driver = {
2222                 .owner =        THIS_MODULE,
2223                 .name =         "moschip7720",
2224         },
2225         .description            = "Moschip 2 port adapter",
2226         .usb_driver             = &usb_driver,
2227         .id_table               = moschip_port_id_table,
2228         .calc_num_ports         = mos77xx_calc_num_ports,
2229         .open                   = mos7720_open,
2230         .close                  = mos7720_close,
2231         .throttle               = mos7720_throttle,
2232         .unthrottle             = mos7720_unthrottle,
2233         .probe                  = mos77xx_probe,
2234         .attach                 = mos7720_startup,
2235         .release                = mos7720_release,
2236         .ioctl                  = mos7720_ioctl,
2237         .tiocmget               = mos7720_tiocmget,
2238         .tiocmset               = mos7720_tiocmset,
2239         .get_icount             = mos7720_get_icount,
2240         .set_termios            = mos7720_set_termios,
2241         .write                  = mos7720_write,
2242         .write_room             = mos7720_write_room,
2243         .chars_in_buffer        = mos7720_chars_in_buffer,
2244         .break_ctl              = mos7720_break,
2245         .read_bulk_callback     = mos7720_bulk_in_callback,
2246         .read_int_callback      = NULL  /* dynamically assigned in probe() */
2247 };
2248
2249 static int __init moschip7720_init(void)
2250 {
2251         int retval;
2252
2253         dbg("%s: Entering ..........", __func__);
2254
2255         /* Register with the usb serial */
2256         retval = usb_serial_register(&moschip7720_2port_driver);
2257         if (retval)
2258                 goto failed_port_device_register;
2259
2260         printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
2261                DRIVER_DESC "\n");
2262
2263         /* Register with the usb */
2264         retval = usb_register(&usb_driver);
2265         if (retval)
2266                 goto failed_usb_register;
2267
2268         return 0;
2269
2270 failed_usb_register:
2271         usb_serial_deregister(&moschip7720_2port_driver);
2272
2273 failed_port_device_register:
2274         return retval;
2275 }
2276
2277 static void __exit moschip7720_exit(void)
2278 {
2279         usb_deregister(&usb_driver);
2280         usb_serial_deregister(&moschip7720_2port_driver);
2281 }
2282
2283 module_init(moschip7720_init);
2284 module_exit(moschip7720_exit);
2285
2286 /* Module information */
2287 MODULE_AUTHOR(DRIVER_AUTHOR);
2288 MODULE_DESCRIPTION(DRIVER_DESC);
2289 MODULE_LICENSE("GPL");
2290
2291 module_param(debug, bool, S_IRUGO | S_IWUSR);
2292 MODULE_PARM_DESC(debug, "Debug enabled or not");