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>
39 #define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
40 #define DRIVER_DESC "Moschip USB Serial Driver"
42 /* default urb timeout */
43 #define MOS_WDR_TIMEOUT (HZ * 5)
45 #define MOS_MAX_PORT 0x02
46 #define MOS_WRITE 0x0E
49 /* Interrupt Rotinue Defines */
50 #define SERIAL_IIR_RLS 0x06
51 #define SERIAL_IIR_RDA 0x04
52 #define SERIAL_IIR_CTI 0x0c
53 #define SERIAL_IIR_THR 0x02
54 #define SERIAL_IIR_MS 0x00
56 #define NUM_URBS 16 /* URB Count */
57 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
59 /* This structure holds all of the local serial port information */
61 __u8 shadowLCR
; /* last LCR value received */
62 __u8 shadowMCR
; /* last MCR value received */
63 __u8 shadowMSR
; /* last MSR value received */
65 struct async_icount icount
;
66 struct usb_serial_port
*port
; /* loop back to the owner */
67 struct urb
*write_urb_pool
[NUM_URBS
];
70 static struct usb_serial_driver moschip7720_2port_driver
;
72 #define USB_VENDOR_ID_MOSCHIP 0x9710
73 #define MOSCHIP_DEVICE_ID_7720 0x7720
74 #define MOSCHIP_DEVICE_ID_7715 0x7715
76 static const struct usb_device_id id_table
[] = {
77 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7720
) },
78 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7715
) },
79 { } /* terminating entry */
81 MODULE_DEVICE_TABLE(usb
, id_table
);
83 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
85 /* initial values for parport regs */
86 #define DCR_INIT_VAL 0x0c /* SLCTIN, nINIT */
87 #define ECR_INIT_VAL 0x00 /* SPP mode */
90 struct mos7715_parport
*mos_parport
;
91 struct list_head urblist_entry
;
92 struct kref ref_count
;
96 enum mos7715_pp_modes
{
98 PS2
= 1<<5, /* moschip calls this 'NIBBLE' mode */
99 PPF
= 2<<5, /* moschip calls this 'CB-FIFO mode */
102 struct mos7715_parport
{
103 struct parport
*pp
; /* back to containing struct */
104 struct kref ref_count
; /* to instance of this struct */
105 struct list_head deferred_urbs
; /* list deferred async urbs */
106 struct list_head active_urbs
; /* list async urbs in flight */
107 spinlock_t listlock
; /* protects list access */
108 bool msg_pending
; /* usb sync call pending */
109 struct completion syncmsg_compl
; /* usb sync call completed */
110 struct tasklet_struct urb_tasklet
; /* for sending deferred urbs */
111 struct usb_serial
*serial
; /* back to containing struct */
112 __u8 shadowECR
; /* parallel port regs... */
114 atomic_t shadowDSR
; /* updated in int-in callback */
117 /* lock guards against dereferencing NULL ptr in parport ops callbacks */
118 static DEFINE_SPINLOCK(release_lock
);
120 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
122 static const unsigned int dummy
; /* for clarity in register access fns */
125 THR
, /* serial port regs */
137 DPR
, /* parallel port regs */
141 SP1_REG
, /* device control regs */
142 SP2_REG
, /* serial port 2 (7720 only) */
148 * Return the correct value for the Windex field of the setup packet
149 * for a control endpoint message. See the 7715 datasheet.
151 static inline __u16
get_reg_index(enum mos_regs reg
)
153 static const __u16 mos7715_index_lookup_table
[] = {
171 0x02, /* SP2_REG (7720 only) */
172 0x04, /* PP_REG (7715 only) */
173 0x08, /* SP_CONTROL_REG */
175 return mos7715_index_lookup_table
[reg
];
179 * Return the correct value for the upper byte of the Wvalue field of
180 * the setup packet for a control endpoint message.
182 static inline __u16
get_reg_value(enum mos_regs reg
,
183 unsigned int serial_portnum
)
185 if (reg
>= SP1_REG
) /* control reg */
188 else if (reg
>= DPR
) /* parallel port reg (7715 only) */
191 else /* serial port reg */
192 return (serial_portnum
+ 2) << 8;
196 * Write data byte to the specified device register. The data is embedded in
197 * the value field of the setup packet. serial_portnum is ignored for registers
198 * not specific to a particular serial port.
200 static int write_mos_reg(struct usb_serial
*serial
, unsigned int serial_portnum
,
201 enum mos_regs reg
, __u8 data
)
203 struct usb_device
*usbdev
= serial
->dev
;
204 unsigned int pipe
= usb_sndctrlpipe(usbdev
, 0);
205 __u8 request
= (__u8
)0x0e;
206 __u8 requesttype
= (__u8
)0x40;
207 __u16 index
= get_reg_index(reg
);
208 __u16 value
= get_reg_value(reg
, serial_portnum
) + data
;
209 int status
= usb_control_msg(usbdev
, pipe
, request
, requesttype
, value
,
210 index
, NULL
, 0, MOS_WDR_TIMEOUT
);
212 dev_err(&usbdev
->dev
,
213 "mos7720: usb_control_msg() failed: %d", status
);
218 * Read data byte from the specified device register. The data returned by the
219 * device is embedded in the value field of the setup packet. serial_portnum is
220 * ignored for registers that are not specific to a particular serial port.
222 static int read_mos_reg(struct usb_serial
*serial
, unsigned int serial_portnum
,
223 enum mos_regs reg
, __u8
*data
)
225 struct usb_device
*usbdev
= serial
->dev
;
226 unsigned int pipe
= usb_rcvctrlpipe(usbdev
, 0);
227 __u8 request
= (__u8
)0x0d;
228 __u8 requesttype
= (__u8
)0xc0;
229 __u16 index
= get_reg_index(reg
);
230 __u16 value
= get_reg_value(reg
, serial_portnum
);
231 int status
= usb_control_msg(usbdev
, pipe
, request
, requesttype
, value
,
232 index
, data
, 1, MOS_WDR_TIMEOUT
);
234 dev_err(&usbdev
->dev
,
235 "mos7720: usb_control_msg() failed: %d", status
);
239 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
241 static inline int mos7715_change_mode(struct mos7715_parport
*mos_parport
,
242 enum mos7715_pp_modes mode
)
244 mos_parport
->shadowECR
= mode
;
245 write_mos_reg(mos_parport
->serial
, dummy
, ECR
, mos_parport
->shadowECR
);
249 static void destroy_mos_parport(struct kref
*kref
)
251 struct mos7715_parport
*mos_parport
=
252 container_of(kref
, struct mos7715_parport
, ref_count
);
257 static void destroy_urbtracker(struct kref
*kref
)
259 struct urbtracker
*urbtrack
=
260 container_of(kref
, struct urbtracker
, ref_count
);
261 struct mos7715_parport
*mos_parport
= urbtrack
->mos_parport
;
263 usb_free_urb(urbtrack
->urb
);
265 kref_put(&mos_parport
->ref_count
, destroy_mos_parport
);
269 * This runs as a tasklet when sending an urb in a non-blocking parallel
270 * port callback had to be deferred because the disconnect mutex could not be
271 * obtained at the time.
273 static void send_deferred_urbs(unsigned long _mos_parport
)
277 struct mos7715_parport
*mos_parport
= (void *)_mos_parport
;
278 struct urbtracker
*urbtrack
, *tmp
;
279 struct list_head
*cursor
, *next
;
282 /* if release function ran, game over */
283 if (unlikely(mos_parport
->serial
== NULL
))
286 dev
= &mos_parport
->serial
->dev
->dev
;
288 /* try again to get the mutex */
289 if (!mutex_trylock(&mos_parport
->serial
->disc_mutex
)) {
290 dev_dbg(dev
, "%s: rescheduling tasklet\n", __func__
);
291 tasklet_schedule(&mos_parport
->urb_tasklet
);
295 /* if device disconnected, game over */
296 if (unlikely(mos_parport
->serial
->disconnected
)) {
297 mutex_unlock(&mos_parport
->serial
->disc_mutex
);
301 spin_lock_irqsave(&mos_parport
->listlock
, flags
);
302 if (list_empty(&mos_parport
->deferred_urbs
)) {
303 spin_unlock_irqrestore(&mos_parport
->listlock
, flags
);
304 mutex_unlock(&mos_parport
->serial
->disc_mutex
);
305 dev_dbg(dev
, "%s: deferred_urbs list empty\n", __func__
);
309 /* move contents of deferred_urbs list to active_urbs list and submit */
310 list_for_each_safe(cursor
, next
, &mos_parport
->deferred_urbs
)
311 list_move_tail(cursor
, &mos_parport
->active_urbs
);
312 list_for_each_entry_safe(urbtrack
, tmp
, &mos_parport
->active_urbs
,
314 ret_val
= usb_submit_urb(urbtrack
->urb
, GFP_ATOMIC
);
315 dev_dbg(dev
, "%s: urb submitted\n", __func__
);
317 dev_err(dev
, "usb_submit_urb() failed: %d\n", ret_val
);
318 list_del(&urbtrack
->urblist_entry
);
319 kref_put(&urbtrack
->ref_count
, destroy_urbtracker
);
322 spin_unlock_irqrestore(&mos_parport
->listlock
, flags
);
323 mutex_unlock(&mos_parport
->serial
->disc_mutex
);
326 /* callback for parallel port control urbs submitted asynchronously */
327 static void async_complete(struct urb
*urb
)
329 struct urbtracker
*urbtrack
= urb
->context
;
330 int status
= urb
->status
;
332 if (unlikely(status
))
333 dev_dbg(&urb
->dev
->dev
, "%s - nonzero urb status received: %d\n", __func__
, status
);
335 /* remove the urbtracker from the active_urbs list */
336 spin_lock(&urbtrack
->mos_parport
->listlock
);
337 list_del(&urbtrack
->urblist_entry
);
338 spin_unlock(&urbtrack
->mos_parport
->listlock
);
339 kref_put(&urbtrack
->ref_count
, destroy_urbtracker
);
342 static int write_parport_reg_nonblock(struct mos7715_parport
*mos_parport
,
343 enum mos_regs reg
, __u8 data
)
345 struct urbtracker
*urbtrack
;
348 struct usb_ctrlrequest setup
;
349 struct usb_serial
*serial
= mos_parport
->serial
;
350 struct usb_device
*usbdev
= serial
->dev
;
352 /* create and initialize the control urb and containing urbtracker */
353 urbtrack
= kmalloc(sizeof(struct urbtracker
), GFP_ATOMIC
);
354 if (urbtrack
== NULL
) {
355 dev_err(&usbdev
->dev
, "out of memory");
358 kref_get(&mos_parport
->ref_count
);
359 urbtrack
->mos_parport
= mos_parport
;
360 urbtrack
->urb
= usb_alloc_urb(0, GFP_ATOMIC
);
361 if (urbtrack
->urb
== NULL
) {
362 dev_err(&usbdev
->dev
, "out of urbs");
366 setup
.bRequestType
= (__u8
)0x40;
367 setup
.bRequest
= (__u8
)0x0e;
368 setup
.wValue
= get_reg_value(reg
, dummy
);
369 setup
.wIndex
= get_reg_index(reg
);
371 usb_fill_control_urb(urbtrack
->urb
, usbdev
,
372 usb_sndctrlpipe(usbdev
, 0),
373 (unsigned char *)&setup
,
374 NULL
, 0, async_complete
, urbtrack
);
375 kref_init(&urbtrack
->ref_count
);
376 INIT_LIST_HEAD(&urbtrack
->urblist_entry
);
379 * get the disconnect mutex, or add tracker to the deferred_urbs list
380 * and schedule a tasklet to try again later
382 if (!mutex_trylock(&serial
->disc_mutex
)) {
383 spin_lock_irqsave(&mos_parport
->listlock
, flags
);
384 list_add_tail(&urbtrack
->urblist_entry
,
385 &mos_parport
->deferred_urbs
);
386 spin_unlock_irqrestore(&mos_parport
->listlock
, flags
);
387 tasklet_schedule(&mos_parport
->urb_tasklet
);
388 dev_dbg(&usbdev
->dev
, "tasklet scheduled");
392 /* bail if device disconnected */
393 if (serial
->disconnected
) {
394 kref_put(&urbtrack
->ref_count
, destroy_urbtracker
);
395 mutex_unlock(&serial
->disc_mutex
);
399 /* add the tracker to the active_urbs list and submit */
400 spin_lock_irqsave(&mos_parport
->listlock
, flags
);
401 list_add_tail(&urbtrack
->urblist_entry
, &mos_parport
->active_urbs
);
402 spin_unlock_irqrestore(&mos_parport
->listlock
, flags
);
403 ret_val
= usb_submit_urb(urbtrack
->urb
, GFP_ATOMIC
);
404 mutex_unlock(&serial
->disc_mutex
);
406 dev_err(&usbdev
->dev
,
407 "%s: submit_urb() failed: %d", __func__
, ret_val
);
408 spin_lock_irqsave(&mos_parport
->listlock
, flags
);
409 list_del(&urbtrack
->urblist_entry
);
410 spin_unlock_irqrestore(&mos_parport
->listlock
, flags
);
411 kref_put(&urbtrack
->ref_count
, destroy_urbtracker
);
418 * This is the the common top part of all parallel port callback operations that
419 * send synchronous messages to the device. This implements convoluted locking
420 * that avoids two scenarios: (1) a port operation is called after usbserial
421 * has called our release function, at which point struct mos7715_parport has
422 * been destroyed, and (2) the device has been disconnected, but usbserial has
423 * not called the release function yet because someone has a serial port open.
424 * The shared release_lock prevents the first, and the mutex and disconnected
425 * flag maintained by usbserial covers the second. We also use the msg_pending
426 * flag to ensure that all synchronous usb messgage calls have completed before
427 * our release function can return.
429 static int parport_prologue(struct parport
*pp
)
431 struct mos7715_parport
*mos_parport
;
433 spin_lock(&release_lock
);
434 mos_parport
= pp
->private_data
;
435 if (unlikely(mos_parport
== NULL
)) {
436 /* release fn called, port struct destroyed */
437 spin_unlock(&release_lock
);
440 mos_parport
->msg_pending
= true; /* synch usb call pending */
441 INIT_COMPLETION(mos_parport
->syncmsg_compl
);
442 spin_unlock(&release_lock
);
444 mutex_lock(&mos_parport
->serial
->disc_mutex
);
445 if (mos_parport
->serial
->disconnected
) {
446 /* device disconnected */
447 mutex_unlock(&mos_parport
->serial
->disc_mutex
);
448 mos_parport
->msg_pending
= false;
449 complete(&mos_parport
->syncmsg_compl
);
457 * This is the the common bottom part of all parallel port functions that send
458 * synchronous messages to the device.
460 static inline void parport_epilogue(struct parport
*pp
)
462 struct mos7715_parport
*mos_parport
= pp
->private_data
;
463 mutex_unlock(&mos_parport
->serial
->disc_mutex
);
464 mos_parport
->msg_pending
= false;
465 complete(&mos_parport
->syncmsg_compl
);
468 static void parport_mos7715_write_data(struct parport
*pp
, unsigned char d
)
470 struct mos7715_parport
*mos_parport
= pp
->private_data
;
472 if (parport_prologue(pp
) < 0)
474 mos7715_change_mode(mos_parport
, SPP
);
475 write_mos_reg(mos_parport
->serial
, dummy
, DPR
, (__u8
)d
);
476 parport_epilogue(pp
);
479 static unsigned char parport_mos7715_read_data(struct parport
*pp
)
481 struct mos7715_parport
*mos_parport
= pp
->private_data
;
484 if (parport_prologue(pp
) < 0)
486 read_mos_reg(mos_parport
->serial
, dummy
, DPR
, &d
);
487 parport_epilogue(pp
);
491 static void parport_mos7715_write_control(struct parport
*pp
, unsigned char d
)
493 struct mos7715_parport
*mos_parport
= pp
->private_data
;
496 if (parport_prologue(pp
) < 0)
498 data
= ((__u8
)d
& 0x0f) | (mos_parport
->shadowDCR
& 0xf0);
499 write_mos_reg(mos_parport
->serial
, dummy
, DCR
, data
);
500 mos_parport
->shadowDCR
= data
;
501 parport_epilogue(pp
);
504 static unsigned char parport_mos7715_read_control(struct parport
*pp
)
506 struct mos7715_parport
*mos_parport
= pp
->private_data
;
509 spin_lock(&release_lock
);
510 mos_parport
= pp
->private_data
;
511 if (unlikely(mos_parport
== NULL
)) {
512 spin_unlock(&release_lock
);
515 dcr
= mos_parport
->shadowDCR
& 0x0f;
516 spin_unlock(&release_lock
);
520 static unsigned char parport_mos7715_frob_control(struct parport
*pp
,
524 struct mos7715_parport
*mos_parport
= pp
->private_data
;
529 if (parport_prologue(pp
) < 0)
531 mos_parport
->shadowDCR
= (mos_parport
->shadowDCR
& (~mask
)) ^ val
;
532 write_mos_reg(mos_parport
->serial
, dummy
, DCR
, mos_parport
->shadowDCR
);
533 dcr
= mos_parport
->shadowDCR
& 0x0f;
534 parport_epilogue(pp
);
538 static unsigned char parport_mos7715_read_status(struct parport
*pp
)
540 unsigned char status
;
541 struct mos7715_parport
*mos_parport
= pp
->private_data
;
543 spin_lock(&release_lock
);
544 mos_parport
= pp
->private_data
;
545 if (unlikely(mos_parport
== NULL
)) { /* release called */
546 spin_unlock(&release_lock
);
549 status
= atomic_read(&mos_parport
->shadowDSR
) & 0xf8;
550 spin_unlock(&release_lock
);
554 static void parport_mos7715_enable_irq(struct parport
*pp
)
558 static void parport_mos7715_disable_irq(struct parport
*pp
)
562 static void parport_mos7715_data_forward(struct parport
*pp
)
564 struct mos7715_parport
*mos_parport
= pp
->private_data
;
566 if (parport_prologue(pp
) < 0)
568 mos7715_change_mode(mos_parport
, PS2
);
569 mos_parport
->shadowDCR
&= ~0x20;
570 write_mos_reg(mos_parport
->serial
, dummy
, DCR
, mos_parport
->shadowDCR
);
571 parport_epilogue(pp
);
574 static void parport_mos7715_data_reverse(struct parport
*pp
)
576 struct mos7715_parport
*mos_parport
= pp
->private_data
;
578 if (parport_prologue(pp
) < 0)
580 mos7715_change_mode(mos_parport
, PS2
);
581 mos_parport
->shadowDCR
|= 0x20;
582 write_mos_reg(mos_parport
->serial
, dummy
, DCR
, mos_parport
->shadowDCR
);
583 parport_epilogue(pp
);
586 static void parport_mos7715_init_state(struct pardevice
*dev
,
587 struct parport_state
*s
)
589 s
->u
.pc
.ctr
= DCR_INIT_VAL
;
590 s
->u
.pc
.ecr
= ECR_INIT_VAL
;
593 /* N.B. Parport core code requires that this function not block */
594 static void parport_mos7715_save_state(struct parport
*pp
,
595 struct parport_state
*s
)
597 struct mos7715_parport
*mos_parport
;
599 spin_lock(&release_lock
);
600 mos_parport
= pp
->private_data
;
601 if (unlikely(mos_parport
== NULL
)) { /* release called */
602 spin_unlock(&release_lock
);
605 s
->u
.pc
.ctr
= mos_parport
->shadowDCR
;
606 s
->u
.pc
.ecr
= mos_parport
->shadowECR
;
607 spin_unlock(&release_lock
);
610 /* N.B. Parport core code requires that this function not block */
611 static void parport_mos7715_restore_state(struct parport
*pp
,
612 struct parport_state
*s
)
614 struct mos7715_parport
*mos_parport
;
616 spin_lock(&release_lock
);
617 mos_parport
= pp
->private_data
;
618 if (unlikely(mos_parport
== NULL
)) { /* release called */
619 spin_unlock(&release_lock
);
622 write_parport_reg_nonblock(mos_parport
, DCR
, mos_parport
->shadowDCR
);
623 write_parport_reg_nonblock(mos_parport
, ECR
, mos_parport
->shadowECR
);
624 spin_unlock(&release_lock
);
627 static size_t parport_mos7715_write_compat(struct parport
*pp
,
629 size_t len
, int flags
)
632 struct mos7715_parport
*mos_parport
= pp
->private_data
;
635 if (parport_prologue(pp
) < 0)
637 mos7715_change_mode(mos_parport
, PPF
);
638 retval
= usb_bulk_msg(mos_parport
->serial
->dev
,
639 usb_sndbulkpipe(mos_parport
->serial
->dev
, 2),
640 (void *)buffer
, len
, &actual_len
,
642 parport_epilogue(pp
);
644 dev_err(&mos_parport
->serial
->dev
->dev
,
645 "mos7720: usb_bulk_msg() failed: %d", retval
);
651 static struct parport_operations parport_mos7715_ops
= {
652 .owner
= THIS_MODULE
,
653 .write_data
= parport_mos7715_write_data
,
654 .read_data
= parport_mos7715_read_data
,
656 .write_control
= parport_mos7715_write_control
,
657 .read_control
= parport_mos7715_read_control
,
658 .frob_control
= parport_mos7715_frob_control
,
660 .read_status
= parport_mos7715_read_status
,
662 .enable_irq
= parport_mos7715_enable_irq
,
663 .disable_irq
= parport_mos7715_disable_irq
,
665 .data_forward
= parport_mos7715_data_forward
,
666 .data_reverse
= parport_mos7715_data_reverse
,
668 .init_state
= parport_mos7715_init_state
,
669 .save_state
= parport_mos7715_save_state
,
670 .restore_state
= parport_mos7715_restore_state
,
672 .compat_write_data
= parport_mos7715_write_compat
,
674 .nibble_read_data
= parport_ieee1284_read_nibble
,
675 .byte_read_data
= parport_ieee1284_read_byte
,
679 * Allocate and initialize parallel port control struct, initialize
680 * the parallel port hardware device, and register with the parport subsystem.
682 static int mos7715_parport_init(struct usb_serial
*serial
)
684 struct mos7715_parport
*mos_parport
;
686 /* allocate and initialize parallel port control struct */
687 mos_parport
= kzalloc(sizeof(struct mos7715_parport
), GFP_KERNEL
);
688 if (mos_parport
== NULL
) {
689 dev_dbg(&serial
->dev
->dev
, "%s: kzalloc failed\n", __func__
);
692 mos_parport
->msg_pending
= false;
693 kref_init(&mos_parport
->ref_count
);
694 spin_lock_init(&mos_parport
->listlock
);
695 INIT_LIST_HEAD(&mos_parport
->active_urbs
);
696 INIT_LIST_HEAD(&mos_parport
->deferred_urbs
);
697 usb_set_serial_data(serial
, mos_parport
); /* hijack private pointer */
698 mos_parport
->serial
= serial
;
699 tasklet_init(&mos_parport
->urb_tasklet
, send_deferred_urbs
,
700 (unsigned long) mos_parport
);
701 init_completion(&mos_parport
->syncmsg_compl
);
703 /* cycle parallel port reset bit */
704 write_mos_reg(mos_parport
->serial
, dummy
, PP_REG
, (__u8
)0x80);
705 write_mos_reg(mos_parport
->serial
, dummy
, PP_REG
, (__u8
)0x00);
707 /* initialize device registers */
708 mos_parport
->shadowDCR
= DCR_INIT_VAL
;
709 write_mos_reg(mos_parport
->serial
, dummy
, DCR
, mos_parport
->shadowDCR
);
710 mos_parport
->shadowECR
= ECR_INIT_VAL
;
711 write_mos_reg(mos_parport
->serial
, dummy
, ECR
, mos_parport
->shadowECR
);
713 /* register with parport core */
714 mos_parport
->pp
= parport_register_port(0, PARPORT_IRQ_NONE
,
716 &parport_mos7715_ops
);
717 if (mos_parport
->pp
== NULL
) {
718 dev_err(&serial
->interface
->dev
,
719 "Could not register parport\n");
720 kref_put(&mos_parport
->ref_count
, destroy_mos_parport
);
723 mos_parport
->pp
->private_data
= mos_parport
;
724 mos_parport
->pp
->modes
= PARPORT_MODE_COMPAT
| PARPORT_MODE_PCSPP
;
725 mos_parport
->pp
->dev
= &serial
->interface
->dev
;
726 parport_announce_port(mos_parport
->pp
);
730 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
733 * mos7720_interrupt_callback
734 * this is the callback function for when we have received data on the
735 * interrupt endpoint.
737 static void mos7720_interrupt_callback(struct urb
*urb
)
741 int status
= urb
->status
;
742 struct device
*dev
= &urb
->dev
->dev
;
754 /* this urb is terminated, clean up */
755 dev_dbg(dev
, "%s - urb shutting down with status: %d\n", __func__
, status
);
758 dev_dbg(dev
, "%s - nonzero urb status received: %d\n", __func__
, status
);
762 length
= urb
->actual_length
;
763 data
= urb
->transfer_buffer
;
765 /* Moschip get 4 bytes
766 * Byte 1 IIR Port 1 (port.number is 0)
767 * Byte 2 IIR Port 2 (port.number is 1)
768 * Byte 3 --------------
769 * Byte 4 FIFO status for both */
771 /* the above description is inverted
772 * oneukum 2007-03-14 */
774 if (unlikely(length
!= 4)) {
775 dev_dbg(dev
, "Wrong data !!!\n");
782 if ((sp1
| sp2
) & 0x01) {
783 /* No Interrupt Pending in both the ports */
784 dev_dbg(dev
, "No Interrupt !!!\n");
786 switch (sp1
& 0x0f) {
788 dev_dbg(dev
, "Serial Port 1: Receiver status error or address bit detected in 9-bit mode\n");
791 dev_dbg(dev
, "Serial Port 1: Receiver time out\n");
794 /* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */
798 switch (sp2
& 0x0f) {
800 dev_dbg(dev
, "Serial Port 2: Receiver status error or address bit detected in 9-bit mode\n");
803 dev_dbg(dev
, "Serial Port 2: Receiver time out\n");
806 /* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */
812 result
= usb_submit_urb(urb
, GFP_ATOMIC
);
814 dev_err(dev
, "%s - Error %d submitting control urb\n", __func__
, result
);
818 * mos7715_interrupt_callback
819 * this is the 7715's callback function for when we have received data on
820 * the interrupt endpoint.
822 static void mos7715_interrupt_callback(struct urb
*urb
)
826 int status
= urb
->status
;
827 struct device
*dev
= &urb
->dev
->dev
;
839 /* this urb is terminated, clean up */
840 dev_dbg(dev
, "%s - urb shutting down with status: %d\n", __func__
, status
);
843 dev_dbg(dev
, "%s - nonzero urb status received: %d\n", __func__
, status
);
847 length
= urb
->actual_length
;
848 data
= urb
->transfer_buffer
;
850 /* Structure of data from 7715 device:
851 * Byte 1: IIR serial Port
853 * Byte 2: DSR parallel port
854 * Byte 4: FIFO status for both */
856 if (unlikely(length
!= 4)) {
857 dev_dbg(dev
, "Wrong data !!!\n");
862 if (!(iir
& 0x01)) { /* serial port interrupt pending */
863 switch (iir
& 0x0f) {
865 dev_dbg(dev
, "Serial Port: Receiver status error or address bit detected in 9-bit mode\n\n");
868 dev_dbg(dev
, "Serial Port: Receiver time out\n");
871 /* dev_dbg(dev, "Serial Port: Modem status change\n"); */
876 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
877 { /* update local copy of DSR reg */
878 struct usb_serial_port
*port
= urb
->context
;
879 struct mos7715_parport
*mos_parport
= port
->serial
->private;
880 if (unlikely(mos_parport
== NULL
))
882 atomic_set(&mos_parport
->shadowDSR
, data
[2]);
887 result
= usb_submit_urb(urb
, GFP_ATOMIC
);
889 dev_err(dev
, "%s - Error %d submitting control urb\n", __func__
, result
);
893 * mos7720_bulk_in_callback
894 * this is the callback function for when we have received data on the
897 static void mos7720_bulk_in_callback(struct urb
*urb
)
900 unsigned char *data
;
901 struct usb_serial_port
*port
;
902 struct tty_struct
*tty
;
903 int status
= urb
->status
;
906 dev_dbg(&urb
->dev
->dev
, "nonzero read bulk status received: %d\n", status
);
912 dev_dbg(&port
->dev
, "Entering...%s\n", __func__
);
914 data
= urb
->transfer_buffer
;
916 tty
= tty_port_tty_get(&port
->port
);
917 if (tty
&& urb
->actual_length
) {
918 tty_insert_flip_string(&port
->port
, data
, urb
->actual_length
);
919 tty_flip_buffer_push(tty
);
923 if (port
->read_urb
->status
!= -EINPROGRESS
) {
924 retval
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
926 dev_dbg(&port
->dev
, "usb_submit_urb(read bulk) failed, retval = %d\n", retval
);
931 * mos7720_bulk_out_data_callback
932 * this is the callback function for when we have finished sending serial
933 * data on the bulk out endpoint.
935 static void mos7720_bulk_out_data_callback(struct urb
*urb
)
937 struct moschip_port
*mos7720_port
;
938 struct tty_struct
*tty
;
939 int status
= urb
->status
;
942 dev_dbg(&urb
->dev
->dev
, "nonzero write bulk status received:%d\n", status
);
946 mos7720_port
= urb
->context
;
948 dev_dbg(&urb
->dev
->dev
, "NULL mos7720_port pointer\n");
952 tty
= tty_port_tty_get(&mos7720_port
->port
->port
);
954 if (tty
&& mos7720_port
->open
)
961 * this function installs the appropriate read interrupt endpoint callback
962 * depending on whether the device is a 7720 or 7715, thus avoiding costly
963 * run-time checks in the high-frequency callback routine itself.
965 static int mos77xx_probe(struct usb_serial
*serial
,
966 const struct usb_device_id
*id
)
968 if (id
->idProduct
== MOSCHIP_DEVICE_ID_7715
)
969 moschip7720_2port_driver
.read_int_callback
=
970 mos7715_interrupt_callback
;
972 moschip7720_2port_driver
.read_int_callback
=
973 mos7720_interrupt_callback
;
978 static int mos77xx_calc_num_ports(struct usb_serial
*serial
)
980 u16 product
= le16_to_cpu(serial
->dev
->descriptor
.idProduct
);
981 if (product
== MOSCHIP_DEVICE_ID_7715
)
987 static int mos7720_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
989 struct usb_serial
*serial
;
991 struct moschip_port
*mos7720_port
;
995 int allocated_urbs
= 0;
998 serial
= port
->serial
;
1000 mos7720_port
= usb_get_serial_port_data(port
);
1001 if (mos7720_port
== NULL
)
1004 usb_clear_halt(serial
->dev
, port
->write_urb
->pipe
);
1005 usb_clear_halt(serial
->dev
, port
->read_urb
->pipe
);
1007 /* Initialising the write urb pool */
1008 for (j
= 0; j
< NUM_URBS
; ++j
) {
1009 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1010 mos7720_port
->write_urb_pool
[j
] = urb
;
1013 dev_err(&port
->dev
, "No more urbs???\n");
1017 urb
->transfer_buffer
= kmalloc(URB_TRANSFER_BUFFER_SIZE
,
1019 if (!urb
->transfer_buffer
) {
1021 "%s-out of memory for urb buffers.\n",
1023 usb_free_urb(mos7720_port
->write_urb_pool
[j
]);
1024 mos7720_port
->write_urb_pool
[j
] = NULL
;
1030 if (!allocated_urbs
)
1033 /* Initialize MCS7720 -- Write Init values to corresponding Registers
1045 * 0x08 : SP1/2 Control Reg
1047 port_number
= port
->number
- port
->serial
->minor
;
1048 read_mos_reg(serial
, port_number
, LSR
, &data
);
1050 dev_dbg(&port
->dev
, "SS::%p LSR:%x\n", mos7720_port
, data
);
1052 write_mos_reg(serial
, dummy
, SP1_REG
, 0x02);
1053 write_mos_reg(serial
, dummy
, SP2_REG
, 0x02);
1055 write_mos_reg(serial
, port_number
, IER
, 0x00);
1056 write_mos_reg(serial
, port_number
, FCR
, 0x00);
1058 write_mos_reg(serial
, port_number
, FCR
, 0xcf);
1059 mos7720_port
->shadowLCR
= 0x03;
1060 write_mos_reg(serial
, port_number
, LCR
, mos7720_port
->shadowLCR
);
1061 mos7720_port
->shadowMCR
= 0x0b;
1062 write_mos_reg(serial
, port_number
, MCR
, mos7720_port
->shadowMCR
);
1064 write_mos_reg(serial
, port_number
, SP_CONTROL_REG
, 0x00);
1065 read_mos_reg(serial
, dummy
, SP_CONTROL_REG
, &data
);
1066 data
= data
| (port
->number
- port
->serial
->minor
+ 1);
1067 write_mos_reg(serial
, dummy
, SP_CONTROL_REG
, data
);
1068 mos7720_port
->shadowLCR
= 0x83;
1069 write_mos_reg(serial
, port_number
, LCR
, mos7720_port
->shadowLCR
);
1070 write_mos_reg(serial
, port_number
, THR
, 0x0c);
1071 write_mos_reg(serial
, port_number
, IER
, 0x00);
1072 mos7720_port
->shadowLCR
= 0x03;
1073 write_mos_reg(serial
, port_number
, LCR
, mos7720_port
->shadowLCR
);
1074 write_mos_reg(serial
, port_number
, IER
, 0x0c);
1076 response
= usb_submit_urb(port
->read_urb
, GFP_KERNEL
);
1078 dev_err(&port
->dev
, "%s - Error %d submitting read urb\n",
1079 __func__
, response
);
1081 /* initialize our icount structure */
1082 memset(&(mos7720_port
->icount
), 0x00, sizeof(mos7720_port
->icount
));
1084 /* initialize our port settings */
1085 mos7720_port
->shadowMCR
= UART_MCR_OUT2
; /* Must set to enable ints! */
1087 /* send a open port command */
1088 mos7720_port
->open
= 1;
1094 * mos7720_chars_in_buffer
1095 * this function is called by the tty driver when it wants to know how many
1096 * bytes of data we currently have outstanding in the port (data that has
1097 * been written, but hasn't made it out the port yet)
1098 * If successful, we return the number of bytes left to be written in the
1100 * Otherwise we return a negative error number.
1102 static int mos7720_chars_in_buffer(struct tty_struct
*tty
)
1104 struct usb_serial_port
*port
= tty
->driver_data
;
1107 struct moschip_port
*mos7720_port
;
1109 mos7720_port
= usb_get_serial_port_data(port
);
1110 if (mos7720_port
== NULL
)
1113 for (i
= 0; i
< NUM_URBS
; ++i
) {
1114 if (mos7720_port
->write_urb_pool
[i
] &&
1115 mos7720_port
->write_urb_pool
[i
]->status
== -EINPROGRESS
)
1116 chars
+= URB_TRANSFER_BUFFER_SIZE
;
1118 dev_dbg(&port
->dev
, "%s - returns %d\n", __func__
, chars
);
1122 static void mos7720_close(struct usb_serial_port
*port
)
1124 struct usb_serial
*serial
;
1125 struct moschip_port
*mos7720_port
;
1128 serial
= port
->serial
;
1130 mos7720_port
= usb_get_serial_port_data(port
);
1131 if (mos7720_port
== NULL
)
1134 for (j
= 0; j
< NUM_URBS
; ++j
)
1135 usb_kill_urb(mos7720_port
->write_urb_pool
[j
]);
1137 /* Freeing Write URBs */
1138 for (j
= 0; j
< NUM_URBS
; ++j
) {
1139 if (mos7720_port
->write_urb_pool
[j
]) {
1140 kfree(mos7720_port
->write_urb_pool
[j
]->transfer_buffer
);
1141 usb_free_urb(mos7720_port
->write_urb_pool
[j
]);
1145 /* While closing port, shutdown all bulk read, write *
1146 * and interrupt read if they exists, otherwise nop */
1147 usb_kill_urb(port
->write_urb
);
1148 usb_kill_urb(port
->read_urb
);
1150 mutex_lock(&serial
->disc_mutex
);
1151 /* these commands must not be issued if the device has
1152 * been disconnected */
1153 if (!serial
->disconnected
) {
1154 write_mos_reg(serial
, port
->number
- port
->serial
->minor
,
1156 write_mos_reg(serial
, port
->number
- port
->serial
->minor
,
1159 mutex_unlock(&serial
->disc_mutex
);
1160 mos7720_port
->open
= 0;
1163 static void mos7720_break(struct tty_struct
*tty
, int break_state
)
1165 struct usb_serial_port
*port
= tty
->driver_data
;
1167 struct usb_serial
*serial
;
1168 struct moschip_port
*mos7720_port
;
1170 serial
= port
->serial
;
1172 mos7720_port
= usb_get_serial_port_data(port
);
1173 if (mos7720_port
== NULL
)
1176 if (break_state
== -1)
1177 data
= mos7720_port
->shadowLCR
| UART_LCR_SBC
;
1179 data
= mos7720_port
->shadowLCR
& ~UART_LCR_SBC
;
1181 mos7720_port
->shadowLCR
= data
;
1182 write_mos_reg(serial
, port
->number
- port
->serial
->minor
,
1183 LCR
, mos7720_port
->shadowLCR
);
1187 * mos7720_write_room
1188 * this function is called by the tty driver when it wants to know how many
1189 * bytes of data we can accept for a specific port.
1190 * If successful, we return the amount of room that we have for this port
1191 * Otherwise we return a negative error number.
1193 static int mos7720_write_room(struct tty_struct
*tty
)
1195 struct usb_serial_port
*port
= tty
->driver_data
;
1196 struct moschip_port
*mos7720_port
;
1200 mos7720_port
= usb_get_serial_port_data(port
);
1201 if (mos7720_port
== NULL
)
1204 /* FIXME: Locking */
1205 for (i
= 0; i
< NUM_URBS
; ++i
) {
1206 if (mos7720_port
->write_urb_pool
[i
] &&
1207 mos7720_port
->write_urb_pool
[i
]->status
!= -EINPROGRESS
)
1208 room
+= URB_TRANSFER_BUFFER_SIZE
;
1211 dev_dbg(&port
->dev
, "%s - returns %d\n", __func__
, room
);
1215 static int mos7720_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
1216 const unsigned char *data
, int count
)
1223 struct moschip_port
*mos7720_port
;
1224 struct usb_serial
*serial
;
1226 const unsigned char *current_position
= data
;
1228 serial
= port
->serial
;
1230 mos7720_port
= usb_get_serial_port_data(port
);
1231 if (mos7720_port
== NULL
)
1234 /* try to find a free urb in the list */
1237 for (i
= 0; i
< NUM_URBS
; ++i
) {
1238 if (mos7720_port
->write_urb_pool
[i
] &&
1239 mos7720_port
->write_urb_pool
[i
]->status
!= -EINPROGRESS
) {
1240 urb
= mos7720_port
->write_urb_pool
[i
];
1241 dev_dbg(&port
->dev
, "URB:%d\n", i
);
1247 dev_dbg(&port
->dev
, "%s - no more free urbs\n", __func__
);
1251 if (urb
->transfer_buffer
== NULL
) {
1252 urb
->transfer_buffer
= kmalloc(URB_TRANSFER_BUFFER_SIZE
,
1254 if (urb
->transfer_buffer
== NULL
) {
1255 dev_err_console(port
, "%s no more kernel memory...\n",
1260 transfer_size
= min(count
, URB_TRANSFER_BUFFER_SIZE
);
1262 memcpy(urb
->transfer_buffer
, current_position
, transfer_size
);
1263 usb_serial_debug_data(&port
->dev
, __func__
, transfer_size
,
1264 urb
->transfer_buffer
);
1266 /* fill urb with data and submit */
1267 usb_fill_bulk_urb(urb
, serial
->dev
,
1268 usb_sndbulkpipe(serial
->dev
,
1269 port
->bulk_out_endpointAddress
),
1270 urb
->transfer_buffer
, transfer_size
,
1271 mos7720_bulk_out_data_callback
, mos7720_port
);
1273 /* send it down the pipe */
1274 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
1276 dev_err_console(port
, "%s - usb_submit_urb(write bulk) failed "
1277 "with status = %d\n", __func__
, status
);
1278 bytes_sent
= status
;
1281 bytes_sent
= transfer_size
;
1287 static void mos7720_throttle(struct tty_struct
*tty
)
1289 struct usb_serial_port
*port
= tty
->driver_data
;
1290 struct moschip_port
*mos7720_port
;
1293 mos7720_port
= usb_get_serial_port_data(port
);
1295 if (mos7720_port
== NULL
)
1298 if (!mos7720_port
->open
) {
1299 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1303 /* if we are implementing XON/XOFF, send the stop character */
1305 unsigned char stop_char
= STOP_CHAR(tty
);
1306 status
= mos7720_write(tty
, port
, &stop_char
, 1);
1311 /* if we are implementing RTS/CTS, toggle that line */
1312 if (tty
->termios
.c_cflag
& CRTSCTS
) {
1313 mos7720_port
->shadowMCR
&= ~UART_MCR_RTS
;
1314 write_mos_reg(port
->serial
, port
->number
- port
->serial
->minor
,
1315 MCR
, mos7720_port
->shadowMCR
);
1321 static void mos7720_unthrottle(struct tty_struct
*tty
)
1323 struct usb_serial_port
*port
= tty
->driver_data
;
1324 struct moschip_port
*mos7720_port
= usb_get_serial_port_data(port
);
1327 if (mos7720_port
== NULL
)
1330 if (!mos7720_port
->open
) {
1331 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1335 /* if we are implementing XON/XOFF, send the start character */
1337 unsigned char start_char
= START_CHAR(tty
);
1338 status
= mos7720_write(tty
, port
, &start_char
, 1);
1343 /* if we are implementing RTS/CTS, toggle that line */
1344 if (tty
->termios
.c_cflag
& CRTSCTS
) {
1345 mos7720_port
->shadowMCR
|= UART_MCR_RTS
;
1346 write_mos_reg(port
->serial
, port
->number
- port
->serial
->minor
,
1347 MCR
, mos7720_port
->shadowMCR
);
1353 /* FIXME: this function does not work */
1354 static int set_higher_rates(struct moschip_port
*mos7720_port
,
1357 struct usb_serial_port
*port
;
1358 struct usb_serial
*serial
;
1360 enum mos_regs sp_reg
;
1361 if (mos7720_port
== NULL
)
1364 port
= mos7720_port
->port
;
1365 serial
= port
->serial
;
1367 /***********************************************
1368 * Init Sequence for higher rates
1369 ***********************************************/
1370 dev_dbg(&port
->dev
, "Sending Setting Commands ..........\n");
1371 port_number
= port
->number
- port
->serial
->minor
;
1373 write_mos_reg(serial
, port_number
, IER
, 0x00);
1374 write_mos_reg(serial
, port_number
, FCR
, 0x00);
1375 write_mos_reg(serial
, port_number
, FCR
, 0xcf);
1376 mos7720_port
->shadowMCR
= 0x0b;
1377 write_mos_reg(serial
, port_number
, MCR
, mos7720_port
->shadowMCR
);
1378 write_mos_reg(serial
, dummy
, SP_CONTROL_REG
, 0x00);
1380 /***********************************************
1381 * Set for higher rates *
1382 ***********************************************/
1383 /* writing baud rate verbatum into uart clock field clearly not right */
1384 if (port_number
== 0)
1388 write_mos_reg(serial
, dummy
, sp_reg
, baud
* 0x10);
1389 write_mos_reg(serial
, dummy
, SP_CONTROL_REG
, 0x03);
1390 mos7720_port
->shadowMCR
= 0x2b;
1391 write_mos_reg(serial
, port_number
, MCR
, mos7720_port
->shadowMCR
);
1393 /***********************************************
1395 ***********************************************/
1396 mos7720_port
->shadowLCR
= mos7720_port
->shadowLCR
| UART_LCR_DLAB
;
1397 write_mos_reg(serial
, port_number
, LCR
, mos7720_port
->shadowLCR
);
1398 write_mos_reg(serial
, port_number
, DLL
, 0x01);
1399 write_mos_reg(serial
, port_number
, DLM
, 0x00);
1400 mos7720_port
->shadowLCR
= mos7720_port
->shadowLCR
& ~UART_LCR_DLAB
;
1401 write_mos_reg(serial
, port_number
, LCR
, mos7720_port
->shadowLCR
);
1406 /* baud rate information */
1407 struct divisor_table_entry
{
1412 /* Define table of divisors for moschip 7720 hardware *
1413 * These assume a 3.6864MHz crystal, the standard /16, and *
1415 static struct divisor_table_entry divisor_table
[] = {
1417 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
1418 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
1434 /*****************************************************************************
1435 * calc_baud_rate_divisor
1436 * this function calculates the proper baud rate divisor for the specified
1438 *****************************************************************************/
1439 static int calc_baud_rate_divisor(struct usb_serial_port
*port
, int baudrate
, int *divisor
)
1447 dev_dbg(&port
->dev
, "%s - %d\n", __func__
, baudrate
);
1449 for (i
= 0; i
< ARRAY_SIZE(divisor_table
); i
++) {
1450 if (divisor_table
[i
].baudrate
== baudrate
) {
1451 *divisor
= divisor_table
[i
].divisor
;
1456 /* After trying for all the standard baud rates *
1457 * Try calculating the divisor for this baud rate */
1458 if (baudrate
> 75 && baudrate
< 230400) {
1459 /* get the divisor */
1460 custom
= (__u16
)(230400L / baudrate
);
1462 /* Check for round off */
1463 round1
= (__u16
)(2304000L / baudrate
);
1464 round
= (__u16
)(round1
- (custom
* 10));
1469 dev_dbg(&port
->dev
, "Baud %d = %d\n", baudrate
, custom
);
1473 dev_dbg(&port
->dev
, "Baud calculation Failed...\n");
1478 * send_cmd_write_baud_rate
1479 * this function sends the proper command to change the baud rate of the
1482 static int send_cmd_write_baud_rate(struct moschip_port
*mos7720_port
,
1485 struct usb_serial_port
*port
;
1486 struct usb_serial
*serial
;
1489 unsigned char number
;
1491 if (mos7720_port
== NULL
)
1494 port
= mos7720_port
->port
;
1495 serial
= port
->serial
;
1497 number
= port
->number
- port
->serial
->minor
;
1498 dev_dbg(&port
->dev
, "%s - baud = %d\n", __func__
, baudrate
);
1500 /* Calculate the Divisor */
1501 status
= calc_baud_rate_divisor(port
, baudrate
, &divisor
);
1503 dev_err(&port
->dev
, "%s - bad baud rate\n", __func__
);
1507 /* Enable access to divisor latch */
1508 mos7720_port
->shadowLCR
= mos7720_port
->shadowLCR
| UART_LCR_DLAB
;
1509 write_mos_reg(serial
, number
, LCR
, mos7720_port
->shadowLCR
);
1511 /* Write the divisor */
1512 write_mos_reg(serial
, number
, DLL
, (__u8
)(divisor
& 0xff));
1513 write_mos_reg(serial
, number
, DLM
, (__u8
)((divisor
& 0xff00) >> 8));
1515 /* Disable access to divisor latch */
1516 mos7720_port
->shadowLCR
= mos7720_port
->shadowLCR
& ~UART_LCR_DLAB
;
1517 write_mos_reg(serial
, number
, LCR
, mos7720_port
->shadowLCR
);
1523 * change_port_settings
1524 * This routine is called to set the UART on the device to match
1525 * the specified new settings.
1527 static void change_port_settings(struct tty_struct
*tty
,
1528 struct moschip_port
*mos7720_port
,
1529 struct ktermios
*old_termios
)
1531 struct usb_serial_port
*port
;
1532 struct usb_serial
*serial
;
1543 if (mos7720_port
== NULL
)
1546 port
= mos7720_port
->port
;
1547 serial
= port
->serial
;
1548 port_number
= port
->number
- port
->serial
->minor
;
1550 if (!mos7720_port
->open
) {
1551 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1555 lData
= UART_LCR_WLEN8
;
1556 lStop
= 0x00; /* 1 stop bit */
1557 lParity
= 0x00; /* No parity */
1559 cflag
= tty
->termios
.c_cflag
;
1560 iflag
= tty
->termios
.c_iflag
;
1562 /* Change the number of bits */
1563 switch (cflag
& CSIZE
) {
1565 lData
= UART_LCR_WLEN5
;
1570 lData
= UART_LCR_WLEN6
;
1575 lData
= UART_LCR_WLEN7
;
1580 lData
= UART_LCR_WLEN8
;
1584 /* Change the Parity bit */
1585 if (cflag
& PARENB
) {
1586 if (cflag
& PARODD
) {
1587 lParity
= UART_LCR_PARITY
;
1588 dev_dbg(&port
->dev
, "%s - parity = odd\n", __func__
);
1590 lParity
= (UART_LCR_EPAR
| UART_LCR_PARITY
);
1591 dev_dbg(&port
->dev
, "%s - parity = even\n", __func__
);
1595 dev_dbg(&port
->dev
, "%s - parity = none\n", __func__
);
1599 lParity
= lParity
| 0x20;
1601 /* Change the Stop bit */
1602 if (cflag
& CSTOPB
) {
1603 lStop
= UART_LCR_STOP
;
1604 dev_dbg(&port
->dev
, "%s - stop bits = 2\n", __func__
);
1607 dev_dbg(&port
->dev
, "%s - stop bits = 1\n", __func__
);
1610 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1611 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1612 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
1614 /* Update the LCR with the correct value */
1615 mos7720_port
->shadowLCR
&=
1616 ~(LCR_BITS_MASK
| LCR_STOP_MASK
| LCR_PAR_MASK
);
1617 mos7720_port
->shadowLCR
|= (lData
| lParity
| lStop
);
1620 /* Disable Interrupts */
1621 write_mos_reg(serial
, port_number
, IER
, 0x00);
1622 write_mos_reg(serial
, port_number
, FCR
, 0x00);
1623 write_mos_reg(serial
, port_number
, FCR
, 0xcf);
1625 /* Send the updated LCR value to the mos7720 */
1626 write_mos_reg(serial
, port_number
, LCR
, mos7720_port
->shadowLCR
);
1627 mos7720_port
->shadowMCR
= 0x0b;
1628 write_mos_reg(serial
, port_number
, MCR
, mos7720_port
->shadowMCR
);
1630 /* set up the MCR register and send it to the mos7720 */
1631 mos7720_port
->shadowMCR
= UART_MCR_OUT2
;
1633 mos7720_port
->shadowMCR
|= (UART_MCR_DTR
| UART_MCR_RTS
);
1635 if (cflag
& CRTSCTS
) {
1636 mos7720_port
->shadowMCR
|= (UART_MCR_XONANY
);
1637 /* To set hardware flow control to the specified *
1638 * serial port, in SP1/2_CONTROL_REG */
1640 write_mos_reg(serial
, dummy
, SP_CONTROL_REG
, 0x01);
1642 write_mos_reg(serial
, dummy
, SP_CONTROL_REG
, 0x02);
1645 mos7720_port
->shadowMCR
&= ~(UART_MCR_XONANY
);
1647 write_mos_reg(serial
, port_number
, MCR
, mos7720_port
->shadowMCR
);
1649 /* Determine divisor based on baud rate */
1650 baud
= tty_get_baud_rate(tty
);
1652 /* pick a default, any default... */
1653 dev_dbg(&port
->dev
, "Picked default baud...\n");
1657 if (baud
>= 230400) {
1658 set_higher_rates(mos7720_port
, baud
);
1659 /* Enable Interrupts */
1660 write_mos_reg(serial
, port_number
, IER
, 0x0c);
1664 dev_dbg(&port
->dev
, "%s - baud rate = %d\n", __func__
, baud
);
1665 status
= send_cmd_write_baud_rate(mos7720_port
, baud
);
1666 /* FIXME: needs to write actual resulting baud back not just
1669 tty_encode_baud_rate(tty
, baud
, baud
);
1670 /* Enable Interrupts */
1671 write_mos_reg(serial
, port_number
, IER
, 0x0c);
1673 if (port
->read_urb
->status
!= -EINPROGRESS
) {
1674 status
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
1676 dev_dbg(&port
->dev
, "usb_submit_urb(read bulk) failed, status = %d\n", status
);
1681 * mos7720_set_termios
1682 * this function is called by the tty driver when it wants to change the
1683 * termios structure.
1685 static void mos7720_set_termios(struct tty_struct
*tty
,
1686 struct usb_serial_port
*port
, struct ktermios
*old_termios
)
1690 struct usb_serial
*serial
;
1691 struct moschip_port
*mos7720_port
;
1693 serial
= port
->serial
;
1695 mos7720_port
= usb_get_serial_port_data(port
);
1697 if (mos7720_port
== NULL
)
1700 if (!mos7720_port
->open
) {
1701 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1705 dev_dbg(&port
->dev
, "setting termios - ASPIRE\n");
1707 cflag
= tty
->termios
.c_cflag
;
1709 dev_dbg(&port
->dev
, "%s - cflag %08x iflag %08x\n", __func__
,
1710 tty
->termios
.c_cflag
, RELEVANT_IFLAG(tty
->termios
.c_iflag
));
1712 dev_dbg(&port
->dev
, "%s - old cflag %08x old iflag %08x\n", __func__
,
1713 old_termios
->c_cflag
, RELEVANT_IFLAG(old_termios
->c_iflag
));
1715 /* change the port settings to the new ones specified */
1716 change_port_settings(tty
, mos7720_port
, old_termios
);
1718 if (port
->read_urb
->status
!= -EINPROGRESS
) {
1719 status
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
1721 dev_dbg(&port
->dev
, "usb_submit_urb(read bulk) failed, status = %d\n", status
);
1726 * get_lsr_info - get line status register info
1728 * Purpose: Let user call ioctl() to get info when the UART physically
1729 * is emptied. On bus types like RS485, the transmitter must
1730 * release the bus after transmitting. This must be done when
1731 * the transmit shift register is empty, not be done when the
1732 * transmit holding register is empty. This functionality
1733 * allows an RS485 driver to be written in user space.
1735 static int get_lsr_info(struct tty_struct
*tty
,
1736 struct moschip_port
*mos7720_port
, unsigned int __user
*value
)
1738 struct usb_serial_port
*port
= tty
->driver_data
;
1739 unsigned int result
= 0;
1740 unsigned char data
= 0;
1741 int port_number
= port
->number
- port
->serial
->minor
;
1744 count
= mos7720_chars_in_buffer(tty
);
1746 read_mos_reg(port
->serial
, port_number
, LSR
, &data
);
1747 if ((data
& (UART_LSR_TEMT
| UART_LSR_THRE
))
1748 == (UART_LSR_TEMT
| UART_LSR_THRE
)) {
1749 dev_dbg(&port
->dev
, "%s -- Empty\n", __func__
);
1750 result
= TIOCSER_TEMT
;
1753 if (copy_to_user(value
, &result
, sizeof(int)))
1758 static int mos7720_tiocmget(struct tty_struct
*tty
)
1760 struct usb_serial_port
*port
= tty
->driver_data
;
1761 struct moschip_port
*mos7720_port
= usb_get_serial_port_data(port
);
1762 unsigned int result
= 0;
1766 mcr
= mos7720_port
->shadowMCR
;
1767 msr
= mos7720_port
->shadowMSR
;
1769 result
= ((mcr
& UART_MCR_DTR
) ? TIOCM_DTR
: 0) /* 0x002 */
1770 | ((mcr
& UART_MCR_RTS
) ? TIOCM_RTS
: 0) /* 0x004 */
1771 | ((msr
& UART_MSR_CTS
) ? TIOCM_CTS
: 0) /* 0x020 */
1772 | ((msr
& UART_MSR_DCD
) ? TIOCM_CAR
: 0) /* 0x040 */
1773 | ((msr
& UART_MSR_RI
) ? TIOCM_RI
: 0) /* 0x080 */
1774 | ((msr
& UART_MSR_DSR
) ? TIOCM_DSR
: 0); /* 0x100 */
1779 static int mos7720_tiocmset(struct tty_struct
*tty
,
1780 unsigned int set
, unsigned int clear
)
1782 struct usb_serial_port
*port
= tty
->driver_data
;
1783 struct moschip_port
*mos7720_port
= usb_get_serial_port_data(port
);
1786 mcr
= mos7720_port
->shadowMCR
;
1788 if (set
& TIOCM_RTS
)
1789 mcr
|= UART_MCR_RTS
;
1790 if (set
& TIOCM_DTR
)
1791 mcr
|= UART_MCR_DTR
;
1792 if (set
& TIOCM_LOOP
)
1793 mcr
|= UART_MCR_LOOP
;
1795 if (clear
& TIOCM_RTS
)
1796 mcr
&= ~UART_MCR_RTS
;
1797 if (clear
& TIOCM_DTR
)
1798 mcr
&= ~UART_MCR_DTR
;
1799 if (clear
& TIOCM_LOOP
)
1800 mcr
&= ~UART_MCR_LOOP
;
1802 mos7720_port
->shadowMCR
= mcr
;
1803 write_mos_reg(port
->serial
, port
->number
- port
->serial
->minor
,
1804 MCR
, mos7720_port
->shadowMCR
);
1809 static int mos7720_get_icount(struct tty_struct
*tty
,
1810 struct serial_icounter_struct
*icount
)
1812 struct usb_serial_port
*port
= tty
->driver_data
;
1813 struct moschip_port
*mos7720_port
;
1814 struct async_icount cnow
;
1816 mos7720_port
= usb_get_serial_port_data(port
);
1817 cnow
= mos7720_port
->icount
;
1819 icount
->cts
= cnow
.cts
;
1820 icount
->dsr
= cnow
.dsr
;
1821 icount
->rng
= cnow
.rng
;
1822 icount
->dcd
= cnow
.dcd
;
1823 icount
->rx
= cnow
.rx
;
1824 icount
->tx
= cnow
.tx
;
1825 icount
->frame
= cnow
.frame
;
1826 icount
->overrun
= cnow
.overrun
;
1827 icount
->parity
= cnow
.parity
;
1828 icount
->brk
= cnow
.brk
;
1829 icount
->buf_overrun
= cnow
.buf_overrun
;
1831 dev_dbg(&port
->dev
, "%s TIOCGICOUNT RX=%d, TX=%d\n", __func__
,
1832 icount
->rx
, icount
->tx
);
1836 static int set_modem_info(struct moschip_port
*mos7720_port
, unsigned int cmd
,
1837 unsigned int __user
*value
)
1842 struct usb_serial_port
*port
;
1844 if (mos7720_port
== NULL
)
1847 port
= (struct usb_serial_port
*)mos7720_port
->port
;
1848 mcr
= mos7720_port
->shadowMCR
;
1850 if (copy_from_user(&arg
, value
, sizeof(int)))
1855 if (arg
& TIOCM_RTS
)
1856 mcr
|= UART_MCR_RTS
;
1857 if (arg
& TIOCM_DTR
)
1858 mcr
|= UART_MCR_RTS
;
1859 if (arg
& TIOCM_LOOP
)
1860 mcr
|= UART_MCR_LOOP
;
1864 if (arg
& TIOCM_RTS
)
1865 mcr
&= ~UART_MCR_RTS
;
1866 if (arg
& TIOCM_DTR
)
1867 mcr
&= ~UART_MCR_RTS
;
1868 if (arg
& TIOCM_LOOP
)
1869 mcr
&= ~UART_MCR_LOOP
;
1874 mos7720_port
->shadowMCR
= mcr
;
1875 write_mos_reg(port
->serial
, port
->number
- port
->serial
->minor
,
1876 MCR
, mos7720_port
->shadowMCR
);
1881 static int get_serial_info(struct moschip_port
*mos7720_port
,
1882 struct serial_struct __user
*retinfo
)
1884 struct serial_struct tmp
;
1889 memset(&tmp
, 0, sizeof(tmp
));
1891 tmp
.type
= PORT_16550A
;
1892 tmp
.line
= mos7720_port
->port
->serial
->minor
;
1893 tmp
.port
= mos7720_port
->port
->number
;
1895 tmp
.flags
= ASYNC_SKIP_TEST
| ASYNC_AUTO_IRQ
;
1896 tmp
.xmit_fifo_size
= NUM_URBS
* URB_TRANSFER_BUFFER_SIZE
;
1897 tmp
.baud_base
= 9600;
1898 tmp
.close_delay
= 5*HZ
;
1899 tmp
.closing_wait
= 30*HZ
;
1901 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
1906 static int mos7720_ioctl(struct tty_struct
*tty
,
1907 unsigned int cmd
, unsigned long arg
)
1909 struct usb_serial_port
*port
= tty
->driver_data
;
1910 struct moschip_port
*mos7720_port
;
1911 struct async_icount cnow
;
1912 struct async_icount cprev
;
1914 mos7720_port
= usb_get_serial_port_data(port
);
1915 if (mos7720_port
== NULL
)
1918 dev_dbg(&port
->dev
, "%s - cmd = 0x%x", __func__
, cmd
);
1922 dev_dbg(&port
->dev
, "%s TIOCSERGETLSR\n", __func__
);
1923 return get_lsr_info(tty
, mos7720_port
,
1924 (unsigned int __user
*)arg
);
1926 /* FIXME: These should be using the mode methods */
1929 dev_dbg(&port
->dev
, "%s TIOCMSET/TIOCMBIC/TIOCMSET\n", __func__
);
1930 return set_modem_info(mos7720_port
, cmd
,
1931 (unsigned int __user
*)arg
);
1934 dev_dbg(&port
->dev
, "%s TIOCGSERIAL\n", __func__
);
1935 return get_serial_info(mos7720_port
,
1936 (struct serial_struct __user
*)arg
);
1939 dev_dbg(&port
->dev
, "%s TIOCMIWAIT\n", __func__
);
1940 cprev
= mos7720_port
->icount
;
1942 if (signal_pending(current
))
1943 return -ERESTARTSYS
;
1944 cnow
= mos7720_port
->icount
;
1945 if (cnow
.rng
== cprev
.rng
&& cnow
.dsr
== cprev
.dsr
&&
1946 cnow
.dcd
== cprev
.dcd
&& cnow
.cts
== cprev
.cts
)
1947 return -EIO
; /* no change => error */
1948 if (((arg
& TIOCM_RNG
) && (cnow
.rng
!= cprev
.rng
)) ||
1949 ((arg
& TIOCM_DSR
) && (cnow
.dsr
!= cprev
.dsr
)) ||
1950 ((arg
& TIOCM_CD
) && (cnow
.dcd
!= cprev
.dcd
)) ||
1951 ((arg
& TIOCM_CTS
) && (cnow
.cts
!= cprev
.cts
))) {
1960 return -ENOIOCTLCMD
;
1963 static int mos7720_startup(struct usb_serial
*serial
)
1965 struct usb_device
*dev
;
1970 product
= le16_to_cpu(serial
->dev
->descriptor
.idProduct
);
1974 * The 7715 uses the first bulk in/out endpoint pair for the parallel
1975 * port, and the second for the serial port. Because the usbserial core
1976 * assumes both pairs are serial ports, we must engage in a bit of
1977 * subterfuge and swap the pointers for ports 0 and 1 in order to make
1978 * port 0 point to the serial port. However, both moschip devices use a
1979 * single interrupt-in endpoint for both ports (as mentioned a little
1980 * further down), and this endpoint was assigned to port 0. So after
1981 * the swap, we must copy the interrupt endpoint elements from port 1
1982 * (as newly assigned) to port 0, and null out port 1 pointers.
1984 if (product
== MOSCHIP_DEVICE_ID_7715
) {
1985 struct usb_serial_port
*tmp
= serial
->port
[0];
1986 serial
->port
[0] = serial
->port
[1];
1987 serial
->port
[1] = tmp
;
1988 serial
->port
[0]->interrupt_in_urb
= tmp
->interrupt_in_urb
;
1989 serial
->port
[0]->interrupt_in_buffer
= tmp
->interrupt_in_buffer
;
1990 serial
->port
[0]->interrupt_in_endpointAddress
=
1991 tmp
->interrupt_in_endpointAddress
;
1992 serial
->port
[1]->interrupt_in_urb
= NULL
;
1993 serial
->port
[1]->interrupt_in_buffer
= NULL
;
1996 /* setting configuration feature to one */
1997 usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
1998 (__u8
)0x03, 0x00, 0x01, 0x00, NULL
, 0x00, 5*HZ
);
2000 /* start the interrupt urb */
2001 ret_val
= usb_submit_urb(serial
->port
[0]->interrupt_in_urb
, GFP_KERNEL
);
2004 "%s - Error %d submitting control urb\n",
2007 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
2008 if (product
== MOSCHIP_DEVICE_ID_7715
) {
2009 ret_val
= mos7715_parport_init(serial
);
2014 /* LSR For Port 1 */
2015 read_mos_reg(serial
, 0, LSR
, &data
);
2016 dev_dbg(&dev
->dev
, "LSR:%x\n", data
);
2021 static void mos7720_release(struct usb_serial
*serial
)
2023 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
2024 /* close the parallel port */
2026 if (le16_to_cpu(serial
->dev
->descriptor
.idProduct
)
2027 == MOSCHIP_DEVICE_ID_7715
) {
2028 struct urbtracker
*urbtrack
;
2029 unsigned long flags
;
2030 struct mos7715_parport
*mos_parport
=
2031 usb_get_serial_data(serial
);
2033 /* prevent NULL ptr dereference in port callbacks */
2034 spin_lock(&release_lock
);
2035 mos_parport
->pp
->private_data
= NULL
;
2036 spin_unlock(&release_lock
);
2038 /* wait for synchronous usb calls to return */
2039 if (mos_parport
->msg_pending
)
2040 wait_for_completion_timeout(&mos_parport
->syncmsg_compl
,
2043 parport_remove_port(mos_parport
->pp
);
2044 usb_set_serial_data(serial
, NULL
);
2045 mos_parport
->serial
= NULL
;
2047 /* if tasklet currently scheduled, wait for it to complete */
2048 tasklet_kill(&mos_parport
->urb_tasklet
);
2050 /* unlink any urbs sent by the tasklet */
2051 spin_lock_irqsave(&mos_parport
->listlock
, flags
);
2052 list_for_each_entry(urbtrack
,
2053 &mos_parport
->active_urbs
,
2055 usb_unlink_urb(urbtrack
->urb
);
2056 spin_unlock_irqrestore(&mos_parport
->listlock
, flags
);
2058 kref_put(&mos_parport
->ref_count
, destroy_mos_parport
);
2063 static int mos7720_port_probe(struct usb_serial_port
*port
)
2065 struct moschip_port
*mos7720_port
;
2067 mos7720_port
= kzalloc(sizeof(*mos7720_port
), GFP_KERNEL
);
2071 /* Initialize all port interrupt end point to port 0 int endpoint.
2072 * Our device has only one interrupt endpoint common to all ports.
2074 port
->interrupt_in_endpointAddress
=
2075 port
->serial
->port
[0]->interrupt_in_endpointAddress
;
2076 mos7720_port
->port
= port
;
2078 usb_set_serial_port_data(port
, mos7720_port
);
2083 static int mos7720_port_remove(struct usb_serial_port
*port
)
2085 struct moschip_port
*mos7720_port
;
2087 mos7720_port
= usb_get_serial_port_data(port
);
2088 kfree(mos7720_port
);
2093 static struct usb_serial_driver moschip7720_2port_driver
= {
2095 .owner
= THIS_MODULE
,
2096 .name
= "moschip7720",
2098 .description
= "Moschip 2 port adapter",
2099 .id_table
= id_table
,
2100 .calc_num_ports
= mos77xx_calc_num_ports
,
2101 .open
= mos7720_open
,
2102 .close
= mos7720_close
,
2103 .throttle
= mos7720_throttle
,
2104 .unthrottle
= mos7720_unthrottle
,
2105 .probe
= mos77xx_probe
,
2106 .attach
= mos7720_startup
,
2107 .release
= mos7720_release
,
2108 .port_probe
= mos7720_port_probe
,
2109 .port_remove
= mos7720_port_remove
,
2110 .ioctl
= mos7720_ioctl
,
2111 .tiocmget
= mos7720_tiocmget
,
2112 .tiocmset
= mos7720_tiocmset
,
2113 .get_icount
= mos7720_get_icount
,
2114 .set_termios
= mos7720_set_termios
,
2115 .write
= mos7720_write
,
2116 .write_room
= mos7720_write_room
,
2117 .chars_in_buffer
= mos7720_chars_in_buffer
,
2118 .break_ctl
= mos7720_break
,
2119 .read_bulk_callback
= mos7720_bulk_in_callback
,
2120 .read_int_callback
= NULL
/* dynamically assigned in probe() */
2123 static struct usb_serial_driver
* const serial_drivers
[] = {
2124 &moschip7720_2port_driver
, NULL
2127 module_usb_serial_driver(serial_drivers
, id_table
);
2129 MODULE_AUTHOR(DRIVER_AUTHOR
);
2130 MODULE_DESCRIPTION(DRIVER_DESC
);
2131 MODULE_LICENSE("GPL");