3 * Controls the Moschip 7720 usb to dual port serial converter
5 * Copyright 2006 Moschip Semiconductor Tech. Ltd.
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.
12 * Vijaya Kumar <vijaykumar.gn@gmail.com>
13 * Ajay Kumar <naanuajay@yahoo.com>
14 * Gurudeva <ngurudeva@yahoo.com>
16 * Cleaned up from the original by:
17 * Greg Kroah-Hartman <gregkh@suse.de>
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>
23 #include <linux/kernel.h>
24 #include <linux/errno.h>
25 #include <linux/slab.h>
26 #include <linux/tty.h>
27 #include <linux/tty_driver.h>
28 #include <linux/tty_flip.h>
29 #include <linux/module.h>
30 #include <linux/spinlock.h>
31 #include <linux/serial.h>
32 #include <linux/serial_reg.h>
33 #include <linux/usb.h>
34 #include <linux/usb/serial.h>
35 #include <linux/uaccess.h>
36 #include <linux/parport.h>
38 #define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
39 #define DRIVER_DESC "Moschip USB Serial Driver"
41 /* default urb timeout */
42 #define MOS_WDR_TIMEOUT 5000
44 #define MOS_MAX_PORT 0x02
45 #define MOS_WRITE 0x0E
48 /* Interrupt Routines Defines */
49 #define SERIAL_IIR_RLS 0x06
50 #define SERIAL_IIR_RDA 0x04
51 #define SERIAL_IIR_CTI 0x0c
52 #define SERIAL_IIR_THR 0x02
53 #define SERIAL_IIR_MS 0x00
55 #define NUM_URBS 16 /* URB Count */
56 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
58 /* This structure holds all of the local serial port information */
60 __u8 shadowLCR; /* last LCR value received */
61 __u8 shadowMCR; /* last MCR value received */
62 __u8 shadowMSR; /* last MSR value received */
64 struct usb_serial_port *port; /* loop back to the owner */
65 struct urb *write_urb_pool[NUM_URBS];
68 static struct usb_serial_driver moschip7720_2port_driver;
70 #define USB_VENDOR_ID_MOSCHIP 0x9710
71 #define MOSCHIP_DEVICE_ID_7720 0x7720
72 #define MOSCHIP_DEVICE_ID_7715 0x7715
74 static const struct usb_device_id id_table[] = {
75 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
76 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) },
77 { } /* terminating entry */
79 MODULE_DEVICE_TABLE(usb, id_table);
81 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
83 /* initial values for parport regs */
84 #define DCR_INIT_VAL 0x0c /* SLCTIN, nINIT */
85 #define ECR_INIT_VAL 0x00 /* SPP mode */
88 struct mos7715_parport *mos_parport;
89 struct list_head urblist_entry;
90 struct kref ref_count;
92 struct usb_ctrlrequest *setup;
95 enum mos7715_pp_modes {
97 PS2 = 1<<5, /* moschip calls this 'NIBBLE' mode */
98 PPF = 2<<5, /* moschip calls this 'CB-FIFO mode */
101 struct mos7715_parport {
102 struct parport *pp; /* back to containing struct */
103 struct kref ref_count; /* to instance of this struct */
104 struct list_head deferred_urbs; /* list deferred async urbs */
105 struct list_head active_urbs; /* list async urbs in flight */
106 spinlock_t listlock; /* protects list access */
107 bool msg_pending; /* usb sync call pending */
108 struct completion syncmsg_compl; /* usb sync call completed */
109 struct tasklet_struct urb_tasklet; /* for sending deferred urbs */
110 struct usb_serial *serial; /* back to containing struct */
111 __u8 shadowECR; /* parallel port regs... */
113 atomic_t shadowDSR; /* updated in int-in callback */
116 /* lock guards against dereferencing NULL ptr in parport ops callbacks */
117 static DEFINE_SPINLOCK(release_lock);
119 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
121 static const unsigned int dummy; /* for clarity in register access fns */
124 MOS7720_THR, /* serial port regs */
136 MOS7720_DPR, /* parallel port regs */
140 MOS7720_SP1_REG, /* device control regs */
141 MOS7720_SP2_REG, /* serial port 2 (7720 only) */
143 MOS7720_SP_CONTROL_REG,
147 * Return the correct value for the Windex field of the setup packet
148 * for a control endpoint message. See the 7715 datasheet.
150 static inline __u16 get_reg_index(enum mos_regs reg)
152 static const __u16 mos7715_index_lookup_table[] = {
153 0x00, /* MOS7720_THR */
154 0x00, /* MOS7720_RHR */
155 0x01, /* MOS7720_IER */
156 0x02, /* MOS7720_FCR */
157 0x02, /* MOS7720_ISR */
158 0x03, /* MOS7720_LCR */
159 0x04, /* MOS7720_MCR */
160 0x05, /* MOS7720_LSR */
161 0x06, /* MOS7720_MSR */
162 0x07, /* MOS7720_SPR */
163 0x00, /* MOS7720_DLL */
164 0x01, /* MOS7720_DLM */
165 0x00, /* MOS7720_DPR */
166 0x01, /* MOS7720_DSR */
167 0x02, /* MOS7720_DCR */
168 0x0a, /* MOS7720_ECR */
169 0x01, /* MOS7720_SP1_REG */
170 0x02, /* MOS7720_SP2_REG (7720 only) */
171 0x04, /* MOS7720_PP_REG (7715 only) */
172 0x08, /* MOS7720_SP_CONTROL_REG */
174 return mos7715_index_lookup_table[reg];
178 * Return the correct value for the upper byte of the Wvalue field of
179 * the setup packet for a control endpoint message.
181 static inline __u16 get_reg_value(enum mos_regs reg,
182 unsigned int serial_portnum)
184 if (reg >= MOS7720_SP1_REG) /* control reg */
187 else if (reg >= MOS7720_DPR) /* parallel port reg (7715 only) */
190 else /* serial port reg */
191 return (serial_portnum + 2) << 8;
195 * Write data byte to the specified device register. The data is embedded in
196 * the value field of the setup packet. serial_portnum is ignored for registers
197 * not specific to a particular serial port.
199 static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
200 enum mos_regs reg, __u8 data)
202 struct usb_device *usbdev = serial->dev;
203 unsigned int pipe = usb_sndctrlpipe(usbdev, 0);
204 __u8 request = (__u8)0x0e;
205 __u8 requesttype = (__u8)0x40;
206 __u16 index = get_reg_index(reg);
207 __u16 value = get_reg_value(reg, serial_portnum) + data;
208 int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
209 index, NULL, 0, MOS_WDR_TIMEOUT);
211 dev_err(&usbdev->dev,
212 "mos7720: usb_control_msg() failed: %d\n", status);
217 * Read data byte from the specified device register. The data returned by the
218 * device is embedded in the value field of the setup packet. serial_portnum is
219 * ignored for registers that are not specific to a particular serial port.
221 static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
222 enum mos_regs reg, __u8 *data)
224 struct usb_device *usbdev = serial->dev;
225 unsigned int pipe = usb_rcvctrlpipe(usbdev, 0);
226 __u8 request = (__u8)0x0d;
227 __u8 requesttype = (__u8)0xc0;
228 __u16 index = get_reg_index(reg);
229 __u16 value = get_reg_value(reg, serial_portnum);
233 buf = kmalloc(1, GFP_KERNEL);
237 status = usb_control_msg(usbdev, pipe, request, requesttype, value,
238 index, buf, 1, MOS_WDR_TIMEOUT);
242 dev_err(&usbdev->dev,
243 "mos7720: usb_control_msg() failed: %d\n", status);
249 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
251 static inline int mos7715_change_mode(struct mos7715_parport *mos_parport,
252 enum mos7715_pp_modes mode)
254 mos_parport->shadowECR = mode;
255 write_mos_reg(mos_parport->serial, dummy, MOS7720_ECR,
256 mos_parport->shadowECR);
260 static void destroy_mos_parport(struct kref *kref)
262 struct mos7715_parport *mos_parport =
263 container_of(kref, struct mos7715_parport, ref_count);
268 static void destroy_urbtracker(struct kref *kref)
270 struct urbtracker *urbtrack =
271 container_of(kref, struct urbtracker, ref_count);
272 struct mos7715_parport *mos_parport = urbtrack->mos_parport;
274 usb_free_urb(urbtrack->urb);
275 kfree(urbtrack->setup);
277 kref_put(&mos_parport->ref_count, destroy_mos_parport);
281 * This runs as a tasklet when sending an urb in a non-blocking parallel
282 * port callback had to be deferred because the disconnect mutex could not be
283 * obtained at the time.
285 static void send_deferred_urbs(unsigned long _mos_parport)
289 struct mos7715_parport *mos_parport = (void *)_mos_parport;
290 struct urbtracker *urbtrack, *tmp;
291 struct list_head *cursor, *next;
294 /* if release function ran, game over */
295 if (unlikely(mos_parport->serial == NULL))
298 dev = &mos_parport->serial->dev->dev;
300 /* try again to get the mutex */
301 if (!mutex_trylock(&mos_parport->serial->disc_mutex)) {
302 dev_dbg(dev, "%s: rescheduling tasklet\n", __func__);
303 tasklet_schedule(&mos_parport->urb_tasklet);
307 /* if device disconnected, game over */
308 if (unlikely(mos_parport->serial->disconnected)) {
309 mutex_unlock(&mos_parport->serial->disc_mutex);
313 spin_lock_irqsave(&mos_parport->listlock, flags);
314 if (list_empty(&mos_parport->deferred_urbs)) {
315 spin_unlock_irqrestore(&mos_parport->listlock, flags);
316 mutex_unlock(&mos_parport->serial->disc_mutex);
317 dev_dbg(dev, "%s: deferred_urbs list empty\n", __func__);
321 /* move contents of deferred_urbs list to active_urbs list and submit */
322 list_for_each_safe(cursor, next, &mos_parport->deferred_urbs)
323 list_move_tail(cursor, &mos_parport->active_urbs);
324 list_for_each_entry_safe(urbtrack, tmp, &mos_parport->active_urbs,
326 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
327 dev_dbg(dev, "%s: urb submitted\n", __func__);
329 dev_err(dev, "usb_submit_urb() failed: %d\n", ret_val);
330 list_del(&urbtrack->urblist_entry);
331 kref_put(&urbtrack->ref_count, destroy_urbtracker);
334 spin_unlock_irqrestore(&mos_parport->listlock, flags);
335 mutex_unlock(&mos_parport->serial->disc_mutex);
338 /* callback for parallel port control urbs submitted asynchronously */
339 static void async_complete(struct urb *urb)
341 struct urbtracker *urbtrack = urb->context;
342 int status = urb->status;
344 if (unlikely(status))
345 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
347 /* remove the urbtracker from the active_urbs list */
348 spin_lock(&urbtrack->mos_parport->listlock);
349 list_del(&urbtrack->urblist_entry);
350 spin_unlock(&urbtrack->mos_parport->listlock);
351 kref_put(&urbtrack->ref_count, destroy_urbtracker);
354 static int write_parport_reg_nonblock(struct mos7715_parport *mos_parport,
355 enum mos_regs reg, __u8 data)
357 struct urbtracker *urbtrack;
360 struct usb_serial *serial = mos_parport->serial;
361 struct usb_device *usbdev = serial->dev;
363 /* create and initialize the control urb and containing urbtracker */
364 urbtrack = kmalloc(sizeof(struct urbtracker), GFP_ATOMIC);
368 kref_get(&mos_parport->ref_count);
369 urbtrack->mos_parport = mos_parport;
370 urbtrack->urb = usb_alloc_urb(0, GFP_ATOMIC);
371 if (!urbtrack->urb) {
375 urbtrack->setup = kmalloc(sizeof(*urbtrack->setup), GFP_ATOMIC);
376 if (!urbtrack->setup) {
377 usb_free_urb(urbtrack->urb);
381 urbtrack->setup->bRequestType = (__u8)0x40;
382 urbtrack->setup->bRequest = (__u8)0x0e;
383 urbtrack->setup->wValue = cpu_to_le16(get_reg_value(reg, dummy));
384 urbtrack->setup->wIndex = cpu_to_le16(get_reg_index(reg));
385 urbtrack->setup->wLength = 0;
386 usb_fill_control_urb(urbtrack->urb, usbdev,
387 usb_sndctrlpipe(usbdev, 0),
388 (unsigned char *)urbtrack->setup,
389 NULL, 0, async_complete, urbtrack);
390 kref_init(&urbtrack->ref_count);
391 INIT_LIST_HEAD(&urbtrack->urblist_entry);
394 * get the disconnect mutex, or add tracker to the deferred_urbs list
395 * and schedule a tasklet to try again later
397 if (!mutex_trylock(&serial->disc_mutex)) {
398 spin_lock_irqsave(&mos_parport->listlock, flags);
399 list_add_tail(&urbtrack->urblist_entry,
400 &mos_parport->deferred_urbs);
401 spin_unlock_irqrestore(&mos_parport->listlock, flags);
402 tasklet_schedule(&mos_parport->urb_tasklet);
403 dev_dbg(&usbdev->dev, "tasklet scheduled\n");
407 /* bail if device disconnected */
408 if (serial->disconnected) {
409 kref_put(&urbtrack->ref_count, destroy_urbtracker);
410 mutex_unlock(&serial->disc_mutex);
414 /* add the tracker to the active_urbs list and submit */
415 spin_lock_irqsave(&mos_parport->listlock, flags);
416 list_add_tail(&urbtrack->urblist_entry, &mos_parport->active_urbs);
417 spin_unlock_irqrestore(&mos_parport->listlock, flags);
418 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
419 mutex_unlock(&serial->disc_mutex);
421 dev_err(&usbdev->dev,
422 "%s: submit_urb() failed: %d\n", __func__, ret_val);
423 spin_lock_irqsave(&mos_parport->listlock, flags);
424 list_del(&urbtrack->urblist_entry);
425 spin_unlock_irqrestore(&mos_parport->listlock, flags);
426 kref_put(&urbtrack->ref_count, destroy_urbtracker);
433 * This is the the common top part of all parallel port callback operations that
434 * send synchronous messages to the device. This implements convoluted locking
435 * that avoids two scenarios: (1) a port operation is called after usbserial
436 * has called our release function, at which point struct mos7715_parport has
437 * been destroyed, and (2) the device has been disconnected, but usbserial has
438 * not called the release function yet because someone has a serial port open.
439 * The shared release_lock prevents the first, and the mutex and disconnected
440 * flag maintained by usbserial covers the second. We also use the msg_pending
441 * flag to ensure that all synchronous usb message calls have completed before
442 * our release function can return.
444 static int parport_prologue(struct parport *pp)
446 struct mos7715_parport *mos_parport;
448 spin_lock(&release_lock);
449 mos_parport = pp->private_data;
450 if (unlikely(mos_parport == NULL)) {
451 /* release fn called, port struct destroyed */
452 spin_unlock(&release_lock);
455 mos_parport->msg_pending = true; /* synch usb call pending */
456 reinit_completion(&mos_parport->syncmsg_compl);
457 spin_unlock(&release_lock);
459 mutex_lock(&mos_parport->serial->disc_mutex);
460 if (mos_parport->serial->disconnected) {
461 /* device disconnected */
462 mutex_unlock(&mos_parport->serial->disc_mutex);
463 mos_parport->msg_pending = false;
464 complete(&mos_parport->syncmsg_compl);
472 * This is the common bottom part of all parallel port functions that send
473 * synchronous messages to the device.
475 static inline void parport_epilogue(struct parport *pp)
477 struct mos7715_parport *mos_parport = pp->private_data;
478 mutex_unlock(&mos_parport->serial->disc_mutex);
479 mos_parport->msg_pending = false;
480 complete(&mos_parport->syncmsg_compl);
483 static void parport_mos7715_write_data(struct parport *pp, unsigned char d)
485 struct mos7715_parport *mos_parport = pp->private_data;
487 if (parport_prologue(pp) < 0)
489 mos7715_change_mode(mos_parport, SPP);
490 write_mos_reg(mos_parport->serial, dummy, MOS7720_DPR, (__u8)d);
491 parport_epilogue(pp);
494 static unsigned char parport_mos7715_read_data(struct parport *pp)
496 struct mos7715_parport *mos_parport = pp->private_data;
499 if (parport_prologue(pp) < 0)
501 read_mos_reg(mos_parport->serial, dummy, MOS7720_DPR, &d);
502 parport_epilogue(pp);
506 static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
508 struct mos7715_parport *mos_parport = pp->private_data;
511 if (parport_prologue(pp) < 0)
513 data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
514 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR, data);
515 mos_parport->shadowDCR = data;
516 parport_epilogue(pp);
519 static unsigned char parport_mos7715_read_control(struct parport *pp)
521 struct mos7715_parport *mos_parport = pp->private_data;
524 spin_lock(&release_lock);
525 mos_parport = pp->private_data;
526 if (unlikely(mos_parport == NULL)) {
527 spin_unlock(&release_lock);
530 dcr = mos_parport->shadowDCR & 0x0f;
531 spin_unlock(&release_lock);
535 static unsigned char parport_mos7715_frob_control(struct parport *pp,
539 struct mos7715_parport *mos_parport = pp->private_data;
544 if (parport_prologue(pp) < 0)
546 mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
547 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
548 mos_parport->shadowDCR);
549 dcr = mos_parport->shadowDCR & 0x0f;
550 parport_epilogue(pp);
554 static unsigned char parport_mos7715_read_status(struct parport *pp)
556 unsigned char status;
557 struct mos7715_parport *mos_parport = pp->private_data;
559 spin_lock(&release_lock);
560 mos_parport = pp->private_data;
561 if (unlikely(mos_parport == NULL)) { /* release called */
562 spin_unlock(&release_lock);
565 status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
566 spin_unlock(&release_lock);
570 static void parport_mos7715_enable_irq(struct parport *pp)
574 static void parport_mos7715_disable_irq(struct parport *pp)
578 static void parport_mos7715_data_forward(struct parport *pp)
580 struct mos7715_parport *mos_parport = pp->private_data;
582 if (parport_prologue(pp) < 0)
584 mos7715_change_mode(mos_parport, PS2);
585 mos_parport->shadowDCR &= ~0x20;
586 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
587 mos_parport->shadowDCR);
588 parport_epilogue(pp);
591 static void parport_mos7715_data_reverse(struct parport *pp)
593 struct mos7715_parport *mos_parport = pp->private_data;
595 if (parport_prologue(pp) < 0)
597 mos7715_change_mode(mos_parport, PS2);
598 mos_parport->shadowDCR |= 0x20;
599 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
600 mos_parport->shadowDCR);
601 parport_epilogue(pp);
604 static void parport_mos7715_init_state(struct pardevice *dev,
605 struct parport_state *s)
607 s->u.pc.ctr = DCR_INIT_VAL;
608 s->u.pc.ecr = ECR_INIT_VAL;
611 /* N.B. Parport core code requires that this function not block */
612 static void parport_mos7715_save_state(struct parport *pp,
613 struct parport_state *s)
615 struct mos7715_parport *mos_parport;
617 spin_lock(&release_lock);
618 mos_parport = pp->private_data;
619 if (unlikely(mos_parport == NULL)) { /* release called */
620 spin_unlock(&release_lock);
623 s->u.pc.ctr = mos_parport->shadowDCR;
624 s->u.pc.ecr = mos_parport->shadowECR;
625 spin_unlock(&release_lock);
628 /* N.B. Parport core code requires that this function not block */
629 static void parport_mos7715_restore_state(struct parport *pp,
630 struct parport_state *s)
632 struct mos7715_parport *mos_parport;
634 spin_lock(&release_lock);
635 mos_parport = pp->private_data;
636 if (unlikely(mos_parport == NULL)) { /* release called */
637 spin_unlock(&release_lock);
640 write_parport_reg_nonblock(mos_parport, MOS7720_DCR,
641 mos_parport->shadowDCR);
642 write_parport_reg_nonblock(mos_parport, MOS7720_ECR,
643 mos_parport->shadowECR);
644 spin_unlock(&release_lock);
647 static size_t parport_mos7715_write_compat(struct parport *pp,
649 size_t len, int flags)
652 struct mos7715_parport *mos_parport = pp->private_data;
655 if (parport_prologue(pp) < 0)
657 mos7715_change_mode(mos_parport, PPF);
658 retval = usb_bulk_msg(mos_parport->serial->dev,
659 usb_sndbulkpipe(mos_parport->serial->dev, 2),
660 (void *)buffer, len, &actual_len,
662 parport_epilogue(pp);
664 dev_err(&mos_parport->serial->dev->dev,
665 "mos7720: usb_bulk_msg() failed: %d\n", retval);
671 static struct parport_operations parport_mos7715_ops = {
672 .owner = THIS_MODULE,
673 .write_data = parport_mos7715_write_data,
674 .read_data = parport_mos7715_read_data,
676 .write_control = parport_mos7715_write_control,
677 .read_control = parport_mos7715_read_control,
678 .frob_control = parport_mos7715_frob_control,
680 .read_status = parport_mos7715_read_status,
682 .enable_irq = parport_mos7715_enable_irq,
683 .disable_irq = parport_mos7715_disable_irq,
685 .data_forward = parport_mos7715_data_forward,
686 .data_reverse = parport_mos7715_data_reverse,
688 .init_state = parport_mos7715_init_state,
689 .save_state = parport_mos7715_save_state,
690 .restore_state = parport_mos7715_restore_state,
692 .compat_write_data = parport_mos7715_write_compat,
694 .nibble_read_data = parport_ieee1284_read_nibble,
695 .byte_read_data = parport_ieee1284_read_byte,
699 * Allocate and initialize parallel port control struct, initialize
700 * the parallel port hardware device, and register with the parport subsystem.
702 static int mos7715_parport_init(struct usb_serial *serial)
704 struct mos7715_parport *mos_parport;
706 /* allocate and initialize parallel port control struct */
707 mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
711 mos_parport->msg_pending = false;
712 kref_init(&mos_parport->ref_count);
713 spin_lock_init(&mos_parport->listlock);
714 INIT_LIST_HEAD(&mos_parport->active_urbs);
715 INIT_LIST_HEAD(&mos_parport->deferred_urbs);
716 usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
717 mos_parport->serial = serial;
718 tasklet_init(&mos_parport->urb_tasklet, send_deferred_urbs,
719 (unsigned long) mos_parport);
720 init_completion(&mos_parport->syncmsg_compl);
722 /* cycle parallel port reset bit */
723 write_mos_reg(mos_parport->serial, dummy, MOS7720_PP_REG, (__u8)0x80);
724 write_mos_reg(mos_parport->serial, dummy, MOS7720_PP_REG, (__u8)0x00);
726 /* initialize device registers */
727 mos_parport->shadowDCR = DCR_INIT_VAL;
728 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
729 mos_parport->shadowDCR);
730 mos_parport->shadowECR = ECR_INIT_VAL;
731 write_mos_reg(mos_parport->serial, dummy, MOS7720_ECR,
732 mos_parport->shadowECR);
734 /* register with parport core */
735 mos_parport->pp = parport_register_port(0, PARPORT_IRQ_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);
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);
751 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
754 * mos7720_interrupt_callback
755 * this is the callback function for when we have received data on the
756 * interrupt endpoint.
758 static void mos7720_interrupt_callback(struct urb *urb)
762 int status = urb->status;
763 struct device *dev = &urb->dev->dev;
775 /* this urb is terminated, clean up */
776 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
779 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
783 length = urb->actual_length;
784 data = urb->transfer_buffer;
786 /* Moschip get 4 bytes
787 * Byte 1 IIR Port 1 (port.number is 0)
788 * Byte 2 IIR Port 2 (port.number is 1)
789 * Byte 3 --------------
790 * Byte 4 FIFO status for both */
792 /* the above description is inverted
793 * oneukum 2007-03-14 */
795 if (unlikely(length != 4)) {
796 dev_dbg(dev, "Wrong data !!!\n");
803 if ((sp1 | sp2) & 0x01) {
804 /* No Interrupt Pending in both the ports */
805 dev_dbg(dev, "No Interrupt !!!\n");
807 switch (sp1 & 0x0f) {
809 dev_dbg(dev, "Serial Port 1: Receiver status error or address bit detected in 9-bit mode\n");
812 dev_dbg(dev, "Serial Port 1: Receiver time out\n");
815 /* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */
819 switch (sp2 & 0x0f) {
821 dev_dbg(dev, "Serial Port 2: Receiver status error or address bit detected in 9-bit mode\n");
824 dev_dbg(dev, "Serial Port 2: Receiver time out\n");
827 /* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */
833 result = usb_submit_urb(urb, GFP_ATOMIC);
835 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
839 * mos7715_interrupt_callback
840 * this is the 7715's callback function for when we have received data on
841 * the interrupt endpoint.
843 static void mos7715_interrupt_callback(struct urb *urb)
847 int status = urb->status;
848 struct device *dev = &urb->dev->dev;
860 /* this urb is terminated, clean up */
861 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
864 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
868 length = urb->actual_length;
869 data = urb->transfer_buffer;
871 /* Structure of data from 7715 device:
872 * Byte 1: IIR serial Port
874 * Byte 2: DSR parallel port
875 * Byte 4: FIFO status for both */
877 if (unlikely(length != 4)) {
878 dev_dbg(dev, "Wrong data !!!\n");
883 if (!(iir & 0x01)) { /* serial port interrupt pending */
884 switch (iir & 0x0f) {
886 dev_dbg(dev, "Serial Port: Receiver status error or address bit detected in 9-bit mode\n");
889 dev_dbg(dev, "Serial Port: Receiver time out\n");
892 /* dev_dbg(dev, "Serial Port: Modem status change\n"); */
897 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
898 { /* update local copy of DSR reg */
899 struct usb_serial_port *port = urb->context;
900 struct mos7715_parport *mos_parport = port->serial->private;
901 if (unlikely(mos_parport == NULL))
903 atomic_set(&mos_parport->shadowDSR, data[2]);
908 result = usb_submit_urb(urb, GFP_ATOMIC);
910 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
914 * mos7720_bulk_in_callback
915 * this is the callback function for when we have received data on the
918 static void mos7720_bulk_in_callback(struct urb *urb)
921 unsigned char *data ;
922 struct usb_serial_port *port;
923 int status = urb->status;
926 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
932 dev_dbg(&port->dev, "Entering...%s\n", __func__);
934 data = urb->transfer_buffer;
936 if (urb->actual_length) {
937 tty_insert_flip_string(&port->port, data, urb->actual_length);
938 tty_flip_buffer_push(&port->port);
941 if (port->read_urb->status != -EINPROGRESS) {
942 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
944 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
949 * mos7720_bulk_out_data_callback
950 * this is the callback function for when we have finished sending serial
951 * data on the bulk out endpoint.
953 static void mos7720_bulk_out_data_callback(struct urb *urb)
955 struct moschip_port *mos7720_port;
956 int status = urb->status;
959 dev_dbg(&urb->dev->dev, "nonzero write bulk status received:%d\n", status);
963 mos7720_port = urb->context;
965 dev_dbg(&urb->dev->dev, "NULL mos7720_port pointer\n");
969 if (mos7720_port->open)
970 tty_port_tty_wakeup(&mos7720_port->port->port);
975 * this function installs the appropriate read interrupt endpoint callback
976 * depending on whether the device is a 7720 or 7715, thus avoiding costly
977 * run-time checks in the high-frequency callback routine itself.
979 static int mos77xx_probe(struct usb_serial *serial,
980 const struct usb_device_id *id)
982 if (id->idProduct == MOSCHIP_DEVICE_ID_7715)
983 moschip7720_2port_driver.read_int_callback =
984 mos7715_interrupt_callback;
986 moschip7720_2port_driver.read_int_callback =
987 mos7720_interrupt_callback;
992 static int mos77xx_calc_num_ports(struct usb_serial *serial)
994 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
995 if (product == MOSCHIP_DEVICE_ID_7715)
1001 static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
1003 struct usb_serial *serial;
1005 struct moschip_port *mos7720_port;
1009 int allocated_urbs = 0;
1012 serial = port->serial;
1014 mos7720_port = usb_get_serial_port_data(port);
1015 if (mos7720_port == NULL)
1018 usb_clear_halt(serial->dev, port->write_urb->pipe);
1019 usb_clear_halt(serial->dev, port->read_urb->pipe);
1021 /* Initialising the write urb pool */
1022 for (j = 0; j < NUM_URBS; ++j) {
1023 urb = usb_alloc_urb(0, GFP_KERNEL);
1024 mos7720_port->write_urb_pool[j] = urb;
1028 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1030 if (!urb->transfer_buffer) {
1031 usb_free_urb(mos7720_port->write_urb_pool[j]);
1032 mos7720_port->write_urb_pool[j] = NULL;
1038 if (!allocated_urbs)
1041 /* Initialize MCS7720 -- Write Init values to corresponding Registers
1044 * 0 : MOS7720_THR/MOS7720_RHR
1053 * 0x08 : SP1/2 Control Reg
1055 port_number = port->port_number;
1056 read_mos_reg(serial, port_number, MOS7720_LSR, &data);
1058 dev_dbg(&port->dev, "SS::%p LSR:%x\n", mos7720_port, data);
1060 write_mos_reg(serial, dummy, MOS7720_SP1_REG, 0x02);
1061 write_mos_reg(serial, dummy, MOS7720_SP2_REG, 0x02);
1063 write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
1064 write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
1066 write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
1067 mos7720_port->shadowLCR = 0x03;
1068 write_mos_reg(serial, port_number, MOS7720_LCR,
1069 mos7720_port->shadowLCR);
1070 mos7720_port->shadowMCR = 0x0b;
1071 write_mos_reg(serial, port_number, MOS7720_MCR,
1072 mos7720_port->shadowMCR);
1074 write_mos_reg(serial, port_number, MOS7720_SP_CONTROL_REG, 0x00);
1075 read_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, &data);
1076 data = data | (port->port_number + 1);
1077 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, data);
1078 mos7720_port->shadowLCR = 0x83;
1079 write_mos_reg(serial, port_number, MOS7720_LCR,
1080 mos7720_port->shadowLCR);
1081 write_mos_reg(serial, port_number, MOS7720_THR, 0x0c);
1082 write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
1083 mos7720_port->shadowLCR = 0x03;
1084 write_mos_reg(serial, port_number, MOS7720_LCR,
1085 mos7720_port->shadowLCR);
1086 write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
1088 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
1090 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
1091 __func__, response);
1093 /* initialize our port settings */
1094 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
1096 /* send a open port command */
1097 mos7720_port->open = 1;
1103 * mos7720_chars_in_buffer
1104 * this function is called by the tty driver when it wants to know how many
1105 * bytes of data we currently have outstanding in the port (data that has
1106 * been written, but hasn't made it out the port yet)
1107 * If successful, we return the number of bytes left to be written in the
1109 * Otherwise we return a negative error number.
1111 static int mos7720_chars_in_buffer(struct tty_struct *tty)
1113 struct usb_serial_port *port = tty->driver_data;
1116 struct moschip_port *mos7720_port;
1118 mos7720_port = usb_get_serial_port_data(port);
1119 if (mos7720_port == NULL)
1122 for (i = 0; i < NUM_URBS; ++i) {
1123 if (mos7720_port->write_urb_pool[i] &&
1124 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
1125 chars += URB_TRANSFER_BUFFER_SIZE;
1127 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
1131 static void mos7720_close(struct usb_serial_port *port)
1133 struct usb_serial *serial;
1134 struct moschip_port *mos7720_port;
1137 serial = port->serial;
1139 mos7720_port = usb_get_serial_port_data(port);
1140 if (mos7720_port == NULL)
1143 for (j = 0; j < NUM_URBS; ++j)
1144 usb_kill_urb(mos7720_port->write_urb_pool[j]);
1146 /* Freeing Write URBs */
1147 for (j = 0; j < NUM_URBS; ++j) {
1148 if (mos7720_port->write_urb_pool[j]) {
1149 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
1150 usb_free_urb(mos7720_port->write_urb_pool[j]);
1154 /* While closing port, shutdown all bulk read, write *
1155 * and interrupt read if they exists, otherwise nop */
1156 usb_kill_urb(port->write_urb);
1157 usb_kill_urb(port->read_urb);
1159 write_mos_reg(serial, port->port_number, MOS7720_MCR, 0x00);
1160 write_mos_reg(serial, port->port_number, MOS7720_IER, 0x00);
1162 mos7720_port->open = 0;
1165 static void mos7720_break(struct tty_struct *tty, int break_state)
1167 struct usb_serial_port *port = tty->driver_data;
1169 struct usb_serial *serial;
1170 struct moschip_port *mos7720_port;
1172 serial = port->serial;
1174 mos7720_port = usb_get_serial_port_data(port);
1175 if (mos7720_port == NULL)
1178 if (break_state == -1)
1179 data = mos7720_port->shadowLCR | UART_LCR_SBC;
1181 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
1183 mos7720_port->shadowLCR = data;
1184 write_mos_reg(serial, port->port_number, MOS7720_LCR,
1185 mos7720_port->shadowLCR);
1189 * mos7720_write_room
1190 * this function is called by the tty driver when it wants to know how many
1191 * bytes of data we can accept for a specific port.
1192 * If successful, we return the amount of room that we have for this port
1193 * Otherwise we return a negative error number.
1195 static int mos7720_write_room(struct tty_struct *tty)
1197 struct usb_serial_port *port = tty->driver_data;
1198 struct moschip_port *mos7720_port;
1202 mos7720_port = usb_get_serial_port_data(port);
1203 if (mos7720_port == NULL)
1206 /* FIXME: Locking */
1207 for (i = 0; i < NUM_URBS; ++i) {
1208 if (mos7720_port->write_urb_pool[i] &&
1209 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
1210 room += URB_TRANSFER_BUFFER_SIZE;
1213 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
1217 static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
1218 const unsigned char *data, int count)
1225 struct moschip_port *mos7720_port;
1226 struct usb_serial *serial;
1228 const unsigned char *current_position = data;
1230 serial = port->serial;
1232 mos7720_port = usb_get_serial_port_data(port);
1233 if (mos7720_port == NULL)
1236 /* try to find a free urb in the list */
1239 for (i = 0; i < NUM_URBS; ++i) {
1240 if (mos7720_port->write_urb_pool[i] &&
1241 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
1242 urb = mos7720_port->write_urb_pool[i];
1243 dev_dbg(&port->dev, "URB:%d\n", i);
1249 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
1253 if (urb->transfer_buffer == NULL) {
1254 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1256 if (!urb->transfer_buffer)
1259 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1261 memcpy(urb->transfer_buffer, current_position, transfer_size);
1262 usb_serial_debug_data(&port->dev, __func__, transfer_size,
1263 urb->transfer_buffer);
1265 /* fill urb with data and submit */
1266 usb_fill_bulk_urb(urb, serial->dev,
1267 usb_sndbulkpipe(serial->dev,
1268 port->bulk_out_endpointAddress),
1269 urb->transfer_buffer, transfer_size,
1270 mos7720_bulk_out_data_callback, mos7720_port);
1272 /* send it down the pipe */
1273 status = usb_submit_urb(urb, GFP_ATOMIC);
1275 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1276 "with status = %d\n", __func__, status);
1277 bytes_sent = status;
1280 bytes_sent = transfer_size;
1286 static void mos7720_throttle(struct tty_struct *tty)
1288 struct usb_serial_port *port = tty->driver_data;
1289 struct moschip_port *mos7720_port;
1292 mos7720_port = usb_get_serial_port_data(port);
1294 if (mos7720_port == NULL)
1297 if (!mos7720_port->open) {
1298 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1302 /* if we are implementing XON/XOFF, send the stop character */
1304 unsigned char stop_char = STOP_CHAR(tty);
1305 status = mos7720_write(tty, port, &stop_char, 1);
1310 /* if we are implementing RTS/CTS, toggle that line */
1311 if (C_CRTSCTS(tty)) {
1312 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
1313 write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
1314 mos7720_port->shadowMCR);
1318 static void mos7720_unthrottle(struct tty_struct *tty)
1320 struct usb_serial_port *port = tty->driver_data;
1321 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1324 if (mos7720_port == NULL)
1327 if (!mos7720_port->open) {
1328 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1332 /* if we are implementing XON/XOFF, send the start character */
1334 unsigned char start_char = START_CHAR(tty);
1335 status = mos7720_write(tty, port, &start_char, 1);
1340 /* if we are implementing RTS/CTS, toggle that line */
1341 if (C_CRTSCTS(tty)) {
1342 mos7720_port->shadowMCR |= UART_MCR_RTS;
1343 write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
1344 mos7720_port->shadowMCR);
1348 /* FIXME: this function does not work */
1349 static int set_higher_rates(struct moschip_port *mos7720_port,
1352 struct usb_serial_port *port;
1353 struct usb_serial *serial;
1355 enum mos_regs sp_reg;
1356 if (mos7720_port == NULL)
1359 port = mos7720_port->port;
1360 serial = port->serial;
1362 /***********************************************
1363 * Init Sequence for higher rates
1364 ***********************************************/
1365 dev_dbg(&port->dev, "Sending Setting Commands ..........\n");
1366 port_number = port->port_number;
1368 write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
1369 write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
1370 write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
1371 mos7720_port->shadowMCR = 0x0b;
1372 write_mos_reg(serial, port_number, MOS7720_MCR,
1373 mos7720_port->shadowMCR);
1374 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, 0x00);
1376 /***********************************************
1377 * Set for higher rates *
1378 ***********************************************/
1379 /* writing baud rate verbatum into uart clock field clearly not right */
1380 if (port_number == 0)
1381 sp_reg = MOS7720_SP1_REG;
1383 sp_reg = MOS7720_SP2_REG;
1384 write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
1385 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, 0x03);
1386 mos7720_port->shadowMCR = 0x2b;
1387 write_mos_reg(serial, port_number, MOS7720_MCR,
1388 mos7720_port->shadowMCR);
1390 /***********************************************
1392 ***********************************************/
1393 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1394 write_mos_reg(serial, port_number, MOS7720_LCR,
1395 mos7720_port->shadowLCR);
1396 write_mos_reg(serial, port_number, MOS7720_DLL, 0x01);
1397 write_mos_reg(serial, port_number, MOS7720_DLM, 0x00);
1398 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1399 write_mos_reg(serial, port_number, MOS7720_LCR,
1400 mos7720_port->shadowLCR);
1405 /* baud rate information */
1406 struct divisor_table_entry {
1411 /* Define table of divisors for moschip 7720 hardware *
1412 * These assume a 3.6864MHz crystal, the standard /16, and *
1414 static struct divisor_table_entry divisor_table[] = {
1416 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
1417 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
1433 /*****************************************************************************
1434 * calc_baud_rate_divisor
1435 * this function calculates the proper baud rate divisor for the specified
1437 *****************************************************************************/
1438 static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, int *divisor)
1446 dev_dbg(&port->dev, "%s - %d\n", __func__, baudrate);
1448 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
1449 if (divisor_table[i].baudrate == baudrate) {
1450 *divisor = divisor_table[i].divisor;
1455 /* After trying for all the standard baud rates *
1456 * Try calculating the divisor for this baud rate */
1457 if (baudrate > 75 && baudrate < 230400) {
1458 /* get the divisor */
1459 custom = (__u16)(230400L / baudrate);
1461 /* Check for round off */
1462 round1 = (__u16)(2304000L / baudrate);
1463 round = (__u16)(round1 - (custom * 10));
1468 dev_dbg(&port->dev, "Baud %d = %d\n", baudrate, custom);
1472 dev_dbg(&port->dev, "Baud calculation Failed...\n");
1477 * send_cmd_write_baud_rate
1478 * this function sends the proper command to change the baud rate of the
1481 static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1484 struct usb_serial_port *port;
1485 struct usb_serial *serial;
1488 unsigned char number;
1490 if (mos7720_port == NULL)
1493 port = mos7720_port->port;
1494 serial = port->serial;
1496 number = port->port_number;
1497 dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudrate);
1499 /* Calculate the Divisor */
1500 status = calc_baud_rate_divisor(port, baudrate, &divisor);
1502 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1506 /* Enable access to divisor latch */
1507 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1508 write_mos_reg(serial, number, MOS7720_LCR, mos7720_port->shadowLCR);
1510 /* Write the divisor */
1511 write_mos_reg(serial, number, MOS7720_DLL, (__u8)(divisor & 0xff));
1512 write_mos_reg(serial, number, MOS7720_DLM,
1513 (__u8)((divisor & 0xff00) >> 8));
1515 /* Disable access to divisor latch */
1516 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1517 write_mos_reg(serial, number, MOS7720_LCR, mos7720_port->shadowLCR);
1523 * change_port_settings
1524 * This routine is called to set the UART on the device to match
1525 * the specified new settings.
1527 static void change_port_settings(struct tty_struct *tty,
1528 struct moschip_port *mos7720_port,
1529 struct ktermios *old_termios)
1531 struct usb_serial_port *port;
1532 struct usb_serial *serial;
1543 if (mos7720_port == NULL)
1546 port = mos7720_port->port;
1547 serial = port->serial;
1548 port_number = port->port_number;
1550 if (!mos7720_port->open) {
1551 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1555 lData = UART_LCR_WLEN8;
1556 lStop = 0x00; /* 1 stop bit */
1557 lParity = 0x00; /* No parity */
1559 cflag = tty->termios.c_cflag;
1560 iflag = tty->termios.c_iflag;
1562 /* Change the number of bits */
1563 switch (cflag & CSIZE) {
1565 lData = UART_LCR_WLEN5;
1570 lData = UART_LCR_WLEN6;
1575 lData = UART_LCR_WLEN7;
1580 lData = UART_LCR_WLEN8;
1584 /* Change the Parity bit */
1585 if (cflag & PARENB) {
1586 if (cflag & PARODD) {
1587 lParity = UART_LCR_PARITY;
1588 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
1590 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
1591 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
1595 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
1599 lParity = lParity | 0x20;
1601 /* Change the Stop bit */
1602 if (cflag & CSTOPB) {
1603 lStop = UART_LCR_STOP;
1604 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
1607 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
1610 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1611 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1612 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
1614 /* Update the LCR with the correct value */
1615 mos7720_port->shadowLCR &=
1616 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1617 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1620 /* Disable Interrupts */
1621 write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
1622 write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
1623 write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
1625 /* Send the updated LCR value to the mos7720 */
1626 write_mos_reg(serial, port_number, MOS7720_LCR,
1627 mos7720_port->shadowLCR);
1628 mos7720_port->shadowMCR = 0x0b;
1629 write_mos_reg(serial, port_number, MOS7720_MCR,
1630 mos7720_port->shadowMCR);
1632 /* set up the MCR register and send it to the mos7720 */
1633 mos7720_port->shadowMCR = UART_MCR_OUT2;
1635 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1637 if (cflag & CRTSCTS) {
1638 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
1639 /* To set hardware flow control to the specified *
1640 * serial port, in SP1/2_CONTROL_REG */
1642 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG,
1645 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG,
1649 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1651 write_mos_reg(serial, port_number, MOS7720_MCR,
1652 mos7720_port->shadowMCR);
1654 /* Determine divisor based on baud rate */
1655 baud = tty_get_baud_rate(tty);
1657 /* pick a default, any default... */
1658 dev_dbg(&port->dev, "Picked default baud...\n");
1662 if (baud >= 230400) {
1663 set_higher_rates(mos7720_port, baud);
1664 /* Enable Interrupts */
1665 write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
1669 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
1670 status = send_cmd_write_baud_rate(mos7720_port, baud);
1671 /* FIXME: needs to write actual resulting baud back not just
1674 tty_encode_baud_rate(tty, baud, baud);
1675 /* Enable Interrupts */
1676 write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
1678 if (port->read_urb->status != -EINPROGRESS) {
1679 status = usb_submit_urb(port->read_urb, GFP_KERNEL);
1681 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
1686 * mos7720_set_termios
1687 * this function is called by the tty driver when it wants to change the
1688 * termios structure.
1690 static void mos7720_set_termios(struct tty_struct *tty,
1691 struct usb_serial_port *port, struct ktermios *old_termios)
1695 struct usb_serial *serial;
1696 struct moschip_port *mos7720_port;
1698 serial = port->serial;
1700 mos7720_port = usb_get_serial_port_data(port);
1702 if (mos7720_port == NULL)
1705 if (!mos7720_port->open) {
1706 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1710 dev_dbg(&port->dev, "setting termios - ASPIRE\n");
1712 cflag = tty->termios.c_cflag;
1714 dev_dbg(&port->dev, "%s - cflag %08x iflag %08x\n", __func__,
1715 tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag));
1717 dev_dbg(&port->dev, "%s - old cflag %08x old iflag %08x\n", __func__,
1718 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
1720 /* change the port settings to the new ones specified */
1721 change_port_settings(tty, mos7720_port, old_termios);
1723 if (port->read_urb->status != -EINPROGRESS) {
1724 status = usb_submit_urb(port->read_urb, GFP_KERNEL);
1726 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
1731 * get_lsr_info - get line status register info
1733 * Purpose: Let user call ioctl() to get info when the UART physically
1734 * is emptied. On bus types like RS485, the transmitter must
1735 * release the bus after transmitting. This must be done when
1736 * the transmit shift register is empty, not be done when the
1737 * transmit holding register is empty. This functionality
1738 * allows an RS485 driver to be written in user space.
1740 static int get_lsr_info(struct tty_struct *tty,
1741 struct moschip_port *mos7720_port, unsigned int __user *value)
1743 struct usb_serial_port *port = tty->driver_data;
1744 unsigned int result = 0;
1745 unsigned char data = 0;
1746 int port_number = port->port_number;
1749 count = mos7720_chars_in_buffer(tty);
1751 read_mos_reg(port->serial, port_number, MOS7720_LSR, &data);
1752 if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1753 == (UART_LSR_TEMT | UART_LSR_THRE)) {
1754 dev_dbg(&port->dev, "%s -- Empty\n", __func__);
1755 result = TIOCSER_TEMT;
1758 if (copy_to_user(value, &result, sizeof(int)))
1763 static int mos7720_tiocmget(struct tty_struct *tty)
1765 struct usb_serial_port *port = tty->driver_data;
1766 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1767 unsigned int result = 0;
1771 mcr = mos7720_port->shadowMCR;
1772 msr = mos7720_port->shadowMSR;
1774 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1775 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1776 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1777 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
1778 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1779 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
1784 static int mos7720_tiocmset(struct tty_struct *tty,
1785 unsigned int set, unsigned int clear)
1787 struct usb_serial_port *port = tty->driver_data;
1788 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1791 mcr = mos7720_port->shadowMCR;
1793 if (set & TIOCM_RTS)
1794 mcr |= UART_MCR_RTS;
1795 if (set & TIOCM_DTR)
1796 mcr |= UART_MCR_DTR;
1797 if (set & TIOCM_LOOP)
1798 mcr |= UART_MCR_LOOP;
1800 if (clear & TIOCM_RTS)
1801 mcr &= ~UART_MCR_RTS;
1802 if (clear & TIOCM_DTR)
1803 mcr &= ~UART_MCR_DTR;
1804 if (clear & TIOCM_LOOP)
1805 mcr &= ~UART_MCR_LOOP;
1807 mos7720_port->shadowMCR = mcr;
1808 write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
1809 mos7720_port->shadowMCR);
1814 static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1815 unsigned int __user *value)
1820 struct usb_serial_port *port;
1822 if (mos7720_port == NULL)
1825 port = (struct usb_serial_port *)mos7720_port->port;
1826 mcr = mos7720_port->shadowMCR;
1828 if (copy_from_user(&arg, value, sizeof(int)))
1833 if (arg & TIOCM_RTS)
1834 mcr |= UART_MCR_RTS;
1835 if (arg & TIOCM_DTR)
1836 mcr |= UART_MCR_RTS;
1837 if (arg & TIOCM_LOOP)
1838 mcr |= UART_MCR_LOOP;
1842 if (arg & TIOCM_RTS)
1843 mcr &= ~UART_MCR_RTS;
1844 if (arg & TIOCM_DTR)
1845 mcr &= ~UART_MCR_RTS;
1846 if (arg & TIOCM_LOOP)
1847 mcr &= ~UART_MCR_LOOP;
1852 mos7720_port->shadowMCR = mcr;
1853 write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
1854 mos7720_port->shadowMCR);
1859 static int get_serial_info(struct moschip_port *mos7720_port,
1860 struct serial_struct __user *retinfo)
1862 struct serial_struct tmp;
1864 memset(&tmp, 0, sizeof(tmp));
1866 tmp.type = PORT_16550A;
1867 tmp.line = mos7720_port->port->minor;
1868 tmp.port = mos7720_port->port->port_number;
1870 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1871 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1872 tmp.baud_base = 9600;
1873 tmp.close_delay = 5*HZ;
1874 tmp.closing_wait = 30*HZ;
1876 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1881 static int mos7720_ioctl(struct tty_struct *tty,
1882 unsigned int cmd, unsigned long arg)
1884 struct usb_serial_port *port = tty->driver_data;
1885 struct moschip_port *mos7720_port;
1887 mos7720_port = usb_get_serial_port_data(port);
1888 if (mos7720_port == NULL)
1893 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
1894 return get_lsr_info(tty, mos7720_port,
1895 (unsigned int __user *)arg);
1897 /* FIXME: These should be using the mode methods */
1900 dev_dbg(&port->dev, "%s TIOCMSET/TIOCMBIC/TIOCMSET\n", __func__);
1901 return set_modem_info(mos7720_port, cmd,
1902 (unsigned int __user *)arg);
1905 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
1906 return get_serial_info(mos7720_port,
1907 (struct serial_struct __user *)arg);
1910 return -ENOIOCTLCMD;
1913 static int mos7720_startup(struct usb_serial *serial)
1915 struct usb_device *dev;
1920 product = le16_to_cpu(serial->dev->descriptor.idProduct);
1924 * The 7715 uses the first bulk in/out endpoint pair for the parallel
1925 * port, and the second for the serial port. Because the usbserial core
1926 * assumes both pairs are serial ports, we must engage in a bit of
1927 * subterfuge and swap the pointers for ports 0 and 1 in order to make
1928 * port 0 point to the serial port. However, both moschip devices use a
1929 * single interrupt-in endpoint for both ports (as mentioned a little
1930 * further down), and this endpoint was assigned to port 0. So after
1931 * the swap, we must copy the interrupt endpoint elements from port 1
1932 * (as newly assigned) to port 0, and null out port 1 pointers.
1934 if (product == MOSCHIP_DEVICE_ID_7715) {
1935 struct usb_serial_port *tmp = serial->port[0];
1936 serial->port[0] = serial->port[1];
1937 serial->port[1] = tmp;
1938 serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb;
1939 serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer;
1940 serial->port[0]->interrupt_in_endpointAddress =
1941 tmp->interrupt_in_endpointAddress;
1942 serial->port[1]->interrupt_in_urb = NULL;
1943 serial->port[1]->interrupt_in_buffer = NULL;
1946 /* setting configuration feature to one */
1947 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1948 (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5000);
1950 /* start the interrupt urb */
1951 ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
1954 "%s - Error %d submitting control urb\n",
1957 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1958 if (product == MOSCHIP_DEVICE_ID_7715) {
1959 ret_val = mos7715_parport_init(serial);
1964 /* LSR For Port 1 */
1965 read_mos_reg(serial, 0, MOS7720_LSR, &data);
1966 dev_dbg(&dev->dev, "LSR:%x\n", data);
1971 static void mos7720_release(struct usb_serial *serial)
1973 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1974 /* close the parallel port */
1976 if (le16_to_cpu(serial->dev->descriptor.idProduct)
1977 == MOSCHIP_DEVICE_ID_7715) {
1978 struct urbtracker *urbtrack;
1979 unsigned long flags;
1980 struct mos7715_parport *mos_parport =
1981 usb_get_serial_data(serial);
1983 /* prevent NULL ptr dereference in port callbacks */
1984 spin_lock(&release_lock);
1985 mos_parport->pp->private_data = NULL;
1986 spin_unlock(&release_lock);
1988 /* wait for synchronous usb calls to return */
1989 if (mos_parport->msg_pending)
1990 wait_for_completion_timeout(&mos_parport->syncmsg_compl,
1991 msecs_to_jiffies(MOS_WDR_TIMEOUT));
1993 parport_remove_port(mos_parport->pp);
1994 usb_set_serial_data(serial, NULL);
1995 mos_parport->serial = NULL;
1997 /* if tasklet currently scheduled, wait for it to complete */
1998 tasklet_kill(&mos_parport->urb_tasklet);
2000 /* unlink any urbs sent by the tasklet */
2001 spin_lock_irqsave(&mos_parport->listlock, flags);
2002 list_for_each_entry(urbtrack,
2003 &mos_parport->active_urbs,
2005 usb_unlink_urb(urbtrack->urb);
2006 spin_unlock_irqrestore(&mos_parport->listlock, flags);
2007 parport_del_port(mos_parport->pp);
2009 kref_put(&mos_parport->ref_count, destroy_mos_parport);
2014 static int mos7720_port_probe(struct usb_serial_port *port)
2016 struct moschip_port *mos7720_port;
2018 mos7720_port = kzalloc(sizeof(*mos7720_port), GFP_KERNEL);
2022 /* Initialize all port interrupt end point to port 0 int endpoint.
2023 * Our device has only one interrupt endpoint common to all ports.
2025 port->interrupt_in_endpointAddress =
2026 port->serial->port[0]->interrupt_in_endpointAddress;
2027 mos7720_port->port = port;
2029 usb_set_serial_port_data(port, mos7720_port);
2034 static int mos7720_port_remove(struct usb_serial_port *port)
2036 struct moschip_port *mos7720_port;
2038 mos7720_port = usb_get_serial_port_data(port);
2039 kfree(mos7720_port);
2044 static struct usb_serial_driver moschip7720_2port_driver = {
2046 .owner = THIS_MODULE,
2047 .name = "moschip7720",
2049 .description = "Moschip 2 port adapter",
2050 .id_table = id_table,
2051 .calc_num_ports = mos77xx_calc_num_ports,
2052 .open = mos7720_open,
2053 .close = mos7720_close,
2054 .throttle = mos7720_throttle,
2055 .unthrottle = mos7720_unthrottle,
2056 .probe = mos77xx_probe,
2057 .attach = mos7720_startup,
2058 .release = mos7720_release,
2059 .port_probe = mos7720_port_probe,
2060 .port_remove = mos7720_port_remove,
2061 .ioctl = mos7720_ioctl,
2062 .tiocmget = mos7720_tiocmget,
2063 .tiocmset = mos7720_tiocmset,
2064 .set_termios = mos7720_set_termios,
2065 .write = mos7720_write,
2066 .write_room = mos7720_write_room,
2067 .chars_in_buffer = mos7720_chars_in_buffer,
2068 .break_ctl = mos7720_break,
2069 .read_bulk_callback = mos7720_bulk_in_callback,
2070 .read_int_callback = NULL /* dynamically assigned in probe() */
2073 static struct usb_serial_driver * const serial_drivers[] = {
2074 &moschip7720_2port_driver, NULL
2077 module_usb_serial_driver(serial_drivers, id_table);
2079 MODULE_AUTHOR(DRIVER_AUTHOR);
2080 MODULE_DESCRIPTION(DRIVER_DESC);
2081 MODULE_LICENSE("GPL");