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