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;
100 struct usb_ctrlrequest *setup;
103 enum mos7715_pp_modes {
105 PS2 = 1<<5, /* moschip calls this 'NIBBLE' mode */
106 PPF = 2<<5, /* moschip calls this 'CB-FIFO mode */
109 struct mos7715_parport {
110 struct parport *pp; /* back to containing struct */
111 struct kref ref_count; /* to instance of this struct */
112 struct list_head deferred_urbs; /* list deferred async urbs */
113 struct list_head active_urbs; /* list async urbs in flight */
114 spinlock_t listlock; /* protects list access */
115 bool msg_pending; /* usb sync call pending */
116 struct completion syncmsg_compl; /* usb sync call completed */
117 struct tasklet_struct urb_tasklet; /* for sending deferred urbs */
118 struct usb_serial *serial; /* back to containing struct */
119 __u8 shadowECR; /* parallel port regs... */
121 atomic_t shadowDSR; /* updated in int-in callback */
124 /* lock guards against dereferencing NULL ptr in parport ops callbacks */
125 static DEFINE_SPINLOCK(release_lock);
127 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
129 static const unsigned int dummy; /* for clarity in register access fns */
132 THR, /* serial port regs */
144 DPR, /* parallel port regs */
148 SP1_REG, /* device control regs */
149 SP2_REG, /* serial port 2 (7720 only) */
155 * Return the correct value for the Windex field of the setup packet
156 * for a control endpoint message. See the 7715 datasheet.
158 static inline __u16 get_reg_index(enum mos_regs reg)
160 static const __u16 mos7715_index_lookup_table[] = {
178 0x02, /* SP2_REG (7720 only) */
179 0x04, /* PP_REG (7715 only) */
180 0x08, /* SP_CONTROL_REG */
182 return mos7715_index_lookup_table[reg];
186 * Return the correct value for the upper byte of the Wvalue field of
187 * the setup packet for a control endpoint message.
189 static inline __u16 get_reg_value(enum mos_regs reg,
190 unsigned int serial_portnum)
192 if (reg >= SP1_REG) /* control reg */
195 else if (reg >= DPR) /* parallel port reg (7715 only) */
198 else /* serial port reg */
199 return (serial_portnum + 2) << 8;
203 * Write data byte to the specified device register. The data is embedded in
204 * the value field of the setup packet. serial_portnum is ignored for registers
205 * not specific to a particular serial port.
207 static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
208 enum mos_regs reg, __u8 data)
210 struct usb_device *usbdev = serial->dev;
211 unsigned int pipe = usb_sndctrlpipe(usbdev, 0);
212 __u8 request = (__u8)0x0e;
213 __u8 requesttype = (__u8)0x40;
214 __u16 index = get_reg_index(reg);
215 __u16 value = get_reg_value(reg, serial_portnum) + data;
216 int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
217 index, NULL, 0, MOS_WDR_TIMEOUT);
219 dev_err(&usbdev->dev,
220 "mos7720: usb_control_msg() failed: %d", status);
225 * Read data byte from the specified device register. The data returned by the
226 * device is embedded in the value field of the setup packet. serial_portnum is
227 * ignored for registers that are not specific to a particular serial port.
229 static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
230 enum mos_regs reg, __u8 *data)
232 struct usb_device *usbdev = serial->dev;
233 unsigned int pipe = usb_rcvctrlpipe(usbdev, 0);
234 __u8 request = (__u8)0x0d;
235 __u8 requesttype = (__u8)0xc0;
236 __u16 index = get_reg_index(reg);
237 __u16 value = get_reg_value(reg, serial_portnum);
241 buf = kmalloc(1, GFP_KERNEL);
245 status = usb_control_msg(usbdev, pipe, request, requesttype, value,
246 index, buf, 1, MOS_WDR_TIMEOUT);
250 dev_err(&usbdev->dev,
251 "mos7720: usb_control_msg() failed: %d", status);
257 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
259 static inline int mos7715_change_mode(struct mos7715_parport *mos_parport,
260 enum mos7715_pp_modes mode)
262 mos_parport->shadowECR = mode;
263 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
267 static void destroy_mos_parport(struct kref *kref)
269 struct mos7715_parport *mos_parport =
270 container_of(kref, struct mos7715_parport, ref_count);
272 dbg("%s called", __func__);
276 static void destroy_urbtracker(struct kref *kref)
278 struct urbtracker *urbtrack =
279 container_of(kref, struct urbtracker, ref_count);
280 struct mos7715_parport *mos_parport = urbtrack->mos_parport;
281 dbg("%s called", __func__);
282 usb_free_urb(urbtrack->urb);
283 kfree(urbtrack->setup);
285 kref_put(&mos_parport->ref_count, destroy_mos_parport);
289 * This runs as a tasklet when sending an urb in a non-blocking parallel
290 * port callback had to be deferred because the disconnect mutex could not be
291 * obtained at the time.
293 static void send_deferred_urbs(unsigned long _mos_parport)
297 struct mos7715_parport *mos_parport = (void *)_mos_parport;
298 struct urbtracker *urbtrack;
299 struct list_head *cursor, *next;
301 dbg("%s called", __func__);
303 /* if release function ran, game over */
304 if (unlikely(mos_parport->serial == NULL))
307 /* try again to get the mutex */
308 if (!mutex_trylock(&mos_parport->serial->disc_mutex)) {
309 dbg("%s: rescheduling tasklet", __func__);
310 tasklet_schedule(&mos_parport->urb_tasklet);
314 /* if device disconnected, game over */
315 if (unlikely(mos_parport->serial->disconnected)) {
316 mutex_unlock(&mos_parport->serial->disc_mutex);
320 spin_lock_irqsave(&mos_parport->listlock, flags);
321 if (list_empty(&mos_parport->deferred_urbs)) {
322 spin_unlock_irqrestore(&mos_parport->listlock, flags);
323 mutex_unlock(&mos_parport->serial->disc_mutex);
324 dbg("%s: deferred_urbs list empty", __func__);
328 /* move contents of deferred_urbs list to active_urbs list and submit */
329 list_for_each_safe(cursor, next, &mos_parport->deferred_urbs)
330 list_move_tail(cursor, &mos_parport->active_urbs);
331 list_for_each_entry(urbtrack, &mos_parport->active_urbs,
333 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
334 dbg("%s: urb submitted", __func__);
336 dev_err(&mos_parport->serial->dev->dev,
337 "usb_submit_urb() failed: %d", ret_val);
338 list_del(&urbtrack->urblist_entry);
339 kref_put(&urbtrack->ref_count, destroy_urbtracker);
342 spin_unlock_irqrestore(&mos_parport->listlock, flags);
343 mutex_unlock(&mos_parport->serial->disc_mutex);
346 /* callback for parallel port control urbs submitted asynchronously */
347 static void async_complete(struct urb *urb)
349 struct urbtracker *urbtrack = urb->context;
350 int status = urb->status;
351 dbg("%s called", __func__);
352 if (unlikely(status))
353 dbg("%s - nonzero urb status received: %d", __func__, status);
355 /* remove the urbtracker from the active_urbs list */
356 spin_lock(&urbtrack->mos_parport->listlock);
357 list_del(&urbtrack->urblist_entry);
358 spin_unlock(&urbtrack->mos_parport->listlock);
359 kref_put(&urbtrack->ref_count, destroy_urbtracker);
362 static int write_parport_reg_nonblock(struct mos7715_parport *mos_parport,
363 enum mos_regs reg, __u8 data)
365 struct urbtracker *urbtrack;
368 struct usb_serial *serial = mos_parport->serial;
369 struct usb_device *usbdev = serial->dev;
370 dbg("%s called", __func__);
372 /* create and initialize the control urb and containing urbtracker */
373 urbtrack = kmalloc(sizeof(struct urbtracker), GFP_ATOMIC);
374 if (urbtrack == NULL) {
375 dev_err(&usbdev->dev, "out of memory");
378 kref_get(&mos_parport->ref_count);
379 urbtrack->mos_parport = mos_parport;
380 urbtrack->urb = usb_alloc_urb(0, GFP_ATOMIC);
381 if (urbtrack->urb == NULL) {
382 dev_err(&usbdev->dev, "out of urbs");
386 urbtrack->setup = kmalloc(sizeof(*urbtrack->setup), GFP_ATOMIC);
387 if (!urbtrack->setup) {
388 usb_free_urb(urbtrack->urb);
392 urbtrack->setup->bRequestType = (__u8)0x40;
393 urbtrack->setup->bRequest = (__u8)0x0e;
394 urbtrack->setup->wValue = cpu_to_le16(get_reg_value(reg, dummy));
395 urbtrack->setup->wIndex = cpu_to_le16(get_reg_index(reg));
396 urbtrack->setup->wLength = 0;
397 usb_fill_control_urb(urbtrack->urb, usbdev,
398 usb_sndctrlpipe(usbdev, 0),
399 (unsigned char *)urbtrack->setup,
400 NULL, 0, async_complete, urbtrack);
401 kref_init(&urbtrack->ref_count);
402 INIT_LIST_HEAD(&urbtrack->urblist_entry);
405 * get the disconnect mutex, or add tracker to the deferred_urbs list
406 * and schedule a tasklet to try again later
408 if (!mutex_trylock(&serial->disc_mutex)) {
409 spin_lock_irqsave(&mos_parport->listlock, flags);
410 list_add_tail(&urbtrack->urblist_entry,
411 &mos_parport->deferred_urbs);
412 spin_unlock_irqrestore(&mos_parport->listlock, flags);
413 tasklet_schedule(&mos_parport->urb_tasklet);
414 dbg("tasklet scheduled");
418 /* bail if device disconnected */
419 if (serial->disconnected) {
420 kref_put(&urbtrack->ref_count, destroy_urbtracker);
421 mutex_unlock(&serial->disc_mutex);
425 /* add the tracker to the active_urbs list and submit */
426 spin_lock_irqsave(&mos_parport->listlock, flags);
427 list_add_tail(&urbtrack->urblist_entry, &mos_parport->active_urbs);
428 spin_unlock_irqrestore(&mos_parport->listlock, flags);
429 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
430 mutex_unlock(&serial->disc_mutex);
432 dev_err(&usbdev->dev,
433 "%s: submit_urb() failed: %d", __func__, ret_val);
434 spin_lock_irqsave(&mos_parport->listlock, flags);
435 list_del(&urbtrack->urblist_entry);
436 spin_unlock_irqrestore(&mos_parport->listlock, flags);
437 kref_put(&urbtrack->ref_count, destroy_urbtracker);
444 * This is the the common top part of all parallel port callback operations that
445 * send synchronous messages to the device. This implements convoluted locking
446 * that avoids two scenarios: (1) a port operation is called after usbserial
447 * has called our release function, at which point struct mos7715_parport has
448 * been destroyed, and (2) the device has been disconnected, but usbserial has
449 * not called the release function yet because someone has a serial port open.
450 * The shared release_lock prevents the first, and the mutex and disconnected
451 * flag maintained by usbserial covers the second. We also use the msg_pending
452 * flag to ensure that all synchronous usb messgage calls have completed before
453 * our release function can return.
455 static int parport_prologue(struct parport *pp)
457 struct mos7715_parport *mos_parport;
459 spin_lock(&release_lock);
460 mos_parport = pp->private_data;
461 if (unlikely(mos_parport == NULL)) {
462 /* release fn called, port struct destroyed */
463 spin_unlock(&release_lock);
466 mos_parport->msg_pending = true; /* synch usb call pending */
467 INIT_COMPLETION(mos_parport->syncmsg_compl);
468 spin_unlock(&release_lock);
470 mutex_lock(&mos_parport->serial->disc_mutex);
471 if (mos_parport->serial->disconnected) {
472 /* device disconnected */
473 mutex_unlock(&mos_parport->serial->disc_mutex);
474 mos_parport->msg_pending = false;
475 complete(&mos_parport->syncmsg_compl);
483 * This is the the common bottom part of all parallel port functions that send
484 * synchronous messages to the device.
486 static inline void parport_epilogue(struct parport *pp)
488 struct mos7715_parport *mos_parport = pp->private_data;
489 mutex_unlock(&mos_parport->serial->disc_mutex);
490 mos_parport->msg_pending = false;
491 complete(&mos_parport->syncmsg_compl);
494 static void parport_mos7715_write_data(struct parport *pp, unsigned char d)
496 struct mos7715_parport *mos_parport = pp->private_data;
497 dbg("%s called: %2.2x", __func__, d);
498 if (parport_prologue(pp) < 0)
500 mos7715_change_mode(mos_parport, SPP);
501 write_mos_reg(mos_parport->serial, dummy, DPR, (__u8)d);
502 parport_epilogue(pp);
505 static unsigned char parport_mos7715_read_data(struct parport *pp)
507 struct mos7715_parport *mos_parport = pp->private_data;
509 dbg("%s called", __func__);
510 if (parport_prologue(pp) < 0)
512 read_mos_reg(mos_parport->serial, dummy, DPR, &d);
513 parport_epilogue(pp);
517 static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
519 struct mos7715_parport *mos_parport = pp->private_data;
521 dbg("%s called: %2.2x", __func__, d);
522 if (parport_prologue(pp) < 0)
524 data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
525 write_mos_reg(mos_parport->serial, dummy, DCR, data);
526 mos_parport->shadowDCR = data;
527 parport_epilogue(pp);
530 static unsigned char parport_mos7715_read_control(struct parport *pp)
532 struct mos7715_parport *mos_parport = pp->private_data;
534 dbg("%s called", __func__);
535 spin_lock(&release_lock);
536 mos_parport = pp->private_data;
537 if (unlikely(mos_parport == NULL)) {
538 spin_unlock(&release_lock);
541 dcr = mos_parport->shadowDCR & 0x0f;
542 spin_unlock(&release_lock);
546 static unsigned char parport_mos7715_frob_control(struct parport *pp,
550 struct mos7715_parport *mos_parport = pp->private_data;
552 dbg("%s called", __func__);
555 if (parport_prologue(pp) < 0)
557 mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
558 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
559 dcr = mos_parport->shadowDCR & 0x0f;
560 parport_epilogue(pp);
564 static unsigned char parport_mos7715_read_status(struct parport *pp)
566 unsigned char status;
567 struct mos7715_parport *mos_parport = pp->private_data;
568 dbg("%s called", __func__);
569 spin_lock(&release_lock);
570 mos_parport = pp->private_data;
571 if (unlikely(mos_parport == NULL)) { /* release called */
572 spin_unlock(&release_lock);
575 status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
576 spin_unlock(&release_lock);
580 static void parport_mos7715_enable_irq(struct parport *pp)
582 dbg("%s called", __func__);
584 static void parport_mos7715_disable_irq(struct parport *pp)
586 dbg("%s called", __func__);
589 static void parport_mos7715_data_forward(struct parport *pp)
591 struct mos7715_parport *mos_parport = pp->private_data;
592 dbg("%s called", __func__);
593 if (parport_prologue(pp) < 0)
595 mos7715_change_mode(mos_parport, PS2);
596 mos_parport->shadowDCR &= ~0x20;
597 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
598 parport_epilogue(pp);
601 static void parport_mos7715_data_reverse(struct parport *pp)
603 struct mos7715_parport *mos_parport = pp->private_data;
604 dbg("%s called", __func__);
605 if (parport_prologue(pp) < 0)
607 mos7715_change_mode(mos_parport, PS2);
608 mos_parport->shadowDCR |= 0x20;
609 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
610 parport_epilogue(pp);
613 static void parport_mos7715_init_state(struct pardevice *dev,
614 struct parport_state *s)
616 dbg("%s called", __func__);
617 s->u.pc.ctr = DCR_INIT_VAL;
618 s->u.pc.ecr = ECR_INIT_VAL;
621 /* N.B. Parport core code requires that this function not block */
622 static void parport_mos7715_save_state(struct parport *pp,
623 struct parport_state *s)
625 struct mos7715_parport *mos_parport;
626 dbg("%s called", __func__);
627 spin_lock(&release_lock);
628 mos_parport = pp->private_data;
629 if (unlikely(mos_parport == NULL)) { /* release called */
630 spin_unlock(&release_lock);
633 s->u.pc.ctr = mos_parport->shadowDCR;
634 s->u.pc.ecr = mos_parport->shadowECR;
635 spin_unlock(&release_lock);
638 /* N.B. Parport core code requires that this function not block */
639 static void parport_mos7715_restore_state(struct parport *pp,
640 struct parport_state *s)
642 struct mos7715_parport *mos_parport;
643 dbg("%s called", __func__);
644 spin_lock(&release_lock);
645 mos_parport = pp->private_data;
646 if (unlikely(mos_parport == NULL)) { /* release called */
647 spin_unlock(&release_lock);
650 write_parport_reg_nonblock(mos_parport, DCR, mos_parport->shadowDCR);
651 write_parport_reg_nonblock(mos_parport, ECR, mos_parport->shadowECR);
652 spin_unlock(&release_lock);
655 static size_t parport_mos7715_write_compat(struct parport *pp,
657 size_t len, int flags)
660 struct mos7715_parport *mos_parport = pp->private_data;
662 dbg("%s called: %u chars", __func__, (unsigned int)len);
663 if (parport_prologue(pp) < 0)
665 mos7715_change_mode(mos_parport, PPF);
666 retval = usb_bulk_msg(mos_parport->serial->dev,
667 usb_sndbulkpipe(mos_parport->serial->dev, 2),
668 (void *)buffer, len, &actual_len,
670 parport_epilogue(pp);
672 dev_err(&mos_parport->serial->dev->dev,
673 "mos7720: usb_bulk_msg() failed: %d", retval);
679 static struct parport_operations parport_mos7715_ops = {
680 .owner = THIS_MODULE,
681 .write_data = parport_mos7715_write_data,
682 .read_data = parport_mos7715_read_data,
684 .write_control = parport_mos7715_write_control,
685 .read_control = parport_mos7715_read_control,
686 .frob_control = parport_mos7715_frob_control,
688 .read_status = parport_mos7715_read_status,
690 .enable_irq = parport_mos7715_enable_irq,
691 .disable_irq = parport_mos7715_disable_irq,
693 .data_forward = parport_mos7715_data_forward,
694 .data_reverse = parport_mos7715_data_reverse,
696 .init_state = parport_mos7715_init_state,
697 .save_state = parport_mos7715_save_state,
698 .restore_state = parport_mos7715_restore_state,
700 .compat_write_data = parport_mos7715_write_compat,
702 .nibble_read_data = parport_ieee1284_read_nibble,
703 .byte_read_data = parport_ieee1284_read_byte,
707 * Allocate and initialize parallel port control struct, initialize
708 * the parallel port hardware device, and register with the parport subsystem.
710 static int mos7715_parport_init(struct usb_serial *serial)
712 struct mos7715_parport *mos_parport;
714 /* allocate and initialize parallel port control struct */
715 mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
716 if (mos_parport == NULL) {
717 dbg("mos7715_parport_init: kzalloc failed");
720 mos_parport->msg_pending = false;
721 kref_init(&mos_parport->ref_count);
722 spin_lock_init(&mos_parport->listlock);
723 INIT_LIST_HEAD(&mos_parport->active_urbs);
724 INIT_LIST_HEAD(&mos_parport->deferred_urbs);
725 usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
726 mos_parport->serial = serial;
727 tasklet_init(&mos_parport->urb_tasklet, send_deferred_urbs,
728 (unsigned long) mos_parport);
729 init_completion(&mos_parport->syncmsg_compl);
731 /* cycle parallel port reset bit */
732 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x80);
733 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x00);
735 /* initialize device registers */
736 mos_parport->shadowDCR = DCR_INIT_VAL;
737 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
738 mos_parport->shadowECR = ECR_INIT_VAL;
739 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
741 /* register with parport core */
742 mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE,
744 &parport_mos7715_ops);
745 if (mos_parport->pp == NULL) {
746 dev_err(&serial->interface->dev,
747 "Could not register parport\n");
748 kref_put(&mos_parport->ref_count, destroy_mos_parport);
751 mos_parport->pp->private_data = mos_parport;
752 mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP;
753 mos_parport->pp->dev = &serial->interface->dev;
754 parport_announce_port(mos_parport->pp);
758 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
761 * mos7720_interrupt_callback
762 * this is the callback function for when we have received data on the
763 * interrupt endpoint.
765 static void mos7720_interrupt_callback(struct urb *urb)
769 int status = urb->status;
781 /* this urb is terminated, clean up */
782 dbg("%s - urb shutting down with status: %d", __func__,
786 dbg("%s - nonzero urb status received: %d", __func__,
791 length = urb->actual_length;
792 data = urb->transfer_buffer;
794 /* Moschip get 4 bytes
795 * Byte 1 IIR Port 1 (port.number is 0)
796 * Byte 2 IIR Port 2 (port.number is 1)
797 * Byte 3 --------------
798 * Byte 4 FIFO status for both */
800 /* the above description is inverted
801 * oneukum 2007-03-14 */
803 if (unlikely(length != 4)) {
804 dbg("Wrong data !!!");
811 if ((sp1 | sp2) & 0x01) {
812 /* No Interrupt Pending in both the ports */
813 dbg("No Interrupt !!!");
815 switch (sp1 & 0x0f) {
817 dbg("Serial Port 1: Receiver status error or address "
818 "bit detected in 9-bit mode\n");
821 dbg("Serial Port 1: Receiver time out");
824 /* dbg("Serial Port 1: Modem status change"); */
828 switch (sp2 & 0x0f) {
830 dbg("Serial Port 2: Receiver status error or address "
831 "bit detected in 9-bit mode");
834 dbg("Serial Port 2: Receiver time out");
837 /* dbg("Serial Port 2: Modem status change"); */
843 result = usb_submit_urb(urb, GFP_ATOMIC);
845 dev_err(&urb->dev->dev,
846 "%s - Error %d submitting control urb\n",
851 * mos7715_interrupt_callback
852 * this is the 7715's callback function for when we have received data on
853 * the interrupt endpoint.
855 static void mos7715_interrupt_callback(struct urb *urb)
859 int status = urb->status;
871 /* this urb is terminated, clean up */
872 dbg("%s - urb shutting down with status: %d", __func__,
876 dbg("%s - nonzero urb status received: %d", __func__,
881 length = urb->actual_length;
882 data = urb->transfer_buffer;
884 /* Structure of data from 7715 device:
885 * Byte 1: IIR serial Port
887 * Byte 2: DSR parallel port
888 * Byte 4: FIFO status for both */
890 if (unlikely(length != 4)) {
891 dbg("Wrong data !!!");
896 if (!(iir & 0x01)) { /* serial port interrupt pending */
897 switch (iir & 0x0f) {
899 dbg("Serial Port: Receiver status error or address "
900 "bit detected in 9-bit mode\n");
903 dbg("Serial Port: Receiver time out");
906 /* dbg("Serial Port: Modem status change"); */
911 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
912 { /* update local copy of DSR reg */
913 struct usb_serial_port *port = urb->context;
914 struct mos7715_parport *mos_parport = port->serial->private;
915 if (unlikely(mos_parport == NULL))
917 atomic_set(&mos_parport->shadowDSR, data[2]);
922 result = usb_submit_urb(urb, GFP_ATOMIC);
924 dev_err(&urb->dev->dev,
925 "%s - Error %d submitting control urb\n",
930 * mos7720_bulk_in_callback
931 * this is the callback function for when we have received data on the
934 static void mos7720_bulk_in_callback(struct urb *urb)
937 unsigned char *data ;
938 struct usb_serial_port *port;
939 struct tty_struct *tty;
940 int status = urb->status;
943 dbg("nonzero read bulk status received: %d", status);
949 dbg("Entering...%s", __func__);
951 data = urb->transfer_buffer;
953 tty = tty_port_tty_get(&port->port);
954 if (tty && urb->actual_length) {
955 tty_insert_flip_string(tty, data, urb->actual_length);
956 tty_flip_buffer_push(tty);
960 if (!port->read_urb) {
961 dbg("URB KILLED !!!");
965 if (port->read_urb->status != -EINPROGRESS) {
966 port->read_urb->dev = port->serial->dev;
968 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
970 dbg("usb_submit_urb(read bulk) failed, retval = %d",
976 * mos7720_bulk_out_data_callback
977 * this is the callback function for when we have finished sending serial
978 * data on the bulk out endpoint.
980 static void mos7720_bulk_out_data_callback(struct urb *urb)
982 struct moschip_port *mos7720_port;
983 struct tty_struct *tty;
984 int status = urb->status;
987 dbg("nonzero write bulk status received:%d", status);
991 mos7720_port = urb->context;
993 dbg("NULL mos7720_port pointer");
997 tty = tty_port_tty_get(&mos7720_port->port->port);
999 if (tty && mos7720_port->open)
1006 * this function installs the appropriate read interrupt endpoint callback
1007 * depending on whether the device is a 7720 or 7715, thus avoiding costly
1008 * run-time checks in the high-frequency callback routine itself.
1010 static int mos77xx_probe(struct usb_serial *serial,
1011 const struct usb_device_id *id)
1013 if (id->idProduct == MOSCHIP_DEVICE_ID_7715)
1014 moschip7720_2port_driver.read_int_callback =
1015 mos7715_interrupt_callback;
1017 moschip7720_2port_driver.read_int_callback =
1018 mos7720_interrupt_callback;
1023 static int mos77xx_calc_num_ports(struct usb_serial *serial)
1025 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
1026 if (product == MOSCHIP_DEVICE_ID_7715)
1032 static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
1034 struct usb_serial *serial;
1035 struct usb_serial_port *port0;
1037 struct moschip_port *mos7720_port;
1041 int allocated_urbs = 0;
1044 serial = port->serial;
1046 mos7720_port = usb_get_serial_port_data(port);
1047 if (mos7720_port == NULL)
1050 port0 = serial->port[0];
1052 usb_clear_halt(serial->dev, port->write_urb->pipe);
1053 usb_clear_halt(serial->dev, port->read_urb->pipe);
1055 /* Initialising the write urb pool */
1056 for (j = 0; j < NUM_URBS; ++j) {
1057 urb = usb_alloc_urb(0, GFP_KERNEL);
1058 mos7720_port->write_urb_pool[j] = urb;
1061 dev_err(&port->dev, "No more urbs???\n");
1065 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1067 if (!urb->transfer_buffer) {
1069 "%s-out of memory for urb buffers.\n",
1071 usb_free_urb(mos7720_port->write_urb_pool[j]);
1072 mos7720_port->write_urb_pool[j] = NULL;
1078 if (!allocated_urbs)
1081 /* Initialize MCS7720 -- Write Init values to corresponding Registers
1093 * 0x08 : SP1/2 Control Reg
1095 port_number = port->number - port->serial->minor;
1096 read_mos_reg(serial, port_number, LSR, &data);
1098 dbg("SS::%p LSR:%x", mos7720_port, data);
1100 dbg("Check:Sending Command ..........");
1102 write_mos_reg(serial, dummy, SP1_REG, 0x02);
1103 write_mos_reg(serial, dummy, SP2_REG, 0x02);
1105 write_mos_reg(serial, port_number, IER, 0x00);
1106 write_mos_reg(serial, port_number, FCR, 0x00);
1108 write_mos_reg(serial, port_number, FCR, 0xcf);
1109 mos7720_port->shadowLCR = 0x03;
1110 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1111 mos7720_port->shadowMCR = 0x0b;
1112 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1114 write_mos_reg(serial, port_number, SP_CONTROL_REG, 0x00);
1115 read_mos_reg(serial, dummy, SP_CONTROL_REG, &data);
1116 data = data | (port->number - port->serial->minor + 1);
1117 write_mos_reg(serial, dummy, SP_CONTROL_REG, data);
1118 mos7720_port->shadowLCR = 0x83;
1119 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1120 write_mos_reg(serial, port_number, THR, 0x0c);
1121 write_mos_reg(serial, port_number, IER, 0x00);
1122 mos7720_port->shadowLCR = 0x03;
1123 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1124 write_mos_reg(serial, port_number, IER, 0x0c);
1126 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
1128 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
1129 __func__, response);
1131 /* initialize our icount structure */
1132 memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
1134 /* initialize our port settings */
1135 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
1137 /* send a open port command */
1138 mos7720_port->open = 1;
1144 * mos7720_chars_in_buffer
1145 * this function is called by the tty driver when it wants to know how many
1146 * bytes of data we currently have outstanding in the port (data that has
1147 * been written, but hasn't made it out the port yet)
1148 * If successful, we return the number of bytes left to be written in the
1150 * Otherwise we return a negative error number.
1152 static int mos7720_chars_in_buffer(struct tty_struct *tty)
1154 struct usb_serial_port *port = tty->driver_data;
1157 struct moschip_port *mos7720_port;
1159 dbg("%s:entering ...........", __func__);
1161 mos7720_port = usb_get_serial_port_data(port);
1162 if (mos7720_port == NULL) {
1163 dbg("%s:leaving ...........", __func__);
1167 for (i = 0; i < NUM_URBS; ++i) {
1168 if (mos7720_port->write_urb_pool[i] &&
1169 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
1170 chars += URB_TRANSFER_BUFFER_SIZE;
1172 dbg("%s - returns %d", __func__, chars);
1176 static void mos7720_close(struct usb_serial_port *port)
1178 struct usb_serial *serial;
1179 struct moschip_port *mos7720_port;
1182 dbg("mos7720_close:entering...");
1184 serial = port->serial;
1186 mos7720_port = usb_get_serial_port_data(port);
1187 if (mos7720_port == NULL)
1190 for (j = 0; j < NUM_URBS; ++j)
1191 usb_kill_urb(mos7720_port->write_urb_pool[j]);
1193 /* Freeing Write URBs */
1194 for (j = 0; j < NUM_URBS; ++j) {
1195 if (mos7720_port->write_urb_pool[j]) {
1196 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
1197 usb_free_urb(mos7720_port->write_urb_pool[j]);
1201 /* While closing port, shutdown all bulk read, write *
1202 * and interrupt read if they exists, otherwise nop */
1203 dbg("Shutdown bulk write");
1204 usb_kill_urb(port->write_urb);
1205 dbg("Shutdown bulk read");
1206 usb_kill_urb(port->read_urb);
1208 mutex_lock(&serial->disc_mutex);
1209 /* these commands must not be issued if the device has
1210 * been disconnected */
1211 if (!serial->disconnected) {
1212 write_mos_reg(serial, port->number - port->serial->minor,
1214 write_mos_reg(serial, port->number - port->serial->minor,
1217 mutex_unlock(&serial->disc_mutex);
1218 mos7720_port->open = 0;
1220 dbg("Leaving %s", __func__);
1223 static void mos7720_break(struct tty_struct *tty, int break_state)
1225 struct usb_serial_port *port = tty->driver_data;
1227 struct usb_serial *serial;
1228 struct moschip_port *mos7720_port;
1230 dbg("Entering %s", __func__);
1232 serial = port->serial;
1234 mos7720_port = usb_get_serial_port_data(port);
1235 if (mos7720_port == NULL)
1238 if (break_state == -1)
1239 data = mos7720_port->shadowLCR | UART_LCR_SBC;
1241 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
1243 mos7720_port->shadowLCR = data;
1244 write_mos_reg(serial, port->number - port->serial->minor,
1245 LCR, mos7720_port->shadowLCR);
1249 * mos7720_write_room
1250 * this function is called by the tty driver when it wants to know how many
1251 * bytes of data we can accept for a specific port.
1252 * If successful, we return the amount of room that we have for this port
1253 * Otherwise we return a negative error number.
1255 static int mos7720_write_room(struct tty_struct *tty)
1257 struct usb_serial_port *port = tty->driver_data;
1258 struct moschip_port *mos7720_port;
1262 dbg("%s:entering ...........", __func__);
1264 mos7720_port = usb_get_serial_port_data(port);
1265 if (mos7720_port == NULL) {
1266 dbg("%s:leaving ...........", __func__);
1270 /* FIXME: Locking */
1271 for (i = 0; i < NUM_URBS; ++i) {
1272 if (mos7720_port->write_urb_pool[i] &&
1273 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
1274 room += URB_TRANSFER_BUFFER_SIZE;
1277 dbg("%s - returns %d", __func__, room);
1281 static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
1282 const unsigned char *data, int count)
1289 struct moschip_port *mos7720_port;
1290 struct usb_serial *serial;
1292 const unsigned char *current_position = data;
1294 dbg("%s:entering ...........", __func__);
1296 serial = port->serial;
1298 mos7720_port = usb_get_serial_port_data(port);
1299 if (mos7720_port == NULL) {
1300 dbg("mos7720_port is NULL");
1304 /* try to find a free urb in the list */
1307 for (i = 0; i < NUM_URBS; ++i) {
1308 if (mos7720_port->write_urb_pool[i] &&
1309 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
1310 urb = mos7720_port->write_urb_pool[i];
1317 dbg("%s - no more free urbs", __func__);
1321 if (urb->transfer_buffer == NULL) {
1322 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1324 if (urb->transfer_buffer == NULL) {
1325 dev_err(&port->dev, "%s no more kernel memory...\n",
1330 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1332 memcpy(urb->transfer_buffer, current_position, transfer_size);
1333 usb_serial_debug_data(debug, &port->dev, __func__, transfer_size,
1334 urb->transfer_buffer);
1336 /* fill urb with data and submit */
1337 usb_fill_bulk_urb(urb, serial->dev,
1338 usb_sndbulkpipe(serial->dev,
1339 port->bulk_out_endpointAddress),
1340 urb->transfer_buffer, transfer_size,
1341 mos7720_bulk_out_data_callback, mos7720_port);
1343 /* send it down the pipe */
1344 status = usb_submit_urb(urb, GFP_ATOMIC);
1346 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
1347 "with status = %d\n", __func__, status);
1348 bytes_sent = status;
1351 bytes_sent = transfer_size;
1357 static void mos7720_throttle(struct tty_struct *tty)
1359 struct usb_serial_port *port = tty->driver_data;
1360 struct moschip_port *mos7720_port;
1363 dbg("%s- port %d", __func__, port->number);
1365 mos7720_port = usb_get_serial_port_data(port);
1367 if (mos7720_port == NULL)
1370 if (!mos7720_port->open) {
1371 dbg("port not opened");
1375 dbg("%s: Entering ..........", __func__);
1377 /* if we are implementing XON/XOFF, send the stop character */
1379 unsigned char stop_char = STOP_CHAR(tty);
1380 status = mos7720_write(tty, port, &stop_char, 1);
1385 /* if we are implementing RTS/CTS, toggle that line */
1386 if (tty->termios->c_cflag & CRTSCTS) {
1387 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
1388 write_mos_reg(port->serial, port->number - port->serial->minor,
1389 MCR, mos7720_port->shadowMCR);
1395 static void mos7720_unthrottle(struct tty_struct *tty)
1397 struct usb_serial_port *port = tty->driver_data;
1398 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1401 if (mos7720_port == NULL)
1404 if (!mos7720_port->open) {
1405 dbg("%s - port not opened", __func__);
1409 dbg("%s: Entering ..........", __func__);
1411 /* if we are implementing XON/XOFF, send the start character */
1413 unsigned char start_char = START_CHAR(tty);
1414 status = mos7720_write(tty, port, &start_char, 1);
1419 /* if we are implementing RTS/CTS, toggle that line */
1420 if (tty->termios->c_cflag & CRTSCTS) {
1421 mos7720_port->shadowMCR |= UART_MCR_RTS;
1422 write_mos_reg(port->serial, port->number - port->serial->minor,
1423 MCR, mos7720_port->shadowMCR);
1429 /* FIXME: this function does not work */
1430 static int set_higher_rates(struct moschip_port *mos7720_port,
1433 struct usb_serial_port *port;
1434 struct usb_serial *serial;
1436 enum mos_regs sp_reg;
1437 if (mos7720_port == NULL)
1440 port = mos7720_port->port;
1441 serial = port->serial;
1443 /***********************************************
1444 * Init Sequence for higher rates
1445 ***********************************************/
1446 dbg("Sending Setting Commands ..........");
1447 port_number = port->number - port->serial->minor;
1449 write_mos_reg(serial, port_number, IER, 0x00);
1450 write_mos_reg(serial, port_number, FCR, 0x00);
1451 write_mos_reg(serial, port_number, FCR, 0xcf);
1452 mos7720_port->shadowMCR = 0x0b;
1453 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1454 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x00);
1456 /***********************************************
1457 * Set for higher rates *
1458 ***********************************************/
1459 /* writing baud rate verbatum into uart clock field clearly not right */
1460 if (port_number == 0)
1464 write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
1465 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x03);
1466 mos7720_port->shadowMCR = 0x2b;
1467 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1469 /***********************************************
1471 ***********************************************/
1472 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1473 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1474 write_mos_reg(serial, port_number, DLL, 0x01);
1475 write_mos_reg(serial, port_number, DLM, 0x00);
1476 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1477 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1482 /* baud rate information */
1483 struct divisor_table_entry {
1488 /* Define table of divisors for moschip 7720 hardware *
1489 * These assume a 3.6864MHz crystal, the standard /16, and *
1491 static struct divisor_table_entry divisor_table[] = {
1493 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
1494 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
1510 /*****************************************************************************
1511 * calc_baud_rate_divisor
1512 * this function calculates the proper baud rate divisor for the specified
1514 *****************************************************************************/
1515 static int calc_baud_rate_divisor(int baudrate, int *divisor)
1523 dbg("%s - %d", __func__, baudrate);
1525 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
1526 if (divisor_table[i].baudrate == baudrate) {
1527 *divisor = divisor_table[i].divisor;
1532 /* After trying for all the standard baud rates *
1533 * Try calculating the divisor for this baud rate */
1534 if (baudrate > 75 && baudrate < 230400) {
1535 /* get the divisor */
1536 custom = (__u16)(230400L / baudrate);
1538 /* Check for round off */
1539 round1 = (__u16)(2304000L / baudrate);
1540 round = (__u16)(round1 - (custom * 10));
1545 dbg("Baud %d = %d", baudrate, custom);
1549 dbg("Baud calculation Failed...");
1554 * send_cmd_write_baud_rate
1555 * this function sends the proper command to change the baud rate of the
1558 static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1561 struct usb_serial_port *port;
1562 struct usb_serial *serial;
1565 unsigned char number;
1567 if (mos7720_port == NULL)
1570 port = mos7720_port->port;
1571 serial = port->serial;
1573 dbg("%s: Entering ..........", __func__);
1575 number = port->number - port->serial->minor;
1576 dbg("%s - port = %d, baud = %d", __func__, port->number, baudrate);
1578 /* Calculate the Divisor */
1579 status = calc_baud_rate_divisor(baudrate, &divisor);
1581 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1585 /* Enable access to divisor latch */
1586 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1587 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
1589 /* Write the divisor */
1590 write_mos_reg(serial, number, DLL, (__u8)(divisor & 0xff));
1591 write_mos_reg(serial, number, DLM, (__u8)((divisor & 0xff00) >> 8));
1593 /* Disable access to divisor latch */
1594 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1595 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
1601 * change_port_settings
1602 * This routine is called to set the UART on the device to match
1603 * the specified new settings.
1605 static void change_port_settings(struct tty_struct *tty,
1606 struct moschip_port *mos7720_port,
1607 struct ktermios *old_termios)
1609 struct usb_serial_port *port;
1610 struct usb_serial *serial;
1621 if (mos7720_port == NULL)
1624 port = mos7720_port->port;
1625 serial = port->serial;
1626 port_number = port->number - port->serial->minor;
1628 dbg("%s - port %d", __func__, port->number);
1630 if (!mos7720_port->open) {
1631 dbg("%s - port not opened", __func__);
1635 dbg("%s: Entering ..........", __func__);
1637 lData = UART_LCR_WLEN8;
1638 lStop = 0x00; /* 1 stop bit */
1639 lParity = 0x00; /* No parity */
1641 cflag = tty->termios->c_cflag;
1642 iflag = tty->termios->c_iflag;
1644 /* Change the number of bits */
1645 switch (cflag & CSIZE) {
1647 lData = UART_LCR_WLEN5;
1652 lData = UART_LCR_WLEN6;
1657 lData = UART_LCR_WLEN7;
1662 lData = UART_LCR_WLEN8;
1666 /* Change the Parity bit */
1667 if (cflag & PARENB) {
1668 if (cflag & PARODD) {
1669 lParity = UART_LCR_PARITY;
1670 dbg("%s - parity = odd", __func__);
1672 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
1673 dbg("%s - parity = even", __func__);
1677 dbg("%s - parity = none", __func__);
1681 lParity = lParity | 0x20;
1683 /* Change the Stop bit */
1684 if (cflag & CSTOPB) {
1685 lStop = UART_LCR_STOP;
1686 dbg("%s - stop bits = 2", __func__);
1689 dbg("%s - stop bits = 1", __func__);
1692 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1693 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1694 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
1696 /* Update the LCR with the correct value */
1697 mos7720_port->shadowLCR &=
1698 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1699 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1702 /* Disable Interrupts */
1703 write_mos_reg(serial, port_number, IER, 0x00);
1704 write_mos_reg(serial, port_number, FCR, 0x00);
1705 write_mos_reg(serial, port_number, FCR, 0xcf);
1707 /* Send the updated LCR value to the mos7720 */
1708 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1709 mos7720_port->shadowMCR = 0x0b;
1710 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1712 /* set up the MCR register and send it to the mos7720 */
1713 mos7720_port->shadowMCR = UART_MCR_OUT2;
1715 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1717 if (cflag & CRTSCTS) {
1718 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
1719 /* To set hardware flow control to the specified *
1720 * serial port, in SP1/2_CONTROL_REG */
1722 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x01);
1724 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x02);
1727 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1729 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1731 /* Determine divisor based on baud rate */
1732 baud = tty_get_baud_rate(tty);
1734 /* pick a default, any default... */
1735 dbg("Picked default baud...");
1739 if (baud >= 230400) {
1740 set_higher_rates(mos7720_port, baud);
1741 /* Enable Interrupts */
1742 write_mos_reg(serial, port_number, IER, 0x0c);
1746 dbg("%s - baud rate = %d", __func__, baud);
1747 status = send_cmd_write_baud_rate(mos7720_port, baud);
1748 /* FIXME: needs to write actual resulting baud back not just
1751 tty_encode_baud_rate(tty, baud, baud);
1752 /* Enable Interrupts */
1753 write_mos_reg(serial, port_number, IER, 0x0c);
1755 if (port->read_urb->status != -EINPROGRESS) {
1756 port->read_urb->dev = serial->dev;
1758 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1760 dbg("usb_submit_urb(read bulk) failed, status = %d",
1766 * mos7720_set_termios
1767 * this function is called by the tty driver when it wants to change the
1768 * termios structure.
1770 static void mos7720_set_termios(struct tty_struct *tty,
1771 struct usb_serial_port *port, struct ktermios *old_termios)
1775 struct usb_serial *serial;
1776 struct moschip_port *mos7720_port;
1778 serial = port->serial;
1780 mos7720_port = usb_get_serial_port_data(port);
1782 if (mos7720_port == NULL)
1785 if (!mos7720_port->open) {
1786 dbg("%s - port not opened", __func__);
1790 dbg("%s\n", "setting termios - ASPIRE");
1792 cflag = tty->termios->c_cflag;
1794 dbg("%s - cflag %08x iflag %08x", __func__,
1795 tty->termios->c_cflag,
1796 RELEVANT_IFLAG(tty->termios->c_iflag));
1798 dbg("%s - old cflag %08x old iflag %08x", __func__,
1799 old_termios->c_cflag,
1800 RELEVANT_IFLAG(old_termios->c_iflag));
1802 dbg("%s - port %d", __func__, port->number);
1804 /* change the port settings to the new ones specified */
1805 change_port_settings(tty, mos7720_port, old_termios);
1807 if (!port->read_urb) {
1808 dbg("%s", "URB KILLED !!!!!");
1812 if (port->read_urb->status != -EINPROGRESS) {
1813 port->read_urb->dev = serial->dev;
1814 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1816 dbg("usb_submit_urb(read bulk) failed, status = %d",
1822 * get_lsr_info - get line status register info
1824 * Purpose: Let user call ioctl() to get info when the UART physically
1825 * is emptied. On bus types like RS485, the transmitter must
1826 * release the bus after transmitting. This must be done when
1827 * the transmit shift register is empty, not be done when the
1828 * transmit holding register is empty. This functionality
1829 * allows an RS485 driver to be written in user space.
1831 static int get_lsr_info(struct tty_struct *tty,
1832 struct moschip_port *mos7720_port, unsigned int __user *value)
1834 struct usb_serial_port *port = tty->driver_data;
1835 unsigned int result = 0;
1836 unsigned char data = 0;
1837 int port_number = port->number - port->serial->minor;
1840 count = mos7720_chars_in_buffer(tty);
1842 read_mos_reg(port->serial, port_number, LSR, &data);
1843 if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1844 == (UART_LSR_TEMT | UART_LSR_THRE)) {
1845 dbg("%s -- Empty", __func__);
1846 result = TIOCSER_TEMT;
1849 if (copy_to_user(value, &result, sizeof(int)))
1854 static int mos7720_tiocmget(struct tty_struct *tty)
1856 struct usb_serial_port *port = tty->driver_data;
1857 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1858 unsigned int result = 0;
1862 dbg("%s - port %d", __func__, port->number);
1864 mcr = mos7720_port->shadowMCR;
1865 msr = mos7720_port->shadowMSR;
1867 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1868 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1869 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1870 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
1871 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1872 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
1874 dbg("%s -- %x", __func__, result);
1879 static int mos7720_tiocmset(struct tty_struct *tty,
1880 unsigned int set, unsigned int clear)
1882 struct usb_serial_port *port = tty->driver_data;
1883 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1885 dbg("%s - port %d", __func__, port->number);
1886 dbg("he was at tiocmset");
1888 mcr = mos7720_port->shadowMCR;
1890 if (set & TIOCM_RTS)
1891 mcr |= UART_MCR_RTS;
1892 if (set & TIOCM_DTR)
1893 mcr |= UART_MCR_DTR;
1894 if (set & TIOCM_LOOP)
1895 mcr |= UART_MCR_LOOP;
1897 if (clear & TIOCM_RTS)
1898 mcr &= ~UART_MCR_RTS;
1899 if (clear & TIOCM_DTR)
1900 mcr &= ~UART_MCR_DTR;
1901 if (clear & TIOCM_LOOP)
1902 mcr &= ~UART_MCR_LOOP;
1904 mos7720_port->shadowMCR = mcr;
1905 write_mos_reg(port->serial, port->number - port->serial->minor,
1906 MCR, mos7720_port->shadowMCR);
1911 static int mos7720_get_icount(struct tty_struct *tty,
1912 struct serial_icounter_struct *icount)
1914 struct usb_serial_port *port = tty->driver_data;
1915 struct moschip_port *mos7720_port;
1916 struct async_icount cnow;
1918 mos7720_port = usb_get_serial_port_data(port);
1919 cnow = mos7720_port->icount;
1921 icount->cts = cnow.cts;
1922 icount->dsr = cnow.dsr;
1923 icount->rng = cnow.rng;
1924 icount->dcd = cnow.dcd;
1925 icount->rx = cnow.rx;
1926 icount->tx = cnow.tx;
1927 icount->frame = cnow.frame;
1928 icount->overrun = cnow.overrun;
1929 icount->parity = cnow.parity;
1930 icount->brk = cnow.brk;
1931 icount->buf_overrun = cnow.buf_overrun;
1933 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
1934 port->number, icount->rx, icount->tx);
1938 static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1939 unsigned int __user *value)
1944 struct usb_serial_port *port;
1946 if (mos7720_port == NULL)
1949 port = (struct usb_serial_port *)mos7720_port->port;
1950 mcr = mos7720_port->shadowMCR;
1952 if (copy_from_user(&arg, value, sizeof(int)))
1957 if (arg & TIOCM_RTS)
1958 mcr |= UART_MCR_RTS;
1959 if (arg & TIOCM_DTR)
1960 mcr |= UART_MCR_RTS;
1961 if (arg & TIOCM_LOOP)
1962 mcr |= UART_MCR_LOOP;
1966 if (arg & TIOCM_RTS)
1967 mcr &= ~UART_MCR_RTS;
1968 if (arg & TIOCM_DTR)
1969 mcr &= ~UART_MCR_RTS;
1970 if (arg & TIOCM_LOOP)
1971 mcr &= ~UART_MCR_LOOP;
1976 mos7720_port->shadowMCR = mcr;
1977 write_mos_reg(port->serial, port->number - port->serial->minor,
1978 MCR, mos7720_port->shadowMCR);
1983 static int get_serial_info(struct moschip_port *mos7720_port,
1984 struct serial_struct __user *retinfo)
1986 struct serial_struct tmp;
1991 memset(&tmp, 0, sizeof(tmp));
1993 tmp.type = PORT_16550A;
1994 tmp.line = mos7720_port->port->serial->minor;
1995 tmp.port = mos7720_port->port->number;
1997 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1998 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1999 tmp.baud_base = 9600;
2000 tmp.close_delay = 5*HZ;
2001 tmp.closing_wait = 30*HZ;
2003 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2008 static int mos7720_ioctl(struct tty_struct *tty,
2009 unsigned int cmd, unsigned long arg)
2011 struct usb_serial_port *port = tty->driver_data;
2012 struct moschip_port *mos7720_port;
2013 struct async_icount cnow;
2014 struct async_icount cprev;
2016 mos7720_port = usb_get_serial_port_data(port);
2017 if (mos7720_port == NULL)
2020 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
2024 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
2025 return get_lsr_info(tty, mos7720_port,
2026 (unsigned int __user *)arg);
2028 /* FIXME: These should be using the mode methods */
2031 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET",
2032 __func__, port->number);
2033 return set_modem_info(mos7720_port, cmd,
2034 (unsigned int __user *)arg);
2037 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
2038 return get_serial_info(mos7720_port,
2039 (struct serial_struct __user *)arg);
2042 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
2043 cprev = mos7720_port->icount;
2045 if (signal_pending(current))
2046 return -ERESTARTSYS;
2047 cnow = mos7720_port->icount;
2048 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2049 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2050 return -EIO; /* no change => error */
2051 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2052 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2053 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2054 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2063 return -ENOIOCTLCMD;
2066 static int mos7720_startup(struct usb_serial *serial)
2068 struct moschip_port *mos7720_port;
2069 struct usb_device *dev;
2075 dbg("%s: Entering ..........", __func__);
2078 dbg("Invalid Handler");
2082 if (serial->num_bulk_in < 2 || serial->num_bulk_out < 2) {
2083 dev_err(&serial->interface->dev, "missing bulk endpoints\n");
2087 product = le16_to_cpu(serial->dev->descriptor.idProduct);
2091 * The 7715 uses the first bulk in/out endpoint pair for the parallel
2092 * port, and the second for the serial port. Because the usbserial core
2093 * assumes both pairs are serial ports, we must engage in a bit of
2094 * subterfuge and swap the pointers for ports 0 and 1 in order to make
2095 * port 0 point to the serial port. However, both moschip devices use a
2096 * single interrupt-in endpoint for both ports (as mentioned a little
2097 * further down), and this endpoint was assigned to port 0. So after
2098 * the swap, we must copy the interrupt endpoint elements from port 1
2099 * (as newly assigned) to port 0, and null out port 1 pointers.
2101 if (product == MOSCHIP_DEVICE_ID_7715) {
2102 struct usb_serial_port *tmp = serial->port[0];
2103 serial->port[0] = serial->port[1];
2104 serial->port[1] = tmp;
2105 serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb;
2106 serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer;
2107 serial->port[0]->interrupt_in_endpointAddress =
2108 tmp->interrupt_in_endpointAddress;
2109 serial->port[1]->interrupt_in_urb = NULL;
2110 serial->port[1]->interrupt_in_buffer = NULL;
2114 /* set up serial port private structures */
2115 for (i = 0; i < serial->num_ports; ++i) {
2116 mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2117 if (mos7720_port == NULL) {
2118 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
2122 /* Initialize all port interrupt end point to port 0 int
2123 * endpoint. Our device has only one interrupt endpoint
2124 * common to all ports */
2125 serial->port[i]->interrupt_in_endpointAddress =
2126 serial->port[0]->interrupt_in_endpointAddress;
2128 mos7720_port->port = serial->port[i];
2129 usb_set_serial_port_data(serial->port[i], mos7720_port);
2131 dbg("port number is %d", serial->port[i]->number);
2132 dbg("serial number is %d", serial->minor);
2136 /* setting configuration feature to one */
2137 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2138 (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5000);
2140 /* start the interrupt urb */
2141 ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
2144 "%s - Error %d submitting control urb\n",
2147 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
2148 if (product == MOSCHIP_DEVICE_ID_7715) {
2149 ret_val = mos7715_parport_init(serial);
2151 usb_kill_urb(serial->port[0]->interrupt_in_urb);
2156 /* LSR For Port 1 */
2157 read_mos_reg(serial, 0, LSR, &data);
2158 dbg("LSR:%x", data);
2163 static void mos7720_release(struct usb_serial *serial)
2167 usb_kill_urb(serial->port[0]->interrupt_in_urb);
2169 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
2170 /* close the parallel port */
2172 if (le16_to_cpu(serial->dev->descriptor.idProduct)
2173 == MOSCHIP_DEVICE_ID_7715) {
2174 struct urbtracker *urbtrack;
2175 unsigned long flags;
2176 struct mos7715_parport *mos_parport =
2177 usb_get_serial_data(serial);
2179 /* prevent NULL ptr dereference in port callbacks */
2180 spin_lock(&release_lock);
2181 mos_parport->pp->private_data = NULL;
2182 spin_unlock(&release_lock);
2184 /* wait for synchronous usb calls to return */
2185 if (mos_parport->msg_pending)
2186 wait_for_completion_timeout(&mos_parport->syncmsg_compl,
2187 msecs_to_jiffies(MOS_WDR_TIMEOUT));
2189 parport_remove_port(mos_parport->pp);
2190 usb_set_serial_data(serial, NULL);
2191 mos_parport->serial = NULL;
2193 /* if tasklet currently scheduled, wait for it to complete */
2194 tasklet_kill(&mos_parport->urb_tasklet);
2196 /* unlink any urbs sent by the tasklet */
2197 spin_lock_irqsave(&mos_parport->listlock, flags);
2198 list_for_each_entry(urbtrack,
2199 &mos_parport->active_urbs,
2201 usb_unlink_urb(urbtrack->urb);
2202 spin_unlock_irqrestore(&mos_parport->listlock, flags);
2204 kref_put(&mos_parport->ref_count, destroy_mos_parport);
2207 /* free private structure allocated for serial port */
2208 for (i = 0; i < serial->num_ports; ++i)
2209 kfree(usb_get_serial_port_data(serial->port[i]));
2212 static struct usb_driver usb_driver = {
2213 .name = "moschip7720",
2214 .probe = usb_serial_probe,
2215 .disconnect = usb_serial_disconnect,
2216 .id_table = moschip_port_id_table,
2220 static struct usb_serial_driver moschip7720_2port_driver = {
2222 .owner = THIS_MODULE,
2223 .name = "moschip7720",
2225 .description = "Moschip 2 port adapter",
2226 .usb_driver = &usb_driver,
2227 .id_table = moschip_port_id_table,
2228 .calc_num_ports = mos77xx_calc_num_ports,
2229 .open = mos7720_open,
2230 .close = mos7720_close,
2231 .throttle = mos7720_throttle,
2232 .unthrottle = mos7720_unthrottle,
2233 .probe = mos77xx_probe,
2234 .attach = mos7720_startup,
2235 .release = mos7720_release,
2236 .ioctl = mos7720_ioctl,
2237 .tiocmget = mos7720_tiocmget,
2238 .tiocmset = mos7720_tiocmset,
2239 .get_icount = mos7720_get_icount,
2240 .set_termios = mos7720_set_termios,
2241 .write = mos7720_write,
2242 .write_room = mos7720_write_room,
2243 .chars_in_buffer = mos7720_chars_in_buffer,
2244 .break_ctl = mos7720_break,
2245 .read_bulk_callback = mos7720_bulk_in_callback,
2246 .read_int_callback = NULL /* dynamically assigned in probe() */
2249 static int __init moschip7720_init(void)
2253 dbg("%s: Entering ..........", __func__);
2255 /* Register with the usb serial */
2256 retval = usb_serial_register(&moschip7720_2port_driver);
2258 goto failed_port_device_register;
2260 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
2263 /* Register with the usb */
2264 retval = usb_register(&usb_driver);
2266 goto failed_usb_register;
2270 failed_usb_register:
2271 usb_serial_deregister(&moschip7720_2port_driver);
2273 failed_port_device_register:
2277 static void __exit moschip7720_exit(void)
2279 usb_deregister(&usb_driver);
2280 usb_serial_deregister(&moschip7720_2port_driver);
2283 module_init(moschip7720_init);
2284 module_exit(moschip7720_exit);
2286 /* Module information */
2287 MODULE_AUTHOR(DRIVER_AUTHOR);
2288 MODULE_DESCRIPTION(DRIVER_DESC);
2289 MODULE_LICENSE("GPL");
2291 module_param(debug, bool, S_IRUGO | S_IWUSR);
2292 MODULE_PARM_DESC(debug, "Debug enabled or not");