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 (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
];
74 static struct usb_serial_driver moschip7720_2port_driver
;
76 #define USB_VENDOR_ID_MOSCHIP 0x9710
77 #define MOSCHIP_DEVICE_ID_7720 0x7720
78 #define MOSCHIP_DEVICE_ID_7715 0x7715
80 static const struct usb_device_id id_table
[] = {
81 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7720
) },
82 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7715
) },
83 { } /* terminating entry */
85 MODULE_DEVICE_TABLE(usb
, id_table
);
87 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
89 /* initial values for parport regs */
90 #define DCR_INIT_VAL 0x0c /* SLCTIN, nINIT */
91 #define ECR_INIT_VAL 0x00 /* SPP mode */
94 struct mos7715_parport
*mos_parport
;
95 struct list_head urblist_entry
;
96 struct kref ref_count
;
100 enum mos7715_pp_modes
{
102 PS2
= 1<<5, /* moschip calls this 'NIBBLE' mode */
103 PPF
= 2<<5, /* moschip calls this 'CB-FIFO mode */
106 struct mos7715_parport
{
107 struct parport
*pp
; /* back to containing struct */
108 struct kref ref_count
; /* to instance of this struct */
109 struct list_head deferred_urbs
; /* list deferred async urbs */
110 struct list_head active_urbs
; /* list async urbs in flight */
111 spinlock_t listlock
; /* protects list access */
112 bool msg_pending
; /* usb sync call pending */
113 struct completion syncmsg_compl
; /* usb sync call completed */
114 struct tasklet_struct urb_tasklet
; /* for sending deferred urbs */
115 struct usb_serial
*serial
; /* back to containing struct */
116 __u8 shadowECR
; /* parallel port regs... */
118 atomic_t shadowDSR
; /* updated in int-in callback */
121 /* lock guards against dereferencing NULL ptr in parport ops callbacks */
122 static DEFINE_SPINLOCK(release_lock
);
124 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
126 static const unsigned int dummy
; /* for clarity in register access fns */
129 THR
, /* serial port regs */
141 DPR
, /* parallel port regs */
145 SP1_REG
, /* device control regs */
146 SP2_REG
, /* serial port 2 (7720 only) */
152 * Return the correct value for the Windex field of the setup packet
153 * for a control endpoint message. See the 7715 datasheet.
155 static inline __u16
get_reg_index(enum mos_regs reg
)
157 static const __u16 mos7715_index_lookup_table
[] = {
175 0x02, /* SP2_REG (7720 only) */
176 0x04, /* PP_REG (7715 only) */
177 0x08, /* SP_CONTROL_REG */
179 return mos7715_index_lookup_table
[reg
];
183 * Return the correct value for the upper byte of the Wvalue field of
184 * the setup packet for a control endpoint message.
186 static inline __u16
get_reg_value(enum mos_regs reg
,
187 unsigned int serial_portnum
)
189 if (reg
>= SP1_REG
) /* control reg */
192 else if (reg
>= DPR
) /* parallel port reg (7715 only) */
195 else /* serial port reg */
196 return (serial_portnum
+ 2) << 8;
200 * Write data byte to the specified device register. The data is embedded in
201 * the value field of the setup packet. serial_portnum is ignored for registers
202 * not specific to a particular serial port.
204 static int write_mos_reg(struct usb_serial
*serial
, unsigned int serial_portnum
,
205 enum mos_regs reg
, __u8 data
)
207 struct usb_device
*usbdev
= serial
->dev
;
208 unsigned int pipe
= usb_sndctrlpipe(usbdev
, 0);
209 __u8 request
= (__u8
)0x0e;
210 __u8 requesttype
= (__u8
)0x40;
211 __u16 index
= get_reg_index(reg
);
212 __u16 value
= get_reg_value(reg
, serial_portnum
) + data
;
213 int status
= usb_control_msg(usbdev
, pipe
, request
, requesttype
, value
,
214 index
, NULL
, 0, MOS_WDR_TIMEOUT
);
216 dev_err(&usbdev
->dev
,
217 "mos7720: usb_control_msg() failed: %d", status
);
222 * Read data byte from the specified device register. The data returned by the
223 * device is embedded in the value field of the setup packet. serial_portnum is
224 * ignored for registers that are not specific to a particular serial port.
226 static int read_mos_reg(struct usb_serial
*serial
, unsigned int serial_portnum
,
227 enum mos_regs reg
, __u8
*data
)
229 struct usb_device
*usbdev
= serial
->dev
;
230 unsigned int pipe
= usb_rcvctrlpipe(usbdev
, 0);
231 __u8 request
= (__u8
)0x0d;
232 __u8 requesttype
= (__u8
)0xc0;
233 __u16 index
= get_reg_index(reg
);
234 __u16 value
= get_reg_value(reg
, serial_portnum
);
235 int status
= usb_control_msg(usbdev
, pipe
, request
, requesttype
, value
,
236 index
, data
, 1, MOS_WDR_TIMEOUT
);
238 dev_err(&usbdev
->dev
,
239 "mos7720: usb_control_msg() failed: %d", status
);
243 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
245 static inline int mos7715_change_mode(struct mos7715_parport
*mos_parport
,
246 enum mos7715_pp_modes mode
)
248 mos_parport
->shadowECR
= mode
;
249 write_mos_reg(mos_parport
->serial
, dummy
, ECR
, mos_parport
->shadowECR
);
253 static void destroy_mos_parport(struct kref
*kref
)
255 struct mos7715_parport
*mos_parport
=
256 container_of(kref
, struct mos7715_parport
, ref_count
);
261 static void destroy_urbtracker(struct kref
*kref
)
263 struct urbtracker
*urbtrack
=
264 container_of(kref
, struct urbtracker
, ref_count
);
265 struct mos7715_parport
*mos_parport
= urbtrack
->mos_parport
;
267 usb_free_urb(urbtrack
->urb
);
269 kref_put(&mos_parport
->ref_count
, destroy_mos_parport
);
273 * This runs as a tasklet when sending an urb in a non-blocking parallel
274 * port callback had to be deferred because the disconnect mutex could not be
275 * obtained at the time.
277 static void send_deferred_urbs(unsigned long _mos_parport
)
281 struct mos7715_parport
*mos_parport
= (void *)_mos_parport
;
282 struct urbtracker
*urbtrack
, *tmp
;
283 struct list_head
*cursor
, *next
;
286 /* if release function ran, game over */
287 if (unlikely(mos_parport
->serial
== NULL
))
290 dev
= &mos_parport
->serial
->dev
->dev
;
292 /* try again to get the mutex */
293 if (!mutex_trylock(&mos_parport
->serial
->disc_mutex
)) {
294 dev_dbg(dev
, "%s: rescheduling tasklet\n", __func__
);
295 tasklet_schedule(&mos_parport
->urb_tasklet
);
299 /* if device disconnected, game over */
300 if (unlikely(mos_parport
->serial
->disconnected
)) {
301 mutex_unlock(&mos_parport
->serial
->disc_mutex
);
305 spin_lock_irqsave(&mos_parport
->listlock
, flags
);
306 if (list_empty(&mos_parport
->deferred_urbs
)) {
307 spin_unlock_irqrestore(&mos_parport
->listlock
, flags
);
308 mutex_unlock(&mos_parport
->serial
->disc_mutex
);
309 dev_dbg(dev
, "%s: deferred_urbs list empty\n", __func__
);
313 /* move contents of deferred_urbs list to active_urbs list and submit */
314 list_for_each_safe(cursor
, next
, &mos_parport
->deferred_urbs
)
315 list_move_tail(cursor
, &mos_parport
->active_urbs
);
316 list_for_each_entry_safe(urbtrack
, tmp
, &mos_parport
->active_urbs
,
318 ret_val
= usb_submit_urb(urbtrack
->urb
, GFP_ATOMIC
);
319 dev_dbg(dev
, "%s: urb submitted\n", __func__
);
321 dev_err(dev
, "usb_submit_urb() failed: %d\n", ret_val
);
322 list_del(&urbtrack
->urblist_entry
);
323 kref_put(&urbtrack
->ref_count
, destroy_urbtracker
);
326 spin_unlock_irqrestore(&mos_parport
->listlock
, flags
);
327 mutex_unlock(&mos_parport
->serial
->disc_mutex
);
330 /* callback for parallel port control urbs submitted asynchronously */
331 static void async_complete(struct urb
*urb
)
333 struct urbtracker
*urbtrack
= urb
->context
;
334 int status
= urb
->status
;
336 if (unlikely(status
))
337 dev_dbg(&urb
->dev
->dev
, "%s - nonzero urb status received: %d\n", __func__
, status
);
339 /* remove the urbtracker from the active_urbs list */
340 spin_lock(&urbtrack
->mos_parport
->listlock
);
341 list_del(&urbtrack
->urblist_entry
);
342 spin_unlock(&urbtrack
->mos_parport
->listlock
);
343 kref_put(&urbtrack
->ref_count
, destroy_urbtracker
);
346 static int write_parport_reg_nonblock(struct mos7715_parport
*mos_parport
,
347 enum mos_regs reg
, __u8 data
)
349 struct urbtracker
*urbtrack
;
352 struct usb_ctrlrequest setup
;
353 struct usb_serial
*serial
= mos_parport
->serial
;
354 struct usb_device
*usbdev
= serial
->dev
;
356 /* create and initialize the control urb and containing urbtracker */
357 urbtrack
= kmalloc(sizeof(struct urbtracker
), GFP_ATOMIC
);
358 if (urbtrack
== NULL
) {
359 dev_err(&usbdev
->dev
, "out of memory");
362 kref_get(&mos_parport
->ref_count
);
363 urbtrack
->mos_parport
= mos_parport
;
364 urbtrack
->urb
= usb_alloc_urb(0, GFP_ATOMIC
);
365 if (urbtrack
->urb
== NULL
) {
366 dev_err(&usbdev
->dev
, "out of urbs");
370 setup
.bRequestType
= (__u8
)0x40;
371 setup
.bRequest
= (__u8
)0x0e;
372 setup
.wValue
= get_reg_value(reg
, dummy
);
373 setup
.wIndex
= get_reg_index(reg
);
375 usb_fill_control_urb(urbtrack
->urb
, usbdev
,
376 usb_sndctrlpipe(usbdev
, 0),
377 (unsigned char *)&setup
,
378 NULL
, 0, async_complete
, urbtrack
);
379 kref_init(&urbtrack
->ref_count
);
380 INIT_LIST_HEAD(&urbtrack
->urblist_entry
);
383 * get the disconnect mutex, or add tracker to the deferred_urbs list
384 * and schedule a tasklet to try again later
386 if (!mutex_trylock(&serial
->disc_mutex
)) {
387 spin_lock_irqsave(&mos_parport
->listlock
, flags
);
388 list_add_tail(&urbtrack
->urblist_entry
,
389 &mos_parport
->deferred_urbs
);
390 spin_unlock_irqrestore(&mos_parport
->listlock
, flags
);
391 tasklet_schedule(&mos_parport
->urb_tasklet
);
392 dev_dbg(&usbdev
->dev
, "tasklet scheduled");
396 /* bail if device disconnected */
397 if (serial
->disconnected
) {
398 kref_put(&urbtrack
->ref_count
, destroy_urbtracker
);
399 mutex_unlock(&serial
->disc_mutex
);
403 /* add the tracker to the active_urbs list and submit */
404 spin_lock_irqsave(&mos_parport
->listlock
, flags
);
405 list_add_tail(&urbtrack
->urblist_entry
, &mos_parport
->active_urbs
);
406 spin_unlock_irqrestore(&mos_parport
->listlock
, flags
);
407 ret_val
= usb_submit_urb(urbtrack
->urb
, GFP_ATOMIC
);
408 mutex_unlock(&serial
->disc_mutex
);
410 dev_err(&usbdev
->dev
,
411 "%s: submit_urb() failed: %d", __func__
, ret_val
);
412 spin_lock_irqsave(&mos_parport
->listlock
, flags
);
413 list_del(&urbtrack
->urblist_entry
);
414 spin_unlock_irqrestore(&mos_parport
->listlock
, flags
);
415 kref_put(&urbtrack
->ref_count
, destroy_urbtracker
);
422 * This is the the common top part of all parallel port callback operations that
423 * send synchronous messages to the device. This implements convoluted locking
424 * that avoids two scenarios: (1) a port operation is called after usbserial
425 * has called our release function, at which point struct mos7715_parport has
426 * been destroyed, and (2) the device has been disconnected, but usbserial has
427 * not called the release function yet because someone has a serial port open.
428 * The shared release_lock prevents the first, and the mutex and disconnected
429 * flag maintained by usbserial covers the second. We also use the msg_pending
430 * flag to ensure that all synchronous usb messgage calls have completed before
431 * our release function can return.
433 static int parport_prologue(struct parport
*pp
)
435 struct mos7715_parport
*mos_parport
;
437 spin_lock(&release_lock
);
438 mos_parport
= pp
->private_data
;
439 if (unlikely(mos_parport
== NULL
)) {
440 /* release fn called, port struct destroyed */
441 spin_unlock(&release_lock
);
444 mos_parport
->msg_pending
= true; /* synch usb call pending */
445 INIT_COMPLETION(mos_parport
->syncmsg_compl
);
446 spin_unlock(&release_lock
);
448 mutex_lock(&mos_parport
->serial
->disc_mutex
);
449 if (mos_parport
->serial
->disconnected
) {
450 /* device disconnected */
451 mutex_unlock(&mos_parport
->serial
->disc_mutex
);
452 mos_parport
->msg_pending
= false;
453 complete(&mos_parport
->syncmsg_compl
);
461 * This is the the common bottom part of all parallel port functions that send
462 * synchronous messages to the device.
464 static inline void parport_epilogue(struct parport
*pp
)
466 struct mos7715_parport
*mos_parport
= pp
->private_data
;
467 mutex_unlock(&mos_parport
->serial
->disc_mutex
);
468 mos_parport
->msg_pending
= false;
469 complete(&mos_parport
->syncmsg_compl
);
472 static void parport_mos7715_write_data(struct parport
*pp
, unsigned char d
)
474 struct mos7715_parport
*mos_parport
= pp
->private_data
;
476 if (parport_prologue(pp
) < 0)
478 mos7715_change_mode(mos_parport
, SPP
);
479 write_mos_reg(mos_parport
->serial
, dummy
, DPR
, (__u8
)d
);
480 parport_epilogue(pp
);
483 static unsigned char parport_mos7715_read_data(struct parport
*pp
)
485 struct mos7715_parport
*mos_parport
= pp
->private_data
;
488 if (parport_prologue(pp
) < 0)
490 read_mos_reg(mos_parport
->serial
, dummy
, DPR
, &d
);
491 parport_epilogue(pp
);
495 static void parport_mos7715_write_control(struct parport
*pp
, unsigned char d
)
497 struct mos7715_parport
*mos_parport
= pp
->private_data
;
500 if (parport_prologue(pp
) < 0)
502 data
= ((__u8
)d
& 0x0f) | (mos_parport
->shadowDCR
& 0xf0);
503 write_mos_reg(mos_parport
->serial
, dummy
, DCR
, data
);
504 mos_parport
->shadowDCR
= data
;
505 parport_epilogue(pp
);
508 static unsigned char parport_mos7715_read_control(struct parport
*pp
)
510 struct mos7715_parport
*mos_parport
= pp
->private_data
;
513 spin_lock(&release_lock
);
514 mos_parport
= pp
->private_data
;
515 if (unlikely(mos_parport
== NULL
)) {
516 spin_unlock(&release_lock
);
519 dcr
= mos_parport
->shadowDCR
& 0x0f;
520 spin_unlock(&release_lock
);
524 static unsigned char parport_mos7715_frob_control(struct parport
*pp
,
528 struct mos7715_parport
*mos_parport
= pp
->private_data
;
533 if (parport_prologue(pp
) < 0)
535 mos_parport
->shadowDCR
= (mos_parport
->shadowDCR
& (~mask
)) ^ val
;
536 write_mos_reg(mos_parport
->serial
, dummy
, DCR
, mos_parport
->shadowDCR
);
537 dcr
= mos_parport
->shadowDCR
& 0x0f;
538 parport_epilogue(pp
);
542 static unsigned char parport_mos7715_read_status(struct parport
*pp
)
544 unsigned char status
;
545 struct mos7715_parport
*mos_parport
= pp
->private_data
;
547 spin_lock(&release_lock
);
548 mos_parport
= pp
->private_data
;
549 if (unlikely(mos_parport
== NULL
)) { /* release called */
550 spin_unlock(&release_lock
);
553 status
= atomic_read(&mos_parport
->shadowDSR
) & 0xf8;
554 spin_unlock(&release_lock
);
558 static void parport_mos7715_enable_irq(struct parport
*pp
)
562 static void parport_mos7715_disable_irq(struct parport
*pp
)
566 static void parport_mos7715_data_forward(struct parport
*pp
)
568 struct mos7715_parport
*mos_parport
= pp
->private_data
;
570 if (parport_prologue(pp
) < 0)
572 mos7715_change_mode(mos_parport
, PS2
);
573 mos_parport
->shadowDCR
&= ~0x20;
574 write_mos_reg(mos_parport
->serial
, dummy
, DCR
, mos_parport
->shadowDCR
);
575 parport_epilogue(pp
);
578 static void parport_mos7715_data_reverse(struct parport
*pp
)
580 struct mos7715_parport
*mos_parport
= pp
->private_data
;
582 if (parport_prologue(pp
) < 0)
584 mos7715_change_mode(mos_parport
, PS2
);
585 mos_parport
->shadowDCR
|= 0x20;
586 write_mos_reg(mos_parport
->serial
, dummy
, DCR
, mos_parport
->shadowDCR
);
587 parport_epilogue(pp
);
590 static void parport_mos7715_init_state(struct pardevice
*dev
,
591 struct parport_state
*s
)
593 s
->u
.pc
.ctr
= DCR_INIT_VAL
;
594 s
->u
.pc
.ecr
= ECR_INIT_VAL
;
597 /* N.B. Parport core code requires that this function not block */
598 static void parport_mos7715_save_state(struct parport
*pp
,
599 struct parport_state
*s
)
601 struct mos7715_parport
*mos_parport
;
603 spin_lock(&release_lock
);
604 mos_parport
= pp
->private_data
;
605 if (unlikely(mos_parport
== NULL
)) { /* release called */
606 spin_unlock(&release_lock
);
609 s
->u
.pc
.ctr
= mos_parport
->shadowDCR
;
610 s
->u
.pc
.ecr
= mos_parport
->shadowECR
;
611 spin_unlock(&release_lock
);
614 /* N.B. Parport core code requires that this function not block */
615 static void parport_mos7715_restore_state(struct parport
*pp
,
616 struct parport_state
*s
)
618 struct mos7715_parport
*mos_parport
;
620 spin_lock(&release_lock
);
621 mos_parport
= pp
->private_data
;
622 if (unlikely(mos_parport
== NULL
)) { /* release called */
623 spin_unlock(&release_lock
);
626 write_parport_reg_nonblock(mos_parport
, DCR
, mos_parport
->shadowDCR
);
627 write_parport_reg_nonblock(mos_parport
, ECR
, mos_parport
->shadowECR
);
628 spin_unlock(&release_lock
);
631 static size_t parport_mos7715_write_compat(struct parport
*pp
,
633 size_t len
, int flags
)
636 struct mos7715_parport
*mos_parport
= pp
->private_data
;
639 if (parport_prologue(pp
) < 0)
641 mos7715_change_mode(mos_parport
, PPF
);
642 retval
= usb_bulk_msg(mos_parport
->serial
->dev
,
643 usb_sndbulkpipe(mos_parport
->serial
->dev
, 2),
644 (void *)buffer
, len
, &actual_len
,
646 parport_epilogue(pp
);
648 dev_err(&mos_parport
->serial
->dev
->dev
,
649 "mos7720: usb_bulk_msg() failed: %d", retval
);
655 static struct parport_operations parport_mos7715_ops
= {
656 .owner
= THIS_MODULE
,
657 .write_data
= parport_mos7715_write_data
,
658 .read_data
= parport_mos7715_read_data
,
660 .write_control
= parport_mos7715_write_control
,
661 .read_control
= parport_mos7715_read_control
,
662 .frob_control
= parport_mos7715_frob_control
,
664 .read_status
= parport_mos7715_read_status
,
666 .enable_irq
= parport_mos7715_enable_irq
,
667 .disable_irq
= parport_mos7715_disable_irq
,
669 .data_forward
= parport_mos7715_data_forward
,
670 .data_reverse
= parport_mos7715_data_reverse
,
672 .init_state
= parport_mos7715_init_state
,
673 .save_state
= parport_mos7715_save_state
,
674 .restore_state
= parport_mos7715_restore_state
,
676 .compat_write_data
= parport_mos7715_write_compat
,
678 .nibble_read_data
= parport_ieee1284_read_nibble
,
679 .byte_read_data
= parport_ieee1284_read_byte
,
683 * Allocate and initialize parallel port control struct, initialize
684 * the parallel port hardware device, and register with the parport subsystem.
686 static int mos7715_parport_init(struct usb_serial
*serial
)
688 struct mos7715_parport
*mos_parport
;
690 /* allocate and initialize parallel port control struct */
691 mos_parport
= kzalloc(sizeof(struct mos7715_parport
), GFP_KERNEL
);
692 if (mos_parport
== NULL
) {
693 dev_dbg(&serial
->dev
->dev
, "%s: kzalloc failed\n", __func__
);
696 mos_parport
->msg_pending
= false;
697 kref_init(&mos_parport
->ref_count
);
698 spin_lock_init(&mos_parport
->listlock
);
699 INIT_LIST_HEAD(&mos_parport
->active_urbs
);
700 INIT_LIST_HEAD(&mos_parport
->deferred_urbs
);
701 usb_set_serial_data(serial
, mos_parport
); /* hijack private pointer */
702 mos_parport
->serial
= serial
;
703 tasklet_init(&mos_parport
->urb_tasklet
, send_deferred_urbs
,
704 (unsigned long) mos_parport
);
705 init_completion(&mos_parport
->syncmsg_compl
);
707 /* cycle parallel port reset bit */
708 write_mos_reg(mos_parport
->serial
, dummy
, PP_REG
, (__u8
)0x80);
709 write_mos_reg(mos_parport
->serial
, dummy
, PP_REG
, (__u8
)0x00);
711 /* initialize device registers */
712 mos_parport
->shadowDCR
= DCR_INIT_VAL
;
713 write_mos_reg(mos_parport
->serial
, dummy
, DCR
, mos_parport
->shadowDCR
);
714 mos_parport
->shadowECR
= ECR_INIT_VAL
;
715 write_mos_reg(mos_parport
->serial
, dummy
, ECR
, mos_parport
->shadowECR
);
717 /* register with parport core */
718 mos_parport
->pp
= parport_register_port(0, PARPORT_IRQ_NONE
,
720 &parport_mos7715_ops
);
721 if (mos_parport
->pp
== NULL
) {
722 dev_err(&serial
->interface
->dev
,
723 "Could not register parport\n");
724 kref_put(&mos_parport
->ref_count
, destroy_mos_parport
);
727 mos_parport
->pp
->private_data
= mos_parport
;
728 mos_parport
->pp
->modes
= PARPORT_MODE_COMPAT
| PARPORT_MODE_PCSPP
;
729 mos_parport
->pp
->dev
= &serial
->interface
->dev
;
730 parport_announce_port(mos_parport
->pp
);
734 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
737 * mos7720_interrupt_callback
738 * this is the callback function for when we have received data on the
739 * interrupt endpoint.
741 static void mos7720_interrupt_callback(struct urb
*urb
)
745 int status
= urb
->status
;
746 struct device
*dev
= &urb
->dev
->dev
;
758 /* this urb is terminated, clean up */
759 dev_dbg(dev
, "%s - urb shutting down with status: %d\n", __func__
, status
);
762 dev_dbg(dev
, "%s - nonzero urb status received: %d\n", __func__
, status
);
766 length
= urb
->actual_length
;
767 data
= urb
->transfer_buffer
;
769 /* Moschip get 4 bytes
770 * Byte 1 IIR Port 1 (port.number is 0)
771 * Byte 2 IIR Port 2 (port.number is 1)
772 * Byte 3 --------------
773 * Byte 4 FIFO status for both */
775 /* the above description is inverted
776 * oneukum 2007-03-14 */
778 if (unlikely(length
!= 4)) {
779 dev_dbg(dev
, "Wrong data !!!\n");
786 if ((sp1
| sp2
) & 0x01) {
787 /* No Interrupt Pending in both the ports */
788 dev_dbg(dev
, "No Interrupt !!!\n");
790 switch (sp1
& 0x0f) {
792 dev_dbg(dev
, "Serial Port 1: Receiver status error or address bit detected in 9-bit mode\n");
795 dev_dbg(dev
, "Serial Port 1: Receiver time out\n");
798 /* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */
802 switch (sp2
& 0x0f) {
804 dev_dbg(dev
, "Serial Port 2: Receiver status error or address bit detected in 9-bit mode\n");
807 dev_dbg(dev
, "Serial Port 2: Receiver time out\n");
810 /* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */
816 result
= usb_submit_urb(urb
, GFP_ATOMIC
);
818 dev_err(dev
, "%s - Error %d submitting control urb\n", __func__
, result
);
822 * mos7715_interrupt_callback
823 * this is the 7715's callback function for when we have received data on
824 * the interrupt endpoint.
826 static void mos7715_interrupt_callback(struct urb
*urb
)
830 int status
= urb
->status
;
831 struct device
*dev
= &urb
->dev
->dev
;
843 /* this urb is terminated, clean up */
844 dev_dbg(dev
, "%s - urb shutting down with status: %d\n", __func__
, status
);
847 dev_dbg(dev
, "%s - nonzero urb status received: %d\n", __func__
, status
);
851 length
= urb
->actual_length
;
852 data
= urb
->transfer_buffer
;
854 /* Structure of data from 7715 device:
855 * Byte 1: IIR serial Port
857 * Byte 2: DSR parallel port
858 * Byte 4: FIFO status for both */
860 if (unlikely(length
!= 4)) {
861 dev_dbg(dev
, "Wrong data !!!\n");
866 if (!(iir
& 0x01)) { /* serial port interrupt pending */
867 switch (iir
& 0x0f) {
869 dev_dbg(dev
, "Serial Port: Receiver status error or address bit detected in 9-bit mode\n\n");
872 dev_dbg(dev
, "Serial Port: Receiver time out\n");
875 /* dev_dbg(dev, "Serial Port: Modem status change\n"); */
880 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
881 { /* update local copy of DSR reg */
882 struct usb_serial_port
*port
= urb
->context
;
883 struct mos7715_parport
*mos_parport
= port
->serial
->private;
884 if (unlikely(mos_parport
== NULL
))
886 atomic_set(&mos_parport
->shadowDSR
, data
[2]);
891 result
= usb_submit_urb(urb
, GFP_ATOMIC
);
893 dev_err(dev
, "%s - Error %d submitting control urb\n", __func__
, result
);
897 * mos7720_bulk_in_callback
898 * this is the callback function for when we have received data on the
901 static void mos7720_bulk_in_callback(struct urb
*urb
)
904 unsigned char *data
;
905 struct usb_serial_port
*port
;
906 struct tty_struct
*tty
;
907 int status
= urb
->status
;
910 dev_dbg(&urb
->dev
->dev
, "nonzero read bulk status received: %d\n", status
);
916 dev_dbg(&port
->dev
, "Entering...%s\n", __func__
);
918 data
= urb
->transfer_buffer
;
920 tty
= tty_port_tty_get(&port
->port
);
921 if (tty
&& urb
->actual_length
) {
922 tty_insert_flip_string(tty
, data
, urb
->actual_length
);
923 tty_flip_buffer_push(tty
);
927 if (port
->read_urb
->status
!= -EINPROGRESS
) {
928 retval
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
930 dev_dbg(&port
->dev
, "usb_submit_urb(read bulk) failed, retval = %d\n", retval
);
935 * mos7720_bulk_out_data_callback
936 * this is the callback function for when we have finished sending serial
937 * data on the bulk out endpoint.
939 static void mos7720_bulk_out_data_callback(struct urb
*urb
)
941 struct moschip_port
*mos7720_port
;
942 struct tty_struct
*tty
;
943 int status
= urb
->status
;
946 dev_dbg(&urb
->dev
->dev
, "nonzero write bulk status received:%d\n", status
);
950 mos7720_port
= urb
->context
;
952 dev_dbg(&urb
->dev
->dev
, "NULL mos7720_port pointer\n");
956 tty
= tty_port_tty_get(&mos7720_port
->port
->port
);
958 if (tty
&& mos7720_port
->open
)
965 * this function installs the appropriate read interrupt endpoint callback
966 * depending on whether the device is a 7720 or 7715, thus avoiding costly
967 * run-time checks in the high-frequency callback routine itself.
969 static int mos77xx_probe(struct usb_serial
*serial
,
970 const struct usb_device_id
*id
)
972 if (id
->idProduct
== MOSCHIP_DEVICE_ID_7715
)
973 moschip7720_2port_driver
.read_int_callback
=
974 mos7715_interrupt_callback
;
976 moschip7720_2port_driver
.read_int_callback
=
977 mos7720_interrupt_callback
;
982 static int mos77xx_calc_num_ports(struct usb_serial
*serial
)
984 u16 product
= le16_to_cpu(serial
->dev
->descriptor
.idProduct
);
985 if (product
== MOSCHIP_DEVICE_ID_7715
)
991 static int mos7720_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
993 struct usb_serial
*serial
;
995 struct moschip_port
*mos7720_port
;
999 int allocated_urbs
= 0;
1002 serial
= port
->serial
;
1004 mos7720_port
= usb_get_serial_port_data(port
);
1005 if (mos7720_port
== NULL
)
1008 usb_clear_halt(serial
->dev
, port
->write_urb
->pipe
);
1009 usb_clear_halt(serial
->dev
, port
->read_urb
->pipe
);
1011 /* Initialising the write urb pool */
1012 for (j
= 0; j
< NUM_URBS
; ++j
) {
1013 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1014 mos7720_port
->write_urb_pool
[j
] = urb
;
1017 dev_err(&port
->dev
, "No more urbs???\n");
1021 urb
->transfer_buffer
= kmalloc(URB_TRANSFER_BUFFER_SIZE
,
1023 if (!urb
->transfer_buffer
) {
1025 "%s-out of memory for urb buffers.\n",
1027 usb_free_urb(mos7720_port
->write_urb_pool
[j
]);
1028 mos7720_port
->write_urb_pool
[j
] = NULL
;
1034 if (!allocated_urbs
)
1037 /* Initialize MCS7720 -- Write Init values to corresponding Registers
1049 * 0x08 : SP1/2 Control Reg
1051 port_number
= port
->number
- port
->serial
->minor
;
1052 read_mos_reg(serial
, port_number
, LSR
, &data
);
1054 dev_dbg(&port
->dev
, "SS::%p LSR:%x\n", mos7720_port
, data
);
1056 write_mos_reg(serial
, dummy
, SP1_REG
, 0x02);
1057 write_mos_reg(serial
, dummy
, SP2_REG
, 0x02);
1059 write_mos_reg(serial
, port_number
, IER
, 0x00);
1060 write_mos_reg(serial
, port_number
, FCR
, 0x00);
1062 write_mos_reg(serial
, port_number
, FCR
, 0xcf);
1063 mos7720_port
->shadowLCR
= 0x03;
1064 write_mos_reg(serial
, port_number
, LCR
, mos7720_port
->shadowLCR
);
1065 mos7720_port
->shadowMCR
= 0x0b;
1066 write_mos_reg(serial
, port_number
, MCR
, mos7720_port
->shadowMCR
);
1068 write_mos_reg(serial
, port_number
, SP_CONTROL_REG
, 0x00);
1069 read_mos_reg(serial
, dummy
, SP_CONTROL_REG
, &data
);
1070 data
= data
| (port
->number
- port
->serial
->minor
+ 1);
1071 write_mos_reg(serial
, dummy
, SP_CONTROL_REG
, data
);
1072 mos7720_port
->shadowLCR
= 0x83;
1073 write_mos_reg(serial
, port_number
, LCR
, mos7720_port
->shadowLCR
);
1074 write_mos_reg(serial
, port_number
, THR
, 0x0c);
1075 write_mos_reg(serial
, port_number
, IER
, 0x00);
1076 mos7720_port
->shadowLCR
= 0x03;
1077 write_mos_reg(serial
, port_number
, LCR
, mos7720_port
->shadowLCR
);
1078 write_mos_reg(serial
, port_number
, IER
, 0x0c);
1080 response
= usb_submit_urb(port
->read_urb
, GFP_KERNEL
);
1082 dev_err(&port
->dev
, "%s - Error %d submitting read urb\n",
1083 __func__
, response
);
1085 /* initialize our icount structure */
1086 memset(&(mos7720_port
->icount
), 0x00, sizeof(mos7720_port
->icount
));
1088 /* initialize our port settings */
1089 mos7720_port
->shadowMCR
= UART_MCR_OUT2
; /* Must set to enable ints! */
1091 /* send a open port command */
1092 mos7720_port
->open
= 1;
1098 * mos7720_chars_in_buffer
1099 * this function is called by the tty driver when it wants to know how many
1100 * bytes of data we currently have outstanding in the port (data that has
1101 * been written, but hasn't made it out the port yet)
1102 * If successful, we return the number of bytes left to be written in the
1104 * Otherwise we return a negative error number.
1106 static int mos7720_chars_in_buffer(struct tty_struct
*tty
)
1108 struct usb_serial_port
*port
= tty
->driver_data
;
1111 struct moschip_port
*mos7720_port
;
1113 mos7720_port
= usb_get_serial_port_data(port
);
1114 if (mos7720_port
== NULL
)
1117 for (i
= 0; i
< NUM_URBS
; ++i
) {
1118 if (mos7720_port
->write_urb_pool
[i
] &&
1119 mos7720_port
->write_urb_pool
[i
]->status
== -EINPROGRESS
)
1120 chars
+= URB_TRANSFER_BUFFER_SIZE
;
1122 dev_dbg(&port
->dev
, "%s - returns %d\n", __func__
, chars
);
1126 static void mos7720_close(struct usb_serial_port
*port
)
1128 struct usb_serial
*serial
;
1129 struct moschip_port
*mos7720_port
;
1132 serial
= port
->serial
;
1134 mos7720_port
= usb_get_serial_port_data(port
);
1135 if (mos7720_port
== NULL
)
1138 for (j
= 0; j
< NUM_URBS
; ++j
)
1139 usb_kill_urb(mos7720_port
->write_urb_pool
[j
]);
1141 /* Freeing Write URBs */
1142 for (j
= 0; j
< NUM_URBS
; ++j
) {
1143 if (mos7720_port
->write_urb_pool
[j
]) {
1144 kfree(mos7720_port
->write_urb_pool
[j
]->transfer_buffer
);
1145 usb_free_urb(mos7720_port
->write_urb_pool
[j
]);
1149 /* While closing port, shutdown all bulk read, write *
1150 * and interrupt read if they exists, otherwise nop */
1151 usb_kill_urb(port
->write_urb
);
1152 usb_kill_urb(port
->read_urb
);
1154 mutex_lock(&serial
->disc_mutex
);
1155 /* these commands must not be issued if the device has
1156 * been disconnected */
1157 if (!serial
->disconnected
) {
1158 write_mos_reg(serial
, port
->number
- port
->serial
->minor
,
1160 write_mos_reg(serial
, port
->number
- port
->serial
->minor
,
1163 mutex_unlock(&serial
->disc_mutex
);
1164 mos7720_port
->open
= 0;
1167 static void mos7720_break(struct tty_struct
*tty
, int break_state
)
1169 struct usb_serial_port
*port
= tty
->driver_data
;
1171 struct usb_serial
*serial
;
1172 struct moschip_port
*mos7720_port
;
1174 serial
= port
->serial
;
1176 mos7720_port
= usb_get_serial_port_data(port
);
1177 if (mos7720_port
== NULL
)
1180 if (break_state
== -1)
1181 data
= mos7720_port
->shadowLCR
| UART_LCR_SBC
;
1183 data
= mos7720_port
->shadowLCR
& ~UART_LCR_SBC
;
1185 mos7720_port
->shadowLCR
= data
;
1186 write_mos_reg(serial
, port
->number
- port
->serial
->minor
,
1187 LCR
, mos7720_port
->shadowLCR
);
1191 * mos7720_write_room
1192 * this function is called by the tty driver when it wants to know how many
1193 * bytes of data we can accept for a specific port.
1194 * If successful, we return the amount of room that we have for this port
1195 * Otherwise we return a negative error number.
1197 static int mos7720_write_room(struct tty_struct
*tty
)
1199 struct usb_serial_port
*port
= tty
->driver_data
;
1200 struct moschip_port
*mos7720_port
;
1204 mos7720_port
= usb_get_serial_port_data(port
);
1205 if (mos7720_port
== NULL
)
1208 /* FIXME: Locking */
1209 for (i
= 0; i
< NUM_URBS
; ++i
) {
1210 if (mos7720_port
->write_urb_pool
[i
] &&
1211 mos7720_port
->write_urb_pool
[i
]->status
!= -EINPROGRESS
)
1212 room
+= URB_TRANSFER_BUFFER_SIZE
;
1215 dev_dbg(&port
->dev
, "%s - returns %d\n", __func__
, room
);
1219 static int mos7720_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
1220 const unsigned char *data
, int count
)
1227 struct moschip_port
*mos7720_port
;
1228 struct usb_serial
*serial
;
1230 const unsigned char *current_position
= data
;
1232 serial
= port
->serial
;
1234 mos7720_port
= usb_get_serial_port_data(port
);
1235 if (mos7720_port
== NULL
)
1238 /* try to find a free urb in the list */
1241 for (i
= 0; i
< NUM_URBS
; ++i
) {
1242 if (mos7720_port
->write_urb_pool
[i
] &&
1243 mos7720_port
->write_urb_pool
[i
]->status
!= -EINPROGRESS
) {
1244 urb
= mos7720_port
->write_urb_pool
[i
];
1245 dev_dbg(&port
->dev
, "URB:%d\n", i
);
1251 dev_dbg(&port
->dev
, "%s - no more free urbs\n", __func__
);
1255 if (urb
->transfer_buffer
== NULL
) {
1256 urb
->transfer_buffer
= kmalloc(URB_TRANSFER_BUFFER_SIZE
,
1258 if (urb
->transfer_buffer
== NULL
) {
1259 dev_err_console(port
, "%s no more kernel memory...\n",
1264 transfer_size
= min(count
, URB_TRANSFER_BUFFER_SIZE
);
1266 memcpy(urb
->transfer_buffer
, current_position
, transfer_size
);
1267 usb_serial_debug_data(&port
->dev
, __func__
, transfer_size
,
1268 urb
->transfer_buffer
);
1270 /* fill urb with data and submit */
1271 usb_fill_bulk_urb(urb
, serial
->dev
,
1272 usb_sndbulkpipe(serial
->dev
,
1273 port
->bulk_out_endpointAddress
),
1274 urb
->transfer_buffer
, transfer_size
,
1275 mos7720_bulk_out_data_callback
, mos7720_port
);
1277 /* send it down the pipe */
1278 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
1280 dev_err_console(port
, "%s - usb_submit_urb(write bulk) failed "
1281 "with status = %d\n", __func__
, status
);
1282 bytes_sent
= status
;
1285 bytes_sent
= transfer_size
;
1291 static void mos7720_throttle(struct tty_struct
*tty
)
1293 struct usb_serial_port
*port
= tty
->driver_data
;
1294 struct moschip_port
*mos7720_port
;
1297 mos7720_port
= usb_get_serial_port_data(port
);
1299 if (mos7720_port
== NULL
)
1302 if (!mos7720_port
->open
) {
1303 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1307 /* if we are implementing XON/XOFF, send the stop character */
1309 unsigned char stop_char
= STOP_CHAR(tty
);
1310 status
= mos7720_write(tty
, port
, &stop_char
, 1);
1315 /* if we are implementing RTS/CTS, toggle that line */
1316 if (tty
->termios
.c_cflag
& CRTSCTS
) {
1317 mos7720_port
->shadowMCR
&= ~UART_MCR_RTS
;
1318 write_mos_reg(port
->serial
, port
->number
- port
->serial
->minor
,
1319 MCR
, mos7720_port
->shadowMCR
);
1325 static void mos7720_unthrottle(struct tty_struct
*tty
)
1327 struct usb_serial_port
*port
= tty
->driver_data
;
1328 struct moschip_port
*mos7720_port
= usb_get_serial_port_data(port
);
1331 if (mos7720_port
== NULL
)
1334 if (!mos7720_port
->open
) {
1335 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1339 /* if we are implementing XON/XOFF, send the start character */
1341 unsigned char start_char
= START_CHAR(tty
);
1342 status
= mos7720_write(tty
, port
, &start_char
, 1);
1347 /* if we are implementing RTS/CTS, toggle that line */
1348 if (tty
->termios
.c_cflag
& CRTSCTS
) {
1349 mos7720_port
->shadowMCR
|= UART_MCR_RTS
;
1350 write_mos_reg(port
->serial
, port
->number
- port
->serial
->minor
,
1351 MCR
, mos7720_port
->shadowMCR
);
1357 /* FIXME: this function does not work */
1358 static int set_higher_rates(struct moschip_port
*mos7720_port
,
1361 struct usb_serial_port
*port
;
1362 struct usb_serial
*serial
;
1364 enum mos_regs sp_reg
;
1365 if (mos7720_port
== NULL
)
1368 port
= mos7720_port
->port
;
1369 serial
= port
->serial
;
1371 /***********************************************
1372 * Init Sequence for higher rates
1373 ***********************************************/
1374 dev_dbg(&port
->dev
, "Sending Setting Commands ..........\n");
1375 port_number
= port
->number
- port
->serial
->minor
;
1377 write_mos_reg(serial
, port_number
, IER
, 0x00);
1378 write_mos_reg(serial
, port_number
, FCR
, 0x00);
1379 write_mos_reg(serial
, port_number
, FCR
, 0xcf);
1380 mos7720_port
->shadowMCR
= 0x0b;
1381 write_mos_reg(serial
, port_number
, MCR
, mos7720_port
->shadowMCR
);
1382 write_mos_reg(serial
, dummy
, SP_CONTROL_REG
, 0x00);
1384 /***********************************************
1385 * Set for higher rates *
1386 ***********************************************/
1387 /* writing baud rate verbatum into uart clock field clearly not right */
1388 if (port_number
== 0)
1392 write_mos_reg(serial
, dummy
, sp_reg
, baud
* 0x10);
1393 write_mos_reg(serial
, dummy
, SP_CONTROL_REG
, 0x03);
1394 mos7720_port
->shadowMCR
= 0x2b;
1395 write_mos_reg(serial
, port_number
, MCR
, mos7720_port
->shadowMCR
);
1397 /***********************************************
1399 ***********************************************/
1400 mos7720_port
->shadowLCR
= mos7720_port
->shadowLCR
| UART_LCR_DLAB
;
1401 write_mos_reg(serial
, port_number
, LCR
, mos7720_port
->shadowLCR
);
1402 write_mos_reg(serial
, port_number
, DLL
, 0x01);
1403 write_mos_reg(serial
, port_number
, DLM
, 0x00);
1404 mos7720_port
->shadowLCR
= mos7720_port
->shadowLCR
& ~UART_LCR_DLAB
;
1405 write_mos_reg(serial
, port_number
, LCR
, mos7720_port
->shadowLCR
);
1410 /* baud rate information */
1411 struct divisor_table_entry
{
1416 /* Define table of divisors for moschip 7720 hardware *
1417 * These assume a 3.6864MHz crystal, the standard /16, and *
1419 static struct divisor_table_entry divisor_table
[] = {
1421 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
1422 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
1438 /*****************************************************************************
1439 * calc_baud_rate_divisor
1440 * this function calculates the proper baud rate divisor for the specified
1442 *****************************************************************************/
1443 static int calc_baud_rate_divisor(struct usb_serial_port
*port
, int baudrate
, int *divisor
)
1451 dev_dbg(&port
->dev
, "%s - %d\n", __func__
, baudrate
);
1453 for (i
= 0; i
< ARRAY_SIZE(divisor_table
); i
++) {
1454 if (divisor_table
[i
].baudrate
== baudrate
) {
1455 *divisor
= divisor_table
[i
].divisor
;
1460 /* After trying for all the standard baud rates *
1461 * Try calculating the divisor for this baud rate */
1462 if (baudrate
> 75 && baudrate
< 230400) {
1463 /* get the divisor */
1464 custom
= (__u16
)(230400L / baudrate
);
1466 /* Check for round off */
1467 round1
= (__u16
)(2304000L / baudrate
);
1468 round
= (__u16
)(round1
- (custom
* 10));
1473 dev_dbg(&port
->dev
, "Baud %d = %d\n", baudrate
, custom
);
1477 dev_dbg(&port
->dev
, "Baud calculation Failed...\n");
1482 * send_cmd_write_baud_rate
1483 * this function sends the proper command to change the baud rate of the
1486 static int send_cmd_write_baud_rate(struct moschip_port
*mos7720_port
,
1489 struct usb_serial_port
*port
;
1490 struct usb_serial
*serial
;
1493 unsigned char number
;
1495 if (mos7720_port
== NULL
)
1498 port
= mos7720_port
->port
;
1499 serial
= port
->serial
;
1501 number
= port
->number
- port
->serial
->minor
;
1502 dev_dbg(&port
->dev
, "%s - baud = %d\n", __func__
, baudrate
);
1504 /* Calculate the Divisor */
1505 status
= calc_baud_rate_divisor(port
, baudrate
, &divisor
);
1507 dev_err(&port
->dev
, "%s - bad baud rate\n", __func__
);
1511 /* Enable access to divisor latch */
1512 mos7720_port
->shadowLCR
= mos7720_port
->shadowLCR
| UART_LCR_DLAB
;
1513 write_mos_reg(serial
, number
, LCR
, mos7720_port
->shadowLCR
);
1515 /* Write the divisor */
1516 write_mos_reg(serial
, number
, DLL
, (__u8
)(divisor
& 0xff));
1517 write_mos_reg(serial
, number
, DLM
, (__u8
)((divisor
& 0xff00) >> 8));
1519 /* Disable access to divisor latch */
1520 mos7720_port
->shadowLCR
= mos7720_port
->shadowLCR
& ~UART_LCR_DLAB
;
1521 write_mos_reg(serial
, number
, LCR
, mos7720_port
->shadowLCR
);
1527 * change_port_settings
1528 * This routine is called to set the UART on the device to match
1529 * the specified new settings.
1531 static void change_port_settings(struct tty_struct
*tty
,
1532 struct moschip_port
*mos7720_port
,
1533 struct ktermios
*old_termios
)
1535 struct usb_serial_port
*port
;
1536 struct usb_serial
*serial
;
1547 if (mos7720_port
== NULL
)
1550 port
= mos7720_port
->port
;
1551 serial
= port
->serial
;
1552 port_number
= port
->number
- port
->serial
->minor
;
1554 if (!mos7720_port
->open
) {
1555 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1559 lData
= UART_LCR_WLEN8
;
1560 lStop
= 0x00; /* 1 stop bit */
1561 lParity
= 0x00; /* No parity */
1563 cflag
= tty
->termios
.c_cflag
;
1564 iflag
= tty
->termios
.c_iflag
;
1566 /* Change the number of bits */
1567 switch (cflag
& CSIZE
) {
1569 lData
= UART_LCR_WLEN5
;
1574 lData
= UART_LCR_WLEN6
;
1579 lData
= UART_LCR_WLEN7
;
1584 lData
= UART_LCR_WLEN8
;
1588 /* Change the Parity bit */
1589 if (cflag
& PARENB
) {
1590 if (cflag
& PARODD
) {
1591 lParity
= UART_LCR_PARITY
;
1592 dev_dbg(&port
->dev
, "%s - parity = odd\n", __func__
);
1594 lParity
= (UART_LCR_EPAR
| UART_LCR_PARITY
);
1595 dev_dbg(&port
->dev
, "%s - parity = even\n", __func__
);
1599 dev_dbg(&port
->dev
, "%s - parity = none\n", __func__
);
1603 lParity
= lParity
| 0x20;
1605 /* Change the Stop bit */
1606 if (cflag
& CSTOPB
) {
1607 lStop
= UART_LCR_STOP
;
1608 dev_dbg(&port
->dev
, "%s - stop bits = 2\n", __func__
);
1611 dev_dbg(&port
->dev
, "%s - stop bits = 1\n", __func__
);
1614 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1615 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1616 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
1618 /* Update the LCR with the correct value */
1619 mos7720_port
->shadowLCR
&=
1620 ~(LCR_BITS_MASK
| LCR_STOP_MASK
| LCR_PAR_MASK
);
1621 mos7720_port
->shadowLCR
|= (lData
| lParity
| lStop
);
1624 /* Disable Interrupts */
1625 write_mos_reg(serial
, port_number
, IER
, 0x00);
1626 write_mos_reg(serial
, port_number
, FCR
, 0x00);
1627 write_mos_reg(serial
, port_number
, FCR
, 0xcf);
1629 /* Send the updated LCR value to the mos7720 */
1630 write_mos_reg(serial
, port_number
, LCR
, mos7720_port
->shadowLCR
);
1631 mos7720_port
->shadowMCR
= 0x0b;
1632 write_mos_reg(serial
, port_number
, MCR
, mos7720_port
->shadowMCR
);
1634 /* set up the MCR register and send it to the mos7720 */
1635 mos7720_port
->shadowMCR
= UART_MCR_OUT2
;
1637 mos7720_port
->shadowMCR
|= (UART_MCR_DTR
| UART_MCR_RTS
);
1639 if (cflag
& CRTSCTS
) {
1640 mos7720_port
->shadowMCR
|= (UART_MCR_XONANY
);
1641 /* To set hardware flow control to the specified *
1642 * serial port, in SP1/2_CONTROL_REG */
1644 write_mos_reg(serial
, dummy
, SP_CONTROL_REG
, 0x01);
1646 write_mos_reg(serial
, dummy
, SP_CONTROL_REG
, 0x02);
1649 mos7720_port
->shadowMCR
&= ~(UART_MCR_XONANY
);
1651 write_mos_reg(serial
, port_number
, MCR
, mos7720_port
->shadowMCR
);
1653 /* Determine divisor based on baud rate */
1654 baud
= tty_get_baud_rate(tty
);
1656 /* pick a default, any default... */
1657 dev_dbg(&port
->dev
, "Picked default baud...\n");
1661 if (baud
>= 230400) {
1662 set_higher_rates(mos7720_port
, baud
);
1663 /* Enable Interrupts */
1664 write_mos_reg(serial
, port_number
, IER
, 0x0c);
1668 dev_dbg(&port
->dev
, "%s - baud rate = %d\n", __func__
, baud
);
1669 status
= send_cmd_write_baud_rate(mos7720_port
, baud
);
1670 /* FIXME: needs to write actual resulting baud back not just
1673 tty_encode_baud_rate(tty
, baud
, baud
);
1674 /* Enable Interrupts */
1675 write_mos_reg(serial
, port_number
, IER
, 0x0c);
1677 if (port
->read_urb
->status
!= -EINPROGRESS
) {
1678 status
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
1680 dev_dbg(&port
->dev
, "usb_submit_urb(read bulk) failed, status = %d\n", status
);
1685 * mos7720_set_termios
1686 * this function is called by the tty driver when it wants to change the
1687 * termios structure.
1689 static void mos7720_set_termios(struct tty_struct
*tty
,
1690 struct usb_serial_port
*port
, struct ktermios
*old_termios
)
1694 struct usb_serial
*serial
;
1695 struct moschip_port
*mos7720_port
;
1697 serial
= port
->serial
;
1699 mos7720_port
= usb_get_serial_port_data(port
);
1701 if (mos7720_port
== NULL
)
1704 if (!mos7720_port
->open
) {
1705 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1709 dev_dbg(&port
->dev
, "setting termios - ASPIRE\n");
1711 cflag
= tty
->termios
.c_cflag
;
1713 dev_dbg(&port
->dev
, "%s - cflag %08x iflag %08x\n", __func__
,
1714 tty
->termios
.c_cflag
, RELEVANT_IFLAG(tty
->termios
.c_iflag
));
1716 dev_dbg(&port
->dev
, "%s - old cflag %08x old iflag %08x\n", __func__
,
1717 old_termios
->c_cflag
, RELEVANT_IFLAG(old_termios
->c_iflag
));
1719 /* change the port settings to the new ones specified */
1720 change_port_settings(tty
, mos7720_port
, old_termios
);
1722 if (port
->read_urb
->status
!= -EINPROGRESS
) {
1723 status
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
1725 dev_dbg(&port
->dev
, "usb_submit_urb(read bulk) failed, status = %d\n", status
);
1730 * get_lsr_info - get line status register info
1732 * Purpose: Let user call ioctl() to get info when the UART physically
1733 * is emptied. On bus types like RS485, the transmitter must
1734 * release the bus after transmitting. This must be done when
1735 * the transmit shift register is empty, not be done when the
1736 * transmit holding register is empty. This functionality
1737 * allows an RS485 driver to be written in user space.
1739 static int get_lsr_info(struct tty_struct
*tty
,
1740 struct moschip_port
*mos7720_port
, unsigned int __user
*value
)
1742 struct usb_serial_port
*port
= tty
->driver_data
;
1743 unsigned int result
= 0;
1744 unsigned char data
= 0;
1745 int port_number
= port
->number
- port
->serial
->minor
;
1748 count
= mos7720_chars_in_buffer(tty
);
1750 read_mos_reg(port
->serial
, port_number
, LSR
, &data
);
1751 if ((data
& (UART_LSR_TEMT
| UART_LSR_THRE
))
1752 == (UART_LSR_TEMT
| UART_LSR_THRE
)) {
1753 dev_dbg(&port
->dev
, "%s -- Empty\n", __func__
);
1754 result
= TIOCSER_TEMT
;
1757 if (copy_to_user(value
, &result
, sizeof(int)))
1762 static int mos7720_tiocmget(struct tty_struct
*tty
)
1764 struct usb_serial_port
*port
= tty
->driver_data
;
1765 struct moschip_port
*mos7720_port
= usb_get_serial_port_data(port
);
1766 unsigned int result
= 0;
1770 mcr
= mos7720_port
->shadowMCR
;
1771 msr
= mos7720_port
->shadowMSR
;
1773 result
= ((mcr
& UART_MCR_DTR
) ? TIOCM_DTR
: 0) /* 0x002 */
1774 | ((mcr
& UART_MCR_RTS
) ? TIOCM_RTS
: 0) /* 0x004 */
1775 | ((msr
& UART_MSR_CTS
) ? TIOCM_CTS
: 0) /* 0x020 */
1776 | ((msr
& UART_MSR_DCD
) ? TIOCM_CAR
: 0) /* 0x040 */
1777 | ((msr
& UART_MSR_RI
) ? TIOCM_RI
: 0) /* 0x080 */
1778 | ((msr
& UART_MSR_DSR
) ? TIOCM_DSR
: 0); /* 0x100 */
1783 static int mos7720_tiocmset(struct tty_struct
*tty
,
1784 unsigned int set
, unsigned int clear
)
1786 struct usb_serial_port
*port
= tty
->driver_data
;
1787 struct moschip_port
*mos7720_port
= usb_get_serial_port_data(port
);
1790 mcr
= mos7720_port
->shadowMCR
;
1792 if (set
& TIOCM_RTS
)
1793 mcr
|= UART_MCR_RTS
;
1794 if (set
& TIOCM_DTR
)
1795 mcr
|= UART_MCR_DTR
;
1796 if (set
& TIOCM_LOOP
)
1797 mcr
|= UART_MCR_LOOP
;
1799 if (clear
& TIOCM_RTS
)
1800 mcr
&= ~UART_MCR_RTS
;
1801 if (clear
& TIOCM_DTR
)
1802 mcr
&= ~UART_MCR_DTR
;
1803 if (clear
& TIOCM_LOOP
)
1804 mcr
&= ~UART_MCR_LOOP
;
1806 mos7720_port
->shadowMCR
= mcr
;
1807 write_mos_reg(port
->serial
, port
->number
- port
->serial
->minor
,
1808 MCR
, mos7720_port
->shadowMCR
);
1813 static int mos7720_get_icount(struct tty_struct
*tty
,
1814 struct serial_icounter_struct
*icount
)
1816 struct usb_serial_port
*port
= tty
->driver_data
;
1817 struct moschip_port
*mos7720_port
;
1818 struct async_icount cnow
;
1820 mos7720_port
= usb_get_serial_port_data(port
);
1821 cnow
= mos7720_port
->icount
;
1823 icount
->cts
= cnow
.cts
;
1824 icount
->dsr
= cnow
.dsr
;
1825 icount
->rng
= cnow
.rng
;
1826 icount
->dcd
= cnow
.dcd
;
1827 icount
->rx
= cnow
.rx
;
1828 icount
->tx
= cnow
.tx
;
1829 icount
->frame
= cnow
.frame
;
1830 icount
->overrun
= cnow
.overrun
;
1831 icount
->parity
= cnow
.parity
;
1832 icount
->brk
= cnow
.brk
;
1833 icount
->buf_overrun
= cnow
.buf_overrun
;
1835 dev_dbg(&port
->dev
, "%s TIOCGICOUNT RX=%d, TX=%d\n", __func__
,
1836 icount
->rx
, icount
->tx
);
1840 static int set_modem_info(struct moschip_port
*mos7720_port
, unsigned int cmd
,
1841 unsigned int __user
*value
)
1846 struct usb_serial_port
*port
;
1848 if (mos7720_port
== NULL
)
1851 port
= (struct usb_serial_port
*)mos7720_port
->port
;
1852 mcr
= mos7720_port
->shadowMCR
;
1854 if (copy_from_user(&arg
, value
, sizeof(int)))
1859 if (arg
& TIOCM_RTS
)
1860 mcr
|= UART_MCR_RTS
;
1861 if (arg
& TIOCM_DTR
)
1862 mcr
|= UART_MCR_RTS
;
1863 if (arg
& TIOCM_LOOP
)
1864 mcr
|= UART_MCR_LOOP
;
1868 if (arg
& TIOCM_RTS
)
1869 mcr
&= ~UART_MCR_RTS
;
1870 if (arg
& TIOCM_DTR
)
1871 mcr
&= ~UART_MCR_RTS
;
1872 if (arg
& TIOCM_LOOP
)
1873 mcr
&= ~UART_MCR_LOOP
;
1878 mos7720_port
->shadowMCR
= mcr
;
1879 write_mos_reg(port
->serial
, port
->number
- port
->serial
->minor
,
1880 MCR
, mos7720_port
->shadowMCR
);
1885 static int get_serial_info(struct moschip_port
*mos7720_port
,
1886 struct serial_struct __user
*retinfo
)
1888 struct serial_struct tmp
;
1893 memset(&tmp
, 0, sizeof(tmp
));
1895 tmp
.type
= PORT_16550A
;
1896 tmp
.line
= mos7720_port
->port
->serial
->minor
;
1897 tmp
.port
= mos7720_port
->port
->number
;
1899 tmp
.flags
= ASYNC_SKIP_TEST
| ASYNC_AUTO_IRQ
;
1900 tmp
.xmit_fifo_size
= NUM_URBS
* URB_TRANSFER_BUFFER_SIZE
;
1901 tmp
.baud_base
= 9600;
1902 tmp
.close_delay
= 5*HZ
;
1903 tmp
.closing_wait
= 30*HZ
;
1905 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
1910 static int mos7720_ioctl(struct tty_struct
*tty
,
1911 unsigned int cmd
, unsigned long arg
)
1913 struct usb_serial_port
*port
= tty
->driver_data
;
1914 struct moschip_port
*mos7720_port
;
1915 struct async_icount cnow
;
1916 struct async_icount cprev
;
1918 mos7720_port
= usb_get_serial_port_data(port
);
1919 if (mos7720_port
== NULL
)
1922 dev_dbg(&port
->dev
, "%s - cmd = 0x%x", __func__
, cmd
);
1926 dev_dbg(&port
->dev
, "%s TIOCSERGETLSR\n", __func__
);
1927 return get_lsr_info(tty
, mos7720_port
,
1928 (unsigned int __user
*)arg
);
1930 /* FIXME: These should be using the mode methods */
1933 dev_dbg(&port
->dev
, "%s TIOCMSET/TIOCMBIC/TIOCMSET\n", __func__
);
1934 return set_modem_info(mos7720_port
, cmd
,
1935 (unsigned int __user
*)arg
);
1938 dev_dbg(&port
->dev
, "%s TIOCGSERIAL\n", __func__
);
1939 return get_serial_info(mos7720_port
,
1940 (struct serial_struct __user
*)arg
);
1943 dev_dbg(&port
->dev
, "%s TIOCMIWAIT\n", __func__
);
1944 cprev
= mos7720_port
->icount
;
1946 if (signal_pending(current
))
1947 return -ERESTARTSYS
;
1948 cnow
= mos7720_port
->icount
;
1949 if (cnow
.rng
== cprev
.rng
&& cnow
.dsr
== cprev
.dsr
&&
1950 cnow
.dcd
== cprev
.dcd
&& cnow
.cts
== cprev
.cts
)
1951 return -EIO
; /* no change => error */
1952 if (((arg
& TIOCM_RNG
) && (cnow
.rng
!= cprev
.rng
)) ||
1953 ((arg
& TIOCM_DSR
) && (cnow
.dsr
!= cprev
.dsr
)) ||
1954 ((arg
& TIOCM_CD
) && (cnow
.dcd
!= cprev
.dcd
)) ||
1955 ((arg
& TIOCM_CTS
) && (cnow
.cts
!= cprev
.cts
))) {
1964 return -ENOIOCTLCMD
;
1967 static int mos7720_startup(struct usb_serial
*serial
)
1969 struct moschip_port
*mos7720_port
;
1970 struct usb_device
*dev
;
1976 product
= le16_to_cpu(serial
->dev
->descriptor
.idProduct
);
1980 * The 7715 uses the first bulk in/out endpoint pair for the parallel
1981 * port, and the second for the serial port. Because the usbserial core
1982 * assumes both pairs are serial ports, we must engage in a bit of
1983 * subterfuge and swap the pointers for ports 0 and 1 in order to make
1984 * port 0 point to the serial port. However, both moschip devices use a
1985 * single interrupt-in endpoint for both ports (as mentioned a little
1986 * further down), and this endpoint was assigned to port 0. So after
1987 * the swap, we must copy the interrupt endpoint elements from port 1
1988 * (as newly assigned) to port 0, and null out port 1 pointers.
1990 if (product
== MOSCHIP_DEVICE_ID_7715
) {
1991 struct usb_serial_port
*tmp
= serial
->port
[0];
1992 serial
->port
[0] = serial
->port
[1];
1993 serial
->port
[1] = tmp
;
1994 serial
->port
[0]->interrupt_in_urb
= tmp
->interrupt_in_urb
;
1995 serial
->port
[0]->interrupt_in_buffer
= tmp
->interrupt_in_buffer
;
1996 serial
->port
[0]->interrupt_in_endpointAddress
=
1997 tmp
->interrupt_in_endpointAddress
;
1998 serial
->port
[1]->interrupt_in_urb
= NULL
;
1999 serial
->port
[1]->interrupt_in_buffer
= NULL
;
2003 /* set up serial port private structures */
2004 for (i
= 0; i
< serial
->num_ports
; ++i
) {
2005 mos7720_port
= kzalloc(sizeof(struct moschip_port
), GFP_KERNEL
);
2006 if (mos7720_port
== NULL
) {
2007 dev_err(&dev
->dev
, "%s - Out of memory\n", __func__
);
2011 /* Initialize all port interrupt end point to port 0 int
2012 * endpoint. Our device has only one interrupt endpoint
2013 * common to all ports */
2014 serial
->port
[i
]->interrupt_in_endpointAddress
=
2015 serial
->port
[0]->interrupt_in_endpointAddress
;
2017 mos7720_port
->port
= serial
->port
[i
];
2018 usb_set_serial_port_data(serial
->port
[i
], mos7720_port
);
2020 dev_dbg(&dev
->dev
, "port number is %d\n", serial
->port
[i
]->number
);
2021 dev_dbg(&dev
->dev
, "serial number is %d\n", serial
->minor
);
2025 /* setting configuration feature to one */
2026 usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
2027 (__u8
)0x03, 0x00, 0x01, 0x00, NULL
, 0x00, 5*HZ
);
2029 /* start the interrupt urb */
2030 ret_val
= usb_submit_urb(serial
->port
[0]->interrupt_in_urb
, GFP_KERNEL
);
2033 "%s - Error %d submitting control urb\n",
2036 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
2037 if (product
== MOSCHIP_DEVICE_ID_7715
) {
2038 ret_val
= mos7715_parport_init(serial
);
2043 /* LSR For Port 1 */
2044 read_mos_reg(serial
, 0, LSR
, &data
);
2045 dev_dbg(&dev
->dev
, "LSR:%x\n", data
);
2050 static void mos7720_release(struct usb_serial
*serial
)
2054 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
2055 /* close the parallel port */
2057 if (le16_to_cpu(serial
->dev
->descriptor
.idProduct
)
2058 == MOSCHIP_DEVICE_ID_7715
) {
2059 struct urbtracker
*urbtrack
;
2060 unsigned long flags
;
2061 struct mos7715_parport
*mos_parport
=
2062 usb_get_serial_data(serial
);
2064 /* prevent NULL ptr dereference in port callbacks */
2065 spin_lock(&release_lock
);
2066 mos_parport
->pp
->private_data
= NULL
;
2067 spin_unlock(&release_lock
);
2069 /* wait for synchronous usb calls to return */
2070 if (mos_parport
->msg_pending
)
2071 wait_for_completion_timeout(&mos_parport
->syncmsg_compl
,
2074 parport_remove_port(mos_parport
->pp
);
2075 usb_set_serial_data(serial
, NULL
);
2076 mos_parport
->serial
= NULL
;
2078 /* if tasklet currently scheduled, wait for it to complete */
2079 tasklet_kill(&mos_parport
->urb_tasklet
);
2081 /* unlink any urbs sent by the tasklet */
2082 spin_lock_irqsave(&mos_parport
->listlock
, flags
);
2083 list_for_each_entry(urbtrack
,
2084 &mos_parport
->active_urbs
,
2086 usb_unlink_urb(urbtrack
->urb
);
2087 spin_unlock_irqrestore(&mos_parport
->listlock
, flags
);
2089 kref_put(&mos_parport
->ref_count
, destroy_mos_parport
);
2092 /* free private structure allocated for serial port */
2093 for (i
= 0; i
< serial
->num_ports
; ++i
)
2094 kfree(usb_get_serial_port_data(serial
->port
[i
]));
2097 static struct usb_serial_driver moschip7720_2port_driver
= {
2099 .owner
= THIS_MODULE
,
2100 .name
= "moschip7720",
2102 .description
= "Moschip 2 port adapter",
2103 .id_table
= id_table
,
2104 .calc_num_ports
= mos77xx_calc_num_ports
,
2105 .open
= mos7720_open
,
2106 .close
= mos7720_close
,
2107 .throttle
= mos7720_throttle
,
2108 .unthrottle
= mos7720_unthrottle
,
2109 .probe
= mos77xx_probe
,
2110 .attach
= mos7720_startup
,
2111 .release
= mos7720_release
,
2112 .ioctl
= mos7720_ioctl
,
2113 .tiocmget
= mos7720_tiocmget
,
2114 .tiocmset
= mos7720_tiocmset
,
2115 .get_icount
= mos7720_get_icount
,
2116 .set_termios
= mos7720_set_termios
,
2117 .write
= mos7720_write
,
2118 .write_room
= mos7720_write_room
,
2119 .chars_in_buffer
= mos7720_chars_in_buffer
,
2120 .break_ctl
= mos7720_break
,
2121 .read_bulk_callback
= mos7720_bulk_in_callback
,
2122 .read_int_callback
= NULL
/* dynamically assigned in probe() */
2125 static struct usb_serial_driver
* const serial_drivers
[] = {
2126 &moschip7720_2port_driver
, NULL
2129 module_usb_serial_driver(serial_drivers
, id_table
);
2131 MODULE_AUTHOR(DRIVER_AUTHOR
);
2132 MODULE_DESCRIPTION(DRIVER_DESC
);
2133 MODULE_LICENSE("GPL");