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.
16 * Cleaned up from the original by:
19 * Originally based on drivers/usb/serial/io_edgeport.c which is:
20 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
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 (HZ * 5)
49 #define MOS_MAX_PORT 0x02
50 #define MOS_WRITE 0x0E
53 /* Interrupt Rotinue Defines */
54 #define SERIAL_IIR_RLS 0x06
55 #define SERIAL_IIR_RDA 0x04
56 #define SERIAL_IIR_CTI 0x0c
57 #define SERIAL_IIR_THR 0x02
58 #define SERIAL_IIR_MS 0x00
60 #define NUM_URBS 16 /* URB Count */
61 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
63 /* This structure holds all of the local serial port information */
65 __u8 shadowLCR; /* last LCR value received */
66 __u8 shadowMCR; /* last MCR value received */
67 __u8 shadowMSR; /* last MSR value received */
69 struct async_icount icount;
70 struct usb_serial_port *port; /* loop back to the owner */
71 struct urb *write_urb_pool[NUM_URBS];
76 static struct usb_serial_driver moschip7720_2port_driver;
78 #define USB_VENDOR_ID_MOSCHIP 0x9710
79 #define MOSCHIP_DEVICE_ID_7720 0x7720
80 #define MOSCHIP_DEVICE_ID_7715 0x7715
82 static const struct usb_device_id moschip_port_id_table[] = {
83 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
84 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) },
85 { } /* terminating entry */
87 MODULE_DEVICE_TABLE(usb, moschip_port_id_table);
89 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
91 /* initial values for parport regs */
92 #define DCR_INIT_VAL 0x0c /* SLCTIN, nINIT */
93 #define ECR_INIT_VAL 0x00 /* SPP mode */
96 struct mos7715_parport *mos_parport;
97 struct list_head urblist_entry;
98 struct kref ref_count;
102 enum mos7715_pp_modes {
104 PS2 = 1<<5, /* moschip calls this 'NIBBLE' mode */
105 PPF = 2<<5, /* moschip calls this 'CB-FIFO mode */
108 struct mos7715_parport {
109 struct parport *pp; /* back to containing struct */
110 struct kref ref_count; /* to instance of this struct */
111 struct list_head deferred_urbs; /* list deferred async urbs */
112 struct list_head active_urbs; /* list async urbs in flight */
113 spinlock_t listlock; /* protects list access */
114 bool msg_pending; /* usb sync call pending */
115 struct completion syncmsg_compl; /* usb sync call completed */
116 struct tasklet_struct urb_tasklet; /* for sending deferred urbs */
117 struct usb_serial *serial; /* back to containing struct */
118 __u8 shadowECR; /* parallel port regs... */
120 atomic_t shadowDSR; /* updated in int-in callback */
123 /* lock guards against dereferencing NULL ptr in parport ops callbacks */
124 static DEFINE_SPINLOCK(release_lock);
126 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
128 static const unsigned int dummy; /* for clarity in register access fns */
131 THR, /* serial port regs */
143 DPR, /* parallel port regs */
147 SP1_REG, /* device control regs */
148 SP2_REG, /* serial port 2 (7720 only) */
154 * Return the correct value for the Windex field of the setup packet
155 * for a control endpoint message. See the 7715 datasheet.
157 static inline __u16 get_reg_index(enum mos_regs reg)
159 static const __u16 mos7715_index_lookup_table[] = {
177 0x02, /* SP2_REG (7720 only) */
178 0x04, /* PP_REG (7715 only) */
179 0x08, /* SP_CONTROL_REG */
181 return mos7715_index_lookup_table[reg];
185 * Return the correct value for the upper byte of the Wvalue field of
186 * the setup packet for a control endpoint message.
188 static inline __u16 get_reg_value(enum mos_regs reg,
189 unsigned int serial_portnum)
191 if (reg >= SP1_REG) /* control reg */
194 else if (reg >= DPR) /* parallel port reg (7715 only) */
197 else /* serial port reg */
198 return (serial_portnum + 2) << 8;
202 * Write data byte to the specified device register. The data is embedded in
203 * the value field of the setup packet. serial_portnum is ignored for registers
204 * not specific to a particular serial port.
206 static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
207 enum mos_regs reg, __u8 data)
209 struct usb_device *usbdev = serial->dev;
210 unsigned int pipe = usb_sndctrlpipe(usbdev, 0);
211 __u8 request = (__u8)0x0e;
212 __u8 requesttype = (__u8)0x40;
213 __u16 index = get_reg_index(reg);
214 __u16 value = get_reg_value(reg, serial_portnum) + data;
215 int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
216 index, NULL, 0, MOS_WDR_TIMEOUT);
218 dev_err(&usbdev->dev,
219 "mos7720: usb_control_msg() failed: %d", status);
224 * Read data byte from the specified device register. The data returned by the
225 * device is embedded in the value field of the setup packet. serial_portnum is
226 * ignored for registers that are not specific to a particular serial port.
228 static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
229 enum mos_regs reg, __u8 *data)
231 struct usb_device *usbdev = serial->dev;
232 unsigned int pipe = usb_rcvctrlpipe(usbdev, 0);
233 __u8 request = (__u8)0x0d;
234 __u8 requesttype = (__u8)0xc0;
235 __u16 index = get_reg_index(reg);
236 __u16 value = get_reg_value(reg, serial_portnum);
237 int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
238 index, data, 1, MOS_WDR_TIMEOUT);
240 dev_err(&usbdev->dev,
241 "mos7720: usb_control_msg() failed: %d", status);
245 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
247 static inline int mos7715_change_mode(struct mos7715_parport *mos_parport,
248 enum mos7715_pp_modes mode)
250 mos_parport->shadowECR = mode;
251 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
255 static void destroy_mos_parport(struct kref *kref)
257 struct mos7715_parport *mos_parport =
258 container_of(kref, struct mos7715_parport, ref_count);
260 dbg("%s called", __func__);
264 static void destroy_urbtracker(struct kref *kref)
266 struct urbtracker *urbtrack =
267 container_of(kref, struct urbtracker, ref_count);
268 struct mos7715_parport *mos_parport = urbtrack->mos_parport;
269 dbg("%s called", __func__);
270 usb_free_urb(urbtrack->urb);
272 kref_put(&mos_parport->ref_count, destroy_mos_parport);
276 * This runs as a tasklet when sending an urb in a non-blocking parallel
277 * port callback had to be deferred because the disconnect mutex could not be
278 * obtained at the time.
280 static void send_deferred_urbs(unsigned long _mos_parport)
284 struct mos7715_parport *mos_parport = (void *)_mos_parport;
285 struct urbtracker *urbtrack;
286 struct list_head *cursor, *next;
288 dbg("%s called", __func__);
290 /* if release function ran, game over */
291 if (unlikely(mos_parport->serial == NULL))
294 /* try again to get the mutex */
295 if (!mutex_trylock(&mos_parport->serial->disc_mutex)) {
296 dbg("%s: rescheduling tasklet", __func__);
297 tasklet_schedule(&mos_parport->urb_tasklet);
301 /* if device disconnected, game over */
302 if (unlikely(mos_parport->serial->disconnected)) {
303 mutex_unlock(&mos_parport->serial->disc_mutex);
307 spin_lock_irqsave(&mos_parport->listlock, flags);
308 if (list_empty(&mos_parport->deferred_urbs)) {
309 spin_unlock_irqrestore(&mos_parport->listlock, flags);
310 mutex_unlock(&mos_parport->serial->disc_mutex);
311 dbg("%s: deferred_urbs list empty", __func__);
315 /* move contents of deferred_urbs list to active_urbs list and submit */
316 list_for_each_safe(cursor, next, &mos_parport->deferred_urbs)
317 list_move_tail(cursor, &mos_parport->active_urbs);
318 list_for_each_entry(urbtrack, &mos_parport->active_urbs,
320 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
321 dbg("%s: urb submitted", __func__);
323 dev_err(&mos_parport->serial->dev->dev,
324 "usb_submit_urb() failed: %d", ret_val);
325 list_del(&urbtrack->urblist_entry);
326 kref_put(&urbtrack->ref_count, destroy_urbtracker);
329 spin_unlock_irqrestore(&mos_parport->listlock, flags);
330 mutex_unlock(&mos_parport->serial->disc_mutex);
333 /* callback for parallel port control urbs submitted asynchronously */
334 static void async_complete(struct urb *urb)
336 struct urbtracker *urbtrack = urb->context;
337 int status = urb->status;
338 dbg("%s called", __func__);
339 if (unlikely(status))
340 dbg("%s - nonzero urb status received: %d", __func__, status);
342 /* remove the urbtracker from the active_urbs list */
343 spin_lock(&urbtrack->mos_parport->listlock);
344 list_del(&urbtrack->urblist_entry);
345 spin_unlock(&urbtrack->mos_parport->listlock);
346 kref_put(&urbtrack->ref_count, destroy_urbtracker);
349 static int write_parport_reg_nonblock(struct mos7715_parport *mos_parport,
350 enum mos_regs reg, __u8 data)
352 struct urbtracker *urbtrack;
355 struct usb_ctrlrequest setup;
356 struct usb_serial *serial = mos_parport->serial;
357 struct usb_device *usbdev = serial->dev;
358 dbg("%s called", __func__);
360 /* create and initialize the control urb and containing urbtracker */
361 urbtrack = kmalloc(sizeof(struct urbtracker), GFP_ATOMIC);
362 if (urbtrack == NULL) {
363 dev_err(&usbdev->dev, "out of memory");
366 kref_get(&mos_parport->ref_count);
367 urbtrack->mos_parport = mos_parport;
368 urbtrack->urb = usb_alloc_urb(0, GFP_ATOMIC);
369 if (urbtrack->urb == NULL) {
370 dev_err(&usbdev->dev, "out of urbs");
374 setup.bRequestType = (__u8)0x40;
375 setup.bRequest = (__u8)0x0e;
376 setup.wValue = get_reg_value(reg, dummy);
377 setup.wIndex = get_reg_index(reg);
379 usb_fill_control_urb(urbtrack->urb, usbdev,
380 usb_sndctrlpipe(usbdev, 0),
381 (unsigned char *)&setup,
382 NULL, 0, async_complete, urbtrack);
383 kref_init(&urbtrack->ref_count);
384 INIT_LIST_HEAD(&urbtrack->urblist_entry);
387 * get the disconnect mutex, or add tracker to the deferred_urbs list
388 * and schedule a tasklet to try again later
390 if (!mutex_trylock(&serial->disc_mutex)) {
391 spin_lock_irqsave(&mos_parport->listlock, flags);
392 list_add_tail(&urbtrack->urblist_entry,
393 &mos_parport->deferred_urbs);
394 spin_unlock_irqrestore(&mos_parport->listlock, flags);
395 tasklet_schedule(&mos_parport->urb_tasklet);
396 dbg("tasklet scheduled");
400 /* bail if device disconnected */
401 if (serial->disconnected) {
402 kref_put(&urbtrack->ref_count, destroy_urbtracker);
403 mutex_unlock(&serial->disc_mutex);
407 /* add the tracker to the active_urbs list and submit */
408 spin_lock_irqsave(&mos_parport->listlock, flags);
409 list_add_tail(&urbtrack->urblist_entry, &mos_parport->active_urbs);
410 spin_unlock_irqrestore(&mos_parport->listlock, flags);
411 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
412 mutex_unlock(&serial->disc_mutex);
414 dev_err(&usbdev->dev,
415 "%s: submit_urb() failed: %d", __func__, ret_val);
416 spin_lock_irqsave(&mos_parport->listlock, flags);
417 list_del(&urbtrack->urblist_entry);
418 spin_unlock_irqrestore(&mos_parport->listlock, flags);
419 kref_put(&urbtrack->ref_count, destroy_urbtracker);
426 * This is the the common top part of all parallel port callback operations that
427 * send synchronous messages to the device. This implements convoluted locking
428 * that avoids two scenarios: (1) a port operation is called after usbserial
429 * has called our release function, at which point struct mos7715_parport has
430 * been destroyed, and (2) the device has been disconnected, but usbserial has
431 * not called the release function yet because someone has a serial port open.
432 * The shared release_lock prevents the first, and the mutex and disconnected
433 * flag maintained by usbserial covers the second. We also use the msg_pending
434 * flag to ensure that all synchronous usb messgage calls have completed before
435 * our release function can return.
437 static int parport_prologue(struct parport *pp)
439 struct mos7715_parport *mos_parport;
441 spin_lock(&release_lock);
442 mos_parport = pp->private_data;
443 if (unlikely(mos_parport == NULL)) {
444 /* release fn called, port struct destroyed */
445 spin_unlock(&release_lock);
448 mos_parport->msg_pending = true; /* synch usb call pending */
449 INIT_COMPLETION(mos_parport->syncmsg_compl);
450 spin_unlock(&release_lock);
452 mutex_lock(&mos_parport->serial->disc_mutex);
453 if (mos_parport->serial->disconnected) {
454 /* device disconnected */
455 mutex_unlock(&mos_parport->serial->disc_mutex);
456 mos_parport->msg_pending = false;
457 complete(&mos_parport->syncmsg_compl);
465 * This is the the common bottom part of all parallel port functions that send
466 * synchronous messages to the device.
468 static inline void parport_epilogue(struct parport *pp)
470 struct mos7715_parport *mos_parport = pp->private_data;
471 mutex_unlock(&mos_parport->serial->disc_mutex);
472 mos_parport->msg_pending = false;
473 complete(&mos_parport->syncmsg_compl);
476 static void parport_mos7715_write_data(struct parport *pp, unsigned char d)
478 struct mos7715_parport *mos_parport = pp->private_data;
479 dbg("%s called: %2.2x", __func__, d);
480 if (parport_prologue(pp) < 0)
482 mos7715_change_mode(mos_parport, SPP);
483 write_mos_reg(mos_parport->serial, dummy, DPR, (__u8)d);
484 parport_epilogue(pp);
487 static unsigned char parport_mos7715_read_data(struct parport *pp)
489 struct mos7715_parport *mos_parport = pp->private_data;
491 dbg("%s called", __func__);
492 if (parport_prologue(pp) < 0)
494 read_mos_reg(mos_parport->serial, dummy, DPR, &d);
495 parport_epilogue(pp);
499 static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
501 struct mos7715_parport *mos_parport = pp->private_data;
503 dbg("%s called: %2.2x", __func__, d);
504 if (parport_prologue(pp) < 0)
506 data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
507 write_mos_reg(mos_parport->serial, dummy, DCR, data);
508 mos_parport->shadowDCR = data;
509 parport_epilogue(pp);
512 static unsigned char parport_mos7715_read_control(struct parport *pp)
514 struct mos7715_parport *mos_parport = pp->private_data;
516 dbg("%s called", __func__);
517 spin_lock(&release_lock);
518 mos_parport = pp->private_data;
519 if (unlikely(mos_parport == NULL)) {
520 spin_unlock(&release_lock);
523 dcr = mos_parport->shadowDCR & 0x0f;
524 spin_unlock(&release_lock);
528 static unsigned char parport_mos7715_frob_control(struct parport *pp,
532 struct mos7715_parport *mos_parport = pp->private_data;
534 dbg("%s called", __func__);
537 if (parport_prologue(pp) < 0)
539 mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
540 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
541 dcr = mos_parport->shadowDCR & 0x0f;
542 parport_epilogue(pp);
546 static unsigned char parport_mos7715_read_status(struct parport *pp)
548 unsigned char status;
549 struct mos7715_parport *mos_parport = pp->private_data;
550 dbg("%s called", __func__);
551 spin_lock(&release_lock);
552 mos_parport = pp->private_data;
553 if (unlikely(mos_parport == NULL)) { /* release called */
554 spin_unlock(&release_lock);
557 status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
558 spin_unlock(&release_lock);
562 static void parport_mos7715_enable_irq(struct parport *pp)
564 dbg("%s called", __func__);
566 static void parport_mos7715_disable_irq(struct parport *pp)
568 dbg("%s called", __func__);
571 static void parport_mos7715_data_forward(struct parport *pp)
573 struct mos7715_parport *mos_parport = pp->private_data;
574 dbg("%s called", __func__);
575 if (parport_prologue(pp) < 0)
577 mos7715_change_mode(mos_parport, PS2);
578 mos_parport->shadowDCR &= ~0x20;
579 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
580 parport_epilogue(pp);
583 static void parport_mos7715_data_reverse(struct parport *pp)
585 struct mos7715_parport *mos_parport = pp->private_data;
586 dbg("%s called", __func__);
587 if (parport_prologue(pp) < 0)
589 mos7715_change_mode(mos_parport, PS2);
590 mos_parport->shadowDCR |= 0x20;
591 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
592 parport_epilogue(pp);
595 static void parport_mos7715_init_state(struct pardevice *dev,
596 struct parport_state *s)
598 dbg("%s called", __func__);
599 s->u.pc.ctr = DCR_INIT_VAL;
600 s->u.pc.ecr = ECR_INIT_VAL;
603 /* N.B. Parport core code requires that this function not block */
604 static void parport_mos7715_save_state(struct parport *pp,
605 struct parport_state *s)
607 struct mos7715_parport *mos_parport;
608 dbg("%s called", __func__);
609 spin_lock(&release_lock);
610 mos_parport = pp->private_data;
611 if (unlikely(mos_parport == NULL)) { /* release called */
612 spin_unlock(&release_lock);
615 s->u.pc.ctr = mos_parport->shadowDCR;
616 s->u.pc.ecr = mos_parport->shadowECR;
617 spin_unlock(&release_lock);
620 /* N.B. Parport core code requires that this function not block */
621 static void parport_mos7715_restore_state(struct parport *pp,
622 struct parport_state *s)
624 struct mos7715_parport *mos_parport;
625 dbg("%s called", __func__);
626 spin_lock(&release_lock);
627 mos_parport = pp->private_data;
628 if (unlikely(mos_parport == NULL)) { /* release called */
629 spin_unlock(&release_lock);
632 write_parport_reg_nonblock(mos_parport, DCR, mos_parport->shadowDCR);
633 write_parport_reg_nonblock(mos_parport, ECR, mos_parport->shadowECR);
634 spin_unlock(&release_lock);
637 static size_t parport_mos7715_write_compat(struct parport *pp,
639 size_t len, int flags)
642 struct mos7715_parport *mos_parport = pp->private_data;
644 dbg("%s called: %u chars", __func__, (unsigned int)len);
645 if (parport_prologue(pp) < 0)
647 mos7715_change_mode(mos_parport, PPF);
648 retval = usb_bulk_msg(mos_parport->serial->dev,
649 usb_sndbulkpipe(mos_parport->serial->dev, 2),
650 (void *)buffer, len, &actual_len,
652 parport_epilogue(pp);
654 dev_err(&mos_parport->serial->dev->dev,
655 "mos7720: usb_bulk_msg() failed: %d", retval);
661 static struct parport_operations parport_mos7715_ops = {
662 .owner = THIS_MODULE,
663 .write_data = parport_mos7715_write_data,
664 .read_data = parport_mos7715_read_data,
666 .write_control = parport_mos7715_write_control,
667 .read_control = parport_mos7715_read_control,
668 .frob_control = parport_mos7715_frob_control,
670 .read_status = parport_mos7715_read_status,
672 .enable_irq = parport_mos7715_enable_irq,
673 .disable_irq = parport_mos7715_disable_irq,
675 .data_forward = parport_mos7715_data_forward,
676 .data_reverse = parport_mos7715_data_reverse,
678 .init_state = parport_mos7715_init_state,
679 .save_state = parport_mos7715_save_state,
680 .restore_state = parport_mos7715_restore_state,
682 .compat_write_data = parport_mos7715_write_compat,
684 .nibble_read_data = parport_ieee1284_read_nibble,
685 .byte_read_data = parport_ieee1284_read_byte,
689 * Allocate and initialize parallel port control struct, initialize
690 * the parallel port hardware device, and register with the parport subsystem.
692 static int mos7715_parport_init(struct usb_serial *serial)
694 struct mos7715_parport *mos_parport;
696 /* allocate and initialize parallel port control struct */
697 mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
698 if (mos_parport == NULL) {
699 dbg("mos7715_parport_init: kzalloc failed");
702 mos_parport->msg_pending = false;
703 kref_init(&mos_parport->ref_count);
704 spin_lock_init(&mos_parport->listlock);
705 INIT_LIST_HEAD(&mos_parport->active_urbs);
706 INIT_LIST_HEAD(&mos_parport->deferred_urbs);
707 usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
708 mos_parport->serial = serial;
709 tasklet_init(&mos_parport->urb_tasklet, send_deferred_urbs,
710 (unsigned long) mos_parport);
711 init_completion(&mos_parport->syncmsg_compl);
713 /* cycle parallel port reset bit */
714 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x80);
715 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x00);
717 /* initialize device registers */
718 mos_parport->shadowDCR = DCR_INIT_VAL;
719 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
720 mos_parport->shadowECR = ECR_INIT_VAL;
721 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
723 /* register with parport core */
724 mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE,
726 &parport_mos7715_ops);
727 if (mos_parport->pp == NULL) {
728 dev_err(&serial->interface->dev,
729 "Could not register parport\n");
730 kref_put(&mos_parport->ref_count, destroy_mos_parport);
733 mos_parport->pp->private_data = mos_parport;
734 mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP;
735 mos_parport->pp->dev = &serial->interface->dev;
736 parport_announce_port(mos_parport->pp);
740 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
743 * mos7720_interrupt_callback
744 * this is the callback function for when we have received data on the
745 * interrupt endpoint.
747 static void mos7720_interrupt_callback(struct urb *urb)
751 int status = urb->status;
763 /* this urb is terminated, clean up */
764 dbg("%s - urb shutting down with status: %d", __func__,
768 dbg("%s - nonzero urb status received: %d", __func__,
773 length = urb->actual_length;
774 data = urb->transfer_buffer;
776 /* Moschip get 4 bytes
777 * Byte 1 IIR Port 1 (port.number is 0)
778 * Byte 2 IIR Port 2 (port.number is 1)
779 * Byte 3 --------------
780 * Byte 4 FIFO status for both */
782 /* the above description is inverted
783 * oneukum 2007-03-14 */
785 if (unlikely(length != 4)) {
786 dbg("Wrong data !!!");
793 if ((sp1 | sp2) & 0x01) {
794 /* No Interrupt Pending in both the ports */
795 dbg("No Interrupt !!!");
797 switch (sp1 & 0x0f) {
799 dbg("Serial Port 1: Receiver status error or address "
800 "bit detected in 9-bit mode\n");
803 dbg("Serial Port 1: Receiver time out");
806 /* dbg("Serial Port 1: Modem status change"); */
810 switch (sp2 & 0x0f) {
812 dbg("Serial Port 2: Receiver status error or address "
813 "bit detected in 9-bit mode");
816 dbg("Serial Port 2: Receiver time out");
819 /* dbg("Serial Port 2: Modem status change"); */
825 result = usb_submit_urb(urb, GFP_ATOMIC);
827 dev_err(&urb->dev->dev,
828 "%s - Error %d submitting control urb\n",
833 * mos7715_interrupt_callback
834 * this is the 7715's callback function for when we have received data on
835 * the interrupt endpoint.
837 static void mos7715_interrupt_callback(struct urb *urb)
841 int status = urb->status;
853 /* this urb is terminated, clean up */
854 dbg("%s - urb shutting down with status: %d", __func__,
858 dbg("%s - nonzero urb status received: %d", __func__,
863 length = urb->actual_length;
864 data = urb->transfer_buffer;
866 /* Structure of data from 7715 device:
867 * Byte 1: IIR serial Port
869 * Byte 2: DSR parallel port
870 * Byte 4: FIFO status for both */
872 if (unlikely(length != 4)) {
873 dbg("Wrong data !!!");
878 if (!(iir & 0x01)) { /* serial port interrupt pending */
879 switch (iir & 0x0f) {
881 dbg("Serial Port: Receiver status error or address "
882 "bit detected in 9-bit mode\n");
885 dbg("Serial Port: Receiver time out");
888 /* dbg("Serial Port: Modem status change"); */
893 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
894 { /* update local copy of DSR reg */
895 struct usb_serial_port *port = urb->context;
896 struct mos7715_parport *mos_parport = port->serial->private;
897 if (unlikely(mos_parport == NULL))
899 atomic_set(&mos_parport->shadowDSR, data[2]);
904 result = usb_submit_urb(urb, GFP_ATOMIC);
906 dev_err(&urb->dev->dev,
907 "%s - Error %d submitting control urb\n",
912 * mos7720_bulk_in_callback
913 * this is the callback function for when we have received data on the
916 static void mos7720_bulk_in_callback(struct urb *urb)
919 unsigned char *data ;
920 struct usb_serial_port *port;
921 struct tty_struct *tty;
922 int status = urb->status;
925 dbg("nonzero read bulk status received: %d", status);
931 dbg("Entering...%s", __func__);
933 data = urb->transfer_buffer;
935 tty = tty_port_tty_get(&port->port);
936 if (tty && urb->actual_length) {
937 tty_insert_flip_string(tty, data, urb->actual_length);
938 tty_flip_buffer_push(tty);
942 if (!port->read_urb) {
943 dbg("URB KILLED !!!");
947 if (port->read_urb->status != -EINPROGRESS) {
948 port->read_urb->dev = port->serial->dev;
950 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
952 dbg("usb_submit_urb(read bulk) failed, retval = %d",
958 * mos7720_bulk_out_data_callback
959 * this is the callback function for when we have finished sending serial
960 * data on the bulk out endpoint.
962 static void mos7720_bulk_out_data_callback(struct urb *urb)
964 struct moschip_port *mos7720_port;
965 struct tty_struct *tty;
966 int status = urb->status;
969 dbg("nonzero write bulk status received:%d", status);
973 mos7720_port = urb->context;
975 dbg("NULL mos7720_port pointer");
979 tty = tty_port_tty_get(&mos7720_port->port->port);
981 if (tty && mos7720_port->open)
988 * this function installs the appropriate read interrupt endpoint callback
989 * depending on whether the device is a 7720 or 7715, thus avoiding costly
990 * run-time checks in the high-frequency callback routine itself.
992 static int mos77xx_probe(struct usb_serial *serial,
993 const struct usb_device_id *id)
995 if (id->idProduct == MOSCHIP_DEVICE_ID_7715)
996 moschip7720_2port_driver.read_int_callback =
997 mos7715_interrupt_callback;
999 moschip7720_2port_driver.read_int_callback =
1000 mos7720_interrupt_callback;
1005 static int mos77xx_calc_num_ports(struct usb_serial *serial)
1007 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
1008 if (product == MOSCHIP_DEVICE_ID_7715)
1014 static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
1016 struct usb_serial *serial;
1017 struct usb_serial_port *port0;
1019 struct moschip_port *mos7720_port;
1023 int allocated_urbs = 0;
1026 serial = port->serial;
1028 mos7720_port = usb_get_serial_port_data(port);
1029 if (mos7720_port == NULL)
1032 port0 = serial->port[0];
1034 usb_clear_halt(serial->dev, port->write_urb->pipe);
1035 usb_clear_halt(serial->dev, port->read_urb->pipe);
1037 /* Initialising the write urb pool */
1038 for (j = 0; j < NUM_URBS; ++j) {
1039 urb = usb_alloc_urb(0, GFP_KERNEL);
1040 mos7720_port->write_urb_pool[j] = urb;
1043 dev_err(&port->dev, "No more urbs???\n");
1047 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1049 if (!urb->transfer_buffer) {
1051 "%s-out of memory for urb buffers.\n",
1053 usb_free_urb(mos7720_port->write_urb_pool[j]);
1054 mos7720_port->write_urb_pool[j] = NULL;
1060 if (!allocated_urbs)
1063 /* Initialize MCS7720 -- Write Init values to corresponding Registers
1075 * 0x08 : SP1/2 Control Reg
1077 port_number = port->number - port->serial->minor;
1078 read_mos_reg(serial, port_number, LSR, &data);
1080 dbg("SS::%p LSR:%x", mos7720_port, data);
1082 dbg("Check:Sending Command ..........");
1084 write_mos_reg(serial, dummy, SP1_REG, 0x02);
1085 write_mos_reg(serial, dummy, SP2_REG, 0x02);
1087 write_mos_reg(serial, port_number, IER, 0x00);
1088 write_mos_reg(serial, port_number, FCR, 0x00);
1090 write_mos_reg(serial, port_number, FCR, 0xcf);
1091 mos7720_port->shadowLCR = 0x03;
1092 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1093 mos7720_port->shadowMCR = 0x0b;
1094 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1096 write_mos_reg(serial, port_number, SP_CONTROL_REG, 0x00);
1097 read_mos_reg(serial, dummy, SP_CONTROL_REG, &data);
1098 data = data | (port->number - port->serial->minor + 1);
1099 write_mos_reg(serial, dummy, SP_CONTROL_REG, data);
1100 mos7720_port->shadowLCR = 0x83;
1101 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1102 write_mos_reg(serial, port_number, THR, 0x0c);
1103 write_mos_reg(serial, port_number, IER, 0x00);
1104 mos7720_port->shadowLCR = 0x03;
1105 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1106 write_mos_reg(serial, port_number, IER, 0x0c);
1108 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
1110 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
1111 __func__, response);
1113 /* initialize our icount structure */
1114 memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
1116 /* initialize our port settings */
1117 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
1119 /* send a open port command */
1120 mos7720_port->open = 1;
1126 * mos7720_chars_in_buffer
1127 * this function is called by the tty driver when it wants to know how many
1128 * bytes of data we currently have outstanding in the port (data that has
1129 * been written, but hasn't made it out the port yet)
1130 * If successful, we return the number of bytes left to be written in the
1132 * Otherwise we return a negative error number.
1134 static int mos7720_chars_in_buffer(struct tty_struct *tty)
1136 struct usb_serial_port *port = tty->driver_data;
1139 struct moschip_port *mos7720_port;
1141 dbg("%s:entering ...........", __func__);
1143 mos7720_port = usb_get_serial_port_data(port);
1144 if (mos7720_port == NULL) {
1145 dbg("%s:leaving ...........", __func__);
1149 for (i = 0; i < NUM_URBS; ++i) {
1150 if (mos7720_port->write_urb_pool[i] &&
1151 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
1152 chars += URB_TRANSFER_BUFFER_SIZE;
1154 dbg("%s - returns %d", __func__, chars);
1158 static void mos7720_close(struct usb_serial_port *port)
1160 struct usb_serial *serial;
1161 struct moschip_port *mos7720_port;
1164 dbg("mos7720_close:entering...");
1166 serial = port->serial;
1168 mos7720_port = usb_get_serial_port_data(port);
1169 if (mos7720_port == NULL)
1172 for (j = 0; j < NUM_URBS; ++j)
1173 usb_kill_urb(mos7720_port->write_urb_pool[j]);
1175 /* Freeing Write URBs */
1176 for (j = 0; j < NUM_URBS; ++j) {
1177 if (mos7720_port->write_urb_pool[j]) {
1178 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
1179 usb_free_urb(mos7720_port->write_urb_pool[j]);
1183 /* While closing port, shutdown all bulk read, write *
1184 * and interrupt read if they exists, otherwise nop */
1185 dbg("Shutdown bulk write");
1186 usb_kill_urb(port->write_urb);
1187 dbg("Shutdown bulk read");
1188 usb_kill_urb(port->read_urb);
1190 mutex_lock(&serial->disc_mutex);
1191 /* these commands must not be issued if the device has
1192 * been disconnected */
1193 if (!serial->disconnected) {
1194 write_mos_reg(serial, port->number - port->serial->minor,
1196 write_mos_reg(serial, port->number - port->serial->minor,
1199 mutex_unlock(&serial->disc_mutex);
1200 mos7720_port->open = 0;
1202 dbg("Leaving %s", __func__);
1205 static void mos7720_break(struct tty_struct *tty, int break_state)
1207 struct usb_serial_port *port = tty->driver_data;
1209 struct usb_serial *serial;
1210 struct moschip_port *mos7720_port;
1212 dbg("Entering %s", __func__);
1214 serial = port->serial;
1216 mos7720_port = usb_get_serial_port_data(port);
1217 if (mos7720_port == NULL)
1220 if (break_state == -1)
1221 data = mos7720_port->shadowLCR | UART_LCR_SBC;
1223 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
1225 mos7720_port->shadowLCR = data;
1226 write_mos_reg(serial, port->number - port->serial->minor,
1227 LCR, mos7720_port->shadowLCR);
1231 * mos7720_write_room
1232 * this function is called by the tty driver when it wants to know how many
1233 * bytes of data we can accept for a specific port.
1234 * If successful, we return the amount of room that we have for this port
1235 * Otherwise we return a negative error number.
1237 static int mos7720_write_room(struct tty_struct *tty)
1239 struct usb_serial_port *port = tty->driver_data;
1240 struct moschip_port *mos7720_port;
1244 dbg("%s:entering ...........", __func__);
1246 mos7720_port = usb_get_serial_port_data(port);
1247 if (mos7720_port == NULL) {
1248 dbg("%s:leaving ...........", __func__);
1252 /* FIXME: Locking */
1253 for (i = 0; i < NUM_URBS; ++i) {
1254 if (mos7720_port->write_urb_pool[i] &&
1255 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
1256 room += URB_TRANSFER_BUFFER_SIZE;
1259 dbg("%s - returns %d", __func__, room);
1263 static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
1264 const unsigned char *data, int count)
1271 struct moschip_port *mos7720_port;
1272 struct usb_serial *serial;
1274 const unsigned char *current_position = data;
1276 dbg("%s:entering ...........", __func__);
1278 serial = port->serial;
1280 mos7720_port = usb_get_serial_port_data(port);
1281 if (mos7720_port == NULL) {
1282 dbg("mos7720_port is NULL");
1286 /* try to find a free urb in the list */
1289 for (i = 0; i < NUM_URBS; ++i) {
1290 if (mos7720_port->write_urb_pool[i] &&
1291 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
1292 urb = mos7720_port->write_urb_pool[i];
1299 dbg("%s - no more free urbs", __func__);
1303 if (urb->transfer_buffer == NULL) {
1304 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1306 if (urb->transfer_buffer == NULL) {
1307 dev_err(&port->dev, "%s no more kernel memory...\n",
1312 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1314 memcpy(urb->transfer_buffer, current_position, transfer_size);
1315 usb_serial_debug_data(debug, &port->dev, __func__, transfer_size,
1316 urb->transfer_buffer);
1318 /* fill urb with data and submit */
1319 usb_fill_bulk_urb(urb, serial->dev,
1320 usb_sndbulkpipe(serial->dev,
1321 port->bulk_out_endpointAddress),
1322 urb->transfer_buffer, transfer_size,
1323 mos7720_bulk_out_data_callback, mos7720_port);
1325 /* send it down the pipe */
1326 status = usb_submit_urb(urb, GFP_ATOMIC);
1328 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
1329 "with status = %d\n", __func__, status);
1330 bytes_sent = status;
1333 bytes_sent = transfer_size;
1339 static void mos7720_throttle(struct tty_struct *tty)
1341 struct usb_serial_port *port = tty->driver_data;
1342 struct moschip_port *mos7720_port;
1345 dbg("%s- port %d", __func__, port->number);
1347 mos7720_port = usb_get_serial_port_data(port);
1349 if (mos7720_port == NULL)
1352 if (!mos7720_port->open) {
1353 dbg("port not opened");
1357 dbg("%s: Entering ..........", __func__);
1359 /* if we are implementing XON/XOFF, send the stop character */
1361 unsigned char stop_char = STOP_CHAR(tty);
1362 status = mos7720_write(tty, port, &stop_char, 1);
1367 /* if we are implementing RTS/CTS, toggle that line */
1368 if (tty->termios->c_cflag & CRTSCTS) {
1369 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
1370 write_mos_reg(port->serial, port->number - port->serial->minor,
1371 MCR, mos7720_port->shadowMCR);
1377 static void mos7720_unthrottle(struct tty_struct *tty)
1379 struct usb_serial_port *port = tty->driver_data;
1380 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1383 if (mos7720_port == NULL)
1386 if (!mos7720_port->open) {
1387 dbg("%s - port not opened", __func__);
1391 dbg("%s: Entering ..........", __func__);
1393 /* if we are implementing XON/XOFF, send the start character */
1395 unsigned char start_char = START_CHAR(tty);
1396 status = mos7720_write(tty, port, &start_char, 1);
1401 /* if we are implementing RTS/CTS, toggle that line */
1402 if (tty->termios->c_cflag & CRTSCTS) {
1403 mos7720_port->shadowMCR |= UART_MCR_RTS;
1404 write_mos_reg(port->serial, port->number - port->serial->minor,
1405 MCR, mos7720_port->shadowMCR);
1411 /* FIXME: this function does not work */
1412 static int set_higher_rates(struct moschip_port *mos7720_port,
1415 struct usb_serial_port *port;
1416 struct usb_serial *serial;
1418 enum mos_regs sp_reg;
1419 if (mos7720_port == NULL)
1422 port = mos7720_port->port;
1423 serial = port->serial;
1425 /***********************************************
1426 * Init Sequence for higher rates
1427 ***********************************************/
1428 dbg("Sending Setting Commands ..........");
1429 port_number = port->number - port->serial->minor;
1431 write_mos_reg(serial, port_number, IER, 0x00);
1432 write_mos_reg(serial, port_number, FCR, 0x00);
1433 write_mos_reg(serial, port_number, FCR, 0xcf);
1434 mos7720_port->shadowMCR = 0x0b;
1435 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1436 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x00);
1438 /***********************************************
1439 * Set for higher rates *
1440 ***********************************************/
1441 /* writing baud rate verbatum into uart clock field clearly not right */
1442 if (port_number == 0)
1446 write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
1447 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x03);
1448 mos7720_port->shadowMCR = 0x2b;
1449 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1451 /***********************************************
1453 ***********************************************/
1454 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1455 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1456 write_mos_reg(serial, port_number, DLL, 0x01);
1457 write_mos_reg(serial, port_number, DLM, 0x00);
1458 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1459 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1464 /* baud rate information */
1465 struct divisor_table_entry {
1470 /* Define table of divisors for moschip 7720 hardware *
1471 * These assume a 3.6864MHz crystal, the standard /16, and *
1473 static struct divisor_table_entry divisor_table[] = {
1475 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
1476 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
1492 /*****************************************************************************
1493 * calc_baud_rate_divisor
1494 * this function calculates the proper baud rate divisor for the specified
1496 *****************************************************************************/
1497 static int calc_baud_rate_divisor(int baudrate, int *divisor)
1505 dbg("%s - %d", __func__, baudrate);
1507 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
1508 if (divisor_table[i].baudrate == baudrate) {
1509 *divisor = divisor_table[i].divisor;
1514 /* After trying for all the standard baud rates *
1515 * Try calculating the divisor for this baud rate */
1516 if (baudrate > 75 && baudrate < 230400) {
1517 /* get the divisor */
1518 custom = (__u16)(230400L / baudrate);
1520 /* Check for round off */
1521 round1 = (__u16)(2304000L / baudrate);
1522 round = (__u16)(round1 - (custom * 10));
1527 dbg("Baud %d = %d", baudrate, custom);
1531 dbg("Baud calculation Failed...");
1536 * send_cmd_write_baud_rate
1537 * this function sends the proper command to change the baud rate of the
1540 static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1543 struct usb_serial_port *port;
1544 struct usb_serial *serial;
1547 unsigned char number;
1549 if (mos7720_port == NULL)
1552 port = mos7720_port->port;
1553 serial = port->serial;
1555 dbg("%s: Entering ..........", __func__);
1557 number = port->number - port->serial->minor;
1558 dbg("%s - port = %d, baud = %d", __func__, port->number, baudrate);
1560 /* Calculate the Divisor */
1561 status = calc_baud_rate_divisor(baudrate, &divisor);
1563 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1567 /* Enable access to divisor latch */
1568 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1569 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
1571 /* Write the divisor */
1572 write_mos_reg(serial, number, DLL, (__u8)(divisor & 0xff));
1573 write_mos_reg(serial, number, DLM, (__u8)((divisor & 0xff00) >> 8));
1575 /* Disable access to divisor latch */
1576 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1577 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
1583 * change_port_settings
1584 * This routine is called to set the UART on the device to match
1585 * the specified new settings.
1587 static void change_port_settings(struct tty_struct *tty,
1588 struct moschip_port *mos7720_port,
1589 struct ktermios *old_termios)
1591 struct usb_serial_port *port;
1592 struct usb_serial *serial;
1603 if (mos7720_port == NULL)
1606 port = mos7720_port->port;
1607 serial = port->serial;
1608 port_number = port->number - port->serial->minor;
1610 dbg("%s - port %d", __func__, port->number);
1612 if (!mos7720_port->open) {
1613 dbg("%s - port not opened", __func__);
1617 dbg("%s: Entering ..........", __func__);
1619 lData = UART_LCR_WLEN8;
1620 lStop = 0x00; /* 1 stop bit */
1621 lParity = 0x00; /* No parity */
1623 cflag = tty->termios->c_cflag;
1624 iflag = tty->termios->c_iflag;
1626 /* Change the number of bits */
1627 switch (cflag & CSIZE) {
1629 lData = UART_LCR_WLEN5;
1634 lData = UART_LCR_WLEN6;
1639 lData = UART_LCR_WLEN7;
1644 lData = UART_LCR_WLEN8;
1648 /* Change the Parity bit */
1649 if (cflag & PARENB) {
1650 if (cflag & PARODD) {
1651 lParity = UART_LCR_PARITY;
1652 dbg("%s - parity = odd", __func__);
1654 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
1655 dbg("%s - parity = even", __func__);
1659 dbg("%s - parity = none", __func__);
1663 lParity = lParity | 0x20;
1665 /* Change the Stop bit */
1666 if (cflag & CSTOPB) {
1667 lStop = UART_LCR_STOP;
1668 dbg("%s - stop bits = 2", __func__);
1671 dbg("%s - stop bits = 1", __func__);
1674 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1675 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1676 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
1678 /* Update the LCR with the correct value */
1679 mos7720_port->shadowLCR &=
1680 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1681 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1684 /* Disable Interrupts */
1685 write_mos_reg(serial, port_number, IER, 0x00);
1686 write_mos_reg(serial, port_number, FCR, 0x00);
1687 write_mos_reg(serial, port_number, FCR, 0xcf);
1689 /* Send the updated LCR value to the mos7720 */
1690 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1691 mos7720_port->shadowMCR = 0x0b;
1692 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1694 /* set up the MCR register and send it to the mos7720 */
1695 mos7720_port->shadowMCR = UART_MCR_OUT2;
1697 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1699 if (cflag & CRTSCTS) {
1700 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
1701 /* To set hardware flow control to the specified *
1702 * serial port, in SP1/2_CONTROL_REG */
1704 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x01);
1706 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x02);
1709 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1711 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1713 /* Determine divisor based on baud rate */
1714 baud = tty_get_baud_rate(tty);
1716 /* pick a default, any default... */
1717 dbg("Picked default baud...");
1721 if (baud >= 230400) {
1722 set_higher_rates(mos7720_port, baud);
1723 /* Enable Interrupts */
1724 write_mos_reg(serial, port_number, IER, 0x0c);
1728 dbg("%s - baud rate = %d", __func__, baud);
1729 status = send_cmd_write_baud_rate(mos7720_port, baud);
1730 /* FIXME: needs to write actual resulting baud back not just
1733 tty_encode_baud_rate(tty, baud, baud);
1734 /* Enable Interrupts */
1735 write_mos_reg(serial, port_number, IER, 0x0c);
1737 if (port->read_urb->status != -EINPROGRESS) {
1738 port->read_urb->dev = serial->dev;
1740 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1742 dbg("usb_submit_urb(read bulk) failed, status = %d",
1748 * mos7720_set_termios
1749 * this function is called by the tty driver when it wants to change the
1750 * termios structure.
1752 static void mos7720_set_termios(struct tty_struct *tty,
1753 struct usb_serial_port *port, struct ktermios *old_termios)
1757 struct usb_serial *serial;
1758 struct moschip_port *mos7720_port;
1760 serial = port->serial;
1762 mos7720_port = usb_get_serial_port_data(port);
1764 if (mos7720_port == NULL)
1767 if (!mos7720_port->open) {
1768 dbg("%s - port not opened", __func__);
1772 dbg("%s\n", "setting termios - ASPIRE");
1774 cflag = tty->termios->c_cflag;
1776 dbg("%s - cflag %08x iflag %08x", __func__,
1777 tty->termios->c_cflag,
1778 RELEVANT_IFLAG(tty->termios->c_iflag));
1780 dbg("%s - old cflag %08x old iflag %08x", __func__,
1781 old_termios->c_cflag,
1782 RELEVANT_IFLAG(old_termios->c_iflag));
1784 dbg("%s - port %d", __func__, port->number);
1786 /* change the port settings to the new ones specified */
1787 change_port_settings(tty, mos7720_port, old_termios);
1789 if (!port->read_urb) {
1790 dbg("%s", "URB KILLED !!!!!");
1794 if (port->read_urb->status != -EINPROGRESS) {
1795 port->read_urb->dev = serial->dev;
1796 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1798 dbg("usb_submit_urb(read bulk) failed, status = %d",
1804 * get_lsr_info - get line status register info
1806 * Purpose: Let user call ioctl() to get info when the UART physically
1807 * is emptied. On bus types like RS485, the transmitter must
1808 * release the bus after transmitting. This must be done when
1809 * the transmit shift register is empty, not be done when the
1810 * transmit holding register is empty. This functionality
1811 * allows an RS485 driver to be written in user space.
1813 static int get_lsr_info(struct tty_struct *tty,
1814 struct moschip_port *mos7720_port, unsigned int __user *value)
1816 struct usb_serial_port *port = tty->driver_data;
1817 unsigned int result = 0;
1818 unsigned char data = 0;
1819 int port_number = port->number - port->serial->minor;
1822 count = mos7720_chars_in_buffer(tty);
1824 read_mos_reg(port->serial, port_number, LSR, &data);
1825 if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1826 == (UART_LSR_TEMT | UART_LSR_THRE)) {
1827 dbg("%s -- Empty", __func__);
1828 result = TIOCSER_TEMT;
1831 if (copy_to_user(value, &result, sizeof(int)))
1836 static int mos7720_tiocmget(struct tty_struct *tty, struct file *file)
1838 struct usb_serial_port *port = tty->driver_data;
1839 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1840 unsigned int result = 0;
1844 dbg("%s - port %d", __func__, port->number);
1846 mcr = mos7720_port->shadowMCR;
1847 msr = mos7720_port->shadowMSR;
1849 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1850 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1851 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1852 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
1853 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1854 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
1856 dbg("%s -- %x", __func__, result);
1861 static int mos7720_tiocmset(struct tty_struct *tty, struct file *file,
1862 unsigned int set, unsigned int clear)
1864 struct usb_serial_port *port = tty->driver_data;
1865 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1867 dbg("%s - port %d", __func__, port->number);
1868 dbg("he was at tiocmget");
1870 mcr = mos7720_port->shadowMCR;
1872 if (set & TIOCM_RTS)
1873 mcr |= UART_MCR_RTS;
1874 if (set & TIOCM_DTR)
1875 mcr |= UART_MCR_DTR;
1876 if (set & TIOCM_LOOP)
1877 mcr |= UART_MCR_LOOP;
1879 if (clear & TIOCM_RTS)
1880 mcr &= ~UART_MCR_RTS;
1881 if (clear & TIOCM_DTR)
1882 mcr &= ~UART_MCR_DTR;
1883 if (clear & TIOCM_LOOP)
1884 mcr &= ~UART_MCR_LOOP;
1886 mos7720_port->shadowMCR = mcr;
1887 write_mos_reg(port->serial, port->number - port->serial->minor,
1888 MCR, mos7720_port->shadowMCR);
1893 static int mos7720_get_icount(struct tty_struct *tty,
1894 struct serial_icounter_struct *icount)
1896 struct usb_serial_port *port = tty->driver_data;
1897 struct moschip_port *mos7720_port;
1898 struct async_icount cnow;
1900 mos7720_port = usb_get_serial_port_data(port);
1901 cnow = mos7720_port->icount;
1903 icount->cts = cnow.cts;
1904 icount->dsr = cnow.dsr;
1905 icount->rng = cnow.rng;
1906 icount->dcd = cnow.dcd;
1907 icount->rx = cnow.rx;
1908 icount->tx = cnow.tx;
1909 icount->frame = cnow.frame;
1910 icount->overrun = cnow.overrun;
1911 icount->parity = cnow.parity;
1912 icount->brk = cnow.brk;
1913 icount->buf_overrun = cnow.buf_overrun;
1915 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
1916 port->number, icount->rx, icount->tx);
1920 static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1921 unsigned int __user *value)
1926 struct usb_serial_port *port;
1928 if (mos7720_port == NULL)
1931 port = (struct usb_serial_port *)mos7720_port->port;
1932 mcr = mos7720_port->shadowMCR;
1934 if (copy_from_user(&arg, value, sizeof(int)))
1939 if (arg & TIOCM_RTS)
1940 mcr |= UART_MCR_RTS;
1941 if (arg & TIOCM_DTR)
1942 mcr |= UART_MCR_RTS;
1943 if (arg & TIOCM_LOOP)
1944 mcr |= UART_MCR_LOOP;
1948 if (arg & TIOCM_RTS)
1949 mcr &= ~UART_MCR_RTS;
1950 if (arg & TIOCM_DTR)
1951 mcr &= ~UART_MCR_RTS;
1952 if (arg & TIOCM_LOOP)
1953 mcr &= ~UART_MCR_LOOP;
1958 mos7720_port->shadowMCR = mcr;
1959 write_mos_reg(port->serial, port->number - port->serial->minor,
1960 MCR, mos7720_port->shadowMCR);
1965 static int get_serial_info(struct moschip_port *mos7720_port,
1966 struct serial_struct __user *retinfo)
1968 struct serial_struct tmp;
1973 memset(&tmp, 0, sizeof(tmp));
1975 tmp.type = PORT_16550A;
1976 tmp.line = mos7720_port->port->serial->minor;
1977 tmp.port = mos7720_port->port->number;
1979 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1980 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1981 tmp.baud_base = 9600;
1982 tmp.close_delay = 5*HZ;
1983 tmp.closing_wait = 30*HZ;
1985 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1990 static int mos7720_ioctl(struct tty_struct *tty, struct file *file,
1991 unsigned int cmd, unsigned long arg)
1993 struct usb_serial_port *port = tty->driver_data;
1994 struct moschip_port *mos7720_port;
1995 struct async_icount cnow;
1996 struct async_icount cprev;
1998 mos7720_port = usb_get_serial_port_data(port);
1999 if (mos7720_port == NULL)
2002 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
2006 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
2007 return get_lsr_info(tty, mos7720_port,
2008 (unsigned int __user *)arg);
2011 /* FIXME: These should be using the mode methods */
2014 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET",
2015 __func__, port->number);
2016 return set_modem_info(mos7720_port, cmd,
2017 (unsigned int __user *)arg);
2020 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
2021 return get_serial_info(mos7720_port,
2022 (struct serial_struct __user *)arg);
2025 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
2026 cprev = mos7720_port->icount;
2028 if (signal_pending(current))
2029 return -ERESTARTSYS;
2030 cnow = mos7720_port->icount;
2031 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2032 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2033 return -EIO; /* no change => error */
2034 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2035 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2036 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2037 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2046 return -ENOIOCTLCMD;
2049 static int mos7720_startup(struct usb_serial *serial)
2051 struct moschip_port *mos7720_port;
2052 struct usb_device *dev;
2055 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
2058 dbg("%s: Entering ..........", __func__);
2061 dbg("Invalid Handler");
2068 * The 7715 uses the first bulk in/out endpoint pair for the parallel
2069 * port, and the second for the serial port. Because the usbserial core
2070 * assumes both pairs are serial ports, we must engage in a bit of
2071 * subterfuge and swap the pointers for ports 0 and 1 in order to make
2072 * port 0 point to the serial port. However, both moschip devices use a
2073 * single interrupt-in endpoint for both ports (as mentioned a little
2074 * further down), and this endpoint was assigned to port 0. So after
2075 * the swap, we must copy the interrupt endpoint elements from port 1
2076 * (as newly assigned) to port 0, and null out port 1 pointers.
2078 if (product == MOSCHIP_DEVICE_ID_7715) {
2079 struct usb_serial_port *tmp = serial->port[0];
2080 serial->port[0] = serial->port[1];
2081 serial->port[1] = tmp;
2082 serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb;
2083 serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer;
2084 serial->port[0]->interrupt_in_endpointAddress =
2085 tmp->interrupt_in_endpointAddress;
2086 serial->port[1]->interrupt_in_urb = NULL;
2087 serial->port[1]->interrupt_in_buffer = NULL;
2091 /* set up serial port private structures */
2092 for (i = 0; i < serial->num_ports; ++i) {
2093 mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2094 if (mos7720_port == NULL) {
2095 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
2099 /* Initialize all port interrupt end point to port 0 int
2100 * endpoint. Our device has only one interrupt endpoint
2101 * common to all ports */
2102 serial->port[i]->interrupt_in_endpointAddress =
2103 serial->port[0]->interrupt_in_endpointAddress;
2105 mos7720_port->port = serial->port[i];
2106 usb_set_serial_port_data(serial->port[i], mos7720_port);
2108 dbg("port number is %d", serial->port[i]->number);
2109 dbg("serial number is %d", serial->minor);
2113 /* setting configuration feature to one */
2114 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2115 (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5*HZ);
2117 /* start the interrupt urb */
2118 ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
2121 "%s - Error %d submitting control urb\n",
2124 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
2125 if (product == MOSCHIP_DEVICE_ID_7715) {
2126 ret_val = mos7715_parport_init(serial);
2131 /* LSR For Port 1 */
2132 read_mos_reg(serial, 0, LSR, &data);
2133 dbg("LSR:%x", data);
2138 static void mos7720_release(struct usb_serial *serial)
2142 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
2143 /* close the parallel port */
2145 if (le16_to_cpu(serial->dev->descriptor.idProduct)
2146 == MOSCHIP_DEVICE_ID_7715) {
2147 struct urbtracker *urbtrack;
2148 unsigned long flags;
2149 struct mos7715_parport *mos_parport =
2150 usb_get_serial_data(serial);
2152 /* prevent NULL ptr dereference in port callbacks */
2153 spin_lock(&release_lock);
2154 mos_parport->pp->private_data = NULL;
2155 spin_unlock(&release_lock);
2157 /* wait for synchronous usb calls to return */
2158 if (mos_parport->msg_pending)
2159 wait_for_completion_timeout(&mos_parport->syncmsg_compl,
2162 parport_remove_port(mos_parport->pp);
2163 usb_set_serial_data(serial, NULL);
2164 mos_parport->serial = NULL;
2166 /* if tasklet currently scheduled, wait for it to complete */
2167 tasklet_kill(&mos_parport->urb_tasklet);
2169 /* unlink any urbs sent by the tasklet */
2170 spin_lock_irqsave(&mos_parport->listlock, flags);
2171 list_for_each_entry(urbtrack,
2172 &mos_parport->active_urbs,
2174 usb_unlink_urb(urbtrack->urb);
2175 spin_unlock_irqrestore(&mos_parport->listlock, flags);
2177 kref_put(&mos_parport->ref_count, destroy_mos_parport);
2180 /* free private structure allocated for serial port */
2181 for (i = 0; i < serial->num_ports; ++i)
2182 kfree(usb_get_serial_port_data(serial->port[i]));
2185 static struct usb_driver usb_driver = {
2186 .name = "moschip7720",
2187 .probe = usb_serial_probe,
2188 .disconnect = usb_serial_disconnect,
2189 .id_table = moschip_port_id_table,
2193 static struct usb_serial_driver moschip7720_2port_driver = {
2195 .owner = THIS_MODULE,
2196 .name = "moschip7720",
2198 .description = "Moschip 2 port adapter",
2199 .usb_driver = &usb_driver,
2200 .id_table = moschip_port_id_table,
2201 .calc_num_ports = mos77xx_calc_num_ports,
2202 .open = mos7720_open,
2203 .close = mos7720_close,
2204 .throttle = mos7720_throttle,
2205 .unthrottle = mos7720_unthrottle,
2206 .probe = mos77xx_probe,
2207 .attach = mos7720_startup,
2208 .release = mos7720_release,
2209 .ioctl = mos7720_ioctl,
2210 .tiocmget = mos7720_tiocmget,
2211 .tiocmset = mos7720_tiocmset,
2212 .get_icount = mos7720_get_icount,
2213 .set_termios = mos7720_set_termios,
2214 .write = mos7720_write,
2215 .write_room = mos7720_write_room,
2216 .chars_in_buffer = mos7720_chars_in_buffer,
2217 .break_ctl = mos7720_break,
2218 .read_bulk_callback = mos7720_bulk_in_callback,
2219 .read_int_callback = NULL /* dynamically assigned in probe() */
2222 static int __init moschip7720_init(void)
2226 dbg("%s: Entering ..........", __func__);
2228 /* Register with the usb serial */
2229 retval = usb_serial_register(&moschip7720_2port_driver);
2231 goto failed_port_device_register;
2233 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
2236 /* Register with the usb */
2237 retval = usb_register(&usb_driver);
2239 goto failed_usb_register;
2243 failed_usb_register:
2244 usb_serial_deregister(&moschip7720_2port_driver);
2246 failed_port_device_register:
2250 static void __exit moschip7720_exit(void)
2252 usb_deregister(&usb_driver);
2253 usb_serial_deregister(&moschip7720_2port_driver);
2256 module_init(moschip7720_init);
2257 module_exit(moschip7720_exit);
2259 /* Module information */
2260 MODULE_AUTHOR(DRIVER_AUTHOR);
2261 MODULE_DESCRIPTION(DRIVER_DESC);
2262 MODULE_LICENSE("GPL");
2264 module_param(debug, bool, S_IRUGO | S_IWUSR);
2265 MODULE_PARM_DESC(debug, "Debug enabled or not");