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