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