1 // SPDX-License-Identifier: GPL-2.0
4 * Controls the Moschip 7720 usb to dual port serial converter
6 * Copyright 2006 Moschip Semiconductor Tech. Ltd.
9 * Vijaya Kumar <vijaykumar.gn@gmail.com>
10 * Ajay Kumar <naanuajay@yahoo.com>
11 * Gurudeva <ngurudeva@yahoo.com>
13 * Cleaned up from the original by:
14 * Greg Kroah-Hartman <gregkh@suse.de>
16 * Originally based on drivers/usb/serial/io_edgeport.c which is:
17 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
18 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/slab.h>
23 #include <linux/tty.h>
24 #include <linux/tty_driver.h>
25 #include <linux/tty_flip.h>
26 #include <linux/module.h>
27 #include <linux/spinlock.h>
28 #include <linux/serial.h>
29 #include <linux/serial_reg.h>
30 #include <linux/usb.h>
31 #include <linux/usb/serial.h>
32 #include <linux/uaccess.h>
33 #include <linux/parport.h>
35 #define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
36 #define DRIVER_DESC "Moschip USB Serial Driver"
38 /* default urb timeout */
39 #define MOS_WDR_TIMEOUT 5000
41 #define MOS_MAX_PORT 0x02
42 #define MOS_WRITE 0x0E
45 /* Interrupt Routines Defines */
46 #define SERIAL_IIR_RLS 0x06
47 #define SERIAL_IIR_RDA 0x04
48 #define SERIAL_IIR_CTI 0x0c
49 #define SERIAL_IIR_THR 0x02
50 #define SERIAL_IIR_MS 0x00
52 #define NUM_URBS 16 /* URB Count */
53 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
55 /* This structure holds all of the local serial port information */
57 __u8 shadowLCR; /* last LCR value received */
58 __u8 shadowMCR; /* last MCR value received */
59 __u8 shadowMSR; /* last MSR value received */
61 struct usb_serial_port *port; /* loop back to the owner */
62 struct urb *write_urb_pool[NUM_URBS];
65 #define USB_VENDOR_ID_MOSCHIP 0x9710
66 #define MOSCHIP_DEVICE_ID_7720 0x7720
67 #define MOSCHIP_DEVICE_ID_7715 0x7715
69 static const struct usb_device_id id_table[] = {
70 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
71 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) },
72 { } /* terminating entry */
74 MODULE_DEVICE_TABLE(usb, id_table);
76 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
78 /* initial values for parport regs */
79 #define DCR_INIT_VAL 0x0c /* SLCTIN, nINIT */
80 #define ECR_INIT_VAL 0x00 /* SPP mode */
83 struct mos7715_parport *mos_parport;
84 struct list_head urblist_entry;
85 struct kref ref_count;
87 struct usb_ctrlrequest *setup;
90 enum mos7715_pp_modes {
92 PS2 = 1<<5, /* moschip calls this 'NIBBLE' mode */
93 PPF = 2<<5, /* moschip calls this 'CB-FIFO mode */
96 struct mos7715_parport {
97 struct parport *pp; /* back to containing struct */
98 struct kref ref_count; /* to instance of this struct */
99 struct list_head deferred_urbs; /* list deferred async urbs */
100 struct list_head active_urbs; /* list async urbs in flight */
101 spinlock_t listlock; /* protects list access */
102 bool msg_pending; /* usb sync call pending */
103 struct completion syncmsg_compl; /* usb sync call completed */
104 struct tasklet_struct urb_tasklet; /* for sending deferred urbs */
105 struct usb_serial *serial; /* back to containing struct */
106 __u8 shadowECR; /* parallel port regs... */
108 atomic_t shadowDSR; /* updated in int-in callback */
111 /* lock guards against dereferencing NULL ptr in parport ops callbacks */
112 static DEFINE_SPINLOCK(release_lock);
114 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
116 static const unsigned int dummy; /* for clarity in register access fns */
119 MOS7720_THR, /* serial port regs */
131 MOS7720_DPR, /* parallel port regs */
135 MOS7720_SP1_REG, /* device control regs */
136 MOS7720_SP2_REG, /* serial port 2 (7720 only) */
138 MOS7720_SP_CONTROL_REG,
142 * Return the correct value for the Windex field of the setup packet
143 * for a control endpoint message. See the 7715 datasheet.
145 static inline __u16 get_reg_index(enum mos_regs reg)
147 static const __u16 mos7715_index_lookup_table[] = {
148 0x00, /* MOS7720_THR */
149 0x00, /* MOS7720_RHR */
150 0x01, /* MOS7720_IER */
151 0x02, /* MOS7720_FCR */
152 0x02, /* MOS7720_ISR */
153 0x03, /* MOS7720_LCR */
154 0x04, /* MOS7720_MCR */
155 0x05, /* MOS7720_LSR */
156 0x06, /* MOS7720_MSR */
157 0x07, /* MOS7720_SPR */
158 0x00, /* MOS7720_DLL */
159 0x01, /* MOS7720_DLM */
160 0x00, /* MOS7720_DPR */
161 0x01, /* MOS7720_DSR */
162 0x02, /* MOS7720_DCR */
163 0x0a, /* MOS7720_ECR */
164 0x01, /* MOS7720_SP1_REG */
165 0x02, /* MOS7720_SP2_REG (7720 only) */
166 0x04, /* MOS7720_PP_REG (7715 only) */
167 0x08, /* MOS7720_SP_CONTROL_REG */
169 return mos7715_index_lookup_table[reg];
173 * Return the correct value for the upper byte of the Wvalue field of
174 * the setup packet for a control endpoint message.
176 static inline __u16 get_reg_value(enum mos_regs reg,
177 unsigned int serial_portnum)
179 if (reg >= MOS7720_SP1_REG) /* control reg */
182 else if (reg >= MOS7720_DPR) /* parallel port reg (7715 only) */
185 else /* serial port reg */
186 return (serial_portnum + 2) << 8;
190 * Write data byte to the specified device register. The data is embedded in
191 * the value field of the setup packet. serial_portnum is ignored for registers
192 * not specific to a particular serial port.
194 static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
195 enum mos_regs reg, __u8 data)
197 struct usb_device *usbdev = serial->dev;
198 unsigned int pipe = usb_sndctrlpipe(usbdev, 0);
199 __u8 request = (__u8)0x0e;
200 __u8 requesttype = (__u8)0x40;
201 __u16 index = get_reg_index(reg);
202 __u16 value = get_reg_value(reg, serial_portnum) + data;
203 int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
204 index, NULL, 0, MOS_WDR_TIMEOUT);
206 dev_err(&usbdev->dev,
207 "mos7720: usb_control_msg() failed: %d\n", status);
212 * Read data byte from the specified device register. The data returned by the
213 * device is embedded in the value field of the setup packet. serial_portnum is
214 * ignored for registers that are not specific to a particular serial port.
216 static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
217 enum mos_regs reg, __u8 *data)
219 struct usb_device *usbdev = serial->dev;
220 unsigned int pipe = usb_rcvctrlpipe(usbdev, 0);
221 __u8 request = (__u8)0x0d;
222 __u8 requesttype = (__u8)0xc0;
223 __u16 index = get_reg_index(reg);
224 __u16 value = get_reg_value(reg, serial_portnum);
228 buf = kmalloc(1, GFP_KERNEL);
232 status = usb_control_msg(usbdev, pipe, request, requesttype, value,
233 index, buf, 1, MOS_WDR_TIMEOUT);
237 dev_err(&usbdev->dev,
238 "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(struct tasklet_struct *t)
289 struct mos7715_parport *mos_parport = from_tasklet(mos_parport, t,
291 struct urbtracker *urbtrack, *tmp;
292 struct list_head *cursor, *next;
295 /* if release function ran, game over */
296 if (unlikely(mos_parport->serial == NULL))
299 dev = &mos_parport->serial->dev->dev;
301 /* try again to get the mutex */
302 if (!mutex_trylock(&mos_parport->serial->disc_mutex)) {
303 dev_dbg(dev, "%s: rescheduling tasklet\n", __func__);
304 tasklet_schedule(&mos_parport->urb_tasklet);
308 /* if device disconnected, game over */
309 if (unlikely(mos_parport->serial->disconnected)) {
310 mutex_unlock(&mos_parport->serial->disc_mutex);
314 spin_lock_irqsave(&mos_parport->listlock, flags);
315 if (list_empty(&mos_parport->deferred_urbs)) {
316 spin_unlock_irqrestore(&mos_parport->listlock, flags);
317 mutex_unlock(&mos_parport->serial->disc_mutex);
318 dev_dbg(dev, "%s: deferred_urbs list empty\n", __func__);
322 /* move contents of deferred_urbs list to active_urbs list and submit */
323 list_for_each_safe(cursor, next, &mos_parport->deferred_urbs)
324 list_move_tail(cursor, &mos_parport->active_urbs);
325 list_for_each_entry_safe(urbtrack, tmp, &mos_parport->active_urbs,
327 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
328 dev_dbg(dev, "%s: urb submitted\n", __func__);
330 dev_err(dev, "usb_submit_urb() failed: %d\n", ret_val);
331 list_del(&urbtrack->urblist_entry);
332 kref_put(&urbtrack->ref_count, destroy_urbtracker);
335 spin_unlock_irqrestore(&mos_parport->listlock, flags);
336 mutex_unlock(&mos_parport->serial->disc_mutex);
339 /* callback for parallel port control urbs submitted asynchronously */
340 static void async_complete(struct urb *urb)
342 struct urbtracker *urbtrack = urb->context;
343 int status = urb->status;
346 if (unlikely(status))
347 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
349 /* remove the urbtracker from the active_urbs list */
350 spin_lock_irqsave(&urbtrack->mos_parport->listlock, flags);
351 list_del(&urbtrack->urblist_entry);
352 spin_unlock_irqrestore(&urbtrack->mos_parport->listlock, flags);
353 kref_put(&urbtrack->ref_count, destroy_urbtracker);
356 static int write_parport_reg_nonblock(struct mos7715_parport *mos_parport,
357 enum mos_regs reg, __u8 data)
359 struct urbtracker *urbtrack;
362 struct usb_serial *serial = mos_parport->serial;
363 struct usb_device *usbdev = serial->dev;
365 /* create and initialize the control urb and containing urbtracker */
366 urbtrack = kmalloc(sizeof(struct urbtracker), GFP_ATOMIC);
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_get(&mos_parport->ref_count);
391 urbtrack->mos_parport = mos_parport;
392 kref_init(&urbtrack->ref_count);
393 INIT_LIST_HEAD(&urbtrack->urblist_entry);
396 * get the disconnect mutex, or add tracker to the deferred_urbs list
397 * and schedule a tasklet to try again later
399 if (!mutex_trylock(&serial->disc_mutex)) {
400 spin_lock_irqsave(&mos_parport->listlock, flags);
401 list_add_tail(&urbtrack->urblist_entry,
402 &mos_parport->deferred_urbs);
403 spin_unlock_irqrestore(&mos_parport->listlock, flags);
404 tasklet_schedule(&mos_parport->urb_tasklet);
405 dev_dbg(&usbdev->dev, "tasklet scheduled\n");
409 /* bail if device disconnected */
410 if (serial->disconnected) {
411 kref_put(&urbtrack->ref_count, destroy_urbtracker);
412 mutex_unlock(&serial->disc_mutex);
416 /* add the tracker to the active_urbs list and submit */
417 spin_lock_irqsave(&mos_parport->listlock, flags);
418 list_add_tail(&urbtrack->urblist_entry, &mos_parport->active_urbs);
419 spin_unlock_irqrestore(&mos_parport->listlock, flags);
420 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
421 mutex_unlock(&serial->disc_mutex);
423 dev_err(&usbdev->dev,
424 "%s: submit_urb() failed: %d\n", __func__, ret_val);
425 spin_lock_irqsave(&mos_parport->listlock, flags);
426 list_del(&urbtrack->urblist_entry);
427 spin_unlock_irqrestore(&mos_parport->listlock, flags);
428 kref_put(&urbtrack->ref_count, destroy_urbtracker);
435 * This is the the common top part of all parallel port callback operations that
436 * send synchronous messages to the device. This implements convoluted locking
437 * that avoids two scenarios: (1) a port operation is called after usbserial
438 * has called our release function, at which point struct mos7715_parport has
439 * been destroyed, and (2) the device has been disconnected, but usbserial has
440 * not called the release function yet because someone has a serial port open.
441 * The shared release_lock prevents the first, and the mutex and disconnected
442 * flag maintained by usbserial covers the second. We also use the msg_pending
443 * flag to ensure that all synchronous usb message calls have completed before
444 * our release function can return.
446 static int parport_prologue(struct parport *pp)
448 struct mos7715_parport *mos_parport;
450 spin_lock(&release_lock);
451 mos_parport = pp->private_data;
452 if (unlikely(mos_parport == NULL)) {
453 /* release fn called, port struct destroyed */
454 spin_unlock(&release_lock);
457 mos_parport->msg_pending = true; /* synch usb call pending */
458 reinit_completion(&mos_parport->syncmsg_compl);
459 spin_unlock(&release_lock);
461 mutex_lock(&mos_parport->serial->disc_mutex);
462 if (mos_parport->serial->disconnected) {
463 /* device disconnected */
464 mutex_unlock(&mos_parport->serial->disc_mutex);
465 mos_parport->msg_pending = false;
466 complete(&mos_parport->syncmsg_compl);
474 * This is the common bottom part of all parallel port functions that send
475 * synchronous messages to the device.
477 static inline void parport_epilogue(struct parport *pp)
479 struct mos7715_parport *mos_parport = pp->private_data;
480 mutex_unlock(&mos_parport->serial->disc_mutex);
481 mos_parport->msg_pending = false;
482 complete(&mos_parport->syncmsg_compl);
485 static void parport_mos7715_write_data(struct parport *pp, unsigned char d)
487 struct mos7715_parport *mos_parport = pp->private_data;
489 if (parport_prologue(pp) < 0)
491 mos7715_change_mode(mos_parport, SPP);
492 write_mos_reg(mos_parport->serial, dummy, MOS7720_DPR, (__u8)d);
493 parport_epilogue(pp);
496 static unsigned char parport_mos7715_read_data(struct parport *pp)
498 struct mos7715_parport *mos_parport = pp->private_data;
501 if (parport_prologue(pp) < 0)
503 read_mos_reg(mos_parport->serial, dummy, MOS7720_DPR, &d);
504 parport_epilogue(pp);
508 static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
510 struct mos7715_parport *mos_parport = pp->private_data;
513 if (parport_prologue(pp) < 0)
515 data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
516 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR, data);
517 mos_parport->shadowDCR = data;
518 parport_epilogue(pp);
521 static unsigned char parport_mos7715_read_control(struct parport *pp)
523 struct mos7715_parport *mos_parport;
526 spin_lock(&release_lock);
527 mos_parport = pp->private_data;
528 if (unlikely(mos_parport == NULL)) {
529 spin_unlock(&release_lock);
532 dcr = mos_parport->shadowDCR & 0x0f;
533 spin_unlock(&release_lock);
537 static unsigned char parport_mos7715_frob_control(struct parport *pp,
541 struct mos7715_parport *mos_parport = pp->private_data;
546 if (parport_prologue(pp) < 0)
548 mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
549 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
550 mos_parport->shadowDCR);
551 dcr = mos_parport->shadowDCR & 0x0f;
552 parport_epilogue(pp);
556 static unsigned char parport_mos7715_read_status(struct parport *pp)
558 unsigned char status;
559 struct mos7715_parport *mos_parport;
561 spin_lock(&release_lock);
562 mos_parport = pp->private_data;
563 if (unlikely(mos_parport == NULL)) { /* release called */
564 spin_unlock(&release_lock);
567 status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
568 spin_unlock(&release_lock);
572 static void parport_mos7715_enable_irq(struct parport *pp)
576 static void parport_mos7715_disable_irq(struct parport *pp)
580 static void parport_mos7715_data_forward(struct parport *pp)
582 struct mos7715_parport *mos_parport = pp->private_data;
584 if (parport_prologue(pp) < 0)
586 mos7715_change_mode(mos_parport, PS2);
587 mos_parport->shadowDCR &= ~0x20;
588 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
589 mos_parport->shadowDCR);
590 parport_epilogue(pp);
593 static void parport_mos7715_data_reverse(struct parport *pp)
595 struct mos7715_parport *mos_parport = pp->private_data;
597 if (parport_prologue(pp) < 0)
599 mos7715_change_mode(mos_parport, PS2);
600 mos_parport->shadowDCR |= 0x20;
601 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
602 mos_parport->shadowDCR);
603 parport_epilogue(pp);
606 static void parport_mos7715_init_state(struct pardevice *dev,
607 struct parport_state *s)
609 s->u.pc.ctr = DCR_INIT_VAL;
610 s->u.pc.ecr = ECR_INIT_VAL;
613 /* N.B. Parport core code requires that this function not block */
614 static void parport_mos7715_save_state(struct parport *pp,
615 struct parport_state *s)
617 struct mos7715_parport *mos_parport;
619 spin_lock(&release_lock);
620 mos_parport = pp->private_data;
621 if (unlikely(mos_parport == NULL)) { /* release called */
622 spin_unlock(&release_lock);
625 s->u.pc.ctr = mos_parport->shadowDCR;
626 s->u.pc.ecr = mos_parport->shadowECR;
627 spin_unlock(&release_lock);
630 /* N.B. Parport core code requires that this function not block */
631 static void parport_mos7715_restore_state(struct parport *pp,
632 struct parport_state *s)
634 struct mos7715_parport *mos_parport;
636 spin_lock(&release_lock);
637 mos_parport = pp->private_data;
638 if (unlikely(mos_parport == NULL)) { /* release called */
639 spin_unlock(&release_lock);
642 write_parport_reg_nonblock(mos_parport, MOS7720_DCR,
643 mos_parport->shadowDCR);
644 write_parport_reg_nonblock(mos_parport, MOS7720_ECR,
645 mos_parport->shadowECR);
646 spin_unlock(&release_lock);
649 static size_t parport_mos7715_write_compat(struct parport *pp,
651 size_t len, int flags)
654 struct mos7715_parport *mos_parport = pp->private_data;
657 if (parport_prologue(pp) < 0)
659 mos7715_change_mode(mos_parport, PPF);
660 retval = usb_bulk_msg(mos_parport->serial->dev,
661 usb_sndbulkpipe(mos_parport->serial->dev, 2),
662 (void *)buffer, len, &actual_len,
664 parport_epilogue(pp);
666 dev_err(&mos_parport->serial->dev->dev,
667 "mos7720: usb_bulk_msg() failed: %d\n", retval);
673 static struct parport_operations parport_mos7715_ops = {
674 .owner = THIS_MODULE,
675 .write_data = parport_mos7715_write_data,
676 .read_data = parport_mos7715_read_data,
678 .write_control = parport_mos7715_write_control,
679 .read_control = parport_mos7715_read_control,
680 .frob_control = parport_mos7715_frob_control,
682 .read_status = parport_mos7715_read_status,
684 .enable_irq = parport_mos7715_enable_irq,
685 .disable_irq = parport_mos7715_disable_irq,
687 .data_forward = parport_mos7715_data_forward,
688 .data_reverse = parport_mos7715_data_reverse,
690 .init_state = parport_mos7715_init_state,
691 .save_state = parport_mos7715_save_state,
692 .restore_state = parport_mos7715_restore_state,
694 .compat_write_data = parport_mos7715_write_compat,
696 .nibble_read_data = parport_ieee1284_read_nibble,
697 .byte_read_data = parport_ieee1284_read_byte,
701 * Allocate and initialize parallel port control struct, initialize
702 * the parallel port hardware device, and register with the parport subsystem.
704 static int mos7715_parport_init(struct usb_serial *serial)
706 struct mos7715_parport *mos_parport;
708 /* allocate and initialize parallel port control struct */
709 mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
713 mos_parport->msg_pending = false;
714 kref_init(&mos_parport->ref_count);
715 spin_lock_init(&mos_parport->listlock);
716 INIT_LIST_HEAD(&mos_parport->active_urbs);
717 INIT_LIST_HEAD(&mos_parport->deferred_urbs);
718 usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
719 mos_parport->serial = serial;
720 tasklet_setup(&mos_parport->urb_tasklet, send_deferred_urbs);
721 init_completion(&mos_parport->syncmsg_compl);
723 /* cycle parallel port reset bit */
724 write_mos_reg(mos_parport->serial, dummy, MOS7720_PP_REG, (__u8)0x80);
725 write_mos_reg(mos_parport->serial, dummy, MOS7720_PP_REG, (__u8)0x00);
727 /* initialize device registers */
728 mos_parport->shadowDCR = DCR_INIT_VAL;
729 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
730 mos_parport->shadowDCR);
731 mos_parport->shadowECR = ECR_INIT_VAL;
732 write_mos_reg(mos_parport->serial, dummy, MOS7720_ECR,
733 mos_parport->shadowECR);
735 /* register with parport core */
736 mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE,
738 &parport_mos7715_ops);
739 if (mos_parport->pp == NULL) {
740 dev_err(&serial->interface->dev,
741 "Could not register parport\n");
742 kref_put(&mos_parport->ref_count, destroy_mos_parport);
745 mos_parport->pp->private_data = mos_parport;
746 mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP;
747 mos_parport->pp->dev = &serial->interface->dev;
748 parport_announce_port(mos_parport->pp);
752 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
755 * mos7720_interrupt_callback
756 * this is the callback function for when we have received data on the
757 * interrupt endpoint.
759 static void mos7720_interrupt_callback(struct urb *urb)
763 int status = urb->status;
764 struct device *dev = &urb->dev->dev;
776 /* this urb is terminated, clean up */
777 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
780 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
784 length = urb->actual_length;
785 data = urb->transfer_buffer;
787 /* Moschip get 4 bytes
788 * Byte 1 IIR Port 1 (port.number is 0)
789 * Byte 2 IIR Port 2 (port.number is 1)
790 * Byte 3 --------------
791 * Byte 4 FIFO status for both */
793 /* the above description is inverted
794 * oneukum 2007-03-14 */
796 if (unlikely(length != 4)) {
797 dev_dbg(dev, "Wrong data !!!\n");
804 if ((sp1 | sp2) & 0x01) {
805 /* No Interrupt Pending in both the ports */
806 dev_dbg(dev, "No Interrupt !!!\n");
808 switch (sp1 & 0x0f) {
810 dev_dbg(dev, "Serial Port 1: Receiver status error or address bit detected in 9-bit mode\n");
813 dev_dbg(dev, "Serial Port 1: Receiver time out\n");
816 /* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */
820 switch (sp2 & 0x0f) {
822 dev_dbg(dev, "Serial Port 2: Receiver status error or address bit detected in 9-bit mode\n");
825 dev_dbg(dev, "Serial Port 2: Receiver time out\n");
828 /* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */
834 result = usb_submit_urb(urb, GFP_ATOMIC);
836 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
840 * mos7715_interrupt_callback
841 * this is the 7715's callback function for when we have received data on
842 * the interrupt endpoint.
844 static void mos7715_interrupt_callback(struct urb *urb)
848 int status = urb->status;
849 struct device *dev = &urb->dev->dev;
861 /* this urb is terminated, clean up */
862 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
865 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
869 length = urb->actual_length;
870 data = urb->transfer_buffer;
872 /* Structure of data from 7715 device:
873 * Byte 1: IIR serial Port
875 * Byte 2: DSR parallel port
876 * Byte 4: FIFO status for both */
878 if (unlikely(length != 4)) {
879 dev_dbg(dev, "Wrong data !!!\n");
884 if (!(iir & 0x01)) { /* serial port interrupt pending */
885 switch (iir & 0x0f) {
887 dev_dbg(dev, "Serial Port: Receiver status error or address bit detected in 9-bit mode\n");
890 dev_dbg(dev, "Serial Port: Receiver time out\n");
893 /* dev_dbg(dev, "Serial Port: Modem status change\n"); */
898 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
899 { /* update local copy of DSR reg */
900 struct usb_serial_port *port = urb->context;
901 struct mos7715_parport *mos_parport = port->serial->private;
902 if (unlikely(mos_parport == NULL))
904 atomic_set(&mos_parport->shadowDSR, data[2]);
909 result = usb_submit_urb(urb, GFP_ATOMIC);
911 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
915 * mos7720_bulk_in_callback
916 * this is the callback function for when we have received data on the
919 static void mos7720_bulk_in_callback(struct urb *urb)
922 unsigned char *data ;
923 struct usb_serial_port *port;
924 int status = urb->status;
927 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
933 dev_dbg(&port->dev, "Entering...%s\n", __func__);
935 data = urb->transfer_buffer;
937 if (urb->actual_length) {
938 tty_insert_flip_string(&port->port, data, urb->actual_length);
939 tty_flip_buffer_push(&port->port);
942 if (port->read_urb->status != -EINPROGRESS) {
943 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
945 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
950 * mos7720_bulk_out_data_callback
951 * this is the callback function for when we have finished sending serial
952 * data on the bulk out endpoint.
954 static void mos7720_bulk_out_data_callback(struct urb *urb)
956 struct moschip_port *mos7720_port;
957 int status = urb->status;
960 dev_dbg(&urb->dev->dev, "nonzero write bulk status received:%d\n", status);
964 mos7720_port = urb->context;
966 dev_dbg(&urb->dev->dev, "NULL mos7720_port pointer\n");
970 if (mos7720_port->open)
971 tty_port_tty_wakeup(&mos7720_port->port->port);
974 static int mos77xx_calc_num_ports(struct usb_serial *serial,
975 struct usb_serial_endpoints *epds)
977 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
979 if (product == MOSCHIP_DEVICE_ID_7715) {
981 * The 7715 uses the first bulk in/out endpoint pair for the
982 * parallel port, and the second for the serial port. We swap
983 * the endpoint descriptors here so that the the first and
984 * only registered port structure uses the serial-port
987 swap(epds->bulk_in[0], epds->bulk_in[1]);
988 swap(epds->bulk_out[0], epds->bulk_out[1]);
996 static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
998 struct usb_serial *serial;
1000 struct moschip_port *mos7720_port;
1004 int allocated_urbs = 0;
1007 serial = port->serial;
1009 mos7720_port = usb_get_serial_port_data(port);
1010 if (mos7720_port == NULL)
1013 usb_clear_halt(serial->dev, port->write_urb->pipe);
1014 usb_clear_halt(serial->dev, port->read_urb->pipe);
1016 /* Initialising the write urb pool */
1017 for (j = 0; j < NUM_URBS; ++j) {
1018 urb = usb_alloc_urb(0, GFP_KERNEL);
1019 mos7720_port->write_urb_pool[j] = urb;
1023 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1025 if (!urb->transfer_buffer) {
1026 usb_free_urb(mos7720_port->write_urb_pool[j]);
1027 mos7720_port->write_urb_pool[j] = NULL;
1033 if (!allocated_urbs)
1036 /* Initialize MCS7720 -- Write Init values to corresponding Registers
1039 * 0 : MOS7720_THR/MOS7720_RHR
1048 * 0x08 : SP1/2 Control Reg
1050 port_number = port->port_number;
1051 read_mos_reg(serial, port_number, MOS7720_LSR, &data);
1053 dev_dbg(&port->dev, "SS::%p LSR:%x\n", mos7720_port, data);
1055 write_mos_reg(serial, dummy, MOS7720_SP1_REG, 0x02);
1056 write_mos_reg(serial, dummy, MOS7720_SP2_REG, 0x02);
1058 write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
1059 write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
1061 write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
1062 mos7720_port->shadowLCR = 0x03;
1063 write_mos_reg(serial, port_number, MOS7720_LCR,
1064 mos7720_port->shadowLCR);
1065 mos7720_port->shadowMCR = 0x0b;
1066 write_mos_reg(serial, port_number, MOS7720_MCR,
1067 mos7720_port->shadowMCR);
1069 write_mos_reg(serial, port_number, MOS7720_SP_CONTROL_REG, 0x00);
1070 read_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, &data);
1071 data = data | (port->port_number + 1);
1072 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, data);
1073 mos7720_port->shadowLCR = 0x83;
1074 write_mos_reg(serial, port_number, MOS7720_LCR,
1075 mos7720_port->shadowLCR);
1076 write_mos_reg(serial, port_number, MOS7720_THR, 0x0c);
1077 write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
1078 mos7720_port->shadowLCR = 0x03;
1079 write_mos_reg(serial, port_number, MOS7720_LCR,
1080 mos7720_port->shadowLCR);
1081 write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
1083 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
1085 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
1086 __func__, response);
1088 /* initialize our port settings */
1089 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
1091 /* send a open port command */
1092 mos7720_port->open = 1;
1098 * mos7720_chars_in_buffer
1099 * this function is called by the tty driver when it wants to know how many
1100 * bytes of data we currently have outstanding in the port (data that has
1101 * been written, but hasn't made it out the port yet)
1102 * If successful, we return the number of bytes left to be written in the
1104 * Otherwise we return a negative error number.
1106 static int mos7720_chars_in_buffer(struct tty_struct *tty)
1108 struct usb_serial_port *port = tty->driver_data;
1111 struct moschip_port *mos7720_port;
1113 mos7720_port = usb_get_serial_port_data(port);
1114 if (mos7720_port == NULL)
1117 for (i = 0; i < NUM_URBS; ++i) {
1118 if (mos7720_port->write_urb_pool[i] &&
1119 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
1120 chars += URB_TRANSFER_BUFFER_SIZE;
1122 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
1126 static void mos7720_close(struct usb_serial_port *port)
1128 struct usb_serial *serial;
1129 struct moschip_port *mos7720_port;
1132 serial = port->serial;
1134 mos7720_port = usb_get_serial_port_data(port);
1135 if (mos7720_port == NULL)
1138 for (j = 0; j < NUM_URBS; ++j)
1139 usb_kill_urb(mos7720_port->write_urb_pool[j]);
1141 /* Freeing Write URBs */
1142 for (j = 0; j < NUM_URBS; ++j) {
1143 if (mos7720_port->write_urb_pool[j]) {
1144 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
1145 usb_free_urb(mos7720_port->write_urb_pool[j]);
1149 /* While closing port, shutdown all bulk read, write *
1150 * and interrupt read if they exists, otherwise nop */
1151 usb_kill_urb(port->write_urb);
1152 usb_kill_urb(port->read_urb);
1154 write_mos_reg(serial, port->port_number, MOS7720_MCR, 0x00);
1155 write_mos_reg(serial, port->port_number, MOS7720_IER, 0x00);
1157 mos7720_port->open = 0;
1160 static void mos7720_break(struct tty_struct *tty, int break_state)
1162 struct usb_serial_port *port = tty->driver_data;
1164 struct usb_serial *serial;
1165 struct moschip_port *mos7720_port;
1167 serial = port->serial;
1169 mos7720_port = usb_get_serial_port_data(port);
1170 if (mos7720_port == NULL)
1173 if (break_state == -1)
1174 data = mos7720_port->shadowLCR | UART_LCR_SBC;
1176 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
1178 mos7720_port->shadowLCR = data;
1179 write_mos_reg(serial, port->port_number, MOS7720_LCR,
1180 mos7720_port->shadowLCR);
1184 * mos7720_write_room
1185 * this function is called by the tty driver when it wants to know how many
1186 * bytes of data we can accept for a specific port.
1187 * If successful, we return the amount of room that we have for this port
1188 * Otherwise we return a negative error number.
1190 static int mos7720_write_room(struct tty_struct *tty)
1192 struct usb_serial_port *port = tty->driver_data;
1193 struct moschip_port *mos7720_port;
1197 mos7720_port = usb_get_serial_port_data(port);
1198 if (mos7720_port == NULL)
1201 /* FIXME: Locking */
1202 for (i = 0; i < NUM_URBS; ++i) {
1203 if (mos7720_port->write_urb_pool[i] &&
1204 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
1205 room += URB_TRANSFER_BUFFER_SIZE;
1208 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
1212 static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
1213 const unsigned char *data, int count)
1220 struct moschip_port *mos7720_port;
1221 struct usb_serial *serial;
1223 const unsigned char *current_position = data;
1225 serial = port->serial;
1227 mos7720_port = usb_get_serial_port_data(port);
1228 if (mos7720_port == NULL)
1231 /* try to find a free urb in the list */
1234 for (i = 0; i < NUM_URBS; ++i) {
1235 if (mos7720_port->write_urb_pool[i] &&
1236 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
1237 urb = mos7720_port->write_urb_pool[i];
1238 dev_dbg(&port->dev, "URB:%d\n", i);
1244 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
1248 if (urb->transfer_buffer == NULL) {
1249 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1251 if (!urb->transfer_buffer)
1254 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1256 memcpy(urb->transfer_buffer, current_position, transfer_size);
1257 usb_serial_debug_data(&port->dev, __func__, transfer_size,
1258 urb->transfer_buffer);
1260 /* fill urb with data and submit */
1261 usb_fill_bulk_urb(urb, serial->dev,
1262 usb_sndbulkpipe(serial->dev,
1263 port->bulk_out_endpointAddress),
1264 urb->transfer_buffer, transfer_size,
1265 mos7720_bulk_out_data_callback, mos7720_port);
1267 /* send it down the pipe */
1268 status = usb_submit_urb(urb, GFP_ATOMIC);
1270 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1271 "with status = %d\n", __func__, status);
1272 bytes_sent = status;
1275 bytes_sent = transfer_size;
1281 static void mos7720_throttle(struct tty_struct *tty)
1283 struct usb_serial_port *port = tty->driver_data;
1284 struct moschip_port *mos7720_port;
1287 mos7720_port = usb_get_serial_port_data(port);
1289 if (mos7720_port == NULL)
1292 if (!mos7720_port->open) {
1293 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1297 /* if we are implementing XON/XOFF, send the stop character */
1299 unsigned char stop_char = STOP_CHAR(tty);
1300 status = mos7720_write(tty, port, &stop_char, 1);
1305 /* if we are implementing RTS/CTS, toggle that line */
1306 if (C_CRTSCTS(tty)) {
1307 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
1308 write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
1309 mos7720_port->shadowMCR);
1313 static void mos7720_unthrottle(struct tty_struct *tty)
1315 struct usb_serial_port *port = tty->driver_data;
1316 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1319 if (mos7720_port == NULL)
1322 if (!mos7720_port->open) {
1323 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1327 /* if we are implementing XON/XOFF, send the start character */
1329 unsigned char start_char = START_CHAR(tty);
1330 status = mos7720_write(tty, port, &start_char, 1);
1335 /* if we are implementing RTS/CTS, toggle that line */
1336 if (C_CRTSCTS(tty)) {
1337 mos7720_port->shadowMCR |= UART_MCR_RTS;
1338 write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
1339 mos7720_port->shadowMCR);
1343 /* FIXME: this function does not work */
1344 static int set_higher_rates(struct moschip_port *mos7720_port,
1347 struct usb_serial_port *port;
1348 struct usb_serial *serial;
1350 enum mos_regs sp_reg;
1351 if (mos7720_port == NULL)
1354 port = mos7720_port->port;
1355 serial = port->serial;
1357 /***********************************************
1358 * Init Sequence for higher rates
1359 ***********************************************/
1360 dev_dbg(&port->dev, "Sending Setting Commands ..........\n");
1361 port_number = port->port_number;
1363 write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
1364 write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
1365 write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
1366 mos7720_port->shadowMCR = 0x0b;
1367 write_mos_reg(serial, port_number, MOS7720_MCR,
1368 mos7720_port->shadowMCR);
1369 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, 0x00);
1371 /***********************************************
1372 * Set for higher rates *
1373 ***********************************************/
1374 /* writing baud rate verbatum into uart clock field clearly not right */
1375 if (port_number == 0)
1376 sp_reg = MOS7720_SP1_REG;
1378 sp_reg = MOS7720_SP2_REG;
1379 write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
1380 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, 0x03);
1381 mos7720_port->shadowMCR = 0x2b;
1382 write_mos_reg(serial, port_number, MOS7720_MCR,
1383 mos7720_port->shadowMCR);
1385 /***********************************************
1387 ***********************************************/
1388 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1389 write_mos_reg(serial, port_number, MOS7720_LCR,
1390 mos7720_port->shadowLCR);
1391 write_mos_reg(serial, port_number, MOS7720_DLL, 0x01);
1392 write_mos_reg(serial, port_number, MOS7720_DLM, 0x00);
1393 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1394 write_mos_reg(serial, port_number, MOS7720_LCR,
1395 mos7720_port->shadowLCR);
1400 /* baud rate information */
1401 struct divisor_table_entry {
1406 /* Define table of divisors for moschip 7720 hardware *
1407 * These assume a 3.6864MHz crystal, the standard /16, and *
1409 static const struct divisor_table_entry divisor_table[] = {
1411 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
1412 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
1428 /*****************************************************************************
1429 * calc_baud_rate_divisor
1430 * this function calculates the proper baud rate divisor for the specified
1432 *****************************************************************************/
1433 static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, int *divisor)
1441 dev_dbg(&port->dev, "%s - %d\n", __func__, baudrate);
1443 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
1444 if (divisor_table[i].baudrate == baudrate) {
1445 *divisor = divisor_table[i].divisor;
1450 /* After trying for all the standard baud rates *
1451 * Try calculating the divisor for this baud rate */
1452 if (baudrate > 75 && baudrate < 230400) {
1453 /* get the divisor */
1454 custom = (__u16)(230400L / baudrate);
1456 /* Check for round off */
1457 round1 = (__u16)(2304000L / baudrate);
1458 round = (__u16)(round1 - (custom * 10));
1463 dev_dbg(&port->dev, "Baud %d = %d\n", baudrate, custom);
1467 dev_dbg(&port->dev, "Baud calculation Failed...\n");
1472 * send_cmd_write_baud_rate
1473 * this function sends the proper command to change the baud rate of the
1476 static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1479 struct usb_serial_port *port;
1480 struct usb_serial *serial;
1483 unsigned char number;
1485 if (mos7720_port == NULL)
1488 port = mos7720_port->port;
1489 serial = port->serial;
1491 number = port->port_number;
1492 dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudrate);
1494 /* Calculate the Divisor */
1495 status = calc_baud_rate_divisor(port, baudrate, &divisor);
1497 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1501 /* Enable access to divisor latch */
1502 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1503 write_mos_reg(serial, number, MOS7720_LCR, mos7720_port->shadowLCR);
1505 /* Write the divisor */
1506 write_mos_reg(serial, number, MOS7720_DLL, (__u8)(divisor & 0xff));
1507 write_mos_reg(serial, number, MOS7720_DLM,
1508 (__u8)((divisor & 0xff00) >> 8));
1510 /* Disable access to divisor latch */
1511 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1512 write_mos_reg(serial, number, MOS7720_LCR, mos7720_port->shadowLCR);
1518 * change_port_settings
1519 * This routine is called to set the UART on the device to match
1520 * the specified new settings.
1522 static void change_port_settings(struct tty_struct *tty,
1523 struct moschip_port *mos7720_port,
1524 struct ktermios *old_termios)
1526 struct usb_serial_port *port;
1527 struct usb_serial *serial;
1536 if (mos7720_port == NULL)
1539 port = mos7720_port->port;
1540 serial = port->serial;
1541 port_number = port->port_number;
1543 if (!mos7720_port->open) {
1544 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1548 lData = UART_LCR_WLEN8;
1549 lStop = 0x00; /* 1 stop bit */
1550 lParity = 0x00; /* No parity */
1552 cflag = tty->termios.c_cflag;
1554 /* Change the number of bits */
1555 switch (cflag & CSIZE) {
1557 lData = UART_LCR_WLEN5;
1561 lData = UART_LCR_WLEN6;
1565 lData = UART_LCR_WLEN7;
1569 lData = UART_LCR_WLEN8;
1573 /* Change the Parity bit */
1574 if (cflag & PARENB) {
1575 if (cflag & PARODD) {
1576 lParity = UART_LCR_PARITY;
1577 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
1579 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
1580 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
1584 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
1588 lParity = lParity | 0x20;
1590 /* Change the Stop bit */
1591 if (cflag & CSTOPB) {
1592 lStop = UART_LCR_STOP;
1593 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
1596 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
1599 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1600 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1601 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
1603 /* Update the LCR with the correct value */
1604 mos7720_port->shadowLCR &=
1605 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1606 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1609 /* Disable Interrupts */
1610 write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
1611 write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
1612 write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
1614 /* Send the updated LCR value to the mos7720 */
1615 write_mos_reg(serial, port_number, MOS7720_LCR,
1616 mos7720_port->shadowLCR);
1617 mos7720_port->shadowMCR = 0x0b;
1618 write_mos_reg(serial, port_number, MOS7720_MCR,
1619 mos7720_port->shadowMCR);
1621 /* set up the MCR register and send it to the mos7720 */
1622 mos7720_port->shadowMCR = UART_MCR_OUT2;
1624 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1626 if (cflag & CRTSCTS) {
1627 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
1628 /* To set hardware flow control to the specified *
1629 * serial port, in SP1/2_CONTROL_REG */
1631 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG,
1634 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG,
1638 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1640 write_mos_reg(serial, port_number, MOS7720_MCR,
1641 mos7720_port->shadowMCR);
1643 /* Determine divisor based on baud rate */
1644 baud = tty_get_baud_rate(tty);
1646 /* pick a default, any default... */
1647 dev_dbg(&port->dev, "Picked default baud...\n");
1651 if (baud >= 230400) {
1652 set_higher_rates(mos7720_port, baud);
1653 /* Enable Interrupts */
1654 write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
1658 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
1659 status = send_cmd_write_baud_rate(mos7720_port, baud);
1660 /* FIXME: needs to write actual resulting baud back not just
1663 tty_encode_baud_rate(tty, baud, baud);
1664 /* Enable Interrupts */
1665 write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
1667 if (port->read_urb->status != -EINPROGRESS) {
1668 status = usb_submit_urb(port->read_urb, GFP_KERNEL);
1670 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
1675 * mos7720_set_termios
1676 * this function is called by the tty driver when it wants to change the
1677 * termios structure.
1679 static void mos7720_set_termios(struct tty_struct *tty,
1680 struct usb_serial_port *port, struct ktermios *old_termios)
1683 struct moschip_port *mos7720_port;
1685 mos7720_port = usb_get_serial_port_data(port);
1687 if (mos7720_port == NULL)
1690 if (!mos7720_port->open) {
1691 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1695 /* change the port settings to the new ones specified */
1696 change_port_settings(tty, mos7720_port, old_termios);
1698 if (port->read_urb->status != -EINPROGRESS) {
1699 status = usb_submit_urb(port->read_urb, GFP_KERNEL);
1701 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
1706 * get_lsr_info - get line status register info
1708 * Purpose: Let user call ioctl() to get info when the UART physically
1709 * is emptied. On bus types like RS485, the transmitter must
1710 * release the bus after transmitting. This must be done when
1711 * the transmit shift register is empty, not be done when the
1712 * transmit holding register is empty. This functionality
1713 * allows an RS485 driver to be written in user space.
1715 static int get_lsr_info(struct tty_struct *tty,
1716 struct moschip_port *mos7720_port, unsigned int __user *value)
1718 struct usb_serial_port *port = tty->driver_data;
1719 unsigned int result = 0;
1720 unsigned char data = 0;
1721 int port_number = port->port_number;
1724 count = mos7720_chars_in_buffer(tty);
1726 read_mos_reg(port->serial, port_number, MOS7720_LSR, &data);
1727 if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1728 == (UART_LSR_TEMT | UART_LSR_THRE)) {
1729 dev_dbg(&port->dev, "%s -- Empty\n", __func__);
1730 result = TIOCSER_TEMT;
1733 if (copy_to_user(value, &result, sizeof(int)))
1738 static int mos7720_tiocmget(struct tty_struct *tty)
1740 struct usb_serial_port *port = tty->driver_data;
1741 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1742 unsigned int result = 0;
1746 mcr = mos7720_port->shadowMCR;
1747 msr = mos7720_port->shadowMSR;
1749 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1750 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1751 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1752 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
1753 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1754 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
1759 static int mos7720_tiocmset(struct tty_struct *tty,
1760 unsigned int set, unsigned int clear)
1762 struct usb_serial_port *port = tty->driver_data;
1763 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1766 mcr = mos7720_port->shadowMCR;
1768 if (set & TIOCM_RTS)
1769 mcr |= UART_MCR_RTS;
1770 if (set & TIOCM_DTR)
1771 mcr |= UART_MCR_DTR;
1772 if (set & TIOCM_LOOP)
1773 mcr |= UART_MCR_LOOP;
1775 if (clear & TIOCM_RTS)
1776 mcr &= ~UART_MCR_RTS;
1777 if (clear & TIOCM_DTR)
1778 mcr &= ~UART_MCR_DTR;
1779 if (clear & TIOCM_LOOP)
1780 mcr &= ~UART_MCR_LOOP;
1782 mos7720_port->shadowMCR = mcr;
1783 write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
1784 mos7720_port->shadowMCR);
1789 static int get_serial_info(struct tty_struct *tty,
1790 struct serial_struct *ss)
1792 struct usb_serial_port *port = tty->driver_data;
1793 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1795 ss->type = PORT_16550A;
1796 ss->line = mos7720_port->port->minor;
1797 ss->port = mos7720_port->port->port_number;
1799 ss->xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1800 ss->baud_base = 9600;
1801 ss->close_delay = 5*HZ;
1802 ss->closing_wait = 30*HZ;
1806 static int mos7720_ioctl(struct tty_struct *tty,
1807 unsigned int cmd, unsigned long arg)
1809 struct usb_serial_port *port = tty->driver_data;
1810 struct moschip_port *mos7720_port;
1812 mos7720_port = usb_get_serial_port_data(port);
1813 if (mos7720_port == NULL)
1818 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
1819 return get_lsr_info(tty, mos7720_port,
1820 (unsigned int __user *)arg);
1823 return -ENOIOCTLCMD;
1826 static int mos7720_startup(struct usb_serial *serial)
1828 struct usb_device *dev;
1833 product = le16_to_cpu(serial->dev->descriptor.idProduct);
1836 if (product == MOSCHIP_DEVICE_ID_7715) {
1837 struct urb *urb = serial->port[0]->interrupt_in_urb;
1839 urb->complete = mos7715_interrupt_callback;
1841 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1842 ret_val = mos7715_parport_init(serial);
1847 /* start the interrupt urb */
1848 ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
1850 dev_err(&dev->dev, "failed to submit interrupt urb: %d\n",
1854 /* LSR For Port 1 */
1855 read_mos_reg(serial, 0, MOS7720_LSR, &data);
1856 dev_dbg(&dev->dev, "LSR:%x\n", data);
1861 static void mos7720_release(struct usb_serial *serial)
1863 usb_kill_urb(serial->port[0]->interrupt_in_urb);
1865 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1866 /* close the parallel port */
1868 if (le16_to_cpu(serial->dev->descriptor.idProduct)
1869 == MOSCHIP_DEVICE_ID_7715) {
1870 struct urbtracker *urbtrack;
1871 unsigned long flags;
1872 struct mos7715_parport *mos_parport =
1873 usb_get_serial_data(serial);
1875 /* prevent NULL ptr dereference in port callbacks */
1876 spin_lock(&release_lock);
1877 mos_parport->pp->private_data = NULL;
1878 spin_unlock(&release_lock);
1880 /* wait for synchronous usb calls to return */
1881 if (mos_parport->msg_pending)
1882 wait_for_completion_timeout(&mos_parport->syncmsg_compl,
1883 msecs_to_jiffies(MOS_WDR_TIMEOUT));
1885 parport_remove_port(mos_parport->pp);
1886 usb_set_serial_data(serial, NULL);
1887 mos_parport->serial = NULL;
1889 /* if tasklet currently scheduled, wait for it to complete */
1890 tasklet_kill(&mos_parport->urb_tasklet);
1892 /* unlink any urbs sent by the tasklet */
1893 spin_lock_irqsave(&mos_parport->listlock, flags);
1894 list_for_each_entry(urbtrack,
1895 &mos_parport->active_urbs,
1897 usb_unlink_urb(urbtrack->urb);
1898 spin_unlock_irqrestore(&mos_parport->listlock, flags);
1899 parport_del_port(mos_parport->pp);
1901 kref_put(&mos_parport->ref_count, destroy_mos_parport);
1906 static int mos7720_port_probe(struct usb_serial_port *port)
1908 struct moschip_port *mos7720_port;
1910 mos7720_port = kzalloc(sizeof(*mos7720_port), GFP_KERNEL);
1914 mos7720_port->port = port;
1916 usb_set_serial_port_data(port, mos7720_port);
1921 static int mos7720_port_remove(struct usb_serial_port *port)
1923 struct moschip_port *mos7720_port;
1925 mos7720_port = usb_get_serial_port_data(port);
1926 kfree(mos7720_port);
1931 static struct usb_serial_driver moschip7720_2port_driver = {
1933 .owner = THIS_MODULE,
1934 .name = "moschip7720",
1936 .description = "Moschip 2 port adapter",
1937 .id_table = id_table,
1940 .num_interrupt_in = 1,
1941 .calc_num_ports = mos77xx_calc_num_ports,
1942 .open = mos7720_open,
1943 .close = mos7720_close,
1944 .throttle = mos7720_throttle,
1945 .unthrottle = mos7720_unthrottle,
1946 .attach = mos7720_startup,
1947 .release = mos7720_release,
1948 .port_probe = mos7720_port_probe,
1949 .port_remove = mos7720_port_remove,
1950 .ioctl = mos7720_ioctl,
1951 .tiocmget = mos7720_tiocmget,
1952 .tiocmset = mos7720_tiocmset,
1953 .get_serial = get_serial_info,
1954 .set_termios = mos7720_set_termios,
1955 .write = mos7720_write,
1956 .write_room = mos7720_write_room,
1957 .chars_in_buffer = mos7720_chars_in_buffer,
1958 .break_ctl = mos7720_break,
1959 .read_bulk_callback = mos7720_bulk_in_callback,
1960 .read_int_callback = mos7720_interrupt_callback,
1963 static struct usb_serial_driver * const serial_drivers[] = {
1964 &moschip7720_2port_driver, NULL
1967 module_usb_serial_driver(serial_drivers, id_table);
1969 MODULE_AUTHOR(DRIVER_AUTHOR);
1970 MODULE_DESCRIPTION(DRIVER_DESC);
1971 MODULE_LICENSE("GPL v2");