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);
234 status = usb_control_msg(usbdev, pipe, request, requesttype, value,
235 index, buf, 1, MOS_WDR_TIMEOUT);
239 dev_err(&usbdev->dev,
240 "mos7720: usb_control_msg() failed: %d\n", status);
251 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
253 static inline int mos7715_change_mode(struct mos7715_parport *mos_parport,
254 enum mos7715_pp_modes mode)
256 mos_parport->shadowECR = mode;
257 write_mos_reg(mos_parport->serial, dummy, MOS7720_ECR,
258 mos_parport->shadowECR);
262 static void destroy_mos_parport(struct kref *kref)
264 struct mos7715_parport *mos_parport =
265 container_of(kref, struct mos7715_parport, ref_count);
270 static void destroy_urbtracker(struct kref *kref)
272 struct urbtracker *urbtrack =
273 container_of(kref, struct urbtracker, ref_count);
274 struct mos7715_parport *mos_parport = urbtrack->mos_parport;
276 usb_free_urb(urbtrack->urb);
277 kfree(urbtrack->setup);
279 kref_put(&mos_parport->ref_count, destroy_mos_parport);
283 * This runs as a tasklet when sending an urb in a non-blocking parallel
284 * port callback had to be deferred because the disconnect mutex could not be
285 * obtained at the time.
287 static void send_deferred_urbs(struct tasklet_struct *t)
291 struct mos7715_parport *mos_parport = from_tasklet(mos_parport, t,
293 struct urbtracker *urbtrack, *tmp;
294 struct list_head *cursor, *next;
297 /* if release function ran, game over */
298 if (unlikely(mos_parport->serial == NULL))
301 dev = &mos_parport->serial->dev->dev;
303 /* try again to get the mutex */
304 if (!mutex_trylock(&mos_parport->serial->disc_mutex)) {
305 dev_dbg(dev, "%s: rescheduling tasklet\n", __func__);
306 tasklet_schedule(&mos_parport->urb_tasklet);
310 /* if device disconnected, game over */
311 if (unlikely(mos_parport->serial->disconnected)) {
312 mutex_unlock(&mos_parport->serial->disc_mutex);
316 spin_lock_irqsave(&mos_parport->listlock, flags);
317 if (list_empty(&mos_parport->deferred_urbs)) {
318 spin_unlock_irqrestore(&mos_parport->listlock, flags);
319 mutex_unlock(&mos_parport->serial->disc_mutex);
320 dev_dbg(dev, "%s: deferred_urbs list empty\n", __func__);
324 /* move contents of deferred_urbs list to active_urbs list and submit */
325 list_for_each_safe(cursor, next, &mos_parport->deferred_urbs)
326 list_move_tail(cursor, &mos_parport->active_urbs);
327 list_for_each_entry_safe(urbtrack, tmp, &mos_parport->active_urbs,
329 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
330 dev_dbg(dev, "%s: urb submitted\n", __func__);
332 dev_err(dev, "usb_submit_urb() failed: %d\n", ret_val);
333 list_del(&urbtrack->urblist_entry);
334 kref_put(&urbtrack->ref_count, destroy_urbtracker);
337 spin_unlock_irqrestore(&mos_parport->listlock, flags);
338 mutex_unlock(&mos_parport->serial->disc_mutex);
341 /* callback for parallel port control urbs submitted asynchronously */
342 static void async_complete(struct urb *urb)
344 struct urbtracker *urbtrack = urb->context;
345 int status = urb->status;
348 if (unlikely(status))
349 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
351 /* remove the urbtracker from the active_urbs list */
352 spin_lock_irqsave(&urbtrack->mos_parport->listlock, flags);
353 list_del(&urbtrack->urblist_entry);
354 spin_unlock_irqrestore(&urbtrack->mos_parport->listlock, flags);
355 kref_put(&urbtrack->ref_count, destroy_urbtracker);
358 static int write_parport_reg_nonblock(struct mos7715_parport *mos_parport,
359 enum mos_regs reg, __u8 data)
361 struct urbtracker *urbtrack;
364 struct usb_serial *serial = mos_parport->serial;
365 struct usb_device *usbdev = serial->dev;
367 /* create and initialize the control urb and containing urbtracker */
368 urbtrack = kmalloc(sizeof(struct urbtracker), GFP_ATOMIC);
372 urbtrack->urb = usb_alloc_urb(0, GFP_ATOMIC);
373 if (!urbtrack->urb) {
377 urbtrack->setup = kmalloc(sizeof(*urbtrack->setup), GFP_ATOMIC);
378 if (!urbtrack->setup) {
379 usb_free_urb(urbtrack->urb);
383 urbtrack->setup->bRequestType = (__u8)0x40;
384 urbtrack->setup->bRequest = (__u8)0x0e;
385 urbtrack->setup->wValue = cpu_to_le16(get_reg_value(reg, dummy));
386 urbtrack->setup->wIndex = cpu_to_le16(get_reg_index(reg));
387 urbtrack->setup->wLength = 0;
388 usb_fill_control_urb(urbtrack->urb, usbdev,
389 usb_sndctrlpipe(usbdev, 0),
390 (unsigned char *)urbtrack->setup,
391 NULL, 0, async_complete, urbtrack);
392 kref_get(&mos_parport->ref_count);
393 urbtrack->mos_parport = mos_parport;
394 kref_init(&urbtrack->ref_count);
395 INIT_LIST_HEAD(&urbtrack->urblist_entry);
398 * get the disconnect mutex, or add tracker to the deferred_urbs list
399 * and schedule a tasklet to try again later
401 if (!mutex_trylock(&serial->disc_mutex)) {
402 spin_lock_irqsave(&mos_parport->listlock, flags);
403 list_add_tail(&urbtrack->urblist_entry,
404 &mos_parport->deferred_urbs);
405 spin_unlock_irqrestore(&mos_parport->listlock, flags);
406 tasklet_schedule(&mos_parport->urb_tasklet);
407 dev_dbg(&usbdev->dev, "tasklet scheduled\n");
411 /* bail if device disconnected */
412 if (serial->disconnected) {
413 kref_put(&urbtrack->ref_count, destroy_urbtracker);
414 mutex_unlock(&serial->disc_mutex);
418 /* add the tracker to the active_urbs list and submit */
419 spin_lock_irqsave(&mos_parport->listlock, flags);
420 list_add_tail(&urbtrack->urblist_entry, &mos_parport->active_urbs);
421 spin_unlock_irqrestore(&mos_parport->listlock, flags);
422 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
423 mutex_unlock(&serial->disc_mutex);
425 dev_err(&usbdev->dev,
426 "%s: submit_urb() failed: %d\n", __func__, ret_val);
427 spin_lock_irqsave(&mos_parport->listlock, flags);
428 list_del(&urbtrack->urblist_entry);
429 spin_unlock_irqrestore(&mos_parport->listlock, flags);
430 kref_put(&urbtrack->ref_count, destroy_urbtracker);
437 * This is the the common top part of all parallel port callback operations that
438 * send synchronous messages to the device. This implements convoluted locking
439 * that avoids two scenarios: (1) a port operation is called after usbserial
440 * has called our release function, at which point struct mos7715_parport has
441 * been destroyed, and (2) the device has been disconnected, but usbserial has
442 * not called the release function yet because someone has a serial port open.
443 * The shared release_lock prevents the first, and the mutex and disconnected
444 * flag maintained by usbserial covers the second. We also use the msg_pending
445 * flag to ensure that all synchronous usb message calls have completed before
446 * our release function can return.
448 static int parport_prologue(struct parport *pp)
450 struct mos7715_parport *mos_parport;
452 spin_lock(&release_lock);
453 mos_parport = pp->private_data;
454 if (unlikely(mos_parport == NULL)) {
455 /* release fn called, port struct destroyed */
456 spin_unlock(&release_lock);
459 mos_parport->msg_pending = true; /* synch usb call pending */
460 reinit_completion(&mos_parport->syncmsg_compl);
461 spin_unlock(&release_lock);
463 mutex_lock(&mos_parport->serial->disc_mutex);
464 if (mos_parport->serial->disconnected) {
465 /* device disconnected */
466 mutex_unlock(&mos_parport->serial->disc_mutex);
467 mos_parport->msg_pending = false;
468 complete(&mos_parport->syncmsg_compl);
476 * This is the common bottom part of all parallel port functions that send
477 * synchronous messages to the device.
479 static inline void parport_epilogue(struct parport *pp)
481 struct mos7715_parport *mos_parport = pp->private_data;
482 mutex_unlock(&mos_parport->serial->disc_mutex);
483 mos_parport->msg_pending = false;
484 complete(&mos_parport->syncmsg_compl);
487 static void parport_mos7715_write_data(struct parport *pp, unsigned char d)
489 struct mos7715_parport *mos_parport = pp->private_data;
491 if (parport_prologue(pp) < 0)
493 mos7715_change_mode(mos_parport, SPP);
494 write_mos_reg(mos_parport->serial, dummy, MOS7720_DPR, (__u8)d);
495 parport_epilogue(pp);
498 static unsigned char parport_mos7715_read_data(struct parport *pp)
500 struct mos7715_parport *mos_parport = pp->private_data;
503 if (parport_prologue(pp) < 0)
505 read_mos_reg(mos_parport->serial, dummy, MOS7720_DPR, &d);
506 parport_epilogue(pp);
510 static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
512 struct mos7715_parport *mos_parport = pp->private_data;
515 if (parport_prologue(pp) < 0)
517 data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
518 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR, data);
519 mos_parport->shadowDCR = data;
520 parport_epilogue(pp);
523 static unsigned char parport_mos7715_read_control(struct parport *pp)
525 struct mos7715_parport *mos_parport;
528 spin_lock(&release_lock);
529 mos_parport = pp->private_data;
530 if (unlikely(mos_parport == NULL)) {
531 spin_unlock(&release_lock);
534 dcr = mos_parport->shadowDCR & 0x0f;
535 spin_unlock(&release_lock);
539 static unsigned char parport_mos7715_frob_control(struct parport *pp,
543 struct mos7715_parport *mos_parport = pp->private_data;
548 if (parport_prologue(pp) < 0)
550 mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
551 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
552 mos_parport->shadowDCR);
553 dcr = mos_parport->shadowDCR & 0x0f;
554 parport_epilogue(pp);
558 static unsigned char parport_mos7715_read_status(struct parport *pp)
560 unsigned char status;
561 struct mos7715_parport *mos_parport;
563 spin_lock(&release_lock);
564 mos_parport = pp->private_data;
565 if (unlikely(mos_parport == NULL)) { /* release called */
566 spin_unlock(&release_lock);
569 status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
570 spin_unlock(&release_lock);
574 static void parport_mos7715_enable_irq(struct parport *pp)
578 static void parport_mos7715_disable_irq(struct parport *pp)
582 static void parport_mos7715_data_forward(struct parport *pp)
584 struct mos7715_parport *mos_parport = pp->private_data;
586 if (parport_prologue(pp) < 0)
588 mos7715_change_mode(mos_parport, PS2);
589 mos_parport->shadowDCR &= ~0x20;
590 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
591 mos_parport->shadowDCR);
592 parport_epilogue(pp);
595 static void parport_mos7715_data_reverse(struct parport *pp)
597 struct mos7715_parport *mos_parport = pp->private_data;
599 if (parport_prologue(pp) < 0)
601 mos7715_change_mode(mos_parport, PS2);
602 mos_parport->shadowDCR |= 0x20;
603 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
604 mos_parport->shadowDCR);
605 parport_epilogue(pp);
608 static void parport_mos7715_init_state(struct pardevice *dev,
609 struct parport_state *s)
611 s->u.pc.ctr = DCR_INIT_VAL;
612 s->u.pc.ecr = ECR_INIT_VAL;
615 /* N.B. Parport core code requires that this function not block */
616 static void parport_mos7715_save_state(struct parport *pp,
617 struct parport_state *s)
619 struct mos7715_parport *mos_parport;
621 spin_lock(&release_lock);
622 mos_parport = pp->private_data;
623 if (unlikely(mos_parport == NULL)) { /* release called */
624 spin_unlock(&release_lock);
627 s->u.pc.ctr = mos_parport->shadowDCR;
628 s->u.pc.ecr = mos_parport->shadowECR;
629 spin_unlock(&release_lock);
632 /* N.B. Parport core code requires that this function not block */
633 static void parport_mos7715_restore_state(struct parport *pp,
634 struct parport_state *s)
636 struct mos7715_parport *mos_parport;
638 spin_lock(&release_lock);
639 mos_parport = pp->private_data;
640 if (unlikely(mos_parport == NULL)) { /* release called */
641 spin_unlock(&release_lock);
644 mos_parport->shadowDCR = s->u.pc.ctr;
645 mos_parport->shadowECR = s->u.pc.ecr;
646 write_parport_reg_nonblock(mos_parport, MOS7720_DCR,
647 mos_parport->shadowDCR);
648 write_parport_reg_nonblock(mos_parport, MOS7720_ECR,
649 mos_parport->shadowECR);
650 spin_unlock(&release_lock);
653 static size_t parport_mos7715_write_compat(struct parport *pp,
655 size_t len, int flags)
658 struct mos7715_parport *mos_parport = pp->private_data;
661 if (parport_prologue(pp) < 0)
663 mos7715_change_mode(mos_parport, PPF);
664 retval = usb_bulk_msg(mos_parport->serial->dev,
665 usb_sndbulkpipe(mos_parport->serial->dev, 2),
666 (void *)buffer, len, &actual_len,
668 parport_epilogue(pp);
670 dev_err(&mos_parport->serial->dev->dev,
671 "mos7720: usb_bulk_msg() failed: %d\n", retval);
677 static struct parport_operations parport_mos7715_ops = {
678 .owner = THIS_MODULE,
679 .write_data = parport_mos7715_write_data,
680 .read_data = parport_mos7715_read_data,
682 .write_control = parport_mos7715_write_control,
683 .read_control = parport_mos7715_read_control,
684 .frob_control = parport_mos7715_frob_control,
686 .read_status = parport_mos7715_read_status,
688 .enable_irq = parport_mos7715_enable_irq,
689 .disable_irq = parport_mos7715_disable_irq,
691 .data_forward = parport_mos7715_data_forward,
692 .data_reverse = parport_mos7715_data_reverse,
694 .init_state = parport_mos7715_init_state,
695 .save_state = parport_mos7715_save_state,
696 .restore_state = parport_mos7715_restore_state,
698 .compat_write_data = parport_mos7715_write_compat,
700 .nibble_read_data = parport_ieee1284_read_nibble,
701 .byte_read_data = parport_ieee1284_read_byte,
705 * Allocate and initialize parallel port control struct, initialize
706 * the parallel port hardware device, and register with the parport subsystem.
708 static int mos7715_parport_init(struct usb_serial *serial)
710 struct mos7715_parport *mos_parport;
712 /* allocate and initialize parallel port control struct */
713 mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
717 mos_parport->msg_pending = false;
718 kref_init(&mos_parport->ref_count);
719 spin_lock_init(&mos_parport->listlock);
720 INIT_LIST_HEAD(&mos_parport->active_urbs);
721 INIT_LIST_HEAD(&mos_parport->deferred_urbs);
722 usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
723 mos_parport->serial = serial;
724 tasklet_setup(&mos_parport->urb_tasklet, send_deferred_urbs);
725 init_completion(&mos_parport->syncmsg_compl);
727 /* cycle parallel port reset bit */
728 write_mos_reg(mos_parport->serial, dummy, MOS7720_PP_REG, (__u8)0x80);
729 write_mos_reg(mos_parport->serial, dummy, MOS7720_PP_REG, (__u8)0x00);
731 /* initialize device registers */
732 mos_parport->shadowDCR = DCR_INIT_VAL;
733 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
734 mos_parport->shadowDCR);
735 mos_parport->shadowECR = ECR_INIT_VAL;
736 write_mos_reg(mos_parport->serial, dummy, MOS7720_ECR,
737 mos_parport->shadowECR);
739 /* register with parport core */
740 mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE,
742 &parport_mos7715_ops);
743 if (mos_parport->pp == NULL) {
744 dev_err(&serial->interface->dev,
745 "Could not register parport\n");
746 kref_put(&mos_parport->ref_count, destroy_mos_parport);
749 mos_parport->pp->private_data = mos_parport;
750 mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP;
751 mos_parport->pp->dev = &serial->interface->dev;
752 parport_announce_port(mos_parport->pp);
756 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
759 * mos7720_interrupt_callback
760 * this is the callback function for when we have received data on the
761 * interrupt endpoint.
763 static void mos7720_interrupt_callback(struct urb *urb)
767 int status = urb->status;
768 struct device *dev = &urb->dev->dev;
780 /* this urb is terminated, clean up */
781 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
784 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
788 length = urb->actual_length;
789 data = urb->transfer_buffer;
791 /* Moschip get 4 bytes
792 * Byte 1 IIR Port 1 (port.number is 0)
793 * Byte 2 IIR Port 2 (port.number is 1)
794 * Byte 3 --------------
795 * Byte 4 FIFO status for both */
797 /* the above description is inverted
798 * oneukum 2007-03-14 */
800 if (unlikely(length != 4)) {
801 dev_dbg(dev, "Wrong data !!!\n");
808 if ((sp1 | sp2) & 0x01) {
809 /* No Interrupt Pending in both the ports */
810 dev_dbg(dev, "No Interrupt !!!\n");
812 switch (sp1 & 0x0f) {
814 dev_dbg(dev, "Serial Port 1: Receiver status error or address bit detected in 9-bit mode\n");
817 dev_dbg(dev, "Serial Port 1: Receiver time out\n");
820 /* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */
824 switch (sp2 & 0x0f) {
826 dev_dbg(dev, "Serial Port 2: Receiver status error or address bit detected in 9-bit mode\n");
829 dev_dbg(dev, "Serial Port 2: Receiver time out\n");
832 /* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */
838 result = usb_submit_urb(urb, GFP_ATOMIC);
840 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
844 * mos7715_interrupt_callback
845 * this is the 7715's callback function for when we have received data on
846 * the interrupt endpoint.
848 static void mos7715_interrupt_callback(struct urb *urb)
852 int status = urb->status;
853 struct device *dev = &urb->dev->dev;
865 /* this urb is terminated, clean up */
866 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
869 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
873 length = urb->actual_length;
874 data = urb->transfer_buffer;
876 /* Structure of data from 7715 device:
877 * Byte 1: IIR serial Port
879 * Byte 2: DSR parallel port
880 * Byte 4: FIFO status for both */
882 if (unlikely(length != 4)) {
883 dev_dbg(dev, "Wrong data !!!\n");
888 if (!(iir & 0x01)) { /* serial port interrupt pending */
889 switch (iir & 0x0f) {
891 dev_dbg(dev, "Serial Port: Receiver status error or address bit detected in 9-bit mode\n");
894 dev_dbg(dev, "Serial Port: Receiver time out\n");
897 /* dev_dbg(dev, "Serial Port: Modem status change\n"); */
902 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
903 { /* update local copy of DSR reg */
904 struct usb_serial_port *port = urb->context;
905 struct mos7715_parport *mos_parport = port->serial->private;
906 if (unlikely(mos_parport == NULL))
908 atomic_set(&mos_parport->shadowDSR, data[2]);
913 result = usb_submit_urb(urb, GFP_ATOMIC);
915 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
919 * mos7720_bulk_in_callback
920 * this is the callback function for when we have received data on the
923 static void mos7720_bulk_in_callback(struct urb *urb)
926 unsigned char *data ;
927 struct usb_serial_port *port;
928 int status = urb->status;
931 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
937 dev_dbg(&port->dev, "Entering...%s\n", __func__);
939 data = urb->transfer_buffer;
941 if (urb->actual_length) {
942 tty_insert_flip_string(&port->port, data, urb->actual_length);
943 tty_flip_buffer_push(&port->port);
946 if (port->read_urb->status != -EINPROGRESS) {
947 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
949 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
954 * mos7720_bulk_out_data_callback
955 * this is the callback function for when we have finished sending serial
956 * data on the bulk out endpoint.
958 static void mos7720_bulk_out_data_callback(struct urb *urb)
960 struct moschip_port *mos7720_port;
961 int status = urb->status;
964 dev_dbg(&urb->dev->dev, "nonzero write bulk status received:%d\n", status);
968 mos7720_port = urb->context;
970 dev_dbg(&urb->dev->dev, "NULL mos7720_port pointer\n");
974 if (mos7720_port->open)
975 tty_port_tty_wakeup(&mos7720_port->port->port);
978 static int mos77xx_calc_num_ports(struct usb_serial *serial,
979 struct usb_serial_endpoints *epds)
981 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
983 if (product == MOSCHIP_DEVICE_ID_7715) {
985 * The 7715 uses the first bulk in/out endpoint pair for the
986 * parallel port, and the second for the serial port. We swap
987 * the endpoint descriptors here so that the the first and
988 * only registered port structure uses the serial-port
991 swap(epds->bulk_in[0], epds->bulk_in[1]);
992 swap(epds->bulk_out[0], epds->bulk_out[1]);
1000 static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
1002 struct usb_serial *serial;
1004 struct moschip_port *mos7720_port;
1008 int allocated_urbs = 0;
1011 serial = port->serial;
1013 mos7720_port = usb_get_serial_port_data(port);
1014 if (mos7720_port == NULL)
1017 usb_clear_halt(serial->dev, port->write_urb->pipe);
1018 usb_clear_halt(serial->dev, port->read_urb->pipe);
1020 /* Initialising the write urb pool */
1021 for (j = 0; j < NUM_URBS; ++j) {
1022 urb = usb_alloc_urb(0, GFP_KERNEL);
1023 mos7720_port->write_urb_pool[j] = urb;
1027 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1029 if (!urb->transfer_buffer) {
1030 usb_free_urb(mos7720_port->write_urb_pool[j]);
1031 mos7720_port->write_urb_pool[j] = NULL;
1037 if (!allocated_urbs)
1040 /* Initialize MCS7720 -- Write Init values to corresponding Registers
1043 * 0 : MOS7720_THR/MOS7720_RHR
1052 * 0x08 : SP1/2 Control Reg
1054 port_number = port->port_number;
1055 read_mos_reg(serial, port_number, MOS7720_LSR, &data);
1057 dev_dbg(&port->dev, "SS::%p LSR:%x\n", mos7720_port, data);
1059 write_mos_reg(serial, dummy, MOS7720_SP1_REG, 0x02);
1060 write_mos_reg(serial, dummy, MOS7720_SP2_REG, 0x02);
1062 write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
1063 write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
1065 write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
1066 mos7720_port->shadowLCR = 0x03;
1067 write_mos_reg(serial, port_number, MOS7720_LCR,
1068 mos7720_port->shadowLCR);
1069 mos7720_port->shadowMCR = 0x0b;
1070 write_mos_reg(serial, port_number, MOS7720_MCR,
1071 mos7720_port->shadowMCR);
1073 write_mos_reg(serial, port_number, MOS7720_SP_CONTROL_REG, 0x00);
1074 read_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, &data);
1075 data = data | (port->port_number + 1);
1076 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, data);
1077 mos7720_port->shadowLCR = 0x83;
1078 write_mos_reg(serial, port_number, MOS7720_LCR,
1079 mos7720_port->shadowLCR);
1080 write_mos_reg(serial, port_number, MOS7720_THR, 0x0c);
1081 write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
1082 mos7720_port->shadowLCR = 0x03;
1083 write_mos_reg(serial, port_number, MOS7720_LCR,
1084 mos7720_port->shadowLCR);
1085 write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
1087 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
1089 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
1090 __func__, response);
1092 /* initialize our port settings */
1093 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
1095 /* send a open port command */
1096 mos7720_port->open = 1;
1102 * mos7720_chars_in_buffer
1103 * this function is called by the tty driver when it wants to know how many
1104 * bytes of data we currently have outstanding in the port (data that has
1105 * been written, but hasn't made it out the port yet)
1106 * If successful, we return the number of bytes left to be written in the
1108 * Otherwise we return a negative error number.
1110 static int mos7720_chars_in_buffer(struct tty_struct *tty)
1112 struct usb_serial_port *port = tty->driver_data;
1115 struct moschip_port *mos7720_port;
1117 mos7720_port = usb_get_serial_port_data(port);
1118 if (mos7720_port == NULL)
1121 for (i = 0; i < NUM_URBS; ++i) {
1122 if (mos7720_port->write_urb_pool[i] &&
1123 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
1124 chars += URB_TRANSFER_BUFFER_SIZE;
1126 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
1130 static void mos7720_close(struct usb_serial_port *port)
1132 struct usb_serial *serial;
1133 struct moschip_port *mos7720_port;
1136 serial = port->serial;
1138 mos7720_port = usb_get_serial_port_data(port);
1139 if (mos7720_port == NULL)
1142 for (j = 0; j < NUM_URBS; ++j)
1143 usb_kill_urb(mos7720_port->write_urb_pool[j]);
1145 /* Freeing Write URBs */
1146 for (j = 0; j < NUM_URBS; ++j) {
1147 if (mos7720_port->write_urb_pool[j]) {
1148 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
1149 usb_free_urb(mos7720_port->write_urb_pool[j]);
1153 /* While closing port, shutdown all bulk read, write *
1154 * and interrupt read if they exists, otherwise nop */
1155 usb_kill_urb(port->write_urb);
1156 usb_kill_urb(port->read_urb);
1158 write_mos_reg(serial, port->port_number, MOS7720_MCR, 0x00);
1159 write_mos_reg(serial, port->port_number, MOS7720_IER, 0x00);
1161 mos7720_port->open = 0;
1164 static void mos7720_break(struct tty_struct *tty, int break_state)
1166 struct usb_serial_port *port = tty->driver_data;
1168 struct usb_serial *serial;
1169 struct moschip_port *mos7720_port;
1171 serial = port->serial;
1173 mos7720_port = usb_get_serial_port_data(port);
1174 if (mos7720_port == NULL)
1177 if (break_state == -1)
1178 data = mos7720_port->shadowLCR | UART_LCR_SBC;
1180 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
1182 mos7720_port->shadowLCR = data;
1183 write_mos_reg(serial, port->port_number, MOS7720_LCR,
1184 mos7720_port->shadowLCR);
1188 * mos7720_write_room
1189 * this function is called by the tty driver when it wants to know how many
1190 * bytes of data we can accept for a specific port.
1191 * If successful, we return the amount of room that we have for this port
1192 * Otherwise we return a negative error number.
1194 static int mos7720_write_room(struct tty_struct *tty)
1196 struct usb_serial_port *port = tty->driver_data;
1197 struct moschip_port *mos7720_port;
1201 mos7720_port = usb_get_serial_port_data(port);
1202 if (mos7720_port == NULL)
1205 /* FIXME: Locking */
1206 for (i = 0; i < NUM_URBS; ++i) {
1207 if (mos7720_port->write_urb_pool[i] &&
1208 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
1209 room += URB_TRANSFER_BUFFER_SIZE;
1212 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
1216 static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
1217 const unsigned char *data, int count)
1224 struct moschip_port *mos7720_port;
1225 struct usb_serial *serial;
1227 const unsigned char *current_position = data;
1229 serial = port->serial;
1231 mos7720_port = usb_get_serial_port_data(port);
1232 if (mos7720_port == NULL)
1235 /* try to find a free urb in the list */
1238 for (i = 0; i < NUM_URBS; ++i) {
1239 if (mos7720_port->write_urb_pool[i] &&
1240 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
1241 urb = mos7720_port->write_urb_pool[i];
1242 dev_dbg(&port->dev, "URB:%d\n", i);
1248 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
1252 if (urb->transfer_buffer == NULL) {
1253 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1255 if (!urb->transfer_buffer) {
1256 bytes_sent = -ENOMEM;
1260 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1262 memcpy(urb->transfer_buffer, current_position, transfer_size);
1263 usb_serial_debug_data(&port->dev, __func__, transfer_size,
1264 urb->transfer_buffer);
1266 /* fill urb with data and submit */
1267 usb_fill_bulk_urb(urb, serial->dev,
1268 usb_sndbulkpipe(serial->dev,
1269 port->bulk_out_endpointAddress),
1270 urb->transfer_buffer, transfer_size,
1271 mos7720_bulk_out_data_callback, mos7720_port);
1273 /* send it down the pipe */
1274 status = usb_submit_urb(urb, GFP_ATOMIC);
1276 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1277 "with status = %d\n", __func__, status);
1278 bytes_sent = status;
1281 bytes_sent = transfer_size;
1287 static void mos7720_throttle(struct tty_struct *tty)
1289 struct usb_serial_port *port = tty->driver_data;
1290 struct moschip_port *mos7720_port;
1293 mos7720_port = usb_get_serial_port_data(port);
1295 if (mos7720_port == NULL)
1298 if (!mos7720_port->open) {
1299 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1303 /* if we are implementing XON/XOFF, send the stop character */
1305 unsigned char stop_char = STOP_CHAR(tty);
1306 status = mos7720_write(tty, port, &stop_char, 1);
1311 /* if we are implementing RTS/CTS, toggle that line */
1312 if (C_CRTSCTS(tty)) {
1313 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
1314 write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
1315 mos7720_port->shadowMCR);
1319 static void mos7720_unthrottle(struct tty_struct *tty)
1321 struct usb_serial_port *port = tty->driver_data;
1322 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1325 if (mos7720_port == NULL)
1328 if (!mos7720_port->open) {
1329 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1333 /* if we are implementing XON/XOFF, send the start character */
1335 unsigned char start_char = START_CHAR(tty);
1336 status = mos7720_write(tty, port, &start_char, 1);
1341 /* if we are implementing RTS/CTS, toggle that line */
1342 if (C_CRTSCTS(tty)) {
1343 mos7720_port->shadowMCR |= UART_MCR_RTS;
1344 write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
1345 mos7720_port->shadowMCR);
1349 /* FIXME: this function does not work */
1350 static int set_higher_rates(struct moschip_port *mos7720_port,
1353 struct usb_serial_port *port;
1354 struct usb_serial *serial;
1356 enum mos_regs sp_reg;
1357 if (mos7720_port == NULL)
1360 port = mos7720_port->port;
1361 serial = port->serial;
1363 /***********************************************
1364 * Init Sequence for higher rates
1365 ***********************************************/
1366 dev_dbg(&port->dev, "Sending Setting Commands ..........\n");
1367 port_number = port->port_number;
1369 write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
1370 write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
1371 write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
1372 mos7720_port->shadowMCR = 0x0b;
1373 write_mos_reg(serial, port_number, MOS7720_MCR,
1374 mos7720_port->shadowMCR);
1375 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, 0x00);
1377 /***********************************************
1378 * Set for higher rates *
1379 ***********************************************/
1380 /* writing baud rate verbatum into uart clock field clearly not right */
1381 if (port_number == 0)
1382 sp_reg = MOS7720_SP1_REG;
1384 sp_reg = MOS7720_SP2_REG;
1385 write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
1386 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, 0x03);
1387 mos7720_port->shadowMCR = 0x2b;
1388 write_mos_reg(serial, port_number, MOS7720_MCR,
1389 mos7720_port->shadowMCR);
1391 /***********************************************
1393 ***********************************************/
1394 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1395 write_mos_reg(serial, port_number, MOS7720_LCR,
1396 mos7720_port->shadowLCR);
1397 write_mos_reg(serial, port_number, MOS7720_DLL, 0x01);
1398 write_mos_reg(serial, port_number, MOS7720_DLM, 0x00);
1399 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1400 write_mos_reg(serial, port_number, MOS7720_LCR,
1401 mos7720_port->shadowLCR);
1406 /* baud rate information */
1407 struct divisor_table_entry {
1412 /* Define table of divisors for moschip 7720 hardware *
1413 * These assume a 3.6864MHz crystal, the standard /16, and *
1415 static const struct divisor_table_entry divisor_table[] = {
1417 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
1418 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
1434 /*****************************************************************************
1435 * calc_baud_rate_divisor
1436 * this function calculates the proper baud rate divisor for the specified
1438 *****************************************************************************/
1439 static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, int *divisor)
1447 dev_dbg(&port->dev, "%s - %d\n", __func__, baudrate);
1449 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
1450 if (divisor_table[i].baudrate == baudrate) {
1451 *divisor = divisor_table[i].divisor;
1456 /* After trying for all the standard baud rates *
1457 * Try calculating the divisor for this baud rate */
1458 if (baudrate > 75 && baudrate < 230400) {
1459 /* get the divisor */
1460 custom = (__u16)(230400L / baudrate);
1462 /* Check for round off */
1463 round1 = (__u16)(2304000L / baudrate);
1464 round = (__u16)(round1 - (custom * 10));
1469 dev_dbg(&port->dev, "Baud %d = %d\n", baudrate, custom);
1473 dev_dbg(&port->dev, "Baud calculation Failed...\n");
1478 * send_cmd_write_baud_rate
1479 * this function sends the proper command to change the baud rate of the
1482 static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1485 struct usb_serial_port *port;
1486 struct usb_serial *serial;
1489 unsigned char number;
1491 if (mos7720_port == NULL)
1494 port = mos7720_port->port;
1495 serial = port->serial;
1497 number = port->port_number;
1498 dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudrate);
1500 /* Calculate the Divisor */
1501 status = calc_baud_rate_divisor(port, baudrate, &divisor);
1503 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1507 /* Enable access to divisor latch */
1508 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1509 write_mos_reg(serial, number, MOS7720_LCR, mos7720_port->shadowLCR);
1511 /* Write the divisor */
1512 write_mos_reg(serial, number, MOS7720_DLL, (__u8)(divisor & 0xff));
1513 write_mos_reg(serial, number, MOS7720_DLM,
1514 (__u8)((divisor & 0xff00) >> 8));
1516 /* Disable access to divisor latch */
1517 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1518 write_mos_reg(serial, number, MOS7720_LCR, mos7720_port->shadowLCR);
1524 * change_port_settings
1525 * This routine is called to set the UART on the device to match
1526 * the specified new settings.
1528 static void change_port_settings(struct tty_struct *tty,
1529 struct moschip_port *mos7720_port,
1530 struct ktermios *old_termios)
1532 struct usb_serial_port *port;
1533 struct usb_serial *serial;
1542 if (mos7720_port == NULL)
1545 port = mos7720_port->port;
1546 serial = port->serial;
1547 port_number = port->port_number;
1549 if (!mos7720_port->open) {
1550 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1554 lData = UART_LCR_WLEN8;
1555 lStop = 0x00; /* 1 stop bit */
1556 lParity = 0x00; /* No parity */
1558 cflag = tty->termios.c_cflag;
1560 /* Change the number of bits */
1561 switch (cflag & CSIZE) {
1563 lData = UART_LCR_WLEN5;
1567 lData = UART_LCR_WLEN6;
1571 lData = UART_LCR_WLEN7;
1575 lData = UART_LCR_WLEN8;
1579 /* Change the Parity bit */
1580 if (cflag & PARENB) {
1581 if (cflag & PARODD) {
1582 lParity = UART_LCR_PARITY;
1583 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
1585 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
1586 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
1590 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
1594 lParity = lParity | 0x20;
1596 /* Change the Stop bit */
1597 if (cflag & CSTOPB) {
1598 lStop = UART_LCR_STOP;
1599 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
1602 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
1605 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1606 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1607 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
1609 /* Update the LCR with the correct value */
1610 mos7720_port->shadowLCR &=
1611 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1612 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1615 /* Disable Interrupts */
1616 write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
1617 write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
1618 write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
1620 /* Send the updated LCR value to the mos7720 */
1621 write_mos_reg(serial, port_number, MOS7720_LCR,
1622 mos7720_port->shadowLCR);
1623 mos7720_port->shadowMCR = 0x0b;
1624 write_mos_reg(serial, port_number, MOS7720_MCR,
1625 mos7720_port->shadowMCR);
1627 /* set up the MCR register and send it to the mos7720 */
1628 mos7720_port->shadowMCR = UART_MCR_OUT2;
1630 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1632 if (cflag & CRTSCTS) {
1633 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
1634 /* To set hardware flow control to the specified *
1635 * serial port, in SP1/2_CONTROL_REG */
1637 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG,
1640 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG,
1644 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1646 write_mos_reg(serial, port_number, MOS7720_MCR,
1647 mos7720_port->shadowMCR);
1649 /* Determine divisor based on baud rate */
1650 baud = tty_get_baud_rate(tty);
1652 /* pick a default, any default... */
1653 dev_dbg(&port->dev, "Picked default baud...\n");
1657 if (baud >= 230400) {
1658 set_higher_rates(mos7720_port, baud);
1659 /* Enable Interrupts */
1660 write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
1664 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
1665 status = send_cmd_write_baud_rate(mos7720_port, baud);
1666 /* FIXME: needs to write actual resulting baud back not just
1669 tty_encode_baud_rate(tty, baud, baud);
1670 /* Enable Interrupts */
1671 write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
1673 if (port->read_urb->status != -EINPROGRESS) {
1674 status = usb_submit_urb(port->read_urb, GFP_KERNEL);
1676 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
1681 * mos7720_set_termios
1682 * this function is called by the tty driver when it wants to change the
1683 * termios structure.
1685 static void mos7720_set_termios(struct tty_struct *tty,
1686 struct usb_serial_port *port, struct ktermios *old_termios)
1689 struct moschip_port *mos7720_port;
1691 mos7720_port = usb_get_serial_port_data(port);
1693 if (mos7720_port == NULL)
1696 if (!mos7720_port->open) {
1697 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1701 /* change the port settings to the new ones specified */
1702 change_port_settings(tty, mos7720_port, old_termios);
1704 if (port->read_urb->status != -EINPROGRESS) {
1705 status = usb_submit_urb(port->read_urb, GFP_KERNEL);
1707 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
1712 * get_lsr_info - get line status register info
1714 * Purpose: Let user call ioctl() to get info when the UART physically
1715 * is emptied. On bus types like RS485, the transmitter must
1716 * release the bus after transmitting. This must be done when
1717 * the transmit shift register is empty, not be done when the
1718 * transmit holding register is empty. This functionality
1719 * allows an RS485 driver to be written in user space.
1721 static int get_lsr_info(struct tty_struct *tty,
1722 struct moschip_port *mos7720_port, unsigned int __user *value)
1724 struct usb_serial_port *port = tty->driver_data;
1725 unsigned int result = 0;
1726 unsigned char data = 0;
1727 int port_number = port->port_number;
1730 count = mos7720_chars_in_buffer(tty);
1732 read_mos_reg(port->serial, port_number, MOS7720_LSR, &data);
1733 if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1734 == (UART_LSR_TEMT | UART_LSR_THRE)) {
1735 dev_dbg(&port->dev, "%s -- Empty\n", __func__);
1736 result = TIOCSER_TEMT;
1739 if (copy_to_user(value, &result, sizeof(int)))
1744 static int mos7720_tiocmget(struct tty_struct *tty)
1746 struct usb_serial_port *port = tty->driver_data;
1747 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1748 unsigned int result = 0;
1752 mcr = mos7720_port->shadowMCR;
1753 msr = mos7720_port->shadowMSR;
1755 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1756 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1757 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1758 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
1759 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1760 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
1765 static int mos7720_tiocmset(struct tty_struct *tty,
1766 unsigned int set, unsigned int clear)
1768 struct usb_serial_port *port = tty->driver_data;
1769 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1772 mcr = mos7720_port->shadowMCR;
1774 if (set & TIOCM_RTS)
1775 mcr |= UART_MCR_RTS;
1776 if (set & TIOCM_DTR)
1777 mcr |= UART_MCR_DTR;
1778 if (set & TIOCM_LOOP)
1779 mcr |= UART_MCR_LOOP;
1781 if (clear & TIOCM_RTS)
1782 mcr &= ~UART_MCR_RTS;
1783 if (clear & TIOCM_DTR)
1784 mcr &= ~UART_MCR_DTR;
1785 if (clear & TIOCM_LOOP)
1786 mcr &= ~UART_MCR_LOOP;
1788 mos7720_port->shadowMCR = mcr;
1789 write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
1790 mos7720_port->shadowMCR);
1795 static int get_serial_info(struct tty_struct *tty,
1796 struct serial_struct *ss)
1798 struct usb_serial_port *port = tty->driver_data;
1799 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1801 ss->type = PORT_16550A;
1802 ss->line = mos7720_port->port->minor;
1803 ss->port = mos7720_port->port->port_number;
1805 ss->xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1806 ss->baud_base = 9600;
1807 ss->close_delay = 5*HZ;
1808 ss->closing_wait = 30*HZ;
1812 static int mos7720_ioctl(struct tty_struct *tty,
1813 unsigned int cmd, unsigned long arg)
1815 struct usb_serial_port *port = tty->driver_data;
1816 struct moschip_port *mos7720_port;
1818 mos7720_port = usb_get_serial_port_data(port);
1819 if (mos7720_port == NULL)
1824 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
1825 return get_lsr_info(tty, mos7720_port,
1826 (unsigned int __user *)arg);
1829 return -ENOIOCTLCMD;
1832 static int mos7720_startup(struct usb_serial *serial)
1834 struct usb_device *dev;
1839 product = le16_to_cpu(serial->dev->descriptor.idProduct);
1842 if (product == MOSCHIP_DEVICE_ID_7715) {
1843 struct urb *urb = serial->port[0]->interrupt_in_urb;
1845 urb->complete = mos7715_interrupt_callback;
1847 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1848 ret_val = mos7715_parport_init(serial);
1853 /* start the interrupt urb */
1854 ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
1856 dev_err(&dev->dev, "failed to submit interrupt urb: %d\n",
1860 /* LSR For Port 1 */
1861 read_mos_reg(serial, 0, MOS7720_LSR, &data);
1862 dev_dbg(&dev->dev, "LSR:%x\n", data);
1867 static void mos7720_release(struct usb_serial *serial)
1869 usb_kill_urb(serial->port[0]->interrupt_in_urb);
1871 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1872 /* close the parallel port */
1874 if (le16_to_cpu(serial->dev->descriptor.idProduct)
1875 == MOSCHIP_DEVICE_ID_7715) {
1876 struct urbtracker *urbtrack;
1877 unsigned long flags;
1878 struct mos7715_parport *mos_parport =
1879 usb_get_serial_data(serial);
1881 /* prevent NULL ptr dereference in port callbacks */
1882 spin_lock(&release_lock);
1883 mos_parport->pp->private_data = NULL;
1884 spin_unlock(&release_lock);
1886 /* wait for synchronous usb calls to return */
1887 if (mos_parport->msg_pending)
1888 wait_for_completion_timeout(&mos_parport->syncmsg_compl,
1889 msecs_to_jiffies(MOS_WDR_TIMEOUT));
1891 parport_remove_port(mos_parport->pp);
1892 usb_set_serial_data(serial, NULL);
1893 mos_parport->serial = NULL;
1895 /* if tasklet currently scheduled, wait for it to complete */
1896 tasklet_kill(&mos_parport->urb_tasklet);
1898 /* unlink any urbs sent by the tasklet */
1899 spin_lock_irqsave(&mos_parport->listlock, flags);
1900 list_for_each_entry(urbtrack,
1901 &mos_parport->active_urbs,
1903 usb_unlink_urb(urbtrack->urb);
1904 spin_unlock_irqrestore(&mos_parport->listlock, flags);
1905 parport_del_port(mos_parport->pp);
1907 kref_put(&mos_parport->ref_count, destroy_mos_parport);
1912 static int mos7720_port_probe(struct usb_serial_port *port)
1914 struct moschip_port *mos7720_port;
1916 mos7720_port = kzalloc(sizeof(*mos7720_port), GFP_KERNEL);
1920 mos7720_port->port = port;
1922 usb_set_serial_port_data(port, mos7720_port);
1927 static int mos7720_port_remove(struct usb_serial_port *port)
1929 struct moschip_port *mos7720_port;
1931 mos7720_port = usb_get_serial_port_data(port);
1932 kfree(mos7720_port);
1937 static struct usb_serial_driver moschip7720_2port_driver = {
1939 .owner = THIS_MODULE,
1940 .name = "moschip7720",
1942 .description = "Moschip 2 port adapter",
1943 .id_table = id_table,
1946 .num_interrupt_in = 1,
1947 .calc_num_ports = mos77xx_calc_num_ports,
1948 .open = mos7720_open,
1949 .close = mos7720_close,
1950 .throttle = mos7720_throttle,
1951 .unthrottle = mos7720_unthrottle,
1952 .attach = mos7720_startup,
1953 .release = mos7720_release,
1954 .port_probe = mos7720_port_probe,
1955 .port_remove = mos7720_port_remove,
1956 .ioctl = mos7720_ioctl,
1957 .tiocmget = mos7720_tiocmget,
1958 .tiocmset = mos7720_tiocmset,
1959 .get_serial = get_serial_info,
1960 .set_termios = mos7720_set_termios,
1961 .write = mos7720_write,
1962 .write_room = mos7720_write_room,
1963 .chars_in_buffer = mos7720_chars_in_buffer,
1964 .break_ctl = mos7720_break,
1965 .read_bulk_callback = mos7720_bulk_in_callback,
1966 .read_int_callback = mos7720_interrupt_callback,
1969 static struct usb_serial_driver * const serial_drivers[] = {
1970 &moschip7720_2port_driver, NULL
1973 module_usb_serial_driver(serial_drivers, id_table);
1975 MODULE_AUTHOR(DRIVER_AUTHOR);
1976 MODULE_DESCRIPTION(DRIVER_DESC);
1977 MODULE_LICENSE("GPL v2");