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