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 #define USB_VENDOR_ID_MOSCHIP 0x9710
77 #define MOSCHIP_DEVICE_ID_7720 0x7720
78 #define MOSCHIP_DEVICE_ID_7715 0x7715
80 static const struct usb_device_id moschip_port_id_table[] = {
81 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
82 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) },
83 { } /* terminating entry */
85 MODULE_DEVICE_TABLE(usb, moschip_port_id_table);
87 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
89 /* initial values for parport regs */
90 #define DCR_INIT_VAL 0x0c /* SLCTIN, nINIT */
91 #define ECR_INIT_VAL 0x00 /* SPP mode */
94 struct mos7715_parport *mos_parport;
95 struct list_head urblist_entry;
96 struct kref ref_count;
98 struct usb_ctrlrequest *setup;
101 enum mos7715_pp_modes {
103 PS2 = 1<<5, /* moschip calls this 'NIBBLE' mode */
104 PPF = 2<<5, /* moschip calls this 'CB-FIFO mode */
107 struct mos7715_parport {
108 struct parport *pp; /* back to containing struct */
109 struct kref ref_count; /* to instance of this struct */
110 struct list_head deferred_urbs; /* list deferred async urbs */
111 struct list_head active_urbs; /* list async urbs in flight */
112 spinlock_t listlock; /* protects list access */
113 bool msg_pending; /* usb sync call pending */
114 struct completion syncmsg_compl; /* usb sync call completed */
115 struct tasklet_struct urb_tasklet; /* for sending deferred urbs */
116 struct usb_serial *serial; /* back to containing struct */
117 __u8 shadowECR; /* parallel port regs... */
119 atomic_t shadowDSR; /* updated in int-in callback */
122 /* lock guards against dereferencing NULL ptr in parport ops callbacks */
123 static DEFINE_SPINLOCK(release_lock);
125 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
127 static const unsigned int dummy; /* for clarity in register access fns */
130 THR, /* serial port regs */
142 DPR, /* parallel port regs */
146 SP1_REG, /* device control regs */
147 SP2_REG, /* serial port 2 (7720 only) */
153 * Return the correct value for the Windex field of the setup packet
154 * for a control endpoint message. See the 7715 datasheet.
156 static inline __u16 get_reg_index(enum mos_regs reg)
158 static const __u16 mos7715_index_lookup_table[] = {
176 0x02, /* SP2_REG (7720 only) */
177 0x04, /* PP_REG (7715 only) */
178 0x08, /* SP_CONTROL_REG */
180 return mos7715_index_lookup_table[reg];
184 * Return the correct value for the upper byte of the Wvalue field of
185 * the setup packet for a control endpoint message.
187 static inline __u16 get_reg_value(enum mos_regs reg,
188 unsigned int serial_portnum)
190 if (reg >= SP1_REG) /* control reg */
193 else if (reg >= DPR) /* parallel port reg (7715 only) */
196 else /* serial port reg */
197 return (serial_portnum + 2) << 8;
201 * Write data byte to the specified device register. The data is embedded in
202 * the value field of the setup packet. serial_portnum is ignored for registers
203 * not specific to a particular serial port.
205 static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
206 enum mos_regs reg, __u8 data)
208 struct usb_device *usbdev = serial->dev;
209 unsigned int pipe = usb_sndctrlpipe(usbdev, 0);
210 __u8 request = (__u8)0x0e;
211 __u8 requesttype = (__u8)0x40;
212 __u16 index = get_reg_index(reg);
213 __u16 value = get_reg_value(reg, serial_portnum) + data;
214 int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
215 index, NULL, 0, MOS_WDR_TIMEOUT);
217 dev_err(&usbdev->dev,
218 "mos7720: usb_control_msg() failed: %d", status);
223 * Read data byte from the specified device register. The data returned by the
224 * device is embedded in the value field of the setup packet. serial_portnum is
225 * ignored for registers that are not specific to a particular serial port.
227 static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
228 enum mos_regs reg, __u8 *data)
230 struct usb_device *usbdev = serial->dev;
231 unsigned int pipe = usb_rcvctrlpipe(usbdev, 0);
232 __u8 request = (__u8)0x0d;
233 __u8 requesttype = (__u8)0xc0;
234 __u16 index = get_reg_index(reg);
235 __u16 value = get_reg_value(reg, serial_portnum);
239 buf = kmalloc(1, GFP_KERNEL);
243 status = usb_control_msg(usbdev, pipe, request, requesttype, value,
244 index, buf, 1, MOS_WDR_TIMEOUT);
248 dev_err(&usbdev->dev,
249 "mos7720: usb_control_msg() failed: %d", status);
255 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
257 static inline int mos7715_change_mode(struct mos7715_parport *mos_parport,
258 enum mos7715_pp_modes mode)
260 mos_parport->shadowECR = mode;
261 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
265 static void destroy_mos_parport(struct kref *kref)
267 struct mos7715_parport *mos_parport =
268 container_of(kref, struct mos7715_parport, ref_count);
270 dbg("%s called", __func__);
274 static void destroy_urbtracker(struct kref *kref)
276 struct urbtracker *urbtrack =
277 container_of(kref, struct urbtracker, ref_count);
278 struct mos7715_parport *mos_parport = urbtrack->mos_parport;
279 dbg("%s called", __func__);
280 usb_free_urb(urbtrack->urb);
281 kfree(urbtrack->setup);
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_serial *serial = mos_parport->serial;
367 struct usb_device *usbdev = serial->dev;
368 dbg("%s called", __func__);
370 /* create and initialize the control urb and containing urbtracker */
371 urbtrack = kmalloc(sizeof(struct urbtracker), GFP_ATOMIC);
372 if (urbtrack == NULL) {
373 dev_err(&usbdev->dev, "out of memory");
376 kref_get(&mos_parport->ref_count);
377 urbtrack->mos_parport = mos_parport;
378 urbtrack->urb = usb_alloc_urb(0, GFP_ATOMIC);
379 if (urbtrack->urb == NULL) {
380 dev_err(&usbdev->dev, "out of urbs");
384 urbtrack->setup = kmalloc(sizeof(*urbtrack->setup), GFP_ATOMIC);
385 if (!urbtrack->setup) {
386 usb_free_urb(urbtrack->urb);
390 urbtrack->setup->bRequestType = (__u8)0x40;
391 urbtrack->setup->bRequest = (__u8)0x0e;
392 urbtrack->setup->wValue = cpu_to_le16(get_reg_value(reg, dummy));
393 urbtrack->setup->wIndex = cpu_to_le16(get_reg_index(reg));
394 urbtrack->setup->wLength = 0;
395 usb_fill_control_urb(urbtrack->urb, usbdev,
396 usb_sndctrlpipe(usbdev, 0),
397 (unsigned char *)urbtrack->setup,
398 NULL, 0, async_complete, urbtrack);
399 kref_init(&urbtrack->ref_count);
400 INIT_LIST_HEAD(&urbtrack->urblist_entry);
403 * get the disconnect mutex, or add tracker to the deferred_urbs list
404 * and schedule a tasklet to try again later
406 if (!mutex_trylock(&serial->disc_mutex)) {
407 spin_lock_irqsave(&mos_parport->listlock, flags);
408 list_add_tail(&urbtrack->urblist_entry,
409 &mos_parport->deferred_urbs);
410 spin_unlock_irqrestore(&mos_parport->listlock, flags);
411 tasklet_schedule(&mos_parport->urb_tasklet);
412 dbg("tasklet scheduled");
416 /* bail if device disconnected */
417 if (serial->disconnected) {
418 kref_put(&urbtrack->ref_count, destroy_urbtracker);
419 mutex_unlock(&serial->disc_mutex);
423 /* add the tracker to the active_urbs list and submit */
424 spin_lock_irqsave(&mos_parport->listlock, flags);
425 list_add_tail(&urbtrack->urblist_entry, &mos_parport->active_urbs);
426 spin_unlock_irqrestore(&mos_parport->listlock, flags);
427 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
428 mutex_unlock(&serial->disc_mutex);
430 dev_err(&usbdev->dev,
431 "%s: submit_urb() failed: %d", __func__, ret_val);
432 spin_lock_irqsave(&mos_parport->listlock, flags);
433 list_del(&urbtrack->urblist_entry);
434 spin_unlock_irqrestore(&mos_parport->listlock, flags);
435 kref_put(&urbtrack->ref_count, destroy_urbtracker);
442 * This is the the common top part of all parallel port callback operations that
443 * send synchronous messages to the device. This implements convoluted locking
444 * that avoids two scenarios: (1) a port operation is called after usbserial
445 * has called our release function, at which point struct mos7715_parport has
446 * been destroyed, and (2) the device has been disconnected, but usbserial has
447 * not called the release function yet because someone has a serial port open.
448 * The shared release_lock prevents the first, and the mutex and disconnected
449 * flag maintained by usbserial covers the second. We also use the msg_pending
450 * flag to ensure that all synchronous usb messgage calls have completed before
451 * our release function can return.
453 static int parport_prologue(struct parport *pp)
455 struct mos7715_parport *mos_parport;
457 spin_lock(&release_lock);
458 mos_parport = pp->private_data;
459 if (unlikely(mos_parport == NULL)) {
460 /* release fn called, port struct destroyed */
461 spin_unlock(&release_lock);
464 mos_parport->msg_pending = true; /* synch usb call pending */
465 INIT_COMPLETION(mos_parport->syncmsg_compl);
466 spin_unlock(&release_lock);
468 mutex_lock(&mos_parport->serial->disc_mutex);
469 if (mos_parport->serial->disconnected) {
470 /* device disconnected */
471 mutex_unlock(&mos_parport->serial->disc_mutex);
472 mos_parport->msg_pending = false;
473 complete(&mos_parport->syncmsg_compl);
481 * This is the the common bottom part of all parallel port functions that send
482 * synchronous messages to the device.
484 static inline void parport_epilogue(struct parport *pp)
486 struct mos7715_parport *mos_parport = pp->private_data;
487 mutex_unlock(&mos_parport->serial->disc_mutex);
488 mos_parport->msg_pending = false;
489 complete(&mos_parport->syncmsg_compl);
492 static void parport_mos7715_write_data(struct parport *pp, unsigned char d)
494 struct mos7715_parport *mos_parport = pp->private_data;
495 dbg("%s called: %2.2x", __func__, d);
496 if (parport_prologue(pp) < 0)
498 mos7715_change_mode(mos_parport, SPP);
499 write_mos_reg(mos_parport->serial, dummy, DPR, (__u8)d);
500 parport_epilogue(pp);
503 static unsigned char parport_mos7715_read_data(struct parport *pp)
505 struct mos7715_parport *mos_parport = pp->private_data;
507 dbg("%s called", __func__);
508 if (parport_prologue(pp) < 0)
510 read_mos_reg(mos_parport->serial, dummy, DPR, &d);
511 parport_epilogue(pp);
515 static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
517 struct mos7715_parport *mos_parport = pp->private_data;
519 dbg("%s called: %2.2x", __func__, d);
520 if (parport_prologue(pp) < 0)
522 data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
523 write_mos_reg(mos_parport->serial, dummy, DCR, data);
524 mos_parport->shadowDCR = data;
525 parport_epilogue(pp);
528 static unsigned char parport_mos7715_read_control(struct parport *pp)
530 struct mos7715_parport *mos_parport = pp->private_data;
532 dbg("%s called", __func__);
533 spin_lock(&release_lock);
534 mos_parport = pp->private_data;
535 if (unlikely(mos_parport == NULL)) {
536 spin_unlock(&release_lock);
539 dcr = mos_parport->shadowDCR & 0x0f;
540 spin_unlock(&release_lock);
544 static unsigned char parport_mos7715_frob_control(struct parport *pp,
548 struct mos7715_parport *mos_parport = pp->private_data;
550 dbg("%s called", __func__);
553 if (parport_prologue(pp) < 0)
555 mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
556 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
557 dcr = mos_parport->shadowDCR & 0x0f;
558 parport_epilogue(pp);
562 static unsigned char parport_mos7715_read_status(struct parport *pp)
564 unsigned char status;
565 struct mos7715_parport *mos_parport = pp->private_data;
566 dbg("%s called", __func__);
567 spin_lock(&release_lock);
568 mos_parport = pp->private_data;
569 if (unlikely(mos_parport == NULL)) { /* release called */
570 spin_unlock(&release_lock);
573 status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
574 spin_unlock(&release_lock);
578 static void parport_mos7715_enable_irq(struct parport *pp)
580 dbg("%s called", __func__);
582 static void parport_mos7715_disable_irq(struct parport *pp)
584 dbg("%s called", __func__);
587 static void parport_mos7715_data_forward(struct parport *pp)
589 struct mos7715_parport *mos_parport = pp->private_data;
590 dbg("%s called", __func__);
591 if (parport_prologue(pp) < 0)
593 mos7715_change_mode(mos_parport, PS2);
594 mos_parport->shadowDCR &= ~0x20;
595 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
596 parport_epilogue(pp);
599 static void parport_mos7715_data_reverse(struct parport *pp)
601 struct mos7715_parport *mos_parport = pp->private_data;
602 dbg("%s called", __func__);
603 if (parport_prologue(pp) < 0)
605 mos7715_change_mode(mos_parport, PS2);
606 mos_parport->shadowDCR |= 0x20;
607 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
608 parport_epilogue(pp);
611 static void parport_mos7715_init_state(struct pardevice *dev,
612 struct parport_state *s)
614 dbg("%s called", __func__);
615 s->u.pc.ctr = DCR_INIT_VAL;
616 s->u.pc.ecr = ECR_INIT_VAL;
619 /* N.B. Parport core code requires that this function not block */
620 static void parport_mos7715_save_state(struct parport *pp,
621 struct parport_state *s)
623 struct mos7715_parport *mos_parport;
624 dbg("%s called", __func__);
625 spin_lock(&release_lock);
626 mos_parport = pp->private_data;
627 if (unlikely(mos_parport == NULL)) { /* release called */
628 spin_unlock(&release_lock);
631 s->u.pc.ctr = mos_parport->shadowDCR;
632 s->u.pc.ecr = mos_parport->shadowECR;
633 spin_unlock(&release_lock);
636 /* N.B. Parport core code requires that this function not block */
637 static void parport_mos7715_restore_state(struct parport *pp,
638 struct parport_state *s)
640 struct mos7715_parport *mos_parport;
641 dbg("%s called", __func__);
642 spin_lock(&release_lock);
643 mos_parport = pp->private_data;
644 if (unlikely(mos_parport == NULL)) { /* release called */
645 spin_unlock(&release_lock);
648 write_parport_reg_nonblock(mos_parport, DCR, mos_parport->shadowDCR);
649 write_parport_reg_nonblock(mos_parport, ECR, mos_parport->shadowECR);
650 spin_unlock(&release_lock);
653 static size_t parport_mos7715_write_compat(struct parport *pp,
655 size_t len, int flags)
658 struct mos7715_parport *mos_parport = pp->private_data;
660 dbg("%s called: %u chars", __func__, (unsigned int)len);
661 if (parport_prologue(pp) < 0)
663 mos7715_change_mode(mos_parport, PPF);
664 retval = usb_bulk_msg(mos_parport->serial->dev,
665 usb_sndbulkpipe(mos_parport->serial->dev, 2),
666 (void *)buffer, len, &actual_len,
668 parport_epilogue(pp);
670 dev_err(&mos_parport->serial->dev->dev,
671 "mos7720: usb_bulk_msg() failed: %d", retval);
677 static struct parport_operations parport_mos7715_ops = {
678 .owner = THIS_MODULE,
679 .write_data = parport_mos7715_write_data,
680 .read_data = parport_mos7715_read_data,
682 .write_control = parport_mos7715_write_control,
683 .read_control = parport_mos7715_read_control,
684 .frob_control = parport_mos7715_frob_control,
686 .read_status = parport_mos7715_read_status,
688 .enable_irq = parport_mos7715_enable_irq,
689 .disable_irq = parport_mos7715_disable_irq,
691 .data_forward = parport_mos7715_data_forward,
692 .data_reverse = parport_mos7715_data_reverse,
694 .init_state = parport_mos7715_init_state,
695 .save_state = parport_mos7715_save_state,
696 .restore_state = parport_mos7715_restore_state,
698 .compat_write_data = parport_mos7715_write_compat,
700 .nibble_read_data = parport_ieee1284_read_nibble,
701 .byte_read_data = parport_ieee1284_read_byte,
705 * Allocate and initialize parallel port control struct, initialize
706 * the parallel port hardware device, and register with the parport subsystem.
708 static int mos7715_parport_init(struct usb_serial *serial)
710 struct mos7715_parport *mos_parport;
712 /* allocate and initialize parallel port control struct */
713 mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
714 if (mos_parport == NULL) {
715 dbg("mos7715_parport_init: kzalloc failed");
718 mos_parport->msg_pending = false;
719 kref_init(&mos_parport->ref_count);
720 spin_lock_init(&mos_parport->listlock);
721 INIT_LIST_HEAD(&mos_parport->active_urbs);
722 INIT_LIST_HEAD(&mos_parport->deferred_urbs);
723 usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
724 mos_parport->serial = serial;
725 tasklet_init(&mos_parport->urb_tasklet, send_deferred_urbs,
726 (unsigned long) mos_parport);
727 init_completion(&mos_parport->syncmsg_compl);
729 /* cycle parallel port reset bit */
730 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x80);
731 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x00);
733 /* initialize device registers */
734 mos_parport->shadowDCR = DCR_INIT_VAL;
735 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
736 mos_parport->shadowECR = ECR_INIT_VAL;
737 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
739 /* register with parport core */
740 mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE,
742 &parport_mos7715_ops);
743 if (mos_parport->pp == NULL) {
744 dev_err(&serial->interface->dev,
745 "Could not register parport\n");
746 kref_put(&mos_parport->ref_count, destroy_mos_parport);
749 mos_parport->pp->private_data = mos_parport;
750 mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP;
751 mos_parport->pp->dev = &serial->interface->dev;
752 parport_announce_port(mos_parport->pp);
756 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
759 * mos7720_interrupt_callback
760 * this is the callback function for when we have received data on the
761 * interrupt endpoint.
763 static void mos7720_interrupt_callback(struct urb *urb)
767 int status = urb->status;
779 /* this urb is terminated, clean up */
780 dbg("%s - urb shutting down with status: %d", __func__,
784 dbg("%s - nonzero urb status received: %d", __func__,
789 length = urb->actual_length;
790 data = urb->transfer_buffer;
792 /* Moschip get 4 bytes
793 * Byte 1 IIR Port 1 (port.number is 0)
794 * Byte 2 IIR Port 2 (port.number is 1)
795 * Byte 3 --------------
796 * Byte 4 FIFO status for both */
798 /* the above description is inverted
799 * oneukum 2007-03-14 */
801 if (unlikely(length != 4)) {
802 dbg("Wrong data !!!");
809 if ((sp1 | sp2) & 0x01) {
810 /* No Interrupt Pending in both the ports */
811 dbg("No Interrupt !!!");
813 switch (sp1 & 0x0f) {
815 dbg("Serial Port 1: Receiver status error or address "
816 "bit detected in 9-bit mode\n");
819 dbg("Serial Port 1: Receiver time out");
822 /* dbg("Serial Port 1: Modem status change"); */
826 switch (sp2 & 0x0f) {
828 dbg("Serial Port 2: Receiver status error or address "
829 "bit detected in 9-bit mode");
832 dbg("Serial Port 2: Receiver time out");
835 /* dbg("Serial Port 2: Modem status change"); */
841 result = usb_submit_urb(urb, GFP_ATOMIC);
843 dev_err(&urb->dev->dev,
844 "%s - Error %d submitting control urb\n",
849 * mos7715_interrupt_callback
850 * this is the 7715's callback function for when we have received data on
851 * the interrupt endpoint.
853 static void mos7715_interrupt_callback(struct urb *urb)
857 int status = urb->status;
869 /* this urb is terminated, clean up */
870 dbg("%s - urb shutting down with status: %d", __func__,
874 dbg("%s - nonzero urb status received: %d", __func__,
879 length = urb->actual_length;
880 data = urb->transfer_buffer;
882 /* Structure of data from 7715 device:
883 * Byte 1: IIR serial Port
885 * Byte 2: DSR parallel port
886 * Byte 4: FIFO status for both */
888 if (unlikely(length != 4)) {
889 dbg("Wrong data !!!");
894 if (!(iir & 0x01)) { /* serial port interrupt pending */
895 switch (iir & 0x0f) {
897 dbg("Serial Port: Receiver status error or address "
898 "bit detected in 9-bit mode\n");
901 dbg("Serial Port: Receiver time out");
904 /* dbg("Serial Port: Modem status change"); */
909 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
910 { /* update local copy of DSR reg */
911 struct usb_serial_port *port = urb->context;
912 struct mos7715_parport *mos_parport = port->serial->private;
913 if (unlikely(mos_parport == NULL))
915 atomic_set(&mos_parport->shadowDSR, data[2]);
920 result = usb_submit_urb(urb, GFP_ATOMIC);
922 dev_err(&urb->dev->dev,
923 "%s - Error %d submitting control urb\n",
928 * mos7720_bulk_in_callback
929 * this is the callback function for when we have received data on the
932 static void mos7720_bulk_in_callback(struct urb *urb)
935 unsigned char *data ;
936 struct usb_serial_port *port;
937 struct tty_struct *tty;
938 int status = urb->status;
941 dbg("nonzero read bulk status received: %d", status);
947 dbg("Entering...%s", __func__);
949 data = urb->transfer_buffer;
951 tty = tty_port_tty_get(&port->port);
952 if (tty && urb->actual_length) {
953 tty_insert_flip_string(tty, data, urb->actual_length);
954 tty_flip_buffer_push(tty);
958 if (!port->read_urb) {
959 dbg("URB KILLED !!!");
963 if (port->read_urb->status != -EINPROGRESS) {
964 port->read_urb->dev = port->serial->dev;
966 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
968 dbg("usb_submit_urb(read bulk) failed, retval = %d",
974 * mos7720_bulk_out_data_callback
975 * this is the callback function for when we have finished sending serial
976 * data on the bulk out endpoint.
978 static void mos7720_bulk_out_data_callback(struct urb *urb)
980 struct moschip_port *mos7720_port;
981 struct tty_struct *tty;
982 int status = urb->status;
985 dbg("nonzero write bulk status received:%d", status);
989 mos7720_port = urb->context;
991 dbg("NULL mos7720_port pointer");
995 tty = tty_port_tty_get(&mos7720_port->port->port);
997 if (tty && mos7720_port->open)
1002 static int mos77xx_calc_num_ports(struct usb_serial *serial)
1004 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
1005 if (product == MOSCHIP_DEVICE_ID_7715)
1011 static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
1013 struct usb_serial *serial;
1014 struct usb_serial_port *port0;
1016 struct moschip_port *mos7720_port;
1020 int allocated_urbs = 0;
1023 serial = port->serial;
1025 mos7720_port = usb_get_serial_port_data(port);
1026 if (mos7720_port == NULL)
1029 port0 = serial->port[0];
1031 usb_clear_halt(serial->dev, port->write_urb->pipe);
1032 usb_clear_halt(serial->dev, port->read_urb->pipe);
1034 /* Initialising the write urb pool */
1035 for (j = 0; j < NUM_URBS; ++j) {
1036 urb = usb_alloc_urb(0, GFP_KERNEL);
1037 mos7720_port->write_urb_pool[j] = urb;
1040 dev_err(&port->dev, "No more urbs???\n");
1044 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1046 if (!urb->transfer_buffer) {
1048 "%s-out of memory for urb buffers.\n",
1050 usb_free_urb(mos7720_port->write_urb_pool[j]);
1051 mos7720_port->write_urb_pool[j] = NULL;
1057 if (!allocated_urbs)
1060 /* Initialize MCS7720 -- Write Init values to corresponding Registers
1072 * 0x08 : SP1/2 Control Reg
1074 port_number = port->number - port->serial->minor;
1075 read_mos_reg(serial, port_number, LSR, &data);
1077 dbg("SS::%p LSR:%x", mos7720_port, data);
1079 dbg("Check:Sending Command ..........");
1081 write_mos_reg(serial, dummy, SP1_REG, 0x02);
1082 write_mos_reg(serial, dummy, SP2_REG, 0x02);
1084 write_mos_reg(serial, port_number, IER, 0x00);
1085 write_mos_reg(serial, port_number, FCR, 0x00);
1087 write_mos_reg(serial, port_number, FCR, 0xcf);
1088 mos7720_port->shadowLCR = 0x03;
1089 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1090 mos7720_port->shadowMCR = 0x0b;
1091 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1093 write_mos_reg(serial, port_number, SP_CONTROL_REG, 0x00);
1094 read_mos_reg(serial, dummy, SP_CONTROL_REG, &data);
1095 data = data | (port->number - port->serial->minor + 1);
1096 write_mos_reg(serial, dummy, SP_CONTROL_REG, data);
1097 mos7720_port->shadowLCR = 0x83;
1098 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1099 write_mos_reg(serial, port_number, THR, 0x0c);
1100 write_mos_reg(serial, port_number, IER, 0x00);
1101 mos7720_port->shadowLCR = 0x03;
1102 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1103 write_mos_reg(serial, port_number, IER, 0x0c);
1105 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
1107 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
1108 __func__, response);
1110 /* initialize our icount structure */
1111 memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
1113 /* initialize our port settings */
1114 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
1116 /* send a open port command */
1117 mos7720_port->open = 1;
1123 * mos7720_chars_in_buffer
1124 * this function is called by the tty driver when it wants to know how many
1125 * bytes of data we currently have outstanding in the port (data that has
1126 * been written, but hasn't made it out the port yet)
1127 * If successful, we return the number of bytes left to be written in the
1129 * Otherwise we return a negative error number.
1131 static int mos7720_chars_in_buffer(struct tty_struct *tty)
1133 struct usb_serial_port *port = tty->driver_data;
1136 struct moschip_port *mos7720_port;
1138 dbg("%s:entering ...........", __func__);
1140 mos7720_port = usb_get_serial_port_data(port);
1141 if (mos7720_port == NULL) {
1142 dbg("%s:leaving ...........", __func__);
1146 for (i = 0; i < NUM_URBS; ++i) {
1147 if (mos7720_port->write_urb_pool[i] &&
1148 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
1149 chars += URB_TRANSFER_BUFFER_SIZE;
1151 dbg("%s - returns %d", __func__, chars);
1155 static void mos7720_close(struct usb_serial_port *port)
1157 struct usb_serial *serial;
1158 struct moschip_port *mos7720_port;
1161 dbg("mos7720_close:entering...");
1163 serial = port->serial;
1165 mos7720_port = usb_get_serial_port_data(port);
1166 if (mos7720_port == NULL)
1169 for (j = 0; j < NUM_URBS; ++j)
1170 usb_kill_urb(mos7720_port->write_urb_pool[j]);
1172 /* Freeing Write URBs */
1173 for (j = 0; j < NUM_URBS; ++j) {
1174 if (mos7720_port->write_urb_pool[j]) {
1175 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
1176 usb_free_urb(mos7720_port->write_urb_pool[j]);
1180 /* While closing port, shutdown all bulk read, write *
1181 * and interrupt read if they exists, otherwise nop */
1182 dbg("Shutdown bulk write");
1183 usb_kill_urb(port->write_urb);
1184 dbg("Shutdown bulk read");
1185 usb_kill_urb(port->read_urb);
1187 mutex_lock(&serial->disc_mutex);
1188 /* these commands must not be issued if the device has
1189 * been disconnected */
1190 if (!serial->disconnected) {
1191 write_mos_reg(serial, port->number - port->serial->minor,
1193 write_mos_reg(serial, port->number - port->serial->minor,
1196 mutex_unlock(&serial->disc_mutex);
1197 mos7720_port->open = 0;
1199 dbg("Leaving %s", __func__);
1202 static void mos7720_break(struct tty_struct *tty, int break_state)
1204 struct usb_serial_port *port = tty->driver_data;
1206 struct usb_serial *serial;
1207 struct moschip_port *mos7720_port;
1209 dbg("Entering %s", __func__);
1211 serial = port->serial;
1213 mos7720_port = usb_get_serial_port_data(port);
1214 if (mos7720_port == NULL)
1217 if (break_state == -1)
1218 data = mos7720_port->shadowLCR | UART_LCR_SBC;
1220 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
1222 mos7720_port->shadowLCR = data;
1223 write_mos_reg(serial, port->number - port->serial->minor,
1224 LCR, mos7720_port->shadowLCR);
1228 * mos7720_write_room
1229 * this function is called by the tty driver when it wants to know how many
1230 * bytes of data we can accept for a specific port.
1231 * If successful, we return the amount of room that we have for this port
1232 * Otherwise we return a negative error number.
1234 static int mos7720_write_room(struct tty_struct *tty)
1236 struct usb_serial_port *port = tty->driver_data;
1237 struct moschip_port *mos7720_port;
1241 dbg("%s:entering ...........", __func__);
1243 mos7720_port = usb_get_serial_port_data(port);
1244 if (mos7720_port == NULL) {
1245 dbg("%s:leaving ...........", __func__);
1249 /* FIXME: Locking */
1250 for (i = 0; i < NUM_URBS; ++i) {
1251 if (mos7720_port->write_urb_pool[i] &&
1252 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
1253 room += URB_TRANSFER_BUFFER_SIZE;
1256 dbg("%s - returns %d", __func__, room);
1260 static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
1261 const unsigned char *data, int count)
1268 struct moschip_port *mos7720_port;
1269 struct usb_serial *serial;
1271 const unsigned char *current_position = data;
1273 dbg("%s:entering ...........", __func__);
1275 serial = port->serial;
1277 mos7720_port = usb_get_serial_port_data(port);
1278 if (mos7720_port == NULL) {
1279 dbg("mos7720_port is NULL");
1283 /* try to find a free urb in the list */
1286 for (i = 0; i < NUM_URBS; ++i) {
1287 if (mos7720_port->write_urb_pool[i] &&
1288 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
1289 urb = mos7720_port->write_urb_pool[i];
1296 dbg("%s - no more free urbs", __func__);
1300 if (urb->transfer_buffer == NULL) {
1301 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1303 if (urb->transfer_buffer == NULL) {
1304 dev_err(&port->dev, "%s no more kernel memory...\n",
1309 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1311 memcpy(urb->transfer_buffer, current_position, transfer_size);
1312 usb_serial_debug_data(debug, &port->dev, __func__, transfer_size,
1313 urb->transfer_buffer);
1315 /* fill urb with data and submit */
1316 usb_fill_bulk_urb(urb, serial->dev,
1317 usb_sndbulkpipe(serial->dev,
1318 port->bulk_out_endpointAddress),
1319 urb->transfer_buffer, transfer_size,
1320 mos7720_bulk_out_data_callback, mos7720_port);
1322 /* send it down the pipe */
1323 status = usb_submit_urb(urb, GFP_ATOMIC);
1325 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
1326 "with status = %d\n", __func__, status);
1327 bytes_sent = status;
1330 bytes_sent = transfer_size;
1336 static void mos7720_throttle(struct tty_struct *tty)
1338 struct usb_serial_port *port = tty->driver_data;
1339 struct moschip_port *mos7720_port;
1342 dbg("%s- port %d", __func__, port->number);
1344 mos7720_port = usb_get_serial_port_data(port);
1346 if (mos7720_port == NULL)
1349 if (!mos7720_port->open) {
1350 dbg("port not opened");
1354 dbg("%s: Entering ..........", __func__);
1356 /* if we are implementing XON/XOFF, send the stop character */
1358 unsigned char stop_char = STOP_CHAR(tty);
1359 status = mos7720_write(tty, port, &stop_char, 1);
1364 /* if we are implementing RTS/CTS, toggle that line */
1365 if (tty->termios->c_cflag & CRTSCTS) {
1366 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
1367 write_mos_reg(port->serial, port->number - port->serial->minor,
1368 MCR, mos7720_port->shadowMCR);
1374 static void mos7720_unthrottle(struct tty_struct *tty)
1376 struct usb_serial_port *port = tty->driver_data;
1377 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1380 if (mos7720_port == NULL)
1383 if (!mos7720_port->open) {
1384 dbg("%s - port not opened", __func__);
1388 dbg("%s: Entering ..........", __func__);
1390 /* if we are implementing XON/XOFF, send the start character */
1392 unsigned char start_char = START_CHAR(tty);
1393 status = mos7720_write(tty, port, &start_char, 1);
1398 /* if we are implementing RTS/CTS, toggle that line */
1399 if (tty->termios->c_cflag & CRTSCTS) {
1400 mos7720_port->shadowMCR |= UART_MCR_RTS;
1401 write_mos_reg(port->serial, port->number - port->serial->minor,
1402 MCR, mos7720_port->shadowMCR);
1408 /* FIXME: this function does not work */
1409 static int set_higher_rates(struct moschip_port *mos7720_port,
1412 struct usb_serial_port *port;
1413 struct usb_serial *serial;
1415 enum mos_regs sp_reg;
1416 if (mos7720_port == NULL)
1419 port = mos7720_port->port;
1420 serial = port->serial;
1422 /***********************************************
1423 * Init Sequence for higher rates
1424 ***********************************************/
1425 dbg("Sending Setting Commands ..........");
1426 port_number = port->number - port->serial->minor;
1428 write_mos_reg(serial, port_number, IER, 0x00);
1429 write_mos_reg(serial, port_number, FCR, 0x00);
1430 write_mos_reg(serial, port_number, FCR, 0xcf);
1431 mos7720_port->shadowMCR = 0x0b;
1432 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1433 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x00);
1435 /***********************************************
1436 * Set for higher rates *
1437 ***********************************************/
1438 /* writing baud rate verbatum into uart clock field clearly not right */
1439 if (port_number == 0)
1443 write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
1444 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x03);
1445 mos7720_port->shadowMCR = 0x2b;
1446 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1448 /***********************************************
1450 ***********************************************/
1451 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1452 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1453 write_mos_reg(serial, port_number, DLL, 0x01);
1454 write_mos_reg(serial, port_number, DLM, 0x00);
1455 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1456 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1461 /* baud rate information */
1462 struct divisor_table_entry {
1467 /* Define table of divisors for moschip 7720 hardware *
1468 * These assume a 3.6864MHz crystal, the standard /16, and *
1470 static struct divisor_table_entry divisor_table[] = {
1472 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
1473 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
1489 /*****************************************************************************
1490 * calc_baud_rate_divisor
1491 * this function calculates the proper baud rate divisor for the specified
1493 *****************************************************************************/
1494 static int calc_baud_rate_divisor(int baudrate, int *divisor)
1502 dbg("%s - %d", __func__, baudrate);
1504 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
1505 if (divisor_table[i].baudrate == baudrate) {
1506 *divisor = divisor_table[i].divisor;
1511 /* After trying for all the standard baud rates *
1512 * Try calculating the divisor for this baud rate */
1513 if (baudrate > 75 && baudrate < 230400) {
1514 /* get the divisor */
1515 custom = (__u16)(230400L / baudrate);
1517 /* Check for round off */
1518 round1 = (__u16)(2304000L / baudrate);
1519 round = (__u16)(round1 - (custom * 10));
1524 dbg("Baud %d = %d", baudrate, custom);
1528 dbg("Baud calculation Failed...");
1533 * send_cmd_write_baud_rate
1534 * this function sends the proper command to change the baud rate of the
1537 static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1540 struct usb_serial_port *port;
1541 struct usb_serial *serial;
1544 unsigned char number;
1546 if (mos7720_port == NULL)
1549 port = mos7720_port->port;
1550 serial = port->serial;
1552 dbg("%s: Entering ..........", __func__);
1554 number = port->number - port->serial->minor;
1555 dbg("%s - port = %d, baud = %d", __func__, port->number, baudrate);
1557 /* Calculate the Divisor */
1558 status = calc_baud_rate_divisor(baudrate, &divisor);
1560 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1564 /* Enable access to divisor latch */
1565 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1566 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
1568 /* Write the divisor */
1569 write_mos_reg(serial, number, DLL, (__u8)(divisor & 0xff));
1570 write_mos_reg(serial, number, DLM, (__u8)((divisor & 0xff00) >> 8));
1572 /* Disable access to divisor latch */
1573 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1574 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
1580 * change_port_settings
1581 * This routine is called to set the UART on the device to match
1582 * the specified new settings.
1584 static void change_port_settings(struct tty_struct *tty,
1585 struct moschip_port *mos7720_port,
1586 struct ktermios *old_termios)
1588 struct usb_serial_port *port;
1589 struct usb_serial *serial;
1600 if (mos7720_port == NULL)
1603 port = mos7720_port->port;
1604 serial = port->serial;
1605 port_number = port->number - port->serial->minor;
1607 dbg("%s - port %d", __func__, port->number);
1609 if (!mos7720_port->open) {
1610 dbg("%s - port not opened", __func__);
1614 dbg("%s: Entering ..........", __func__);
1616 lData = UART_LCR_WLEN8;
1617 lStop = 0x00; /* 1 stop bit */
1618 lParity = 0x00; /* No parity */
1620 cflag = tty->termios->c_cflag;
1621 iflag = tty->termios->c_iflag;
1623 /* Change the number of bits */
1624 switch (cflag & CSIZE) {
1626 lData = UART_LCR_WLEN5;
1631 lData = UART_LCR_WLEN6;
1636 lData = UART_LCR_WLEN7;
1641 lData = UART_LCR_WLEN8;
1645 /* Change the Parity bit */
1646 if (cflag & PARENB) {
1647 if (cflag & PARODD) {
1648 lParity = UART_LCR_PARITY;
1649 dbg("%s - parity = odd", __func__);
1651 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
1652 dbg("%s - parity = even", __func__);
1656 dbg("%s - parity = none", __func__);
1660 lParity = lParity | 0x20;
1662 /* Change the Stop bit */
1663 if (cflag & CSTOPB) {
1664 lStop = UART_LCR_STOP;
1665 dbg("%s - stop bits = 2", __func__);
1668 dbg("%s - stop bits = 1", __func__);
1671 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1672 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1673 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
1675 /* Update the LCR with the correct value */
1676 mos7720_port->shadowLCR &=
1677 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1678 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1681 /* Disable Interrupts */
1682 write_mos_reg(serial, port_number, IER, 0x00);
1683 write_mos_reg(serial, port_number, FCR, 0x00);
1684 write_mos_reg(serial, port_number, FCR, 0xcf);
1686 /* Send the updated LCR value to the mos7720 */
1687 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1688 mos7720_port->shadowMCR = 0x0b;
1689 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1691 /* set up the MCR register and send it to the mos7720 */
1692 mos7720_port->shadowMCR = UART_MCR_OUT2;
1694 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1696 if (cflag & CRTSCTS) {
1697 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
1698 /* To set hardware flow control to the specified *
1699 * serial port, in SP1/2_CONTROL_REG */
1701 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x01);
1703 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x02);
1706 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1708 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1710 /* Determine divisor based on baud rate */
1711 baud = tty_get_baud_rate(tty);
1713 /* pick a default, any default... */
1714 dbg("Picked default baud...");
1718 if (baud >= 230400) {
1719 set_higher_rates(mos7720_port, baud);
1720 /* Enable Interrupts */
1721 write_mos_reg(serial, port_number, IER, 0x0c);
1725 dbg("%s - baud rate = %d", __func__, baud);
1726 status = send_cmd_write_baud_rate(mos7720_port, baud);
1727 /* FIXME: needs to write actual resulting baud back not just
1730 tty_encode_baud_rate(tty, baud, baud);
1731 /* Enable Interrupts */
1732 write_mos_reg(serial, port_number, IER, 0x0c);
1734 if (port->read_urb->status != -EINPROGRESS) {
1735 port->read_urb->dev = serial->dev;
1737 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1739 dbg("usb_submit_urb(read bulk) failed, status = %d",
1745 * mos7720_set_termios
1746 * this function is called by the tty driver when it wants to change the
1747 * termios structure.
1749 static void mos7720_set_termios(struct tty_struct *tty,
1750 struct usb_serial_port *port, struct ktermios *old_termios)
1754 struct usb_serial *serial;
1755 struct moschip_port *mos7720_port;
1757 serial = port->serial;
1759 mos7720_port = usb_get_serial_port_data(port);
1761 if (mos7720_port == NULL)
1764 if (!mos7720_port->open) {
1765 dbg("%s - port not opened", __func__);
1769 dbg("%s\n", "setting termios - ASPIRE");
1771 cflag = tty->termios->c_cflag;
1773 dbg("%s - cflag %08x iflag %08x", __func__,
1774 tty->termios->c_cflag,
1775 RELEVANT_IFLAG(tty->termios->c_iflag));
1777 dbg("%s - old cflag %08x old iflag %08x", __func__,
1778 old_termios->c_cflag,
1779 RELEVANT_IFLAG(old_termios->c_iflag));
1781 dbg("%s - port %d", __func__, port->number);
1783 /* change the port settings to the new ones specified */
1784 change_port_settings(tty, mos7720_port, old_termios);
1786 if (!port->read_urb) {
1787 dbg("%s", "URB KILLED !!!!!");
1791 if (port->read_urb->status != -EINPROGRESS) {
1792 port->read_urb->dev = serial->dev;
1793 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1795 dbg("usb_submit_urb(read bulk) failed, status = %d",
1801 * get_lsr_info - get line status register info
1803 * Purpose: Let user call ioctl() to get info when the UART physically
1804 * is emptied. On bus types like RS485, the transmitter must
1805 * release the bus after transmitting. This must be done when
1806 * the transmit shift register is empty, not be done when the
1807 * transmit holding register is empty. This functionality
1808 * allows an RS485 driver to be written in user space.
1810 static int get_lsr_info(struct tty_struct *tty,
1811 struct moschip_port *mos7720_port, unsigned int __user *value)
1813 struct usb_serial_port *port = tty->driver_data;
1814 unsigned int result = 0;
1815 unsigned char data = 0;
1816 int port_number = port->number - port->serial->minor;
1819 count = mos7720_chars_in_buffer(tty);
1821 read_mos_reg(port->serial, port_number, LSR, &data);
1822 if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1823 == (UART_LSR_TEMT | UART_LSR_THRE)) {
1824 dbg("%s -- Empty", __func__);
1825 result = TIOCSER_TEMT;
1828 if (copy_to_user(value, &result, sizeof(int)))
1833 static int mos7720_tiocmget(struct tty_struct *tty)
1835 struct usb_serial_port *port = tty->driver_data;
1836 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1837 unsigned int result = 0;
1841 dbg("%s - port %d", __func__, port->number);
1843 mcr = mos7720_port->shadowMCR;
1844 msr = mos7720_port->shadowMSR;
1846 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1847 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1848 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1849 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
1850 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1851 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
1853 dbg("%s -- %x", __func__, result);
1858 static int mos7720_tiocmset(struct tty_struct *tty,
1859 unsigned int set, unsigned int clear)
1861 struct usb_serial_port *port = tty->driver_data;
1862 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1864 dbg("%s - port %d", __func__, port->number);
1865 dbg("he was at tiocmset");
1867 mcr = mos7720_port->shadowMCR;
1869 if (set & TIOCM_RTS)
1870 mcr |= UART_MCR_RTS;
1871 if (set & TIOCM_DTR)
1872 mcr |= UART_MCR_DTR;
1873 if (set & TIOCM_LOOP)
1874 mcr |= UART_MCR_LOOP;
1876 if (clear & TIOCM_RTS)
1877 mcr &= ~UART_MCR_RTS;
1878 if (clear & TIOCM_DTR)
1879 mcr &= ~UART_MCR_DTR;
1880 if (clear & TIOCM_LOOP)
1881 mcr &= ~UART_MCR_LOOP;
1883 mos7720_port->shadowMCR = mcr;
1884 write_mos_reg(port->serial, port->number - port->serial->minor,
1885 MCR, mos7720_port->shadowMCR);
1890 static int mos7720_get_icount(struct tty_struct *tty,
1891 struct serial_icounter_struct *icount)
1893 struct usb_serial_port *port = tty->driver_data;
1894 struct moschip_port *mos7720_port;
1895 struct async_icount cnow;
1897 mos7720_port = usb_get_serial_port_data(port);
1898 cnow = mos7720_port->icount;
1900 icount->cts = cnow.cts;
1901 icount->dsr = cnow.dsr;
1902 icount->rng = cnow.rng;
1903 icount->dcd = cnow.dcd;
1904 icount->rx = cnow.rx;
1905 icount->tx = cnow.tx;
1906 icount->frame = cnow.frame;
1907 icount->overrun = cnow.overrun;
1908 icount->parity = cnow.parity;
1909 icount->brk = cnow.brk;
1910 icount->buf_overrun = cnow.buf_overrun;
1912 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
1913 port->number, icount->rx, icount->tx);
1917 static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1918 unsigned int __user *value)
1923 struct usb_serial_port *port;
1925 if (mos7720_port == NULL)
1928 port = (struct usb_serial_port *)mos7720_port->port;
1929 mcr = mos7720_port->shadowMCR;
1931 if (copy_from_user(&arg, value, sizeof(int)))
1936 if (arg & TIOCM_RTS)
1937 mcr |= UART_MCR_RTS;
1938 if (arg & TIOCM_DTR)
1939 mcr |= UART_MCR_RTS;
1940 if (arg & TIOCM_LOOP)
1941 mcr |= UART_MCR_LOOP;
1945 if (arg & TIOCM_RTS)
1946 mcr &= ~UART_MCR_RTS;
1947 if (arg & TIOCM_DTR)
1948 mcr &= ~UART_MCR_RTS;
1949 if (arg & TIOCM_LOOP)
1950 mcr &= ~UART_MCR_LOOP;
1955 mos7720_port->shadowMCR = mcr;
1956 write_mos_reg(port->serial, port->number - port->serial->minor,
1957 MCR, mos7720_port->shadowMCR);
1962 static int get_serial_info(struct moschip_port *mos7720_port,
1963 struct serial_struct __user *retinfo)
1965 struct serial_struct tmp;
1970 memset(&tmp, 0, sizeof(tmp));
1972 tmp.type = PORT_16550A;
1973 tmp.line = mos7720_port->port->serial->minor;
1974 tmp.port = mos7720_port->port->number;
1976 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1977 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1978 tmp.baud_base = 9600;
1979 tmp.close_delay = 5*HZ;
1980 tmp.closing_wait = 30*HZ;
1982 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1987 static int mos7720_ioctl(struct tty_struct *tty,
1988 unsigned int cmd, unsigned long arg)
1990 struct usb_serial_port *port = tty->driver_data;
1991 struct moschip_port *mos7720_port;
1992 struct async_icount cnow;
1993 struct async_icount cprev;
1995 mos7720_port = usb_get_serial_port_data(port);
1996 if (mos7720_port == NULL)
1999 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
2003 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
2004 return get_lsr_info(tty, mos7720_port,
2005 (unsigned int __user *)arg);
2007 /* FIXME: These should be using the mode methods */
2010 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET",
2011 __func__, port->number);
2012 return set_modem_info(mos7720_port, cmd,
2013 (unsigned int __user *)arg);
2016 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
2017 return get_serial_info(mos7720_port,
2018 (struct serial_struct __user *)arg);
2021 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
2022 cprev = mos7720_port->icount;
2024 if (signal_pending(current))
2025 return -ERESTARTSYS;
2026 cnow = mos7720_port->icount;
2027 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2028 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2029 return -EIO; /* no change => error */
2030 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2031 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2032 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2033 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2042 return -ENOIOCTLCMD;
2045 static int mos7720_startup(struct usb_serial *serial)
2047 struct moschip_port *mos7720_port;
2048 struct usb_device *dev;
2054 dbg("%s: Entering ..........", __func__);
2057 dbg("Invalid Handler");
2061 if (serial->num_bulk_in < 2 || serial->num_bulk_out < 2) {
2062 dev_err(&serial->interface->dev, "missing bulk endpoints\n");
2066 product = le16_to_cpu(serial->dev->descriptor.idProduct);
2070 * The 7715 uses the first bulk in/out endpoint pair for the parallel
2071 * port, and the second for the serial port. Because the usbserial core
2072 * assumes both pairs are serial ports, we must engage in a bit of
2073 * subterfuge and swap the pointers for ports 0 and 1 in order to make
2074 * port 0 point to the serial port. However, both moschip devices use a
2075 * single interrupt-in endpoint for both ports (as mentioned a little
2076 * further down), and this endpoint was assigned to port 0. So after
2077 * the swap, we must copy the interrupt endpoint elements from port 1
2078 * (as newly assigned) to port 0, and null out port 1 pointers.
2080 if (product == MOSCHIP_DEVICE_ID_7715) {
2081 struct usb_serial_port *tmp = serial->port[0];
2082 serial->port[0] = serial->port[1];
2083 serial->port[1] = tmp;
2084 serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb;
2085 serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer;
2086 serial->port[0]->interrupt_in_endpointAddress =
2087 tmp->interrupt_in_endpointAddress;
2088 serial->port[1]->interrupt_in_urb = NULL;
2089 serial->port[1]->interrupt_in_buffer = NULL;
2091 if (serial->port[0]->interrupt_in_urb) {
2092 struct urb *urb = serial->port[0]->interrupt_in_urb;
2094 urb->complete = mos7715_interrupt_callback;
2099 /* set up serial port private structures */
2100 for (i = 0; i < serial->num_ports; ++i) {
2101 mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2102 if (mos7720_port == NULL) {
2103 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
2107 /* Initialize all port interrupt end point to port 0 int
2108 * endpoint. Our device has only one interrupt endpoint
2109 * common to all ports */
2110 serial->port[i]->interrupt_in_endpointAddress =
2111 serial->port[0]->interrupt_in_endpointAddress;
2113 mos7720_port->port = serial->port[i];
2114 usb_set_serial_port_data(serial->port[i], mos7720_port);
2116 dbg("port number is %d", serial->port[i]->number);
2117 dbg("serial number is %d", serial->minor);
2121 /* setting configuration feature to one */
2122 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2123 (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5000);
2125 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
2126 if (product == MOSCHIP_DEVICE_ID_7715) {
2127 ret_val = mos7715_parport_init(serial);
2132 /* start the interrupt urb */
2133 ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
2135 dev_err(&dev->dev, "failed to submit interrupt urb: %d\n",
2139 /* LSR For Port 1 */
2140 read_mos_reg(serial, 0, LSR, &data);
2141 dbg("LSR:%x", data);
2146 static void mos7720_release(struct usb_serial *serial)
2150 usb_kill_urb(serial->port[0]->interrupt_in_urb);
2152 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
2153 /* close the parallel port */
2155 if (le16_to_cpu(serial->dev->descriptor.idProduct)
2156 == MOSCHIP_DEVICE_ID_7715) {
2157 struct urbtracker *urbtrack;
2158 unsigned long flags;
2159 struct mos7715_parport *mos_parport =
2160 usb_get_serial_data(serial);
2162 /* prevent NULL ptr dereference in port callbacks */
2163 spin_lock(&release_lock);
2164 mos_parport->pp->private_data = NULL;
2165 spin_unlock(&release_lock);
2167 /* wait for synchronous usb calls to return */
2168 if (mos_parport->msg_pending)
2169 wait_for_completion_timeout(&mos_parport->syncmsg_compl,
2170 msecs_to_jiffies(MOS_WDR_TIMEOUT));
2172 parport_remove_port(mos_parport->pp);
2173 usb_set_serial_data(serial, NULL);
2174 mos_parport->serial = NULL;
2176 /* if tasklet currently scheduled, wait for it to complete */
2177 tasklet_kill(&mos_parport->urb_tasklet);
2179 /* unlink any urbs sent by the tasklet */
2180 spin_lock_irqsave(&mos_parport->listlock, flags);
2181 list_for_each_entry(urbtrack,
2182 &mos_parport->active_urbs,
2184 usb_unlink_urb(urbtrack->urb);
2185 spin_unlock_irqrestore(&mos_parport->listlock, flags);
2187 kref_put(&mos_parport->ref_count, destroy_mos_parport);
2190 /* free private structure allocated for serial port */
2191 for (i = 0; i < serial->num_ports; ++i)
2192 kfree(usb_get_serial_port_data(serial->port[i]));
2195 static struct usb_driver usb_driver = {
2196 .name = "moschip7720",
2197 .probe = usb_serial_probe,
2198 .disconnect = usb_serial_disconnect,
2199 .id_table = moschip_port_id_table,
2203 static struct usb_serial_driver moschip7720_2port_driver = {
2205 .owner = THIS_MODULE,
2206 .name = "moschip7720",
2208 .description = "Moschip 2 port adapter",
2209 .usb_driver = &usb_driver,
2210 .id_table = moschip_port_id_table,
2211 .calc_num_ports = mos77xx_calc_num_ports,
2212 .open = mos7720_open,
2213 .close = mos7720_close,
2214 .throttle = mos7720_throttle,
2215 .unthrottle = mos7720_unthrottle,
2216 .attach = mos7720_startup,
2217 .release = mos7720_release,
2218 .ioctl = mos7720_ioctl,
2219 .tiocmget = mos7720_tiocmget,
2220 .tiocmset = mos7720_tiocmset,
2221 .get_icount = mos7720_get_icount,
2222 .set_termios = mos7720_set_termios,
2223 .write = mos7720_write,
2224 .write_room = mos7720_write_room,
2225 .chars_in_buffer = mos7720_chars_in_buffer,
2226 .break_ctl = mos7720_break,
2227 .read_bulk_callback = mos7720_bulk_in_callback,
2228 .read_int_callback = mos7720_interrupt_callback,
2231 static int __init moschip7720_init(void)
2235 dbg("%s: Entering ..........", __func__);
2237 /* Register with the usb serial */
2238 retval = usb_serial_register(&moschip7720_2port_driver);
2240 goto failed_port_device_register;
2242 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
2245 /* Register with the usb */
2246 retval = usb_register(&usb_driver);
2248 goto failed_usb_register;
2252 failed_usb_register:
2253 usb_serial_deregister(&moschip7720_2port_driver);
2255 failed_port_device_register:
2259 static void __exit moschip7720_exit(void)
2261 usb_deregister(&usb_driver);
2262 usb_serial_deregister(&moschip7720_2port_driver);
2265 module_init(moschip7720_init);
2266 module_exit(moschip7720_exit);
2268 /* Module information */
2269 MODULE_AUTHOR(DRIVER_AUTHOR);
2270 MODULE_DESCRIPTION(DRIVER_DESC);
2271 MODULE_LICENSE("GPL");
2273 module_param(debug, bool, S_IRUGO | S_IWUSR);
2274 MODULE_PARM_DESC(debug, "Debug enabled or not");