3 * Controls the Moschip 7720 usb to dual port serial convertor
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/init.h>
26 #include <linux/slab.h>
27 #include <linux/tty.h>
28 #include <linux/tty_driver.h>
29 #include <linux/tty_flip.h>
30 #include <linux/module.h>
31 #include <linux/spinlock.h>
32 #include <linux/serial.h>
33 #include <linux/serial_reg.h>
34 #include <linux/usb.h>
35 #include <linux/usb/serial.h>
36 #include <linux/uaccess.h>
37 #include <linux/parport.h>
42 #define DRIVER_VERSION "2.1"
43 #define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
44 #define DRIVER_DESC "Moschip USB Serial Driver"
46 /* default urb timeout */
47 #define MOS_WDR_TIMEOUT 5000
49 #define MOS_MAX_PORT 0x02
50 #define MOS_WRITE 0x0E
53 /* Interrupt Rotinue Defines */
54 #define SERIAL_IIR_RLS 0x06
55 #define SERIAL_IIR_RDA 0x04
56 #define SERIAL_IIR_CTI 0x0c
57 #define SERIAL_IIR_THR 0x02
58 #define SERIAL_IIR_MS 0x00
60 #define NUM_URBS 16 /* URB Count */
61 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
63 /* This structure holds all of the local serial port information */
65 __u8 shadowLCR; /* last LCR value received */
66 __u8 shadowMCR; /* last MCR value received */
67 __u8 shadowMSR; /* last MSR value received */
69 struct async_icount icount;
70 struct usb_serial_port *port; /* loop back to the owner */
71 struct urb *write_urb_pool[NUM_URBS];
76 static struct usb_serial_driver moschip7720_2port_driver;
78 #define USB_VENDOR_ID_MOSCHIP 0x9710
79 #define MOSCHIP_DEVICE_ID_7720 0x7720
80 #define MOSCHIP_DEVICE_ID_7715 0x7715
82 static const struct usb_device_id moschip_port_id_table[] = {
83 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
84 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) },
85 { } /* terminating entry */
87 MODULE_DEVICE_TABLE(usb, moschip_port_id_table);
89 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
91 /* initial values for parport regs */
92 #define DCR_INIT_VAL 0x0c /* SLCTIN, nINIT */
93 #define ECR_INIT_VAL 0x00 /* SPP mode */
96 struct mos7715_parport *mos_parport;
97 struct list_head urblist_entry;
98 struct kref ref_count;
102 enum mos7715_pp_modes {
104 PS2 = 1<<5, /* moschip calls this 'NIBBLE' mode */
105 PPF = 2<<5, /* moschip calls this 'CB-FIFO mode */
108 struct mos7715_parport {
109 struct parport *pp; /* back to containing struct */
110 struct kref ref_count; /* to instance of this struct */
111 struct list_head deferred_urbs; /* list deferred async urbs */
112 struct list_head active_urbs; /* list async urbs in flight */
113 spinlock_t listlock; /* protects list access */
114 bool msg_pending; /* usb sync call pending */
115 struct completion syncmsg_compl; /* usb sync call completed */
116 struct tasklet_struct urb_tasklet; /* for sending deferred urbs */
117 struct usb_serial *serial; /* back to containing struct */
118 __u8 shadowECR; /* parallel port regs... */
120 atomic_t shadowDSR; /* updated in int-in callback */
123 /* lock guards against dereferencing NULL ptr in parport ops callbacks */
124 static DEFINE_SPINLOCK(release_lock);
126 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
128 static const unsigned int dummy; /* for clarity in register access fns */
131 THR, /* serial port regs */
143 DPR, /* parallel port regs */
147 SP1_REG, /* device control regs */
148 SP2_REG, /* serial port 2 (7720 only) */
154 * Return the correct value for the Windex field of the setup packet
155 * for a control endpoint message. See the 7715 datasheet.
157 static inline __u16 get_reg_index(enum mos_regs reg)
159 static const __u16 mos7715_index_lookup_table[] = {
177 0x02, /* SP2_REG (7720 only) */
178 0x04, /* PP_REG (7715 only) */
179 0x08, /* SP_CONTROL_REG */
181 return mos7715_index_lookup_table[reg];
185 * Return the correct value for the upper byte of the Wvalue field of
186 * the setup packet for a control endpoint message.
188 static inline __u16 get_reg_value(enum mos_regs reg,
189 unsigned int serial_portnum)
191 if (reg >= SP1_REG) /* control reg */
194 else if (reg >= DPR) /* parallel port reg (7715 only) */
197 else /* serial port reg */
198 return (serial_portnum + 2) << 8;
202 * Write data byte to the specified device register. The data is embedded in
203 * the value field of the setup packet. serial_portnum is ignored for registers
204 * not specific to a particular serial port.
206 static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
207 enum mos_regs reg, __u8 data)
209 struct usb_device *usbdev = serial->dev;
210 unsigned int pipe = usb_sndctrlpipe(usbdev, 0);
211 __u8 request = (__u8)0x0e;
212 __u8 requesttype = (__u8)0x40;
213 __u16 index = get_reg_index(reg);
214 __u16 value = get_reg_value(reg, serial_portnum) + data;
215 int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
216 index, NULL, 0, MOS_WDR_TIMEOUT);
218 dev_err(&usbdev->dev,
219 "mos7720: usb_control_msg() failed: %d", status);
224 * Read data byte from the specified device register. The data returned by the
225 * device is embedded in the value field of the setup packet. serial_portnum is
226 * ignored for registers that are not specific to a particular serial port.
228 static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
229 enum mos_regs reg, __u8 *data)
231 struct usb_device *usbdev = serial->dev;
232 unsigned int pipe = usb_rcvctrlpipe(usbdev, 0);
233 __u8 request = (__u8)0x0d;
234 __u8 requesttype = (__u8)0xc0;
235 __u16 index = get_reg_index(reg);
236 __u16 value = get_reg_value(reg, serial_portnum);
240 buf = kmalloc(1, GFP_KERNEL);
244 status = usb_control_msg(usbdev, pipe, request, requesttype, value,
245 index, buf, 1, MOS_WDR_TIMEOUT);
249 dev_err(&usbdev->dev,
250 "mos7720: usb_control_msg() failed: %d", status);
256 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
258 static inline int mos7715_change_mode(struct mos7715_parport *mos_parport,
259 enum mos7715_pp_modes mode)
261 mos_parport->shadowECR = mode;
262 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
266 static void destroy_mos_parport(struct kref *kref)
268 struct mos7715_parport *mos_parport =
269 container_of(kref, struct mos7715_parport, ref_count);
271 dbg("%s called", __func__);
275 static void destroy_urbtracker(struct kref *kref)
277 struct urbtracker *urbtrack =
278 container_of(kref, struct urbtracker, ref_count);
279 struct mos7715_parport *mos_parport = urbtrack->mos_parport;
280 dbg("%s called", __func__);
281 usb_free_urb(urbtrack->urb);
283 kref_put(&mos_parport->ref_count, destroy_mos_parport);
287 * This runs as a tasklet when sending an urb in a non-blocking parallel
288 * port callback had to be deferred because the disconnect mutex could not be
289 * obtained at the time.
291 static void send_deferred_urbs(unsigned long _mos_parport)
295 struct mos7715_parport *mos_parport = (void *)_mos_parport;
296 struct urbtracker *urbtrack;
297 struct list_head *cursor, *next;
299 dbg("%s called", __func__);
301 /* if release function ran, game over */
302 if (unlikely(mos_parport->serial == NULL))
305 /* try again to get the mutex */
306 if (!mutex_trylock(&mos_parport->serial->disc_mutex)) {
307 dbg("%s: rescheduling tasklet", __func__);
308 tasklet_schedule(&mos_parport->urb_tasklet);
312 /* if device disconnected, game over */
313 if (unlikely(mos_parport->serial->disconnected)) {
314 mutex_unlock(&mos_parport->serial->disc_mutex);
318 spin_lock_irqsave(&mos_parport->listlock, flags);
319 if (list_empty(&mos_parport->deferred_urbs)) {
320 spin_unlock_irqrestore(&mos_parport->listlock, flags);
321 mutex_unlock(&mos_parport->serial->disc_mutex);
322 dbg("%s: deferred_urbs list empty", __func__);
326 /* move contents of deferred_urbs list to active_urbs list and submit */
327 list_for_each_safe(cursor, next, &mos_parport->deferred_urbs)
328 list_move_tail(cursor, &mos_parport->active_urbs);
329 list_for_each_entry(urbtrack, &mos_parport->active_urbs,
331 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
332 dbg("%s: urb submitted", __func__);
334 dev_err(&mos_parport->serial->dev->dev,
335 "usb_submit_urb() failed: %d", ret_val);
336 list_del(&urbtrack->urblist_entry);
337 kref_put(&urbtrack->ref_count, destroy_urbtracker);
340 spin_unlock_irqrestore(&mos_parport->listlock, flags);
341 mutex_unlock(&mos_parport->serial->disc_mutex);
344 /* callback for parallel port control urbs submitted asynchronously */
345 static void async_complete(struct urb *urb)
347 struct urbtracker *urbtrack = urb->context;
348 int status = urb->status;
349 dbg("%s called", __func__);
350 if (unlikely(status))
351 dbg("%s - nonzero urb status received: %d", __func__, status);
353 /* remove the urbtracker from the active_urbs list */
354 spin_lock(&urbtrack->mos_parport->listlock);
355 list_del(&urbtrack->urblist_entry);
356 spin_unlock(&urbtrack->mos_parport->listlock);
357 kref_put(&urbtrack->ref_count, destroy_urbtracker);
360 static int write_parport_reg_nonblock(struct mos7715_parport *mos_parport,
361 enum mos_regs reg, __u8 data)
363 struct urbtracker *urbtrack;
366 struct usb_ctrlrequest setup;
367 struct usb_serial *serial = mos_parport->serial;
368 struct usb_device *usbdev = serial->dev;
369 dbg("%s called", __func__);
371 /* create and initialize the control urb and containing urbtracker */
372 urbtrack = kmalloc(sizeof(struct urbtracker), GFP_ATOMIC);
373 if (urbtrack == NULL) {
374 dev_err(&usbdev->dev, "out of memory");
377 kref_get(&mos_parport->ref_count);
378 urbtrack->mos_parport = mos_parport;
379 urbtrack->urb = usb_alloc_urb(0, GFP_ATOMIC);
380 if (urbtrack->urb == NULL) {
381 dev_err(&usbdev->dev, "out of urbs");
385 setup.bRequestType = (__u8)0x40;
386 setup.bRequest = (__u8)0x0e;
387 setup.wValue = get_reg_value(reg, dummy);
388 setup.wIndex = get_reg_index(reg);
390 usb_fill_control_urb(urbtrack->urb, usbdev,
391 usb_sndctrlpipe(usbdev, 0),
392 (unsigned char *)&setup,
393 NULL, 0, async_complete, urbtrack);
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 dbg("tasklet scheduled");
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", __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 messgage 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 INIT_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 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;
490 dbg("%s called: %2.2x", __func__, d);
491 if (parport_prologue(pp) < 0)
493 mos7715_change_mode(mos_parport, SPP);
494 write_mos_reg(mos_parport->serial, dummy, 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;
502 dbg("%s called", __func__);
503 if (parport_prologue(pp) < 0)
505 read_mos_reg(mos_parport->serial, dummy, 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;
514 dbg("%s called: %2.2x", __func__, d);
515 if (parport_prologue(pp) < 0)
517 data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
518 write_mos_reg(mos_parport->serial, dummy, 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 = pp->private_data;
527 dbg("%s called", __func__);
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;
545 dbg("%s called", __func__);
548 if (parport_prologue(pp) < 0)
550 mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
551 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
552 dcr = mos_parport->shadowDCR & 0x0f;
553 parport_epilogue(pp);
557 static unsigned char parport_mos7715_read_status(struct parport *pp)
559 unsigned char status;
560 struct mos7715_parport *mos_parport = pp->private_data;
561 dbg("%s called", __func__);
562 spin_lock(&release_lock);
563 mos_parport = pp->private_data;
564 if (unlikely(mos_parport == NULL)) { /* release called */
565 spin_unlock(&release_lock);
568 status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
569 spin_unlock(&release_lock);
573 static void parport_mos7715_enable_irq(struct parport *pp)
575 dbg("%s called", __func__);
577 static void parport_mos7715_disable_irq(struct parport *pp)
579 dbg("%s called", __func__);
582 static void parport_mos7715_data_forward(struct parport *pp)
584 struct mos7715_parport *mos_parport = pp->private_data;
585 dbg("%s called", __func__);
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, DCR, mos_parport->shadowDCR);
591 parport_epilogue(pp);
594 static void parport_mos7715_data_reverse(struct parport *pp)
596 struct mos7715_parport *mos_parport = pp->private_data;
597 dbg("%s called", __func__);
598 if (parport_prologue(pp) < 0)
600 mos7715_change_mode(mos_parport, PS2);
601 mos_parport->shadowDCR |= 0x20;
602 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
603 parport_epilogue(pp);
606 static void parport_mos7715_init_state(struct pardevice *dev,
607 struct parport_state *s)
609 dbg("%s called", __func__);
610 s->u.pc.ctr = DCR_INIT_VAL;
611 s->u.pc.ecr = ECR_INIT_VAL;
614 /* N.B. Parport core code requires that this function not block */
615 static void parport_mos7715_save_state(struct parport *pp,
616 struct parport_state *s)
618 struct mos7715_parport *mos_parport;
619 dbg("%s called", __func__);
620 spin_lock(&release_lock);
621 mos_parport = pp->private_data;
622 if (unlikely(mos_parport == NULL)) { /* release called */
623 spin_unlock(&release_lock);
626 s->u.pc.ctr = mos_parport->shadowDCR;
627 s->u.pc.ecr = mos_parport->shadowECR;
628 spin_unlock(&release_lock);
631 /* N.B. Parport core code requires that this function not block */
632 static void parport_mos7715_restore_state(struct parport *pp,
633 struct parport_state *s)
635 struct mos7715_parport *mos_parport;
636 dbg("%s called", __func__);
637 spin_lock(&release_lock);
638 mos_parport = pp->private_data;
639 if (unlikely(mos_parport == NULL)) { /* release called */
640 spin_unlock(&release_lock);
643 write_parport_reg_nonblock(mos_parport, DCR, mos_parport->shadowDCR);
644 write_parport_reg_nonblock(mos_parport, ECR, mos_parport->shadowECR);
645 spin_unlock(&release_lock);
648 static size_t parport_mos7715_write_compat(struct parport *pp,
650 size_t len, int flags)
653 struct mos7715_parport *mos_parport = pp->private_data;
655 dbg("%s called: %u chars", __func__, (unsigned int)len);
656 if (parport_prologue(pp) < 0)
658 mos7715_change_mode(mos_parport, PPF);
659 retval = usb_bulk_msg(mos_parport->serial->dev,
660 usb_sndbulkpipe(mos_parport->serial->dev, 2),
661 (void *)buffer, len, &actual_len,
663 parport_epilogue(pp);
665 dev_err(&mos_parport->serial->dev->dev,
666 "mos7720: usb_bulk_msg() failed: %d", retval);
672 static struct parport_operations parport_mos7715_ops = {
673 .owner = THIS_MODULE,
674 .write_data = parport_mos7715_write_data,
675 .read_data = parport_mos7715_read_data,
677 .write_control = parport_mos7715_write_control,
678 .read_control = parport_mos7715_read_control,
679 .frob_control = parport_mos7715_frob_control,
681 .read_status = parport_mos7715_read_status,
683 .enable_irq = parport_mos7715_enable_irq,
684 .disable_irq = parport_mos7715_disable_irq,
686 .data_forward = parport_mos7715_data_forward,
687 .data_reverse = parport_mos7715_data_reverse,
689 .init_state = parport_mos7715_init_state,
690 .save_state = parport_mos7715_save_state,
691 .restore_state = parport_mos7715_restore_state,
693 .compat_write_data = parport_mos7715_write_compat,
695 .nibble_read_data = parport_ieee1284_read_nibble,
696 .byte_read_data = parport_ieee1284_read_byte,
700 * Allocate and initialize parallel port control struct, initialize
701 * the parallel port hardware device, and register with the parport subsystem.
703 static int mos7715_parport_init(struct usb_serial *serial)
705 struct mos7715_parport *mos_parport;
707 /* allocate and initialize parallel port control struct */
708 mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
709 if (mos_parport == NULL) {
710 dbg("mos7715_parport_init: kzalloc failed");
713 mos_parport->msg_pending = false;
714 kref_init(&mos_parport->ref_count);
715 spin_lock_init(&mos_parport->listlock);
716 INIT_LIST_HEAD(&mos_parport->active_urbs);
717 INIT_LIST_HEAD(&mos_parport->deferred_urbs);
718 usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
719 mos_parport->serial = serial;
720 tasklet_init(&mos_parport->urb_tasklet, send_deferred_urbs,
721 (unsigned long) mos_parport);
722 init_completion(&mos_parport->syncmsg_compl);
724 /* cycle parallel port reset bit */
725 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x80);
726 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x00);
728 /* initialize device registers */
729 mos_parport->shadowDCR = DCR_INIT_VAL;
730 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
731 mos_parport->shadowECR = ECR_INIT_VAL;
732 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
734 /* register with parport core */
735 mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE,
737 &parport_mos7715_ops);
738 if (mos_parport->pp == NULL) {
739 dev_err(&serial->interface->dev,
740 "Could not register parport\n");
741 kref_put(&mos_parport->ref_count, destroy_mos_parport);
744 mos_parport->pp->private_data = mos_parport;
745 mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP;
746 mos_parport->pp->dev = &serial->interface->dev;
747 parport_announce_port(mos_parport->pp);
751 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
754 * mos7720_interrupt_callback
755 * this is the callback function for when we have received data on the
756 * interrupt endpoint.
758 static void mos7720_interrupt_callback(struct urb *urb)
762 int status = urb->status;
774 /* this urb is terminated, clean up */
775 dbg("%s - urb shutting down with status: %d", __func__,
779 dbg("%s - nonzero urb status received: %d", __func__,
784 length = urb->actual_length;
785 data = urb->transfer_buffer;
787 /* Moschip get 4 bytes
788 * Byte 1 IIR Port 1 (port.number is 0)
789 * Byte 2 IIR Port 2 (port.number is 1)
790 * Byte 3 --------------
791 * Byte 4 FIFO status for both */
793 /* the above description is inverted
794 * oneukum 2007-03-14 */
796 if (unlikely(length != 4)) {
797 dbg("Wrong data !!!");
804 if ((sp1 | sp2) & 0x01) {
805 /* No Interrupt Pending in both the ports */
806 dbg("No Interrupt !!!");
808 switch (sp1 & 0x0f) {
810 dbg("Serial Port 1: Receiver status error or address "
811 "bit detected in 9-bit mode\n");
814 dbg("Serial Port 1: Receiver time out");
817 /* dbg("Serial Port 1: Modem status change"); */
821 switch (sp2 & 0x0f) {
823 dbg("Serial Port 2: Receiver status error or address "
824 "bit detected in 9-bit mode");
827 dbg("Serial Port 2: Receiver time out");
830 /* dbg("Serial Port 2: Modem status change"); */
836 result = usb_submit_urb(urb, GFP_ATOMIC);
838 dev_err(&urb->dev->dev,
839 "%s - Error %d submitting control urb\n",
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;
864 /* this urb is terminated, clean up */
865 dbg("%s - urb shutting down with status: %d", __func__,
869 dbg("%s - nonzero urb status received: %d", __func__,
874 length = urb->actual_length;
875 data = urb->transfer_buffer;
877 /* Structure of data from 7715 device:
878 * Byte 1: IIR serial Port
880 * Byte 2: DSR parallel port
881 * Byte 4: FIFO status for both */
883 if (unlikely(length != 4)) {
884 dbg("Wrong data !!!");
889 if (!(iir & 0x01)) { /* serial port interrupt pending */
890 switch (iir & 0x0f) {
892 dbg("Serial Port: Receiver status error or address "
893 "bit detected in 9-bit mode\n");
896 dbg("Serial Port: Receiver time out");
899 /* dbg("Serial Port: Modem status change"); */
904 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
905 { /* update local copy of DSR reg */
906 struct usb_serial_port *port = urb->context;
907 struct mos7715_parport *mos_parport = port->serial->private;
908 if (unlikely(mos_parport == NULL))
910 atomic_set(&mos_parport->shadowDSR, data[2]);
915 result = usb_submit_urb(urb, GFP_ATOMIC);
917 dev_err(&urb->dev->dev,
918 "%s - Error %d submitting control urb\n",
923 * mos7720_bulk_in_callback
924 * this is the callback function for when we have received data on the
927 static void mos7720_bulk_in_callback(struct urb *urb)
930 unsigned char *data ;
931 struct usb_serial_port *port;
932 struct tty_struct *tty;
933 int status = urb->status;
936 dbg("nonzero read bulk status received: %d", status);
942 dbg("Entering...%s", __func__);
944 data = urb->transfer_buffer;
946 tty = tty_port_tty_get(&port->port);
947 if (tty && urb->actual_length) {
948 tty_insert_flip_string(tty, data, urb->actual_length);
949 tty_flip_buffer_push(tty);
953 if (!port->read_urb) {
954 dbg("URB KILLED !!!");
958 if (port->read_urb->status != -EINPROGRESS) {
959 port->read_urb->dev = port->serial->dev;
961 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
963 dbg("usb_submit_urb(read bulk) failed, retval = %d",
969 * mos7720_bulk_out_data_callback
970 * this is the callback function for when we have finished sending serial
971 * data on the bulk out endpoint.
973 static void mos7720_bulk_out_data_callback(struct urb *urb)
975 struct moschip_port *mos7720_port;
976 struct tty_struct *tty;
977 int status = urb->status;
980 dbg("nonzero write bulk status received:%d", status);
984 mos7720_port = urb->context;
986 dbg("NULL mos7720_port pointer");
990 tty = tty_port_tty_get(&mos7720_port->port->port);
992 if (tty && mos7720_port->open)
999 * this function installs the appropriate read interrupt endpoint callback
1000 * depending on whether the device is a 7720 or 7715, thus avoiding costly
1001 * run-time checks in the high-frequency callback routine itself.
1003 static int mos77xx_probe(struct usb_serial *serial,
1004 const struct usb_device_id *id)
1006 if (id->idProduct == MOSCHIP_DEVICE_ID_7715)
1007 moschip7720_2port_driver.read_int_callback =
1008 mos7715_interrupt_callback;
1010 moschip7720_2port_driver.read_int_callback =
1011 mos7720_interrupt_callback;
1016 static int mos77xx_calc_num_ports(struct usb_serial *serial)
1018 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
1019 if (product == MOSCHIP_DEVICE_ID_7715)
1025 static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
1027 struct usb_serial *serial;
1028 struct usb_serial_port *port0;
1030 struct moschip_port *mos7720_port;
1034 int allocated_urbs = 0;
1037 serial = port->serial;
1039 mos7720_port = usb_get_serial_port_data(port);
1040 if (mos7720_port == NULL)
1043 port0 = serial->port[0];
1045 usb_clear_halt(serial->dev, port->write_urb->pipe);
1046 usb_clear_halt(serial->dev, port->read_urb->pipe);
1048 /* Initialising the write urb pool */
1049 for (j = 0; j < NUM_URBS; ++j) {
1050 urb = usb_alloc_urb(0, GFP_KERNEL);
1051 mos7720_port->write_urb_pool[j] = urb;
1054 dev_err(&port->dev, "No more urbs???\n");
1058 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1060 if (!urb->transfer_buffer) {
1062 "%s-out of memory for urb buffers.\n",
1064 usb_free_urb(mos7720_port->write_urb_pool[j]);
1065 mos7720_port->write_urb_pool[j] = NULL;
1071 if (!allocated_urbs)
1074 /* Initialize MCS7720 -- Write Init values to corresponding Registers
1086 * 0x08 : SP1/2 Control Reg
1088 port_number = port->number - port->serial->minor;
1089 read_mos_reg(serial, port_number, LSR, &data);
1091 dbg("SS::%p LSR:%x", mos7720_port, data);
1093 dbg("Check:Sending Command ..........");
1095 write_mos_reg(serial, dummy, SP1_REG, 0x02);
1096 write_mos_reg(serial, dummy, SP2_REG, 0x02);
1098 write_mos_reg(serial, port_number, IER, 0x00);
1099 write_mos_reg(serial, port_number, FCR, 0x00);
1101 write_mos_reg(serial, port_number, FCR, 0xcf);
1102 mos7720_port->shadowLCR = 0x03;
1103 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1104 mos7720_port->shadowMCR = 0x0b;
1105 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1107 write_mos_reg(serial, port_number, SP_CONTROL_REG, 0x00);
1108 read_mos_reg(serial, dummy, SP_CONTROL_REG, &data);
1109 data = data | (port->number - port->serial->minor + 1);
1110 write_mos_reg(serial, dummy, SP_CONTROL_REG, data);
1111 mos7720_port->shadowLCR = 0x83;
1112 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1113 write_mos_reg(serial, port_number, THR, 0x0c);
1114 write_mos_reg(serial, port_number, IER, 0x00);
1115 mos7720_port->shadowLCR = 0x03;
1116 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1117 write_mos_reg(serial, port_number, IER, 0x0c);
1119 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
1121 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
1122 __func__, response);
1124 /* initialize our icount structure */
1125 memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
1127 /* initialize our port settings */
1128 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
1130 /* send a open port command */
1131 mos7720_port->open = 1;
1137 * mos7720_chars_in_buffer
1138 * this function is called by the tty driver when it wants to know how many
1139 * bytes of data we currently have outstanding in the port (data that has
1140 * been written, but hasn't made it out the port yet)
1141 * If successful, we return the number of bytes left to be written in the
1143 * Otherwise we return a negative error number.
1145 static int mos7720_chars_in_buffer(struct tty_struct *tty)
1147 struct usb_serial_port *port = tty->driver_data;
1150 struct moschip_port *mos7720_port;
1152 dbg("%s:entering ...........", __func__);
1154 mos7720_port = usb_get_serial_port_data(port);
1155 if (mos7720_port == NULL) {
1156 dbg("%s:leaving ...........", __func__);
1160 for (i = 0; i < NUM_URBS; ++i) {
1161 if (mos7720_port->write_urb_pool[i] &&
1162 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
1163 chars += URB_TRANSFER_BUFFER_SIZE;
1165 dbg("%s - returns %d", __func__, chars);
1169 static void mos7720_close(struct usb_serial_port *port)
1171 struct usb_serial *serial;
1172 struct moschip_port *mos7720_port;
1175 dbg("mos7720_close:entering...");
1177 serial = port->serial;
1179 mos7720_port = usb_get_serial_port_data(port);
1180 if (mos7720_port == NULL)
1183 for (j = 0; j < NUM_URBS; ++j)
1184 usb_kill_urb(mos7720_port->write_urb_pool[j]);
1186 /* Freeing Write URBs */
1187 for (j = 0; j < NUM_URBS; ++j) {
1188 if (mos7720_port->write_urb_pool[j]) {
1189 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
1190 usb_free_urb(mos7720_port->write_urb_pool[j]);
1194 /* While closing port, shutdown all bulk read, write *
1195 * and interrupt read if they exists, otherwise nop */
1196 dbg("Shutdown bulk write");
1197 usb_kill_urb(port->write_urb);
1198 dbg("Shutdown bulk read");
1199 usb_kill_urb(port->read_urb);
1201 mutex_lock(&serial->disc_mutex);
1202 /* these commands must not be issued if the device has
1203 * been disconnected */
1204 if (!serial->disconnected) {
1205 write_mos_reg(serial, port->number - port->serial->minor,
1207 write_mos_reg(serial, port->number - port->serial->minor,
1210 mutex_unlock(&serial->disc_mutex);
1211 mos7720_port->open = 0;
1213 dbg("Leaving %s", __func__);
1216 static void mos7720_break(struct tty_struct *tty, int break_state)
1218 struct usb_serial_port *port = tty->driver_data;
1220 struct usb_serial *serial;
1221 struct moschip_port *mos7720_port;
1223 dbg("Entering %s", __func__);
1225 serial = port->serial;
1227 mos7720_port = usb_get_serial_port_data(port);
1228 if (mos7720_port == NULL)
1231 if (break_state == -1)
1232 data = mos7720_port->shadowLCR | UART_LCR_SBC;
1234 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
1236 mos7720_port->shadowLCR = data;
1237 write_mos_reg(serial, port->number - port->serial->minor,
1238 LCR, mos7720_port->shadowLCR);
1242 * mos7720_write_room
1243 * this function is called by the tty driver when it wants to know how many
1244 * bytes of data we can accept for a specific port.
1245 * If successful, we return the amount of room that we have for this port
1246 * Otherwise we return a negative error number.
1248 static int mos7720_write_room(struct tty_struct *tty)
1250 struct usb_serial_port *port = tty->driver_data;
1251 struct moschip_port *mos7720_port;
1255 dbg("%s:entering ...........", __func__);
1257 mos7720_port = usb_get_serial_port_data(port);
1258 if (mos7720_port == NULL) {
1259 dbg("%s:leaving ...........", __func__);
1263 /* FIXME: Locking */
1264 for (i = 0; i < NUM_URBS; ++i) {
1265 if (mos7720_port->write_urb_pool[i] &&
1266 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
1267 room += URB_TRANSFER_BUFFER_SIZE;
1270 dbg("%s - returns %d", __func__, room);
1274 static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
1275 const unsigned char *data, int count)
1282 struct moschip_port *mos7720_port;
1283 struct usb_serial *serial;
1285 const unsigned char *current_position = data;
1287 dbg("%s:entering ...........", __func__);
1289 serial = port->serial;
1291 mos7720_port = usb_get_serial_port_data(port);
1292 if (mos7720_port == NULL) {
1293 dbg("mos7720_port is NULL");
1297 /* try to find a free urb in the list */
1300 for (i = 0; i < NUM_URBS; ++i) {
1301 if (mos7720_port->write_urb_pool[i] &&
1302 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
1303 urb = mos7720_port->write_urb_pool[i];
1310 dbg("%s - no more free urbs", __func__);
1314 if (urb->transfer_buffer == NULL) {
1315 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1317 if (urb->transfer_buffer == NULL) {
1318 dev_err(&port->dev, "%s no more kernel memory...\n",
1323 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1325 memcpy(urb->transfer_buffer, current_position, transfer_size);
1326 usb_serial_debug_data(debug, &port->dev, __func__, transfer_size,
1327 urb->transfer_buffer);
1329 /* fill urb with data and submit */
1330 usb_fill_bulk_urb(urb, serial->dev,
1331 usb_sndbulkpipe(serial->dev,
1332 port->bulk_out_endpointAddress),
1333 urb->transfer_buffer, transfer_size,
1334 mos7720_bulk_out_data_callback, mos7720_port);
1336 /* send it down the pipe */
1337 status = usb_submit_urb(urb, GFP_ATOMIC);
1339 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
1340 "with status = %d\n", __func__, status);
1341 bytes_sent = status;
1344 bytes_sent = transfer_size;
1350 static void mos7720_throttle(struct tty_struct *tty)
1352 struct usb_serial_port *port = tty->driver_data;
1353 struct moschip_port *mos7720_port;
1356 dbg("%s- port %d", __func__, port->number);
1358 mos7720_port = usb_get_serial_port_data(port);
1360 if (mos7720_port == NULL)
1363 if (!mos7720_port->open) {
1364 dbg("port not opened");
1368 dbg("%s: Entering ..........", __func__);
1370 /* if we are implementing XON/XOFF, send the stop character */
1372 unsigned char stop_char = STOP_CHAR(tty);
1373 status = mos7720_write(tty, port, &stop_char, 1);
1378 /* if we are implementing RTS/CTS, toggle that line */
1379 if (tty->termios->c_cflag & CRTSCTS) {
1380 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
1381 write_mos_reg(port->serial, port->number - port->serial->minor,
1382 MCR, mos7720_port->shadowMCR);
1388 static void mos7720_unthrottle(struct tty_struct *tty)
1390 struct usb_serial_port *port = tty->driver_data;
1391 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1394 if (mos7720_port == NULL)
1397 if (!mos7720_port->open) {
1398 dbg("%s - port not opened", __func__);
1402 dbg("%s: Entering ..........", __func__);
1404 /* if we are implementing XON/XOFF, send the start character */
1406 unsigned char start_char = START_CHAR(tty);
1407 status = mos7720_write(tty, port, &start_char, 1);
1412 /* if we are implementing RTS/CTS, toggle that line */
1413 if (tty->termios->c_cflag & CRTSCTS) {
1414 mos7720_port->shadowMCR |= UART_MCR_RTS;
1415 write_mos_reg(port->serial, port->number - port->serial->minor,
1416 MCR, mos7720_port->shadowMCR);
1422 /* FIXME: this function does not work */
1423 static int set_higher_rates(struct moschip_port *mos7720_port,
1426 struct usb_serial_port *port;
1427 struct usb_serial *serial;
1429 enum mos_regs sp_reg;
1430 if (mos7720_port == NULL)
1433 port = mos7720_port->port;
1434 serial = port->serial;
1436 /***********************************************
1437 * Init Sequence for higher rates
1438 ***********************************************/
1439 dbg("Sending Setting Commands ..........");
1440 port_number = port->number - port->serial->minor;
1442 write_mos_reg(serial, port_number, IER, 0x00);
1443 write_mos_reg(serial, port_number, FCR, 0x00);
1444 write_mos_reg(serial, port_number, FCR, 0xcf);
1445 mos7720_port->shadowMCR = 0x0b;
1446 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1447 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x00);
1449 /***********************************************
1450 * Set for higher rates *
1451 ***********************************************/
1452 /* writing baud rate verbatum into uart clock field clearly not right */
1453 if (port_number == 0)
1457 write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
1458 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x03);
1459 mos7720_port->shadowMCR = 0x2b;
1460 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1462 /***********************************************
1464 ***********************************************/
1465 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1466 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1467 write_mos_reg(serial, port_number, DLL, 0x01);
1468 write_mos_reg(serial, port_number, DLM, 0x00);
1469 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1470 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1475 /* baud rate information */
1476 struct divisor_table_entry {
1481 /* Define table of divisors for moschip 7720 hardware *
1482 * These assume a 3.6864MHz crystal, the standard /16, and *
1484 static struct divisor_table_entry divisor_table[] = {
1486 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
1487 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
1503 /*****************************************************************************
1504 * calc_baud_rate_divisor
1505 * this function calculates the proper baud rate divisor for the specified
1507 *****************************************************************************/
1508 static int calc_baud_rate_divisor(int baudrate, int *divisor)
1516 dbg("%s - %d", __func__, baudrate);
1518 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
1519 if (divisor_table[i].baudrate == baudrate) {
1520 *divisor = divisor_table[i].divisor;
1525 /* After trying for all the standard baud rates *
1526 * Try calculating the divisor for this baud rate */
1527 if (baudrate > 75 && baudrate < 230400) {
1528 /* get the divisor */
1529 custom = (__u16)(230400L / baudrate);
1531 /* Check for round off */
1532 round1 = (__u16)(2304000L / baudrate);
1533 round = (__u16)(round1 - (custom * 10));
1538 dbg("Baud %d = %d", baudrate, custom);
1542 dbg("Baud calculation Failed...");
1547 * send_cmd_write_baud_rate
1548 * this function sends the proper command to change the baud rate of the
1551 static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1554 struct usb_serial_port *port;
1555 struct usb_serial *serial;
1558 unsigned char number;
1560 if (mos7720_port == NULL)
1563 port = mos7720_port->port;
1564 serial = port->serial;
1566 dbg("%s: Entering ..........", __func__);
1568 number = port->number - port->serial->minor;
1569 dbg("%s - port = %d, baud = %d", __func__, port->number, baudrate);
1571 /* Calculate the Divisor */
1572 status = calc_baud_rate_divisor(baudrate, &divisor);
1574 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1578 /* Enable access to divisor latch */
1579 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1580 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
1582 /* Write the divisor */
1583 write_mos_reg(serial, number, DLL, (__u8)(divisor & 0xff));
1584 write_mos_reg(serial, number, DLM, (__u8)((divisor & 0xff00) >> 8));
1586 /* Disable access to divisor latch */
1587 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1588 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
1594 * change_port_settings
1595 * This routine is called to set the UART on the device to match
1596 * the specified new settings.
1598 static void change_port_settings(struct tty_struct *tty,
1599 struct moschip_port *mos7720_port,
1600 struct ktermios *old_termios)
1602 struct usb_serial_port *port;
1603 struct usb_serial *serial;
1614 if (mos7720_port == NULL)
1617 port = mos7720_port->port;
1618 serial = port->serial;
1619 port_number = port->number - port->serial->minor;
1621 dbg("%s - port %d", __func__, port->number);
1623 if (!mos7720_port->open) {
1624 dbg("%s - port not opened", __func__);
1628 dbg("%s: Entering ..........", __func__);
1630 lData = UART_LCR_WLEN8;
1631 lStop = 0x00; /* 1 stop bit */
1632 lParity = 0x00; /* No parity */
1634 cflag = tty->termios->c_cflag;
1635 iflag = tty->termios->c_iflag;
1637 /* Change the number of bits */
1638 switch (cflag & CSIZE) {
1640 lData = UART_LCR_WLEN5;
1645 lData = UART_LCR_WLEN6;
1650 lData = UART_LCR_WLEN7;
1655 lData = UART_LCR_WLEN8;
1659 /* Change the Parity bit */
1660 if (cflag & PARENB) {
1661 if (cflag & PARODD) {
1662 lParity = UART_LCR_PARITY;
1663 dbg("%s - parity = odd", __func__);
1665 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
1666 dbg("%s - parity = even", __func__);
1670 dbg("%s - parity = none", __func__);
1674 lParity = lParity | 0x20;
1676 /* Change the Stop bit */
1677 if (cflag & CSTOPB) {
1678 lStop = UART_LCR_STOP;
1679 dbg("%s - stop bits = 2", __func__);
1682 dbg("%s - stop bits = 1", __func__);
1685 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1686 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1687 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
1689 /* Update the LCR with the correct value */
1690 mos7720_port->shadowLCR &=
1691 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1692 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1695 /* Disable Interrupts */
1696 write_mos_reg(serial, port_number, IER, 0x00);
1697 write_mos_reg(serial, port_number, FCR, 0x00);
1698 write_mos_reg(serial, port_number, FCR, 0xcf);
1700 /* Send the updated LCR value to the mos7720 */
1701 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1702 mos7720_port->shadowMCR = 0x0b;
1703 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1705 /* set up the MCR register and send it to the mos7720 */
1706 mos7720_port->shadowMCR = UART_MCR_OUT2;
1708 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1710 if (cflag & CRTSCTS) {
1711 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
1712 /* To set hardware flow control to the specified *
1713 * serial port, in SP1/2_CONTROL_REG */
1715 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x01);
1717 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x02);
1720 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1722 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1724 /* Determine divisor based on baud rate */
1725 baud = tty_get_baud_rate(tty);
1727 /* pick a default, any default... */
1728 dbg("Picked default baud...");
1732 if (baud >= 230400) {
1733 set_higher_rates(mos7720_port, baud);
1734 /* Enable Interrupts */
1735 write_mos_reg(serial, port_number, IER, 0x0c);
1739 dbg("%s - baud rate = %d", __func__, baud);
1740 status = send_cmd_write_baud_rate(mos7720_port, baud);
1741 /* FIXME: needs to write actual resulting baud back not just
1744 tty_encode_baud_rate(tty, baud, baud);
1745 /* Enable Interrupts */
1746 write_mos_reg(serial, port_number, IER, 0x0c);
1748 if (port->read_urb->status != -EINPROGRESS) {
1749 port->read_urb->dev = serial->dev;
1751 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1753 dbg("usb_submit_urb(read bulk) failed, status = %d",
1759 * mos7720_set_termios
1760 * this function is called by the tty driver when it wants to change the
1761 * termios structure.
1763 static void mos7720_set_termios(struct tty_struct *tty,
1764 struct usb_serial_port *port, struct ktermios *old_termios)
1768 struct usb_serial *serial;
1769 struct moschip_port *mos7720_port;
1771 serial = port->serial;
1773 mos7720_port = usb_get_serial_port_data(port);
1775 if (mos7720_port == NULL)
1778 if (!mos7720_port->open) {
1779 dbg("%s - port not opened", __func__);
1783 dbg("%s\n", "setting termios - ASPIRE");
1785 cflag = tty->termios->c_cflag;
1787 dbg("%s - cflag %08x iflag %08x", __func__,
1788 tty->termios->c_cflag,
1789 RELEVANT_IFLAG(tty->termios->c_iflag));
1791 dbg("%s - old cflag %08x old iflag %08x", __func__,
1792 old_termios->c_cflag,
1793 RELEVANT_IFLAG(old_termios->c_iflag));
1795 dbg("%s - port %d", __func__, port->number);
1797 /* change the port settings to the new ones specified */
1798 change_port_settings(tty, mos7720_port, old_termios);
1800 if (!port->read_urb) {
1801 dbg("%s", "URB KILLED !!!!!");
1805 if (port->read_urb->status != -EINPROGRESS) {
1806 port->read_urb->dev = serial->dev;
1807 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1809 dbg("usb_submit_urb(read bulk) failed, status = %d",
1815 * get_lsr_info - get line status register info
1817 * Purpose: Let user call ioctl() to get info when the UART physically
1818 * is emptied. On bus types like RS485, the transmitter must
1819 * release the bus after transmitting. This must be done when
1820 * the transmit shift register is empty, not be done when the
1821 * transmit holding register is empty. This functionality
1822 * allows an RS485 driver to be written in user space.
1824 static int get_lsr_info(struct tty_struct *tty,
1825 struct moschip_port *mos7720_port, unsigned int __user *value)
1827 struct usb_serial_port *port = tty->driver_data;
1828 unsigned int result = 0;
1829 unsigned char data = 0;
1830 int port_number = port->number - port->serial->minor;
1833 count = mos7720_chars_in_buffer(tty);
1835 read_mos_reg(port->serial, port_number, LSR, &data);
1836 if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1837 == (UART_LSR_TEMT | UART_LSR_THRE)) {
1838 dbg("%s -- Empty", __func__);
1839 result = TIOCSER_TEMT;
1842 if (copy_to_user(value, &result, sizeof(int)))
1847 static int mos7720_tiocmget(struct tty_struct *tty)
1849 struct usb_serial_port *port = tty->driver_data;
1850 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1851 unsigned int result = 0;
1855 dbg("%s - port %d", __func__, port->number);
1857 mcr = mos7720_port->shadowMCR;
1858 msr = mos7720_port->shadowMSR;
1860 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1861 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1862 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1863 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
1864 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1865 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
1867 dbg("%s -- %x", __func__, result);
1872 static int mos7720_tiocmset(struct tty_struct *tty,
1873 unsigned int set, unsigned int clear)
1875 struct usb_serial_port *port = tty->driver_data;
1876 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1878 dbg("%s - port %d", __func__, port->number);
1879 dbg("he was at tiocmset");
1881 mcr = mos7720_port->shadowMCR;
1883 if (set & TIOCM_RTS)
1884 mcr |= UART_MCR_RTS;
1885 if (set & TIOCM_DTR)
1886 mcr |= UART_MCR_DTR;
1887 if (set & TIOCM_LOOP)
1888 mcr |= UART_MCR_LOOP;
1890 if (clear & TIOCM_RTS)
1891 mcr &= ~UART_MCR_RTS;
1892 if (clear & TIOCM_DTR)
1893 mcr &= ~UART_MCR_DTR;
1894 if (clear & TIOCM_LOOP)
1895 mcr &= ~UART_MCR_LOOP;
1897 mos7720_port->shadowMCR = mcr;
1898 write_mos_reg(port->serial, port->number - port->serial->minor,
1899 MCR, mos7720_port->shadowMCR);
1904 static int mos7720_get_icount(struct tty_struct *tty,
1905 struct serial_icounter_struct *icount)
1907 struct usb_serial_port *port = tty->driver_data;
1908 struct moschip_port *mos7720_port;
1909 struct async_icount cnow;
1911 mos7720_port = usb_get_serial_port_data(port);
1912 cnow = mos7720_port->icount;
1914 icount->cts = cnow.cts;
1915 icount->dsr = cnow.dsr;
1916 icount->rng = cnow.rng;
1917 icount->dcd = cnow.dcd;
1918 icount->rx = cnow.rx;
1919 icount->tx = cnow.tx;
1920 icount->frame = cnow.frame;
1921 icount->overrun = cnow.overrun;
1922 icount->parity = cnow.parity;
1923 icount->brk = cnow.brk;
1924 icount->buf_overrun = cnow.buf_overrun;
1926 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
1927 port->number, icount->rx, icount->tx);
1931 static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1932 unsigned int __user *value)
1937 struct usb_serial_port *port;
1939 if (mos7720_port == NULL)
1942 port = (struct usb_serial_port *)mos7720_port->port;
1943 mcr = mos7720_port->shadowMCR;
1945 if (copy_from_user(&arg, value, sizeof(int)))
1950 if (arg & TIOCM_RTS)
1951 mcr |= UART_MCR_RTS;
1952 if (arg & TIOCM_DTR)
1953 mcr |= UART_MCR_RTS;
1954 if (arg & TIOCM_LOOP)
1955 mcr |= UART_MCR_LOOP;
1959 if (arg & TIOCM_RTS)
1960 mcr &= ~UART_MCR_RTS;
1961 if (arg & TIOCM_DTR)
1962 mcr &= ~UART_MCR_RTS;
1963 if (arg & TIOCM_LOOP)
1964 mcr &= ~UART_MCR_LOOP;
1969 mos7720_port->shadowMCR = mcr;
1970 write_mos_reg(port->serial, port->number - port->serial->minor,
1971 MCR, mos7720_port->shadowMCR);
1976 static int get_serial_info(struct moschip_port *mos7720_port,
1977 struct serial_struct __user *retinfo)
1979 struct serial_struct tmp;
1984 memset(&tmp, 0, sizeof(tmp));
1986 tmp.type = PORT_16550A;
1987 tmp.line = mos7720_port->port->serial->minor;
1988 tmp.port = mos7720_port->port->number;
1990 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1991 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1992 tmp.baud_base = 9600;
1993 tmp.close_delay = 5*HZ;
1994 tmp.closing_wait = 30*HZ;
1996 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2001 static int mos7720_ioctl(struct tty_struct *tty,
2002 unsigned int cmd, unsigned long arg)
2004 struct usb_serial_port *port = tty->driver_data;
2005 struct moschip_port *mos7720_port;
2006 struct async_icount cnow;
2007 struct async_icount cprev;
2009 mos7720_port = usb_get_serial_port_data(port);
2010 if (mos7720_port == NULL)
2013 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
2017 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
2018 return get_lsr_info(tty, mos7720_port,
2019 (unsigned int __user *)arg);
2021 /* FIXME: These should be using the mode methods */
2024 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET",
2025 __func__, port->number);
2026 return set_modem_info(mos7720_port, cmd,
2027 (unsigned int __user *)arg);
2030 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
2031 return get_serial_info(mos7720_port,
2032 (struct serial_struct __user *)arg);
2035 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
2036 cprev = mos7720_port->icount;
2038 if (signal_pending(current))
2039 return -ERESTARTSYS;
2040 cnow = mos7720_port->icount;
2041 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2042 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2043 return -EIO; /* no change => error */
2044 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2045 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2046 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2047 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2056 return -ENOIOCTLCMD;
2059 static int mos7720_startup(struct usb_serial *serial)
2061 struct moschip_port *mos7720_port;
2062 struct usb_device *dev;
2068 dbg("%s: Entering ..........", __func__);
2071 dbg("Invalid Handler");
2075 product = le16_to_cpu(serial->dev->descriptor.idProduct);
2079 * The 7715 uses the first bulk in/out endpoint pair for the parallel
2080 * port, and the second for the serial port. Because the usbserial core
2081 * assumes both pairs are serial ports, we must engage in a bit of
2082 * subterfuge and swap the pointers for ports 0 and 1 in order to make
2083 * port 0 point to the serial port. However, both moschip devices use a
2084 * single interrupt-in endpoint for both ports (as mentioned a little
2085 * further down), and this endpoint was assigned to port 0. So after
2086 * the swap, we must copy the interrupt endpoint elements from port 1
2087 * (as newly assigned) to port 0, and null out port 1 pointers.
2089 if (product == MOSCHIP_DEVICE_ID_7715) {
2090 struct usb_serial_port *tmp = serial->port[0];
2091 serial->port[0] = serial->port[1];
2092 serial->port[1] = tmp;
2093 serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb;
2094 serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer;
2095 serial->port[0]->interrupt_in_endpointAddress =
2096 tmp->interrupt_in_endpointAddress;
2097 serial->port[1]->interrupt_in_urb = NULL;
2098 serial->port[1]->interrupt_in_buffer = NULL;
2102 /* set up serial port private structures */
2103 for (i = 0; i < serial->num_ports; ++i) {
2104 mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2105 if (mos7720_port == NULL) {
2106 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
2110 /* Initialize all port interrupt end point to port 0 int
2111 * endpoint. Our device has only one interrupt endpoint
2112 * common to all ports */
2113 serial->port[i]->interrupt_in_endpointAddress =
2114 serial->port[0]->interrupt_in_endpointAddress;
2116 mos7720_port->port = serial->port[i];
2117 usb_set_serial_port_data(serial->port[i], mos7720_port);
2119 dbg("port number is %d", serial->port[i]->number);
2120 dbg("serial number is %d", serial->minor);
2124 /* setting configuration feature to one */
2125 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2126 (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5000);
2128 /* start the interrupt urb */
2129 ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
2132 "%s - Error %d submitting control urb\n",
2135 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
2136 if (product == MOSCHIP_DEVICE_ID_7715) {
2137 ret_val = mos7715_parport_init(serial);
2142 /* LSR For Port 1 */
2143 read_mos_reg(serial, 0, LSR, &data);
2144 dbg("LSR:%x", data);
2149 static void mos7720_release(struct usb_serial *serial)
2153 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
2154 /* close the parallel port */
2156 if (le16_to_cpu(serial->dev->descriptor.idProduct)
2157 == MOSCHIP_DEVICE_ID_7715) {
2158 struct urbtracker *urbtrack;
2159 unsigned long flags;
2160 struct mos7715_parport *mos_parport =
2161 usb_get_serial_data(serial);
2163 /* prevent NULL ptr dereference in port callbacks */
2164 spin_lock(&release_lock);
2165 mos_parport->pp->private_data = NULL;
2166 spin_unlock(&release_lock);
2168 /* wait for synchronous usb calls to return */
2169 if (mos_parport->msg_pending)
2170 wait_for_completion_timeout(&mos_parport->syncmsg_compl,
2171 msecs_to_jiffies(MOS_WDR_TIMEOUT));
2173 parport_remove_port(mos_parport->pp);
2174 usb_set_serial_data(serial, NULL);
2175 mos_parport->serial = NULL;
2177 /* if tasklet currently scheduled, wait for it to complete */
2178 tasklet_kill(&mos_parport->urb_tasklet);
2180 /* unlink any urbs sent by the tasklet */
2181 spin_lock_irqsave(&mos_parport->listlock, flags);
2182 list_for_each_entry(urbtrack,
2183 &mos_parport->active_urbs,
2185 usb_unlink_urb(urbtrack->urb);
2186 spin_unlock_irqrestore(&mos_parport->listlock, flags);
2188 kref_put(&mos_parport->ref_count, destroy_mos_parport);
2191 /* free private structure allocated for serial port */
2192 for (i = 0; i < serial->num_ports; ++i)
2193 kfree(usb_get_serial_port_data(serial->port[i]));
2196 static struct usb_driver usb_driver = {
2197 .name = "moschip7720",
2198 .probe = usb_serial_probe,
2199 .disconnect = usb_serial_disconnect,
2200 .id_table = moschip_port_id_table,
2204 static struct usb_serial_driver moschip7720_2port_driver = {
2206 .owner = THIS_MODULE,
2207 .name = "moschip7720",
2209 .description = "Moschip 2 port adapter",
2210 .usb_driver = &usb_driver,
2211 .id_table = moschip_port_id_table,
2212 .calc_num_ports = mos77xx_calc_num_ports,
2213 .open = mos7720_open,
2214 .close = mos7720_close,
2215 .throttle = mos7720_throttle,
2216 .unthrottle = mos7720_unthrottle,
2217 .probe = mos77xx_probe,
2218 .attach = mos7720_startup,
2219 .release = mos7720_release,
2220 .ioctl = mos7720_ioctl,
2221 .tiocmget = mos7720_tiocmget,
2222 .tiocmset = mos7720_tiocmset,
2223 .get_icount = mos7720_get_icount,
2224 .set_termios = mos7720_set_termios,
2225 .write = mos7720_write,
2226 .write_room = mos7720_write_room,
2227 .chars_in_buffer = mos7720_chars_in_buffer,
2228 .break_ctl = mos7720_break,
2229 .read_bulk_callback = mos7720_bulk_in_callback,
2230 .read_int_callback = NULL /* dynamically assigned in probe() */
2233 static int __init moschip7720_init(void)
2237 dbg("%s: Entering ..........", __func__);
2239 /* Register with the usb serial */
2240 retval = usb_serial_register(&moschip7720_2port_driver);
2242 goto failed_port_device_register;
2244 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
2247 /* Register with the usb */
2248 retval = usb_register(&usb_driver);
2250 goto failed_usb_register;
2254 failed_usb_register:
2255 usb_serial_deregister(&moschip7720_2port_driver);
2257 failed_port_device_register:
2261 static void __exit moschip7720_exit(void)
2263 usb_deregister(&usb_driver);
2264 usb_serial_deregister(&moschip7720_2port_driver);
2267 module_init(moschip7720_init);
2268 module_exit(moschip7720_exit);
2270 /* Module information */
2271 MODULE_AUTHOR(DRIVER_AUTHOR);
2272 MODULE_DESCRIPTION(DRIVER_DESC);
2273 MODULE_LICENSE("GPL");
2275 module_param(debug, bool, S_IRUGO | S_IWUSR);
2276 MODULE_PARM_DESC(debug, "Debug enabled or not");