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