3 * Controls the Moschip 7720 usb to dual port serial converter
5 * Copyright 2006 Moschip Semiconductor Tech. Ltd.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, version 2 of the License.
12 * Vijaya Kumar <vijaykumar.gn@gmail.com>
13 * Ajay Kumar <naanuajay@yahoo.com>
14 * Gurudeva <ngurudeva@yahoo.com>
16 * Cleaned up from the original by:
17 * Greg Kroah-Hartman <gregkh@suse.de>
19 * Originally based on drivers/usb/serial/io_edgeport.c which is:
20 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
21 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
23 #include <linux/kernel.h>
24 #include <linux/errno.h>
25 #include <linux/slab.h>
26 #include <linux/tty.h>
27 #include <linux/tty_driver.h>
28 #include <linux/tty_flip.h>
29 #include <linux/module.h>
30 #include <linux/spinlock.h>
31 #include <linux/serial.h>
32 #include <linux/serial_reg.h>
33 #include <linux/usb.h>
34 #include <linux/usb/serial.h>
35 #include <linux/uaccess.h>
36 #include <linux/parport.h>
38 #define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
39 #define DRIVER_DESC "Moschip USB Serial Driver"
41 /* default urb timeout */
42 #define MOS_WDR_TIMEOUT 5000
44 #define MOS_MAX_PORT 0x02
45 #define MOS_WRITE 0x0E
48 /* Interrupt Routines Defines */
49 #define SERIAL_IIR_RLS 0x06
50 #define SERIAL_IIR_RDA 0x04
51 #define SERIAL_IIR_CTI 0x0c
52 #define SERIAL_IIR_THR 0x02
53 #define SERIAL_IIR_MS 0x00
55 #define NUM_URBS 16 /* URB Count */
56 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
58 /* This structure holds all of the local serial port information */
60 __u8 shadowLCR; /* last LCR value received */
61 __u8 shadowMCR; /* last MCR value received */
62 __u8 shadowMSR; /* last MSR value received */
64 struct usb_serial_port *port; /* loop back to the owner */
65 struct urb *write_urb_pool[NUM_URBS];
68 #define USB_VENDOR_ID_MOSCHIP 0x9710
69 #define MOSCHIP_DEVICE_ID_7720 0x7720
70 #define MOSCHIP_DEVICE_ID_7715 0x7715
72 static const struct usb_device_id id_table[] = {
73 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
74 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) },
75 { } /* terminating entry */
77 MODULE_DEVICE_TABLE(usb, id_table);
79 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
81 /* initial values for parport regs */
82 #define DCR_INIT_VAL 0x0c /* SLCTIN, nINIT */
83 #define ECR_INIT_VAL 0x00 /* SPP mode */
86 struct mos7715_parport *mos_parport;
87 struct list_head urblist_entry;
88 struct kref ref_count;
90 struct usb_ctrlrequest *setup;
93 enum mos7715_pp_modes {
95 PS2 = 1<<5, /* moschip calls this 'NIBBLE' mode */
96 PPF = 2<<5, /* moschip calls this 'CB-FIFO mode */
99 struct mos7715_parport {
100 struct parport *pp; /* back to containing struct */
101 struct kref ref_count; /* to instance of this struct */
102 struct list_head deferred_urbs; /* list deferred async urbs */
103 struct list_head active_urbs; /* list async urbs in flight */
104 spinlock_t listlock; /* protects list access */
105 bool msg_pending; /* usb sync call pending */
106 struct completion syncmsg_compl; /* usb sync call completed */
107 struct tasklet_struct urb_tasklet; /* for sending deferred urbs */
108 struct usb_serial *serial; /* back to containing struct */
109 __u8 shadowECR; /* parallel port regs... */
111 atomic_t shadowDSR; /* updated in int-in callback */
114 /* lock guards against dereferencing NULL ptr in parport ops callbacks */
115 static DEFINE_SPINLOCK(release_lock);
117 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
119 static const unsigned int dummy; /* for clarity in register access fns */
122 MOS7720_THR, /* serial port regs */
134 MOS7720_DPR, /* parallel port regs */
138 MOS7720_SP1_REG, /* device control regs */
139 MOS7720_SP2_REG, /* serial port 2 (7720 only) */
141 MOS7720_SP_CONTROL_REG,
145 * Return the correct value for the Windex field of the setup packet
146 * for a control endpoint message. See the 7715 datasheet.
148 static inline __u16 get_reg_index(enum mos_regs reg)
150 static const __u16 mos7715_index_lookup_table[] = {
151 0x00, /* MOS7720_THR */
152 0x00, /* MOS7720_RHR */
153 0x01, /* MOS7720_IER */
154 0x02, /* MOS7720_FCR */
155 0x02, /* MOS7720_ISR */
156 0x03, /* MOS7720_LCR */
157 0x04, /* MOS7720_MCR */
158 0x05, /* MOS7720_LSR */
159 0x06, /* MOS7720_MSR */
160 0x07, /* MOS7720_SPR */
161 0x00, /* MOS7720_DLL */
162 0x01, /* MOS7720_DLM */
163 0x00, /* MOS7720_DPR */
164 0x01, /* MOS7720_DSR */
165 0x02, /* MOS7720_DCR */
166 0x0a, /* MOS7720_ECR */
167 0x01, /* MOS7720_SP1_REG */
168 0x02, /* MOS7720_SP2_REG (7720 only) */
169 0x04, /* MOS7720_PP_REG (7715 only) */
170 0x08, /* MOS7720_SP_CONTROL_REG */
172 return mos7715_index_lookup_table[reg];
176 * Return the correct value for the upper byte of the Wvalue field of
177 * the setup packet for a control endpoint message.
179 static inline __u16 get_reg_value(enum mos_regs reg,
180 unsigned int serial_portnum)
182 if (reg >= MOS7720_SP1_REG) /* control reg */
185 else if (reg >= MOS7720_DPR) /* parallel port reg (7715 only) */
188 else /* serial port reg */
189 return (serial_portnum + 2) << 8;
193 * Write data byte to the specified device register. The data is embedded in
194 * the value field of the setup packet. serial_portnum is ignored for registers
195 * not specific to a particular serial port.
197 static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
198 enum mos_regs reg, __u8 data)
200 struct usb_device *usbdev = serial->dev;
201 unsigned int pipe = usb_sndctrlpipe(usbdev, 0);
202 __u8 request = (__u8)0x0e;
203 __u8 requesttype = (__u8)0x40;
204 __u16 index = get_reg_index(reg);
205 __u16 value = get_reg_value(reg, serial_portnum) + data;
206 int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
207 index, NULL, 0, MOS_WDR_TIMEOUT);
209 dev_err(&usbdev->dev,
210 "mos7720: usb_control_msg() failed: %d\n", status);
215 * Read data byte from the specified device register. The data returned by the
216 * device is embedded in the value field of the setup packet. serial_portnum is
217 * ignored for registers that are not specific to a particular serial port.
219 static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
220 enum mos_regs reg, __u8 *data)
222 struct usb_device *usbdev = serial->dev;
223 unsigned int pipe = usb_rcvctrlpipe(usbdev, 0);
224 __u8 request = (__u8)0x0d;
225 __u8 requesttype = (__u8)0xc0;
226 __u16 index = get_reg_index(reg);
227 __u16 value = get_reg_value(reg, serial_portnum);
231 buf = kmalloc(1, GFP_KERNEL);
235 status = usb_control_msg(usbdev, pipe, request, requesttype, value,
236 index, buf, 1, MOS_WDR_TIMEOUT);
240 dev_err(&usbdev->dev,
241 "mos7720: usb_control_msg() failed: %d\n", status);
247 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
249 static inline int mos7715_change_mode(struct mos7715_parport *mos_parport,
250 enum mos7715_pp_modes mode)
252 mos_parport->shadowECR = mode;
253 write_mos_reg(mos_parport->serial, dummy, MOS7720_ECR,
254 mos_parport->shadowECR);
258 static void destroy_mos_parport(struct kref *kref)
260 struct mos7715_parport *mos_parport =
261 container_of(kref, struct mos7715_parport, ref_count);
266 static void destroy_urbtracker(struct kref *kref)
268 struct urbtracker *urbtrack =
269 container_of(kref, struct urbtracker, ref_count);
270 struct mos7715_parport *mos_parport = urbtrack->mos_parport;
272 usb_free_urb(urbtrack->urb);
273 kfree(urbtrack->setup);
275 kref_put(&mos_parport->ref_count, destroy_mos_parport);
279 * This runs as a tasklet when sending an urb in a non-blocking parallel
280 * port callback had to be deferred because the disconnect mutex could not be
281 * obtained at the time.
283 static void send_deferred_urbs(unsigned long _mos_parport)
287 struct mos7715_parport *mos_parport = (void *)_mos_parport;
288 struct urbtracker *urbtrack, *tmp;
289 struct list_head *cursor, *next;
292 /* if release function ran, game over */
293 if (unlikely(mos_parport->serial == NULL))
296 dev = &mos_parport->serial->dev->dev;
298 /* try again to get the mutex */
299 if (!mutex_trylock(&mos_parport->serial->disc_mutex)) {
300 dev_dbg(dev, "%s: rescheduling tasklet\n", __func__);
301 tasklet_schedule(&mos_parport->urb_tasklet);
305 /* if device disconnected, game over */
306 if (unlikely(mos_parport->serial->disconnected)) {
307 mutex_unlock(&mos_parport->serial->disc_mutex);
311 spin_lock_irqsave(&mos_parport->listlock, flags);
312 if (list_empty(&mos_parport->deferred_urbs)) {
313 spin_unlock_irqrestore(&mos_parport->listlock, flags);
314 mutex_unlock(&mos_parport->serial->disc_mutex);
315 dev_dbg(dev, "%s: deferred_urbs list empty\n", __func__);
319 /* move contents of deferred_urbs list to active_urbs list and submit */
320 list_for_each_safe(cursor, next, &mos_parport->deferred_urbs)
321 list_move_tail(cursor, &mos_parport->active_urbs);
322 list_for_each_entry_safe(urbtrack, tmp, &mos_parport->active_urbs,
324 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
325 dev_dbg(dev, "%s: urb submitted\n", __func__);
327 dev_err(dev, "usb_submit_urb() failed: %d\n", ret_val);
328 list_del(&urbtrack->urblist_entry);
329 kref_put(&urbtrack->ref_count, destroy_urbtracker);
332 spin_unlock_irqrestore(&mos_parport->listlock, flags);
333 mutex_unlock(&mos_parport->serial->disc_mutex);
336 /* callback for parallel port control urbs submitted asynchronously */
337 static void async_complete(struct urb *urb)
339 struct urbtracker *urbtrack = urb->context;
340 int status = urb->status;
342 if (unlikely(status))
343 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
345 /* remove the urbtracker from the active_urbs list */
346 spin_lock(&urbtrack->mos_parport->listlock);
347 list_del(&urbtrack->urblist_entry);
348 spin_unlock(&urbtrack->mos_parport->listlock);
349 kref_put(&urbtrack->ref_count, destroy_urbtracker);
352 static int write_parport_reg_nonblock(struct mos7715_parport *mos_parport,
353 enum mos_regs reg, __u8 data)
355 struct urbtracker *urbtrack;
358 struct usb_serial *serial = mos_parport->serial;
359 struct usb_device *usbdev = serial->dev;
361 /* create and initialize the control urb and containing urbtracker */
362 urbtrack = kmalloc(sizeof(struct urbtracker), GFP_ATOMIC);
366 kref_get(&mos_parport->ref_count);
367 urbtrack->mos_parport = mos_parport;
368 urbtrack->urb = usb_alloc_urb(0, GFP_ATOMIC);
369 if (!urbtrack->urb) {
373 urbtrack->setup = kmalloc(sizeof(*urbtrack->setup), GFP_ATOMIC);
374 if (!urbtrack->setup) {
375 usb_free_urb(urbtrack->urb);
379 urbtrack->setup->bRequestType = (__u8)0x40;
380 urbtrack->setup->bRequest = (__u8)0x0e;
381 urbtrack->setup->wValue = cpu_to_le16(get_reg_value(reg, dummy));
382 urbtrack->setup->wIndex = cpu_to_le16(get_reg_index(reg));
383 urbtrack->setup->wLength = 0;
384 usb_fill_control_urb(urbtrack->urb, usbdev,
385 usb_sndctrlpipe(usbdev, 0),
386 (unsigned char *)urbtrack->setup,
387 NULL, 0, async_complete, urbtrack);
388 kref_init(&urbtrack->ref_count);
389 INIT_LIST_HEAD(&urbtrack->urblist_entry);
392 * get the disconnect mutex, or add tracker to the deferred_urbs list
393 * and schedule a tasklet to try again later
395 if (!mutex_trylock(&serial->disc_mutex)) {
396 spin_lock_irqsave(&mos_parport->listlock, flags);
397 list_add_tail(&urbtrack->urblist_entry,
398 &mos_parport->deferred_urbs);
399 spin_unlock_irqrestore(&mos_parport->listlock, flags);
400 tasklet_schedule(&mos_parport->urb_tasklet);
401 dev_dbg(&usbdev->dev, "tasklet scheduled\n");
405 /* bail if device disconnected */
406 if (serial->disconnected) {
407 kref_put(&urbtrack->ref_count, destroy_urbtracker);
408 mutex_unlock(&serial->disc_mutex);
412 /* add the tracker to the active_urbs list and submit */
413 spin_lock_irqsave(&mos_parport->listlock, flags);
414 list_add_tail(&urbtrack->urblist_entry, &mos_parport->active_urbs);
415 spin_unlock_irqrestore(&mos_parport->listlock, flags);
416 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
417 mutex_unlock(&serial->disc_mutex);
419 dev_err(&usbdev->dev,
420 "%s: submit_urb() failed: %d\n", __func__, ret_val);
421 spin_lock_irqsave(&mos_parport->listlock, flags);
422 list_del(&urbtrack->urblist_entry);
423 spin_unlock_irqrestore(&mos_parport->listlock, flags);
424 kref_put(&urbtrack->ref_count, destroy_urbtracker);
431 * This is the the common top part of all parallel port callback operations that
432 * send synchronous messages to the device. This implements convoluted locking
433 * that avoids two scenarios: (1) a port operation is called after usbserial
434 * has called our release function, at which point struct mos7715_parport has
435 * been destroyed, and (2) the device has been disconnected, but usbserial has
436 * not called the release function yet because someone has a serial port open.
437 * The shared release_lock prevents the first, and the mutex and disconnected
438 * flag maintained by usbserial covers the second. We also use the msg_pending
439 * flag to ensure that all synchronous usb message calls have completed before
440 * our release function can return.
442 static int parport_prologue(struct parport *pp)
444 struct mos7715_parport *mos_parport;
446 spin_lock(&release_lock);
447 mos_parport = pp->private_data;
448 if (unlikely(mos_parport == NULL)) {
449 /* release fn called, port struct destroyed */
450 spin_unlock(&release_lock);
453 mos_parport->msg_pending = true; /* synch usb call pending */
454 reinit_completion(&mos_parport->syncmsg_compl);
455 spin_unlock(&release_lock);
457 mutex_lock(&mos_parport->serial->disc_mutex);
458 if (mos_parport->serial->disconnected) {
459 /* device disconnected */
460 mutex_unlock(&mos_parport->serial->disc_mutex);
461 mos_parport->msg_pending = false;
462 complete(&mos_parport->syncmsg_compl);
470 * This is the common bottom part of all parallel port functions that send
471 * synchronous messages to the device.
473 static inline void parport_epilogue(struct parport *pp)
475 struct mos7715_parport *mos_parport = pp->private_data;
476 mutex_unlock(&mos_parport->serial->disc_mutex);
477 mos_parport->msg_pending = false;
478 complete(&mos_parport->syncmsg_compl);
481 static void parport_mos7715_write_data(struct parport *pp, unsigned char d)
483 struct mos7715_parport *mos_parport = pp->private_data;
485 if (parport_prologue(pp) < 0)
487 mos7715_change_mode(mos_parport, SPP);
488 write_mos_reg(mos_parport->serial, dummy, MOS7720_DPR, (__u8)d);
489 parport_epilogue(pp);
492 static unsigned char parport_mos7715_read_data(struct parport *pp)
494 struct mos7715_parport *mos_parport = pp->private_data;
497 if (parport_prologue(pp) < 0)
499 read_mos_reg(mos_parport->serial, dummy, MOS7720_DPR, &d);
500 parport_epilogue(pp);
504 static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
506 struct mos7715_parport *mos_parport = pp->private_data;
509 if (parport_prologue(pp) < 0)
511 data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
512 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR, data);
513 mos_parport->shadowDCR = data;
514 parport_epilogue(pp);
517 static unsigned char parport_mos7715_read_control(struct parport *pp)
519 struct mos7715_parport *mos_parport = pp->private_data;
522 spin_lock(&release_lock);
523 mos_parport = pp->private_data;
524 if (unlikely(mos_parport == NULL)) {
525 spin_unlock(&release_lock);
528 dcr = mos_parport->shadowDCR & 0x0f;
529 spin_unlock(&release_lock);
533 static unsigned char parport_mos7715_frob_control(struct parport *pp,
537 struct mos7715_parport *mos_parport = pp->private_data;
542 if (parport_prologue(pp) < 0)
544 mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
545 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
546 mos_parport->shadowDCR);
547 dcr = mos_parport->shadowDCR & 0x0f;
548 parport_epilogue(pp);
552 static unsigned char parport_mos7715_read_status(struct parport *pp)
554 unsigned char status;
555 struct mos7715_parport *mos_parport = pp->private_data;
557 spin_lock(&release_lock);
558 mos_parport = pp->private_data;
559 if (unlikely(mos_parport == NULL)) { /* release called */
560 spin_unlock(&release_lock);
563 status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
564 spin_unlock(&release_lock);
568 static void parport_mos7715_enable_irq(struct parport *pp)
572 static void parport_mos7715_disable_irq(struct parport *pp)
576 static void parport_mos7715_data_forward(struct parport *pp)
578 struct mos7715_parport *mos_parport = pp->private_data;
580 if (parport_prologue(pp) < 0)
582 mos7715_change_mode(mos_parport, PS2);
583 mos_parport->shadowDCR &= ~0x20;
584 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
585 mos_parport->shadowDCR);
586 parport_epilogue(pp);
589 static void parport_mos7715_data_reverse(struct parport *pp)
591 struct mos7715_parport *mos_parport = pp->private_data;
593 if (parport_prologue(pp) < 0)
595 mos7715_change_mode(mos_parport, PS2);
596 mos_parport->shadowDCR |= 0x20;
597 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
598 mos_parport->shadowDCR);
599 parport_epilogue(pp);
602 static void parport_mos7715_init_state(struct pardevice *dev,
603 struct parport_state *s)
605 s->u.pc.ctr = DCR_INIT_VAL;
606 s->u.pc.ecr = ECR_INIT_VAL;
609 /* N.B. Parport core code requires that this function not block */
610 static void parport_mos7715_save_state(struct parport *pp,
611 struct parport_state *s)
613 struct mos7715_parport *mos_parport;
615 spin_lock(&release_lock);
616 mos_parport = pp->private_data;
617 if (unlikely(mos_parport == NULL)) { /* release called */
618 spin_unlock(&release_lock);
621 s->u.pc.ctr = mos_parport->shadowDCR;
622 s->u.pc.ecr = mos_parport->shadowECR;
623 spin_unlock(&release_lock);
626 /* N.B. Parport core code requires that this function not block */
627 static void parport_mos7715_restore_state(struct parport *pp,
628 struct parport_state *s)
630 struct mos7715_parport *mos_parport;
632 spin_lock(&release_lock);
633 mos_parport = pp->private_data;
634 if (unlikely(mos_parport == NULL)) { /* release called */
635 spin_unlock(&release_lock);
638 write_parport_reg_nonblock(mos_parport, MOS7720_DCR,
639 mos_parport->shadowDCR);
640 write_parport_reg_nonblock(mos_parport, MOS7720_ECR,
641 mos_parport->shadowECR);
642 spin_unlock(&release_lock);
645 static size_t parport_mos7715_write_compat(struct parport *pp,
647 size_t len, int flags)
650 struct mos7715_parport *mos_parport = pp->private_data;
653 if (parport_prologue(pp) < 0)
655 mos7715_change_mode(mos_parport, PPF);
656 retval = usb_bulk_msg(mos_parport->serial->dev,
657 usb_sndbulkpipe(mos_parport->serial->dev, 2),
658 (void *)buffer, len, &actual_len,
660 parport_epilogue(pp);
662 dev_err(&mos_parport->serial->dev->dev,
663 "mos7720: usb_bulk_msg() failed: %d\n", retval);
669 static struct parport_operations parport_mos7715_ops = {
670 .owner = THIS_MODULE,
671 .write_data = parport_mos7715_write_data,
672 .read_data = parport_mos7715_read_data,
674 .write_control = parport_mos7715_write_control,
675 .read_control = parport_mos7715_read_control,
676 .frob_control = parport_mos7715_frob_control,
678 .read_status = parport_mos7715_read_status,
680 .enable_irq = parport_mos7715_enable_irq,
681 .disable_irq = parport_mos7715_disable_irq,
683 .data_forward = parport_mos7715_data_forward,
684 .data_reverse = parport_mos7715_data_reverse,
686 .init_state = parport_mos7715_init_state,
687 .save_state = parport_mos7715_save_state,
688 .restore_state = parport_mos7715_restore_state,
690 .compat_write_data = parport_mos7715_write_compat,
692 .nibble_read_data = parport_ieee1284_read_nibble,
693 .byte_read_data = parport_ieee1284_read_byte,
697 * Allocate and initialize parallel port control struct, initialize
698 * the parallel port hardware device, and register with the parport subsystem.
700 static int mos7715_parport_init(struct usb_serial *serial)
702 struct mos7715_parport *mos_parport;
704 /* allocate and initialize parallel port control struct */
705 mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
709 mos_parport->msg_pending = false;
710 kref_init(&mos_parport->ref_count);
711 spin_lock_init(&mos_parport->listlock);
712 INIT_LIST_HEAD(&mos_parport->active_urbs);
713 INIT_LIST_HEAD(&mos_parport->deferred_urbs);
714 usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
715 mos_parport->serial = serial;
716 tasklet_init(&mos_parport->urb_tasklet, send_deferred_urbs,
717 (unsigned long) mos_parport);
718 init_completion(&mos_parport->syncmsg_compl);
720 /* cycle parallel port reset bit */
721 write_mos_reg(mos_parport->serial, dummy, MOS7720_PP_REG, (__u8)0x80);
722 write_mos_reg(mos_parport->serial, dummy, MOS7720_PP_REG, (__u8)0x00);
724 /* initialize device registers */
725 mos_parport->shadowDCR = DCR_INIT_VAL;
726 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
727 mos_parport->shadowDCR);
728 mos_parport->shadowECR = ECR_INIT_VAL;
729 write_mos_reg(mos_parport->serial, dummy, MOS7720_ECR,
730 mos_parport->shadowECR);
732 /* register with parport core */
733 mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE,
735 &parport_mos7715_ops);
736 if (mos_parport->pp == NULL) {
737 dev_err(&serial->interface->dev,
738 "Could not register parport\n");
739 kref_put(&mos_parport->ref_count, destroy_mos_parport);
742 mos_parport->pp->private_data = mos_parport;
743 mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP;
744 mos_parport->pp->dev = &serial->interface->dev;
745 parport_announce_port(mos_parport->pp);
749 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
752 * mos7720_interrupt_callback
753 * this is the callback function for when we have received data on the
754 * interrupt endpoint.
756 static void mos7720_interrupt_callback(struct urb *urb)
760 int status = urb->status;
761 struct device *dev = &urb->dev->dev;
773 /* this urb is terminated, clean up */
774 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
777 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
781 length = urb->actual_length;
782 data = urb->transfer_buffer;
784 /* Moschip get 4 bytes
785 * Byte 1 IIR Port 1 (port.number is 0)
786 * Byte 2 IIR Port 2 (port.number is 1)
787 * Byte 3 --------------
788 * Byte 4 FIFO status for both */
790 /* the above description is inverted
791 * oneukum 2007-03-14 */
793 if (unlikely(length != 4)) {
794 dev_dbg(dev, "Wrong data !!!\n");
801 if ((sp1 | sp2) & 0x01) {
802 /* No Interrupt Pending in both the ports */
803 dev_dbg(dev, "No Interrupt !!!\n");
805 switch (sp1 & 0x0f) {
807 dev_dbg(dev, "Serial Port 1: Receiver status error or address bit detected in 9-bit mode\n");
810 dev_dbg(dev, "Serial Port 1: Receiver time out\n");
813 /* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */
817 switch (sp2 & 0x0f) {
819 dev_dbg(dev, "Serial Port 2: Receiver status error or address bit detected in 9-bit mode\n");
822 dev_dbg(dev, "Serial Port 2: Receiver time out\n");
825 /* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */
831 result = usb_submit_urb(urb, GFP_ATOMIC);
833 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
837 * mos7715_interrupt_callback
838 * this is the 7715's callback function for when we have received data on
839 * the interrupt endpoint.
841 static void mos7715_interrupt_callback(struct urb *urb)
845 int status = urb->status;
846 struct device *dev = &urb->dev->dev;
858 /* this urb is terminated, clean up */
859 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
862 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
866 length = urb->actual_length;
867 data = urb->transfer_buffer;
869 /* Structure of data from 7715 device:
870 * Byte 1: IIR serial Port
872 * Byte 2: DSR parallel port
873 * Byte 4: FIFO status for both */
875 if (unlikely(length != 4)) {
876 dev_dbg(dev, "Wrong data !!!\n");
881 if (!(iir & 0x01)) { /* serial port interrupt pending */
882 switch (iir & 0x0f) {
884 dev_dbg(dev, "Serial Port: Receiver status error or address bit detected in 9-bit mode\n");
887 dev_dbg(dev, "Serial Port: Receiver time out\n");
890 /* dev_dbg(dev, "Serial Port: Modem status change\n"); */
895 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
896 { /* update local copy of DSR reg */
897 struct usb_serial_port *port = urb->context;
898 struct mos7715_parport *mos_parport = port->serial->private;
899 if (unlikely(mos_parport == NULL))
901 atomic_set(&mos_parport->shadowDSR, data[2]);
906 result = usb_submit_urb(urb, GFP_ATOMIC);
908 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
912 * mos7720_bulk_in_callback
913 * this is the callback function for when we have received data on the
916 static void mos7720_bulk_in_callback(struct urb *urb)
919 unsigned char *data ;
920 struct usb_serial_port *port;
921 int status = urb->status;
924 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
930 dev_dbg(&port->dev, "Entering...%s\n", __func__);
932 data = urb->transfer_buffer;
934 if (urb->actual_length) {
935 tty_insert_flip_string(&port->port, data, urb->actual_length);
936 tty_flip_buffer_push(&port->port);
939 if (port->read_urb->status != -EINPROGRESS) {
940 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
942 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
947 * mos7720_bulk_out_data_callback
948 * this is the callback function for when we have finished sending serial
949 * data on the bulk out endpoint.
951 static void mos7720_bulk_out_data_callback(struct urb *urb)
953 struct moschip_port *mos7720_port;
954 int status = urb->status;
957 dev_dbg(&urb->dev->dev, "nonzero write bulk status received:%d\n", status);
961 mos7720_port = urb->context;
963 dev_dbg(&urb->dev->dev, "NULL mos7720_port pointer\n");
967 if (mos7720_port->open)
968 tty_port_tty_wakeup(&mos7720_port->port->port);
971 static int mos77xx_calc_num_ports(struct usb_serial *serial)
973 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
974 if (product == MOSCHIP_DEVICE_ID_7715)
980 static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
982 struct usb_serial *serial;
984 struct moschip_port *mos7720_port;
988 int allocated_urbs = 0;
991 serial = port->serial;
993 mos7720_port = usb_get_serial_port_data(port);
994 if (mos7720_port == NULL)
997 usb_clear_halt(serial->dev, port->write_urb->pipe);
998 usb_clear_halt(serial->dev, port->read_urb->pipe);
1000 /* Initialising the write urb pool */
1001 for (j = 0; j < NUM_URBS; ++j) {
1002 urb = usb_alloc_urb(0, GFP_KERNEL);
1003 mos7720_port->write_urb_pool[j] = urb;
1007 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1009 if (!urb->transfer_buffer) {
1010 usb_free_urb(mos7720_port->write_urb_pool[j]);
1011 mos7720_port->write_urb_pool[j] = NULL;
1017 if (!allocated_urbs)
1020 /* Initialize MCS7720 -- Write Init values to corresponding Registers
1023 * 0 : MOS7720_THR/MOS7720_RHR
1032 * 0x08 : SP1/2 Control Reg
1034 port_number = port->port_number;
1035 read_mos_reg(serial, port_number, MOS7720_LSR, &data);
1037 dev_dbg(&port->dev, "SS::%p LSR:%x\n", mos7720_port, data);
1039 write_mos_reg(serial, dummy, MOS7720_SP1_REG, 0x02);
1040 write_mos_reg(serial, dummy, MOS7720_SP2_REG, 0x02);
1042 write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
1043 write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
1045 write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
1046 mos7720_port->shadowLCR = 0x03;
1047 write_mos_reg(serial, port_number, MOS7720_LCR,
1048 mos7720_port->shadowLCR);
1049 mos7720_port->shadowMCR = 0x0b;
1050 write_mos_reg(serial, port_number, MOS7720_MCR,
1051 mos7720_port->shadowMCR);
1053 write_mos_reg(serial, port_number, MOS7720_SP_CONTROL_REG, 0x00);
1054 read_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, &data);
1055 data = data | (port->port_number + 1);
1056 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, data);
1057 mos7720_port->shadowLCR = 0x83;
1058 write_mos_reg(serial, port_number, MOS7720_LCR,
1059 mos7720_port->shadowLCR);
1060 write_mos_reg(serial, port_number, MOS7720_THR, 0x0c);
1061 write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
1062 mos7720_port->shadowLCR = 0x03;
1063 write_mos_reg(serial, port_number, MOS7720_LCR,
1064 mos7720_port->shadowLCR);
1065 write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
1067 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
1069 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
1070 __func__, response);
1072 /* initialize our port settings */
1073 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
1075 /* send a open port command */
1076 mos7720_port->open = 1;
1082 * mos7720_chars_in_buffer
1083 * this function is called by the tty driver when it wants to know how many
1084 * bytes of data we currently have outstanding in the port (data that has
1085 * been written, but hasn't made it out the port yet)
1086 * If successful, we return the number of bytes left to be written in the
1088 * Otherwise we return a negative error number.
1090 static int mos7720_chars_in_buffer(struct tty_struct *tty)
1092 struct usb_serial_port *port = tty->driver_data;
1095 struct moschip_port *mos7720_port;
1097 mos7720_port = usb_get_serial_port_data(port);
1098 if (mos7720_port == NULL)
1101 for (i = 0; i < NUM_URBS; ++i) {
1102 if (mos7720_port->write_urb_pool[i] &&
1103 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
1104 chars += URB_TRANSFER_BUFFER_SIZE;
1106 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
1110 static void mos7720_close(struct usb_serial_port *port)
1112 struct usb_serial *serial;
1113 struct moschip_port *mos7720_port;
1116 serial = port->serial;
1118 mos7720_port = usb_get_serial_port_data(port);
1119 if (mos7720_port == NULL)
1122 for (j = 0; j < NUM_URBS; ++j)
1123 usb_kill_urb(mos7720_port->write_urb_pool[j]);
1125 /* Freeing Write URBs */
1126 for (j = 0; j < NUM_URBS; ++j) {
1127 if (mos7720_port->write_urb_pool[j]) {
1128 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
1129 usb_free_urb(mos7720_port->write_urb_pool[j]);
1133 /* While closing port, shutdown all bulk read, write *
1134 * and interrupt read if they exists, otherwise nop */
1135 usb_kill_urb(port->write_urb);
1136 usb_kill_urb(port->read_urb);
1138 write_mos_reg(serial, port->port_number, MOS7720_MCR, 0x00);
1139 write_mos_reg(serial, port->port_number, MOS7720_IER, 0x00);
1141 mos7720_port->open = 0;
1144 static void mos7720_break(struct tty_struct *tty, int break_state)
1146 struct usb_serial_port *port = tty->driver_data;
1148 struct usb_serial *serial;
1149 struct moschip_port *mos7720_port;
1151 serial = port->serial;
1153 mos7720_port = usb_get_serial_port_data(port);
1154 if (mos7720_port == NULL)
1157 if (break_state == -1)
1158 data = mos7720_port->shadowLCR | UART_LCR_SBC;
1160 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
1162 mos7720_port->shadowLCR = data;
1163 write_mos_reg(serial, port->port_number, MOS7720_LCR,
1164 mos7720_port->shadowLCR);
1168 * mos7720_write_room
1169 * this function is called by the tty driver when it wants to know how many
1170 * bytes of data we can accept for a specific port.
1171 * If successful, we return the amount of room that we have for this port
1172 * Otherwise we return a negative error number.
1174 static int mos7720_write_room(struct tty_struct *tty)
1176 struct usb_serial_port *port = tty->driver_data;
1177 struct moschip_port *mos7720_port;
1181 mos7720_port = usb_get_serial_port_data(port);
1182 if (mos7720_port == NULL)
1185 /* FIXME: Locking */
1186 for (i = 0; i < NUM_URBS; ++i) {
1187 if (mos7720_port->write_urb_pool[i] &&
1188 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
1189 room += URB_TRANSFER_BUFFER_SIZE;
1192 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
1196 static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
1197 const unsigned char *data, int count)
1204 struct moschip_port *mos7720_port;
1205 struct usb_serial *serial;
1207 const unsigned char *current_position = data;
1209 serial = port->serial;
1211 mos7720_port = usb_get_serial_port_data(port);
1212 if (mos7720_port == NULL)
1215 /* try to find a free urb in the list */
1218 for (i = 0; i < NUM_URBS; ++i) {
1219 if (mos7720_port->write_urb_pool[i] &&
1220 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
1221 urb = mos7720_port->write_urb_pool[i];
1222 dev_dbg(&port->dev, "URB:%d\n", i);
1228 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
1232 if (urb->transfer_buffer == NULL) {
1233 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1235 if (!urb->transfer_buffer)
1238 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1240 memcpy(urb->transfer_buffer, current_position, transfer_size);
1241 usb_serial_debug_data(&port->dev, __func__, transfer_size,
1242 urb->transfer_buffer);
1244 /* fill urb with data and submit */
1245 usb_fill_bulk_urb(urb, serial->dev,
1246 usb_sndbulkpipe(serial->dev,
1247 port->bulk_out_endpointAddress),
1248 urb->transfer_buffer, transfer_size,
1249 mos7720_bulk_out_data_callback, mos7720_port);
1251 /* send it down the pipe */
1252 status = usb_submit_urb(urb, GFP_ATOMIC);
1254 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1255 "with status = %d\n", __func__, status);
1256 bytes_sent = status;
1259 bytes_sent = transfer_size;
1265 static void mos7720_throttle(struct tty_struct *tty)
1267 struct usb_serial_port *port = tty->driver_data;
1268 struct moschip_port *mos7720_port;
1271 mos7720_port = usb_get_serial_port_data(port);
1273 if (mos7720_port == NULL)
1276 if (!mos7720_port->open) {
1277 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1281 /* if we are implementing XON/XOFF, send the stop character */
1283 unsigned char stop_char = STOP_CHAR(tty);
1284 status = mos7720_write(tty, port, &stop_char, 1);
1289 /* if we are implementing RTS/CTS, toggle that line */
1290 if (tty->termios.c_cflag & CRTSCTS) {
1291 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
1292 write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
1293 mos7720_port->shadowMCR);
1297 static void mos7720_unthrottle(struct tty_struct *tty)
1299 struct usb_serial_port *port = tty->driver_data;
1300 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1303 if (mos7720_port == NULL)
1306 if (!mos7720_port->open) {
1307 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1311 /* if we are implementing XON/XOFF, send the start character */
1313 unsigned char start_char = START_CHAR(tty);
1314 status = mos7720_write(tty, port, &start_char, 1);
1319 /* if we are implementing RTS/CTS, toggle that line */
1320 if (tty->termios.c_cflag & CRTSCTS) {
1321 mos7720_port->shadowMCR |= UART_MCR_RTS;
1322 write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
1323 mos7720_port->shadowMCR);
1327 /* FIXME: this function does not work */
1328 static int set_higher_rates(struct moschip_port *mos7720_port,
1331 struct usb_serial_port *port;
1332 struct usb_serial *serial;
1334 enum mos_regs sp_reg;
1335 if (mos7720_port == NULL)
1338 port = mos7720_port->port;
1339 serial = port->serial;
1341 /***********************************************
1342 * Init Sequence for higher rates
1343 ***********************************************/
1344 dev_dbg(&port->dev, "Sending Setting Commands ..........\n");
1345 port_number = port->port_number;
1347 write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
1348 write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
1349 write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
1350 mos7720_port->shadowMCR = 0x0b;
1351 write_mos_reg(serial, port_number, MOS7720_MCR,
1352 mos7720_port->shadowMCR);
1353 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, 0x00);
1355 /***********************************************
1356 * Set for higher rates *
1357 ***********************************************/
1358 /* writing baud rate verbatum into uart clock field clearly not right */
1359 if (port_number == 0)
1360 sp_reg = MOS7720_SP1_REG;
1362 sp_reg = MOS7720_SP2_REG;
1363 write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
1364 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, 0x03);
1365 mos7720_port->shadowMCR = 0x2b;
1366 write_mos_reg(serial, port_number, MOS7720_MCR,
1367 mos7720_port->shadowMCR);
1369 /***********************************************
1371 ***********************************************/
1372 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1373 write_mos_reg(serial, port_number, MOS7720_LCR,
1374 mos7720_port->shadowLCR);
1375 write_mos_reg(serial, port_number, MOS7720_DLL, 0x01);
1376 write_mos_reg(serial, port_number, MOS7720_DLM, 0x00);
1377 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1378 write_mos_reg(serial, port_number, MOS7720_LCR,
1379 mos7720_port->shadowLCR);
1384 /* baud rate information */
1385 struct divisor_table_entry {
1390 /* Define table of divisors for moschip 7720 hardware *
1391 * These assume a 3.6864MHz crystal, the standard /16, and *
1393 static struct divisor_table_entry divisor_table[] = {
1395 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
1396 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
1412 /*****************************************************************************
1413 * calc_baud_rate_divisor
1414 * this function calculates the proper baud rate divisor for the specified
1416 *****************************************************************************/
1417 static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, int *divisor)
1425 dev_dbg(&port->dev, "%s - %d\n", __func__, baudrate);
1427 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
1428 if (divisor_table[i].baudrate == baudrate) {
1429 *divisor = divisor_table[i].divisor;
1434 /* After trying for all the standard baud rates *
1435 * Try calculating the divisor for this baud rate */
1436 if (baudrate > 75 && baudrate < 230400) {
1437 /* get the divisor */
1438 custom = (__u16)(230400L / baudrate);
1440 /* Check for round off */
1441 round1 = (__u16)(2304000L / baudrate);
1442 round = (__u16)(round1 - (custom * 10));
1447 dev_dbg(&port->dev, "Baud %d = %d\n", baudrate, custom);
1451 dev_dbg(&port->dev, "Baud calculation Failed...\n");
1456 * send_cmd_write_baud_rate
1457 * this function sends the proper command to change the baud rate of the
1460 static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1463 struct usb_serial_port *port;
1464 struct usb_serial *serial;
1467 unsigned char number;
1469 if (mos7720_port == NULL)
1472 port = mos7720_port->port;
1473 serial = port->serial;
1475 number = port->port_number;
1476 dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudrate);
1478 /* Calculate the Divisor */
1479 status = calc_baud_rate_divisor(port, baudrate, &divisor);
1481 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1485 /* Enable access to divisor latch */
1486 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1487 write_mos_reg(serial, number, MOS7720_LCR, mos7720_port->shadowLCR);
1489 /* Write the divisor */
1490 write_mos_reg(serial, number, MOS7720_DLL, (__u8)(divisor & 0xff));
1491 write_mos_reg(serial, number, MOS7720_DLM,
1492 (__u8)((divisor & 0xff00) >> 8));
1494 /* Disable access to divisor latch */
1495 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1496 write_mos_reg(serial, number, MOS7720_LCR, mos7720_port->shadowLCR);
1502 * change_port_settings
1503 * This routine is called to set the UART on the device to match
1504 * the specified new settings.
1506 static void change_port_settings(struct tty_struct *tty,
1507 struct moschip_port *mos7720_port,
1508 struct ktermios *old_termios)
1510 struct usb_serial_port *port;
1511 struct usb_serial *serial;
1522 if (mos7720_port == NULL)
1525 port = mos7720_port->port;
1526 serial = port->serial;
1527 port_number = port->port_number;
1529 if (!mos7720_port->open) {
1530 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1534 lData = UART_LCR_WLEN8;
1535 lStop = 0x00; /* 1 stop bit */
1536 lParity = 0x00; /* No parity */
1538 cflag = tty->termios.c_cflag;
1539 iflag = tty->termios.c_iflag;
1541 /* Change the number of bits */
1542 switch (cflag & CSIZE) {
1544 lData = UART_LCR_WLEN5;
1549 lData = UART_LCR_WLEN6;
1554 lData = UART_LCR_WLEN7;
1559 lData = UART_LCR_WLEN8;
1563 /* Change the Parity bit */
1564 if (cflag & PARENB) {
1565 if (cflag & PARODD) {
1566 lParity = UART_LCR_PARITY;
1567 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
1569 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
1570 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
1574 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
1578 lParity = lParity | 0x20;
1580 /* Change the Stop bit */
1581 if (cflag & CSTOPB) {
1582 lStop = UART_LCR_STOP;
1583 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
1586 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
1589 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1590 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1591 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
1593 /* Update the LCR with the correct value */
1594 mos7720_port->shadowLCR &=
1595 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1596 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1599 /* Disable Interrupts */
1600 write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
1601 write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
1602 write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
1604 /* Send the updated LCR value to the mos7720 */
1605 write_mos_reg(serial, port_number, MOS7720_LCR,
1606 mos7720_port->shadowLCR);
1607 mos7720_port->shadowMCR = 0x0b;
1608 write_mos_reg(serial, port_number, MOS7720_MCR,
1609 mos7720_port->shadowMCR);
1611 /* set up the MCR register and send it to the mos7720 */
1612 mos7720_port->shadowMCR = UART_MCR_OUT2;
1614 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1616 if (cflag & CRTSCTS) {
1617 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
1618 /* To set hardware flow control to the specified *
1619 * serial port, in SP1/2_CONTROL_REG */
1621 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG,
1624 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG,
1628 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1630 write_mos_reg(serial, port_number, MOS7720_MCR,
1631 mos7720_port->shadowMCR);
1633 /* Determine divisor based on baud rate */
1634 baud = tty_get_baud_rate(tty);
1636 /* pick a default, any default... */
1637 dev_dbg(&port->dev, "Picked default baud...\n");
1641 if (baud >= 230400) {
1642 set_higher_rates(mos7720_port, baud);
1643 /* Enable Interrupts */
1644 write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
1648 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
1649 status = send_cmd_write_baud_rate(mos7720_port, baud);
1650 /* FIXME: needs to write actual resulting baud back not just
1653 tty_encode_baud_rate(tty, baud, baud);
1654 /* Enable Interrupts */
1655 write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
1657 if (port->read_urb->status != -EINPROGRESS) {
1658 status = usb_submit_urb(port->read_urb, GFP_KERNEL);
1660 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
1665 * mos7720_set_termios
1666 * this function is called by the tty driver when it wants to change the
1667 * termios structure.
1669 static void mos7720_set_termios(struct tty_struct *tty,
1670 struct usb_serial_port *port, struct ktermios *old_termios)
1674 struct usb_serial *serial;
1675 struct moschip_port *mos7720_port;
1677 serial = port->serial;
1679 mos7720_port = usb_get_serial_port_data(port);
1681 if (mos7720_port == NULL)
1684 if (!mos7720_port->open) {
1685 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1689 dev_dbg(&port->dev, "setting termios - ASPIRE\n");
1691 cflag = tty->termios.c_cflag;
1693 dev_dbg(&port->dev, "%s - cflag %08x iflag %08x\n", __func__,
1694 tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag));
1696 dev_dbg(&port->dev, "%s - old cflag %08x old iflag %08x\n", __func__,
1697 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
1699 /* change the port settings to the new ones specified */
1700 change_port_settings(tty, mos7720_port, old_termios);
1702 if (port->read_urb->status != -EINPROGRESS) {
1703 status = usb_submit_urb(port->read_urb, GFP_KERNEL);
1705 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
1710 * get_lsr_info - get line status register info
1712 * Purpose: Let user call ioctl() to get info when the UART physically
1713 * is emptied. On bus types like RS485, the transmitter must
1714 * release the bus after transmitting. This must be done when
1715 * the transmit shift register is empty, not be done when the
1716 * transmit holding register is empty. This functionality
1717 * allows an RS485 driver to be written in user space.
1719 static int get_lsr_info(struct tty_struct *tty,
1720 struct moschip_port *mos7720_port, unsigned int __user *value)
1722 struct usb_serial_port *port = tty->driver_data;
1723 unsigned int result = 0;
1724 unsigned char data = 0;
1725 int port_number = port->port_number;
1728 count = mos7720_chars_in_buffer(tty);
1730 read_mos_reg(port->serial, port_number, MOS7720_LSR, &data);
1731 if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1732 == (UART_LSR_TEMT | UART_LSR_THRE)) {
1733 dev_dbg(&port->dev, "%s -- Empty\n", __func__);
1734 result = TIOCSER_TEMT;
1737 if (copy_to_user(value, &result, sizeof(int)))
1742 static int mos7720_tiocmget(struct tty_struct *tty)
1744 struct usb_serial_port *port = tty->driver_data;
1745 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1746 unsigned int result = 0;
1750 mcr = mos7720_port->shadowMCR;
1751 msr = mos7720_port->shadowMSR;
1753 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1754 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1755 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1756 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
1757 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1758 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
1763 static int mos7720_tiocmset(struct tty_struct *tty,
1764 unsigned int set, unsigned int clear)
1766 struct usb_serial_port *port = tty->driver_data;
1767 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1770 mcr = mos7720_port->shadowMCR;
1772 if (set & TIOCM_RTS)
1773 mcr |= UART_MCR_RTS;
1774 if (set & TIOCM_DTR)
1775 mcr |= UART_MCR_DTR;
1776 if (set & TIOCM_LOOP)
1777 mcr |= UART_MCR_LOOP;
1779 if (clear & TIOCM_RTS)
1780 mcr &= ~UART_MCR_RTS;
1781 if (clear & TIOCM_DTR)
1782 mcr &= ~UART_MCR_DTR;
1783 if (clear & TIOCM_LOOP)
1784 mcr &= ~UART_MCR_LOOP;
1786 mos7720_port->shadowMCR = mcr;
1787 write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
1788 mos7720_port->shadowMCR);
1793 static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1794 unsigned int __user *value)
1799 struct usb_serial_port *port;
1801 if (mos7720_port == NULL)
1804 port = (struct usb_serial_port *)mos7720_port->port;
1805 mcr = mos7720_port->shadowMCR;
1807 if (copy_from_user(&arg, value, sizeof(int)))
1812 if (arg & TIOCM_RTS)
1813 mcr |= UART_MCR_RTS;
1814 if (arg & TIOCM_DTR)
1815 mcr |= UART_MCR_RTS;
1816 if (arg & TIOCM_LOOP)
1817 mcr |= UART_MCR_LOOP;
1821 if (arg & TIOCM_RTS)
1822 mcr &= ~UART_MCR_RTS;
1823 if (arg & TIOCM_DTR)
1824 mcr &= ~UART_MCR_RTS;
1825 if (arg & TIOCM_LOOP)
1826 mcr &= ~UART_MCR_LOOP;
1831 mos7720_port->shadowMCR = mcr;
1832 write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
1833 mos7720_port->shadowMCR);
1838 static int get_serial_info(struct moschip_port *mos7720_port,
1839 struct serial_struct __user *retinfo)
1841 struct serial_struct tmp;
1846 memset(&tmp, 0, sizeof(tmp));
1848 tmp.type = PORT_16550A;
1849 tmp.line = mos7720_port->port->minor;
1850 tmp.port = mos7720_port->port->port_number;
1852 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1853 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1854 tmp.baud_base = 9600;
1855 tmp.close_delay = 5*HZ;
1856 tmp.closing_wait = 30*HZ;
1858 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1863 static int mos7720_ioctl(struct tty_struct *tty,
1864 unsigned int cmd, unsigned long arg)
1866 struct usb_serial_port *port = tty->driver_data;
1867 struct moschip_port *mos7720_port;
1869 mos7720_port = usb_get_serial_port_data(port);
1870 if (mos7720_port == NULL)
1875 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
1876 return get_lsr_info(tty, mos7720_port,
1877 (unsigned int __user *)arg);
1879 /* FIXME: These should be using the mode methods */
1882 dev_dbg(&port->dev, "%s TIOCMSET/TIOCMBIC/TIOCMSET\n", __func__);
1883 return set_modem_info(mos7720_port, cmd,
1884 (unsigned int __user *)arg);
1887 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
1888 return get_serial_info(mos7720_port,
1889 (struct serial_struct __user *)arg);
1892 return -ENOIOCTLCMD;
1895 static int mos7720_startup(struct usb_serial *serial)
1897 struct usb_device *dev;
1902 if (serial->num_bulk_in < 2 || serial->num_bulk_out < 2) {
1903 dev_err(&serial->interface->dev, "missing bulk endpoints\n");
1907 product = le16_to_cpu(serial->dev->descriptor.idProduct);
1911 * The 7715 uses the first bulk in/out endpoint pair for the parallel
1912 * port, and the second for the serial port. Because the usbserial core
1913 * assumes both pairs are serial ports, we must engage in a bit of
1914 * subterfuge and swap the pointers for ports 0 and 1 in order to make
1915 * port 0 point to the serial port. However, both moschip devices use a
1916 * single interrupt-in endpoint for both ports (as mentioned a little
1917 * further down), and this endpoint was assigned to port 0. So after
1918 * the swap, we must copy the interrupt endpoint elements from port 1
1919 * (as newly assigned) to port 0, and null out port 1 pointers.
1921 if (product == MOSCHIP_DEVICE_ID_7715) {
1922 struct usb_serial_port *tmp = serial->port[0];
1923 serial->port[0] = serial->port[1];
1924 serial->port[1] = tmp;
1925 serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb;
1926 serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer;
1927 serial->port[0]->interrupt_in_endpointAddress =
1928 tmp->interrupt_in_endpointAddress;
1929 serial->port[1]->interrupt_in_urb = NULL;
1930 serial->port[1]->interrupt_in_buffer = NULL;
1932 if (serial->port[0]->interrupt_in_urb) {
1933 struct urb *urb = serial->port[0]->interrupt_in_urb;
1935 urb->complete = mos7715_interrupt_callback;
1939 /* setting configuration feature to one */
1940 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1941 (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5000);
1943 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1944 if (product == MOSCHIP_DEVICE_ID_7715) {
1945 ret_val = mos7715_parport_init(serial);
1950 /* start the interrupt urb */
1951 ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
1953 dev_err(&dev->dev, "failed to submit interrupt urb: %d\n",
1957 /* LSR For Port 1 */
1958 read_mos_reg(serial, 0, MOS7720_LSR, &data);
1959 dev_dbg(&dev->dev, "LSR:%x\n", data);
1964 static void mos7720_release(struct usb_serial *serial)
1966 usb_kill_urb(serial->port[0]->interrupt_in_urb);
1968 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1969 /* close the parallel port */
1971 if (le16_to_cpu(serial->dev->descriptor.idProduct)
1972 == MOSCHIP_DEVICE_ID_7715) {
1973 struct urbtracker *urbtrack;
1974 unsigned long flags;
1975 struct mos7715_parport *mos_parport =
1976 usb_get_serial_data(serial);
1978 /* prevent NULL ptr dereference in port callbacks */
1979 spin_lock(&release_lock);
1980 mos_parport->pp->private_data = NULL;
1981 spin_unlock(&release_lock);
1983 /* wait for synchronous usb calls to return */
1984 if (mos_parport->msg_pending)
1985 wait_for_completion_timeout(&mos_parport->syncmsg_compl,
1986 msecs_to_jiffies(MOS_WDR_TIMEOUT));
1988 parport_remove_port(mos_parport->pp);
1989 usb_set_serial_data(serial, NULL);
1990 mos_parport->serial = NULL;
1992 /* if tasklet currently scheduled, wait for it to complete */
1993 tasklet_kill(&mos_parport->urb_tasklet);
1995 /* unlink any urbs sent by the tasklet */
1996 spin_lock_irqsave(&mos_parport->listlock, flags);
1997 list_for_each_entry(urbtrack,
1998 &mos_parport->active_urbs,
2000 usb_unlink_urb(urbtrack->urb);
2001 spin_unlock_irqrestore(&mos_parport->listlock, flags);
2002 parport_del_port(mos_parport->pp);
2004 kref_put(&mos_parport->ref_count, destroy_mos_parport);
2009 static int mos7720_port_probe(struct usb_serial_port *port)
2011 struct moschip_port *mos7720_port;
2013 mos7720_port = kzalloc(sizeof(*mos7720_port), GFP_KERNEL);
2017 /* Initialize all port interrupt end point to port 0 int endpoint.
2018 * Our device has only one interrupt endpoint common to all ports.
2020 port->interrupt_in_endpointAddress =
2021 port->serial->port[0]->interrupt_in_endpointAddress;
2022 mos7720_port->port = port;
2024 usb_set_serial_port_data(port, mos7720_port);
2029 static int mos7720_port_remove(struct usb_serial_port *port)
2031 struct moschip_port *mos7720_port;
2033 mos7720_port = usb_get_serial_port_data(port);
2034 kfree(mos7720_port);
2039 static struct usb_serial_driver moschip7720_2port_driver = {
2041 .owner = THIS_MODULE,
2042 .name = "moschip7720",
2044 .description = "Moschip 2 port adapter",
2045 .id_table = id_table,
2046 .calc_num_ports = mos77xx_calc_num_ports,
2047 .open = mos7720_open,
2048 .close = mos7720_close,
2049 .throttle = mos7720_throttle,
2050 .unthrottle = mos7720_unthrottle,
2051 .attach = mos7720_startup,
2052 .release = mos7720_release,
2053 .port_probe = mos7720_port_probe,
2054 .port_remove = mos7720_port_remove,
2055 .ioctl = mos7720_ioctl,
2056 .tiocmget = mos7720_tiocmget,
2057 .tiocmset = mos7720_tiocmset,
2058 .set_termios = mos7720_set_termios,
2059 .write = mos7720_write,
2060 .write_room = mos7720_write_room,
2061 .chars_in_buffer = mos7720_chars_in_buffer,
2062 .break_ctl = mos7720_break,
2063 .read_bulk_callback = mos7720_bulk_in_callback,
2064 .read_int_callback = mos7720_interrupt_callback,
2067 static struct usb_serial_driver * const serial_drivers[] = {
2068 &moschip7720_2port_driver, NULL
2071 module_usb_serial_driver(serial_drivers, id_table);
2073 MODULE_AUTHOR(DRIVER_AUTHOR);
2074 MODULE_DESCRIPTION(DRIVER_DESC);
2075 MODULE_LICENSE("GPL");