3 * Controls the Moschip 7720 usb to dual port serial converter
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/slab.h>
26 #include <linux/tty.h>
27 #include <linux/tty_driver.h>
28 #include <linux/tty_flip.h>
29 #include <linux/module.h>
30 #include <linux/spinlock.h>
31 #include <linux/serial.h>
32 #include <linux/serial_reg.h>
33 #include <linux/usb.h>
34 #include <linux/usb/serial.h>
35 #include <linux/uaccess.h>
36 #include <linux/parport.h>
38 #define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
39 #define DRIVER_DESC "Moschip USB Serial Driver"
41 /* default urb timeout */
42 #define MOS_WDR_TIMEOUT 5000
44 #define MOS_MAX_PORT 0x02
45 #define MOS_WRITE 0x0E
48 /* Interrupt Routines Defines */
49 #define SERIAL_IIR_RLS 0x06
50 #define SERIAL_IIR_RDA 0x04
51 #define SERIAL_IIR_CTI 0x0c
52 #define SERIAL_IIR_THR 0x02
53 #define SERIAL_IIR_MS 0x00
55 #define NUM_URBS 16 /* URB Count */
56 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
58 /* This structure holds all of the local serial port information */
60 __u8 shadowLCR
; /* last LCR value received */
61 __u8 shadowMCR
; /* last MCR value received */
62 __u8 shadowMSR
; /* last MSR value received */
64 struct usb_serial_port
*port
; /* loop back to the owner */
65 struct urb
*write_urb_pool
[NUM_URBS
];
68 static struct usb_serial_driver moschip7720_2port_driver
;
70 #define USB_VENDOR_ID_MOSCHIP 0x9710
71 #define MOSCHIP_DEVICE_ID_7720 0x7720
72 #define MOSCHIP_DEVICE_ID_7715 0x7715
74 static const struct usb_device_id id_table
[] = {
75 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7720
) },
76 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7715
) },
77 { } /* terminating entry */
79 MODULE_DEVICE_TABLE(usb
, id_table
);
81 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
83 /* initial values for parport regs */
84 #define DCR_INIT_VAL 0x0c /* SLCTIN, nINIT */
85 #define ECR_INIT_VAL 0x00 /* SPP mode */
88 struct mos7715_parport
*mos_parport
;
89 struct list_head urblist_entry
;
90 struct kref ref_count
;
92 struct usb_ctrlrequest
*setup
;
95 enum mos7715_pp_modes
{
97 PS2
= 1<<5, /* moschip calls this 'NIBBLE' mode */
98 PPF
= 2<<5, /* moschip calls this 'CB-FIFO mode */
101 struct mos7715_parport
{
102 struct parport
*pp
; /* back to containing struct */
103 struct kref ref_count
; /* to instance of this struct */
104 struct list_head deferred_urbs
; /* list deferred async urbs */
105 struct list_head active_urbs
; /* list async urbs in flight */
106 spinlock_t listlock
; /* protects list access */
107 bool msg_pending
; /* usb sync call pending */
108 struct completion syncmsg_compl
; /* usb sync call completed */
109 struct tasklet_struct urb_tasklet
; /* for sending deferred urbs */
110 struct usb_serial
*serial
; /* back to containing struct */
111 __u8 shadowECR
; /* parallel port regs... */
113 atomic_t shadowDSR
; /* updated in int-in callback */
116 /* lock guards against dereferencing NULL ptr in parport ops callbacks */
117 static DEFINE_SPINLOCK(release_lock
);
119 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
121 static const unsigned int dummy
; /* for clarity in register access fns */
124 THR
, /* serial port regs */
136 DPR
, /* parallel port regs */
140 SP1_REG
, /* device control regs */
141 SP2_REG
, /* serial port 2 (7720 only) */
147 * Return the correct value for the Windex field of the setup packet
148 * for a control endpoint message. See the 7715 datasheet.
150 static inline __u16
get_reg_index(enum mos_regs reg
)
152 static const __u16 mos7715_index_lookup_table
[] = {
170 0x02, /* SP2_REG (7720 only) */
171 0x04, /* PP_REG (7715 only) */
172 0x08, /* SP_CONTROL_REG */
174 return mos7715_index_lookup_table
[reg
];
178 * Return the correct value for the upper byte of the Wvalue field of
179 * the setup packet for a control endpoint message.
181 static inline __u16
get_reg_value(enum mos_regs reg
,
182 unsigned int serial_portnum
)
184 if (reg
>= SP1_REG
) /* control reg */
187 else if (reg
>= DPR
) /* parallel port reg (7715 only) */
190 else /* serial port reg */
191 return (serial_portnum
+ 2) << 8;
195 * Write data byte to the specified device register. The data is embedded in
196 * the value field of the setup packet. serial_portnum is ignored for registers
197 * not specific to a particular serial port.
199 static int write_mos_reg(struct usb_serial
*serial
, unsigned int serial_portnum
,
200 enum mos_regs reg
, __u8 data
)
202 struct usb_device
*usbdev
= serial
->dev
;
203 unsigned int pipe
= usb_sndctrlpipe(usbdev
, 0);
204 __u8 request
= (__u8
)0x0e;
205 __u8 requesttype
= (__u8
)0x40;
206 __u16 index
= get_reg_index(reg
);
207 __u16 value
= get_reg_value(reg
, serial_portnum
) + data
;
208 int status
= usb_control_msg(usbdev
, pipe
, request
, requesttype
, value
,
209 index
, NULL
, 0, MOS_WDR_TIMEOUT
);
211 dev_err(&usbdev
->dev
,
212 "mos7720: usb_control_msg() failed: %d\n", status
);
217 * Read data byte from the specified device register. The data returned by the
218 * device is embedded in the value field of the setup packet. serial_portnum is
219 * ignored for registers that are not specific to a particular serial port.
221 static int read_mos_reg(struct usb_serial
*serial
, unsigned int serial_portnum
,
222 enum mos_regs reg
, __u8
*data
)
224 struct usb_device
*usbdev
= serial
->dev
;
225 unsigned int pipe
= usb_rcvctrlpipe(usbdev
, 0);
226 __u8 request
= (__u8
)0x0d;
227 __u8 requesttype
= (__u8
)0xc0;
228 __u16 index
= get_reg_index(reg
);
229 __u16 value
= get_reg_value(reg
, serial_portnum
);
233 buf
= kmalloc(1, GFP_KERNEL
);
237 status
= usb_control_msg(usbdev
, pipe
, request
, requesttype
, value
,
238 index
, buf
, 1, MOS_WDR_TIMEOUT
);
242 dev_err(&usbdev
->dev
,
243 "mos7720: usb_control_msg() failed: %d\n", status
);
249 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
251 static inline int mos7715_change_mode(struct mos7715_parport
*mos_parport
,
252 enum mos7715_pp_modes mode
)
254 mos_parport
->shadowECR
= mode
;
255 write_mos_reg(mos_parport
->serial
, dummy
, ECR
, mos_parport
->shadowECR
);
259 static void destroy_mos_parport(struct kref
*kref
)
261 struct mos7715_parport
*mos_parport
=
262 container_of(kref
, struct mos7715_parport
, ref_count
);
267 static void destroy_urbtracker(struct kref
*kref
)
269 struct urbtracker
*urbtrack
=
270 container_of(kref
, struct urbtracker
, ref_count
);
271 struct mos7715_parport
*mos_parport
= urbtrack
->mos_parport
;
273 usb_free_urb(urbtrack
->urb
);
274 kfree(urbtrack
->setup
);
276 kref_put(&mos_parport
->ref_count
, destroy_mos_parport
);
280 * This runs as a tasklet when sending an urb in a non-blocking parallel
281 * port callback had to be deferred because the disconnect mutex could not be
282 * obtained at the time.
284 static void send_deferred_urbs(unsigned long _mos_parport
)
288 struct mos7715_parport
*mos_parport
= (void *)_mos_parport
;
289 struct urbtracker
*urbtrack
, *tmp
;
290 struct list_head
*cursor
, *next
;
293 /* if release function ran, game over */
294 if (unlikely(mos_parport
->serial
== NULL
))
297 dev
= &mos_parport
->serial
->dev
->dev
;
299 /* try again to get the mutex */
300 if (!mutex_trylock(&mos_parport
->serial
->disc_mutex
)) {
301 dev_dbg(dev
, "%s: rescheduling tasklet\n", __func__
);
302 tasklet_schedule(&mos_parport
->urb_tasklet
);
306 /* if device disconnected, game over */
307 if (unlikely(mos_parport
->serial
->disconnected
)) {
308 mutex_unlock(&mos_parport
->serial
->disc_mutex
);
312 spin_lock_irqsave(&mos_parport
->listlock
, flags
);
313 if (list_empty(&mos_parport
->deferred_urbs
)) {
314 spin_unlock_irqrestore(&mos_parport
->listlock
, flags
);
315 mutex_unlock(&mos_parport
->serial
->disc_mutex
);
316 dev_dbg(dev
, "%s: deferred_urbs list empty\n", __func__
);
320 /* move contents of deferred_urbs list to active_urbs list and submit */
321 list_for_each_safe(cursor
, next
, &mos_parport
->deferred_urbs
)
322 list_move_tail(cursor
, &mos_parport
->active_urbs
);
323 list_for_each_entry_safe(urbtrack
, tmp
, &mos_parport
->active_urbs
,
325 ret_val
= usb_submit_urb(urbtrack
->urb
, GFP_ATOMIC
);
326 dev_dbg(dev
, "%s: urb submitted\n", __func__
);
328 dev_err(dev
, "usb_submit_urb() failed: %d\n", ret_val
);
329 list_del(&urbtrack
->urblist_entry
);
330 kref_put(&urbtrack
->ref_count
, destroy_urbtracker
);
333 spin_unlock_irqrestore(&mos_parport
->listlock
, flags
);
334 mutex_unlock(&mos_parport
->serial
->disc_mutex
);
337 /* callback for parallel port control urbs submitted asynchronously */
338 static void async_complete(struct urb
*urb
)
340 struct urbtracker
*urbtrack
= urb
->context
;
341 int status
= urb
->status
;
343 if (unlikely(status
))
344 dev_dbg(&urb
->dev
->dev
, "%s - nonzero urb status received: %d\n", __func__
, status
);
346 /* remove the urbtracker from the active_urbs list */
347 spin_lock(&urbtrack
->mos_parport
->listlock
);
348 list_del(&urbtrack
->urblist_entry
);
349 spin_unlock(&urbtrack
->mos_parport
->listlock
);
350 kref_put(&urbtrack
->ref_count
, destroy_urbtracker
);
353 static int write_parport_reg_nonblock(struct mos7715_parport
*mos_parport
,
354 enum mos_regs reg
, __u8 data
)
356 struct urbtracker
*urbtrack
;
359 struct usb_serial
*serial
= mos_parport
->serial
;
360 struct usb_device
*usbdev
= serial
->dev
;
362 /* create and initialize the control urb and containing urbtracker */
363 urbtrack
= kmalloc(sizeof(struct urbtracker
), GFP_ATOMIC
);
367 kref_get(&mos_parport
->ref_count
);
368 urbtrack
->mos_parport
= mos_parport
;
369 urbtrack
->urb
= usb_alloc_urb(0, GFP_ATOMIC
);
370 if (!urbtrack
->urb
) {
374 urbtrack
->setup
= kmalloc(sizeof(*urbtrack
->setup
), GFP_ATOMIC
);
375 if (!urbtrack
->setup
) {
376 usb_free_urb(urbtrack
->urb
);
380 urbtrack
->setup
->bRequestType
= (__u8
)0x40;
381 urbtrack
->setup
->bRequest
= (__u8
)0x0e;
382 urbtrack
->setup
->wValue
= cpu_to_le16(get_reg_value(reg
, dummy
));
383 urbtrack
->setup
->wIndex
= cpu_to_le16(get_reg_index(reg
));
384 urbtrack
->setup
->wLength
= 0;
385 usb_fill_control_urb(urbtrack
->urb
, usbdev
,
386 usb_sndctrlpipe(usbdev
, 0),
387 (unsigned char *)urbtrack
->setup
,
388 NULL
, 0, async_complete
, urbtrack
);
389 kref_init(&urbtrack
->ref_count
);
390 INIT_LIST_HEAD(&urbtrack
->urblist_entry
);
393 * get the disconnect mutex, or add tracker to the deferred_urbs list
394 * and schedule a tasklet to try again later
396 if (!mutex_trylock(&serial
->disc_mutex
)) {
397 spin_lock_irqsave(&mos_parport
->listlock
, flags
);
398 list_add_tail(&urbtrack
->urblist_entry
,
399 &mos_parport
->deferred_urbs
);
400 spin_unlock_irqrestore(&mos_parport
->listlock
, flags
);
401 tasklet_schedule(&mos_parport
->urb_tasklet
);
402 dev_dbg(&usbdev
->dev
, "tasklet scheduled\n");
406 /* bail if device disconnected */
407 if (serial
->disconnected
) {
408 kref_put(&urbtrack
->ref_count
, destroy_urbtracker
);
409 mutex_unlock(&serial
->disc_mutex
);
413 /* add the tracker to the active_urbs list and submit */
414 spin_lock_irqsave(&mos_parport
->listlock
, flags
);
415 list_add_tail(&urbtrack
->urblist_entry
, &mos_parport
->active_urbs
);
416 spin_unlock_irqrestore(&mos_parport
->listlock
, flags
);
417 ret_val
= usb_submit_urb(urbtrack
->urb
, GFP_ATOMIC
);
418 mutex_unlock(&serial
->disc_mutex
);
420 dev_err(&usbdev
->dev
,
421 "%s: submit_urb() failed: %d\n", __func__
, ret_val
);
422 spin_lock_irqsave(&mos_parport
->listlock
, flags
);
423 list_del(&urbtrack
->urblist_entry
);
424 spin_unlock_irqrestore(&mos_parport
->listlock
, flags
);
425 kref_put(&urbtrack
->ref_count
, destroy_urbtracker
);
432 * This is the the common top part of all parallel port callback operations that
433 * send synchronous messages to the device. This implements convoluted locking
434 * that avoids two scenarios: (1) a port operation is called after usbserial
435 * has called our release function, at which point struct mos7715_parport has
436 * been destroyed, and (2) the device has been disconnected, but usbserial has
437 * not called the release function yet because someone has a serial port open.
438 * The shared release_lock prevents the first, and the mutex and disconnected
439 * flag maintained by usbserial covers the second. We also use the msg_pending
440 * flag to ensure that all synchronous usb message calls have completed before
441 * our release function can return.
443 static int parport_prologue(struct parport
*pp
)
445 struct mos7715_parport
*mos_parport
;
447 spin_lock(&release_lock
);
448 mos_parport
= pp
->private_data
;
449 if (unlikely(mos_parport
== NULL
)) {
450 /* release fn called, port struct destroyed */
451 spin_unlock(&release_lock
);
454 mos_parport
->msg_pending
= true; /* synch usb call pending */
455 reinit_completion(&mos_parport
->syncmsg_compl
);
456 spin_unlock(&release_lock
);
458 mutex_lock(&mos_parport
->serial
->disc_mutex
);
459 if (mos_parport
->serial
->disconnected
) {
460 /* device disconnected */
461 mutex_unlock(&mos_parport
->serial
->disc_mutex
);
462 mos_parport
->msg_pending
= false;
463 complete(&mos_parport
->syncmsg_compl
);
471 * This is the common bottom part of all parallel port functions that send
472 * synchronous messages to the device.
474 static inline void parport_epilogue(struct parport
*pp
)
476 struct mos7715_parport
*mos_parport
= pp
->private_data
;
477 mutex_unlock(&mos_parport
->serial
->disc_mutex
);
478 mos_parport
->msg_pending
= false;
479 complete(&mos_parport
->syncmsg_compl
);
482 static void parport_mos7715_write_data(struct parport
*pp
, unsigned char d
)
484 struct mos7715_parport
*mos_parport
= pp
->private_data
;
486 if (parport_prologue(pp
) < 0)
488 mos7715_change_mode(mos_parport
, SPP
);
489 write_mos_reg(mos_parport
->serial
, dummy
, DPR
, (__u8
)d
);
490 parport_epilogue(pp
);
493 static unsigned char parport_mos7715_read_data(struct parport
*pp
)
495 struct mos7715_parport
*mos_parport
= pp
->private_data
;
498 if (parport_prologue(pp
) < 0)
500 read_mos_reg(mos_parport
->serial
, dummy
, DPR
, &d
);
501 parport_epilogue(pp
);
505 static void parport_mos7715_write_control(struct parport
*pp
, unsigned char d
)
507 struct mos7715_parport
*mos_parport
= pp
->private_data
;
510 if (parport_prologue(pp
) < 0)
512 data
= ((__u8
)d
& 0x0f) | (mos_parport
->shadowDCR
& 0xf0);
513 write_mos_reg(mos_parport
->serial
, dummy
, DCR
, data
);
514 mos_parport
->shadowDCR
= data
;
515 parport_epilogue(pp
);
518 static unsigned char parport_mos7715_read_control(struct parport
*pp
)
520 struct mos7715_parport
*mos_parport
= pp
->private_data
;
523 spin_lock(&release_lock
);
524 mos_parport
= pp
->private_data
;
525 if (unlikely(mos_parport
== NULL
)) {
526 spin_unlock(&release_lock
);
529 dcr
= mos_parport
->shadowDCR
& 0x0f;
530 spin_unlock(&release_lock
);
534 static unsigned char parport_mos7715_frob_control(struct parport
*pp
,
538 struct mos7715_parport
*mos_parport
= pp
->private_data
;
543 if (parport_prologue(pp
) < 0)
545 mos_parport
->shadowDCR
= (mos_parport
->shadowDCR
& (~mask
)) ^ val
;
546 write_mos_reg(mos_parport
->serial
, dummy
, DCR
, mos_parport
->shadowDCR
);
547 dcr
= mos_parport
->shadowDCR
& 0x0f;
548 parport_epilogue(pp
);
552 static unsigned char parport_mos7715_read_status(struct parport
*pp
)
554 unsigned char status
;
555 struct mos7715_parport
*mos_parport
= pp
->private_data
;
557 spin_lock(&release_lock
);
558 mos_parport
= pp
->private_data
;
559 if (unlikely(mos_parport
== NULL
)) { /* release called */
560 spin_unlock(&release_lock
);
563 status
= atomic_read(&mos_parport
->shadowDSR
) & 0xf8;
564 spin_unlock(&release_lock
);
568 static void parport_mos7715_enable_irq(struct parport
*pp
)
572 static void parport_mos7715_disable_irq(struct parport
*pp
)
576 static void parport_mos7715_data_forward(struct parport
*pp
)
578 struct mos7715_parport
*mos_parport
= pp
->private_data
;
580 if (parport_prologue(pp
) < 0)
582 mos7715_change_mode(mos_parport
, PS2
);
583 mos_parport
->shadowDCR
&= ~0x20;
584 write_mos_reg(mos_parport
->serial
, dummy
, DCR
, mos_parport
->shadowDCR
);
585 parport_epilogue(pp
);
588 static void parport_mos7715_data_reverse(struct parport
*pp
)
590 struct mos7715_parport
*mos_parport
= pp
->private_data
;
592 if (parport_prologue(pp
) < 0)
594 mos7715_change_mode(mos_parport
, PS2
);
595 mos_parport
->shadowDCR
|= 0x20;
596 write_mos_reg(mos_parport
->serial
, dummy
, DCR
, mos_parport
->shadowDCR
);
597 parport_epilogue(pp
);
600 static void parport_mos7715_init_state(struct pardevice
*dev
,
601 struct parport_state
*s
)
603 s
->u
.pc
.ctr
= DCR_INIT_VAL
;
604 s
->u
.pc
.ecr
= ECR_INIT_VAL
;
607 /* N.B. Parport core code requires that this function not block */
608 static void parport_mos7715_save_state(struct parport
*pp
,
609 struct parport_state
*s
)
611 struct mos7715_parport
*mos_parport
;
613 spin_lock(&release_lock
);
614 mos_parport
= pp
->private_data
;
615 if (unlikely(mos_parport
== NULL
)) { /* release called */
616 spin_unlock(&release_lock
);
619 s
->u
.pc
.ctr
= mos_parport
->shadowDCR
;
620 s
->u
.pc
.ecr
= mos_parport
->shadowECR
;
621 spin_unlock(&release_lock
);
624 /* N.B. Parport core code requires that this function not block */
625 static void parport_mos7715_restore_state(struct parport
*pp
,
626 struct parport_state
*s
)
628 struct mos7715_parport
*mos_parport
;
630 spin_lock(&release_lock
);
631 mos_parport
= pp
->private_data
;
632 if (unlikely(mos_parport
== NULL
)) { /* release called */
633 spin_unlock(&release_lock
);
636 write_parport_reg_nonblock(mos_parport
, DCR
, mos_parport
->shadowDCR
);
637 write_parport_reg_nonblock(mos_parport
, ECR
, mos_parport
->shadowECR
);
638 spin_unlock(&release_lock
);
641 static size_t parport_mos7715_write_compat(struct parport
*pp
,
643 size_t len
, int flags
)
646 struct mos7715_parport
*mos_parport
= pp
->private_data
;
649 if (parport_prologue(pp
) < 0)
651 mos7715_change_mode(mos_parport
, PPF
);
652 retval
= usb_bulk_msg(mos_parport
->serial
->dev
,
653 usb_sndbulkpipe(mos_parport
->serial
->dev
, 2),
654 (void *)buffer
, len
, &actual_len
,
656 parport_epilogue(pp
);
658 dev_err(&mos_parport
->serial
->dev
->dev
,
659 "mos7720: usb_bulk_msg() failed: %d\n", retval
);
665 static struct parport_operations parport_mos7715_ops
= {
666 .owner
= THIS_MODULE
,
667 .write_data
= parport_mos7715_write_data
,
668 .read_data
= parport_mos7715_read_data
,
670 .write_control
= parport_mos7715_write_control
,
671 .read_control
= parport_mos7715_read_control
,
672 .frob_control
= parport_mos7715_frob_control
,
674 .read_status
= parport_mos7715_read_status
,
676 .enable_irq
= parport_mos7715_enable_irq
,
677 .disable_irq
= parport_mos7715_disable_irq
,
679 .data_forward
= parport_mos7715_data_forward
,
680 .data_reverse
= parport_mos7715_data_reverse
,
682 .init_state
= parport_mos7715_init_state
,
683 .save_state
= parport_mos7715_save_state
,
684 .restore_state
= parport_mos7715_restore_state
,
686 .compat_write_data
= parport_mos7715_write_compat
,
688 .nibble_read_data
= parport_ieee1284_read_nibble
,
689 .byte_read_data
= parport_ieee1284_read_byte
,
693 * Allocate and initialize parallel port control struct, initialize
694 * the parallel port hardware device, and register with the parport subsystem.
696 static int mos7715_parport_init(struct usb_serial
*serial
)
698 struct mos7715_parport
*mos_parport
;
700 /* allocate and initialize parallel port control struct */
701 mos_parport
= kzalloc(sizeof(struct mos7715_parport
), GFP_KERNEL
);
705 mos_parport
->msg_pending
= false;
706 kref_init(&mos_parport
->ref_count
);
707 spin_lock_init(&mos_parport
->listlock
);
708 INIT_LIST_HEAD(&mos_parport
->active_urbs
);
709 INIT_LIST_HEAD(&mos_parport
->deferred_urbs
);
710 usb_set_serial_data(serial
, mos_parport
); /* hijack private pointer */
711 mos_parport
->serial
= serial
;
712 tasklet_init(&mos_parport
->urb_tasklet
, send_deferred_urbs
,
713 (unsigned long) mos_parport
);
714 init_completion(&mos_parport
->syncmsg_compl
);
716 /* cycle parallel port reset bit */
717 write_mos_reg(mos_parport
->serial
, dummy
, PP_REG
, (__u8
)0x80);
718 write_mos_reg(mos_parport
->serial
, dummy
, PP_REG
, (__u8
)0x00);
720 /* initialize device registers */
721 mos_parport
->shadowDCR
= DCR_INIT_VAL
;
722 write_mos_reg(mos_parport
->serial
, dummy
, DCR
, mos_parport
->shadowDCR
);
723 mos_parport
->shadowECR
= ECR_INIT_VAL
;
724 write_mos_reg(mos_parport
->serial
, dummy
, ECR
, mos_parport
->shadowECR
);
726 /* register with parport core */
727 mos_parport
->pp
= parport_register_port(0, PARPORT_IRQ_NONE
,
729 &parport_mos7715_ops
);
730 if (mos_parport
->pp
== NULL
) {
731 dev_err(&serial
->interface
->dev
,
732 "Could not register parport\n");
733 kref_put(&mos_parport
->ref_count
, destroy_mos_parport
);
736 mos_parport
->pp
->private_data
= mos_parport
;
737 mos_parport
->pp
->modes
= PARPORT_MODE_COMPAT
| PARPORT_MODE_PCSPP
;
738 mos_parport
->pp
->dev
= &serial
->interface
->dev
;
739 parport_announce_port(mos_parport
->pp
);
743 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
746 * mos7720_interrupt_callback
747 * this is the callback function for when we have received data on the
748 * interrupt endpoint.
750 static void mos7720_interrupt_callback(struct urb
*urb
)
754 int status
= urb
->status
;
755 struct device
*dev
= &urb
->dev
->dev
;
767 /* this urb is terminated, clean up */
768 dev_dbg(dev
, "%s - urb shutting down with status: %d\n", __func__
, status
);
771 dev_dbg(dev
, "%s - nonzero urb status received: %d\n", __func__
, status
);
775 length
= urb
->actual_length
;
776 data
= urb
->transfer_buffer
;
778 /* Moschip get 4 bytes
779 * Byte 1 IIR Port 1 (port.number is 0)
780 * Byte 2 IIR Port 2 (port.number is 1)
781 * Byte 3 --------------
782 * Byte 4 FIFO status for both */
784 /* the above description is inverted
785 * oneukum 2007-03-14 */
787 if (unlikely(length
!= 4)) {
788 dev_dbg(dev
, "Wrong data !!!\n");
795 if ((sp1
| sp2
) & 0x01) {
796 /* No Interrupt Pending in both the ports */
797 dev_dbg(dev
, "No Interrupt !!!\n");
799 switch (sp1
& 0x0f) {
801 dev_dbg(dev
, "Serial Port 1: Receiver status error or address bit detected in 9-bit mode\n");
804 dev_dbg(dev
, "Serial Port 1: Receiver time out\n");
807 /* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */
811 switch (sp2
& 0x0f) {
813 dev_dbg(dev
, "Serial Port 2: Receiver status error or address bit detected in 9-bit mode\n");
816 dev_dbg(dev
, "Serial Port 2: Receiver time out\n");
819 /* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */
825 result
= usb_submit_urb(urb
, GFP_ATOMIC
);
827 dev_err(dev
, "%s - Error %d submitting control urb\n", __func__
, result
);
831 * mos7715_interrupt_callback
832 * this is the 7715's callback function for when we have received data on
833 * the interrupt endpoint.
835 static void mos7715_interrupt_callback(struct urb
*urb
)
839 int status
= urb
->status
;
840 struct device
*dev
= &urb
->dev
->dev
;
852 /* this urb is terminated, clean up */
853 dev_dbg(dev
, "%s - urb shutting down with status: %d\n", __func__
, status
);
856 dev_dbg(dev
, "%s - nonzero urb status received: %d\n", __func__
, status
);
860 length
= urb
->actual_length
;
861 data
= urb
->transfer_buffer
;
863 /* Structure of data from 7715 device:
864 * Byte 1: IIR serial Port
866 * Byte 2: DSR parallel port
867 * Byte 4: FIFO status for both */
869 if (unlikely(length
!= 4)) {
870 dev_dbg(dev
, "Wrong data !!!\n");
875 if (!(iir
& 0x01)) { /* serial port interrupt pending */
876 switch (iir
& 0x0f) {
878 dev_dbg(dev
, "Serial Port: Receiver status error or address bit detected in 9-bit mode\n");
881 dev_dbg(dev
, "Serial Port: Receiver time out\n");
884 /* dev_dbg(dev, "Serial Port: Modem status change\n"); */
889 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
890 { /* update local copy of DSR reg */
891 struct usb_serial_port
*port
= urb
->context
;
892 struct mos7715_parport
*mos_parport
= port
->serial
->private;
893 if (unlikely(mos_parport
== NULL
))
895 atomic_set(&mos_parport
->shadowDSR
, data
[2]);
900 result
= usb_submit_urb(urb
, GFP_ATOMIC
);
902 dev_err(dev
, "%s - Error %d submitting control urb\n", __func__
, result
);
906 * mos7720_bulk_in_callback
907 * this is the callback function for when we have received data on the
910 static void mos7720_bulk_in_callback(struct urb
*urb
)
913 unsigned char *data
;
914 struct usb_serial_port
*port
;
915 int status
= urb
->status
;
918 dev_dbg(&urb
->dev
->dev
, "nonzero read bulk status received: %d\n", status
);
924 dev_dbg(&port
->dev
, "Entering...%s\n", __func__
);
926 data
= urb
->transfer_buffer
;
928 if (urb
->actual_length
) {
929 tty_insert_flip_string(&port
->port
, data
, urb
->actual_length
);
930 tty_flip_buffer_push(&port
->port
);
933 if (port
->read_urb
->status
!= -EINPROGRESS
) {
934 retval
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
936 dev_dbg(&port
->dev
, "usb_submit_urb(read bulk) failed, retval = %d\n", retval
);
941 * mos7720_bulk_out_data_callback
942 * this is the callback function for when we have finished sending serial
943 * data on the bulk out endpoint.
945 static void mos7720_bulk_out_data_callback(struct urb
*urb
)
947 struct moschip_port
*mos7720_port
;
948 int status
= urb
->status
;
951 dev_dbg(&urb
->dev
->dev
, "nonzero write bulk status received:%d\n", status
);
955 mos7720_port
= urb
->context
;
957 dev_dbg(&urb
->dev
->dev
, "NULL mos7720_port pointer\n");
961 if (mos7720_port
->open
)
962 tty_port_tty_wakeup(&mos7720_port
->port
->port
);
967 * this function installs the appropriate read interrupt endpoint callback
968 * depending on whether the device is a 7720 or 7715, thus avoiding costly
969 * run-time checks in the high-frequency callback routine itself.
971 static int mos77xx_probe(struct usb_serial
*serial
,
972 const struct usb_device_id
*id
)
974 if (id
->idProduct
== MOSCHIP_DEVICE_ID_7715
)
975 moschip7720_2port_driver
.read_int_callback
=
976 mos7715_interrupt_callback
;
978 moschip7720_2port_driver
.read_int_callback
=
979 mos7720_interrupt_callback
;
984 static int mos77xx_calc_num_ports(struct usb_serial
*serial
)
986 u16 product
= le16_to_cpu(serial
->dev
->descriptor
.idProduct
);
987 if (product
== MOSCHIP_DEVICE_ID_7715
)
993 static int mos7720_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
995 struct usb_serial
*serial
;
997 struct moschip_port
*mos7720_port
;
1001 int allocated_urbs
= 0;
1004 serial
= port
->serial
;
1006 mos7720_port
= usb_get_serial_port_data(port
);
1007 if (mos7720_port
== NULL
)
1010 usb_clear_halt(serial
->dev
, port
->write_urb
->pipe
);
1011 usb_clear_halt(serial
->dev
, port
->read_urb
->pipe
);
1013 /* Initialising the write urb pool */
1014 for (j
= 0; j
< NUM_URBS
; ++j
) {
1015 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1016 mos7720_port
->write_urb_pool
[j
] = urb
;
1020 urb
->transfer_buffer
= kmalloc(URB_TRANSFER_BUFFER_SIZE
,
1022 if (!urb
->transfer_buffer
) {
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
->port_number
;
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
->port_number
+ 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 port settings */
1082 mos7720_port
->shadowMCR
= UART_MCR_OUT2
; /* Must set to enable ints! */
1084 /* send a open port command */
1085 mos7720_port
->open
= 1;
1091 * mos7720_chars_in_buffer
1092 * this function is called by the tty driver when it wants to know how many
1093 * bytes of data we currently have outstanding in the port (data that has
1094 * been written, but hasn't made it out the port yet)
1095 * If successful, we return the number of bytes left to be written in the
1097 * Otherwise we return a negative error number.
1099 static int mos7720_chars_in_buffer(struct tty_struct
*tty
)
1101 struct usb_serial_port
*port
= tty
->driver_data
;
1104 struct moschip_port
*mos7720_port
;
1106 mos7720_port
= usb_get_serial_port_data(port
);
1107 if (mos7720_port
== NULL
)
1110 for (i
= 0; i
< NUM_URBS
; ++i
) {
1111 if (mos7720_port
->write_urb_pool
[i
] &&
1112 mos7720_port
->write_urb_pool
[i
]->status
== -EINPROGRESS
)
1113 chars
+= URB_TRANSFER_BUFFER_SIZE
;
1115 dev_dbg(&port
->dev
, "%s - returns %d\n", __func__
, chars
);
1119 static void mos7720_close(struct usb_serial_port
*port
)
1121 struct usb_serial
*serial
;
1122 struct moschip_port
*mos7720_port
;
1125 serial
= port
->serial
;
1127 mos7720_port
= usb_get_serial_port_data(port
);
1128 if (mos7720_port
== NULL
)
1131 for (j
= 0; j
< NUM_URBS
; ++j
)
1132 usb_kill_urb(mos7720_port
->write_urb_pool
[j
]);
1134 /* Freeing Write URBs */
1135 for (j
= 0; j
< NUM_URBS
; ++j
) {
1136 if (mos7720_port
->write_urb_pool
[j
]) {
1137 kfree(mos7720_port
->write_urb_pool
[j
]->transfer_buffer
);
1138 usb_free_urb(mos7720_port
->write_urb_pool
[j
]);
1142 /* While closing port, shutdown all bulk read, write *
1143 * and interrupt read if they exists, otherwise nop */
1144 usb_kill_urb(port
->write_urb
);
1145 usb_kill_urb(port
->read_urb
);
1147 write_mos_reg(serial
, port
->port_number
, MCR
, 0x00);
1148 write_mos_reg(serial
, port
->port_number
, IER
, 0x00);
1150 mos7720_port
->open
= 0;
1153 static void mos7720_break(struct tty_struct
*tty
, int break_state
)
1155 struct usb_serial_port
*port
= tty
->driver_data
;
1157 struct usb_serial
*serial
;
1158 struct moschip_port
*mos7720_port
;
1160 serial
= port
->serial
;
1162 mos7720_port
= usb_get_serial_port_data(port
);
1163 if (mos7720_port
== NULL
)
1166 if (break_state
== -1)
1167 data
= mos7720_port
->shadowLCR
| UART_LCR_SBC
;
1169 data
= mos7720_port
->shadowLCR
& ~UART_LCR_SBC
;
1171 mos7720_port
->shadowLCR
= data
;
1172 write_mos_reg(serial
, port
->port_number
, LCR
, mos7720_port
->shadowLCR
);
1176 * mos7720_write_room
1177 * this function is called by the tty driver when it wants to know how many
1178 * bytes of data we can accept for a specific port.
1179 * If successful, we return the amount of room that we have for this port
1180 * Otherwise we return a negative error number.
1182 static int mos7720_write_room(struct tty_struct
*tty
)
1184 struct usb_serial_port
*port
= tty
->driver_data
;
1185 struct moschip_port
*mos7720_port
;
1189 mos7720_port
= usb_get_serial_port_data(port
);
1190 if (mos7720_port
== NULL
)
1193 /* FIXME: Locking */
1194 for (i
= 0; i
< NUM_URBS
; ++i
) {
1195 if (mos7720_port
->write_urb_pool
[i
] &&
1196 mos7720_port
->write_urb_pool
[i
]->status
!= -EINPROGRESS
)
1197 room
+= URB_TRANSFER_BUFFER_SIZE
;
1200 dev_dbg(&port
->dev
, "%s - returns %d\n", __func__
, room
);
1204 static int mos7720_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
1205 const unsigned char *data
, int count
)
1212 struct moschip_port
*mos7720_port
;
1213 struct usb_serial
*serial
;
1215 const unsigned char *current_position
= data
;
1217 serial
= port
->serial
;
1219 mos7720_port
= usb_get_serial_port_data(port
);
1220 if (mos7720_port
== NULL
)
1223 /* try to find a free urb in the list */
1226 for (i
= 0; i
< NUM_URBS
; ++i
) {
1227 if (mos7720_port
->write_urb_pool
[i
] &&
1228 mos7720_port
->write_urb_pool
[i
]->status
!= -EINPROGRESS
) {
1229 urb
= mos7720_port
->write_urb_pool
[i
];
1230 dev_dbg(&port
->dev
, "URB:%d\n", i
);
1236 dev_dbg(&port
->dev
, "%s - no more free urbs\n", __func__
);
1240 if (urb
->transfer_buffer
== NULL
) {
1241 urb
->transfer_buffer
= kmalloc(URB_TRANSFER_BUFFER_SIZE
,
1243 if (!urb
->transfer_buffer
)
1246 transfer_size
= min(count
, URB_TRANSFER_BUFFER_SIZE
);
1248 memcpy(urb
->transfer_buffer
, current_position
, transfer_size
);
1249 usb_serial_debug_data(&port
->dev
, __func__
, transfer_size
,
1250 urb
->transfer_buffer
);
1252 /* fill urb with data and submit */
1253 usb_fill_bulk_urb(urb
, serial
->dev
,
1254 usb_sndbulkpipe(serial
->dev
,
1255 port
->bulk_out_endpointAddress
),
1256 urb
->transfer_buffer
, transfer_size
,
1257 mos7720_bulk_out_data_callback
, mos7720_port
);
1259 /* send it down the pipe */
1260 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
1262 dev_err_console(port
, "%s - usb_submit_urb(write bulk) failed "
1263 "with status = %d\n", __func__
, status
);
1264 bytes_sent
= status
;
1267 bytes_sent
= transfer_size
;
1273 static void mos7720_throttle(struct tty_struct
*tty
)
1275 struct usb_serial_port
*port
= tty
->driver_data
;
1276 struct moschip_port
*mos7720_port
;
1279 mos7720_port
= usb_get_serial_port_data(port
);
1281 if (mos7720_port
== NULL
)
1284 if (!mos7720_port
->open
) {
1285 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1289 /* if we are implementing XON/XOFF, send the stop character */
1291 unsigned char stop_char
= STOP_CHAR(tty
);
1292 status
= mos7720_write(tty
, port
, &stop_char
, 1);
1297 /* if we are implementing RTS/CTS, toggle that line */
1298 if (tty
->termios
.c_cflag
& CRTSCTS
) {
1299 mos7720_port
->shadowMCR
&= ~UART_MCR_RTS
;
1300 write_mos_reg(port
->serial
, port
->port_number
, MCR
,
1301 mos7720_port
->shadowMCR
);
1305 static void mos7720_unthrottle(struct tty_struct
*tty
)
1307 struct usb_serial_port
*port
= tty
->driver_data
;
1308 struct moschip_port
*mos7720_port
= usb_get_serial_port_data(port
);
1311 if (mos7720_port
== NULL
)
1314 if (!mos7720_port
->open
) {
1315 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1319 /* if we are implementing XON/XOFF, send the start character */
1321 unsigned char start_char
= START_CHAR(tty
);
1322 status
= mos7720_write(tty
, port
, &start_char
, 1);
1327 /* if we are implementing RTS/CTS, toggle that line */
1328 if (tty
->termios
.c_cflag
& CRTSCTS
) {
1329 mos7720_port
->shadowMCR
|= UART_MCR_RTS
;
1330 write_mos_reg(port
->serial
, port
->port_number
, MCR
,
1331 mos7720_port
->shadowMCR
);
1335 /* FIXME: this function does not work */
1336 static int set_higher_rates(struct moschip_port
*mos7720_port
,
1339 struct usb_serial_port
*port
;
1340 struct usb_serial
*serial
;
1342 enum mos_regs sp_reg
;
1343 if (mos7720_port
== NULL
)
1346 port
= mos7720_port
->port
;
1347 serial
= port
->serial
;
1349 /***********************************************
1350 * Init Sequence for higher rates
1351 ***********************************************/
1352 dev_dbg(&port
->dev
, "Sending Setting Commands ..........\n");
1353 port_number
= port
->port_number
;
1355 write_mos_reg(serial
, port_number
, IER
, 0x00);
1356 write_mos_reg(serial
, port_number
, FCR
, 0x00);
1357 write_mos_reg(serial
, port_number
, FCR
, 0xcf);
1358 mos7720_port
->shadowMCR
= 0x0b;
1359 write_mos_reg(serial
, port_number
, MCR
, mos7720_port
->shadowMCR
);
1360 write_mos_reg(serial
, dummy
, SP_CONTROL_REG
, 0x00);
1362 /***********************************************
1363 * Set for higher rates *
1364 ***********************************************/
1365 /* writing baud rate verbatum into uart clock field clearly not right */
1366 if (port_number
== 0)
1370 write_mos_reg(serial
, dummy
, sp_reg
, baud
* 0x10);
1371 write_mos_reg(serial
, dummy
, SP_CONTROL_REG
, 0x03);
1372 mos7720_port
->shadowMCR
= 0x2b;
1373 write_mos_reg(serial
, port_number
, MCR
, mos7720_port
->shadowMCR
);
1375 /***********************************************
1377 ***********************************************/
1378 mos7720_port
->shadowLCR
= mos7720_port
->shadowLCR
| UART_LCR_DLAB
;
1379 write_mos_reg(serial
, port_number
, LCR
, mos7720_port
->shadowLCR
);
1380 write_mos_reg(serial
, port_number
, DLL
, 0x01);
1381 write_mos_reg(serial
, port_number
, DLM
, 0x00);
1382 mos7720_port
->shadowLCR
= mos7720_port
->shadowLCR
& ~UART_LCR_DLAB
;
1383 write_mos_reg(serial
, port_number
, LCR
, mos7720_port
->shadowLCR
);
1388 /* baud rate information */
1389 struct divisor_table_entry
{
1394 /* Define table of divisors for moschip 7720 hardware *
1395 * These assume a 3.6864MHz crystal, the standard /16, and *
1397 static struct divisor_table_entry divisor_table
[] = {
1399 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
1400 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
1416 /*****************************************************************************
1417 * calc_baud_rate_divisor
1418 * this function calculates the proper baud rate divisor for the specified
1420 *****************************************************************************/
1421 static int calc_baud_rate_divisor(struct usb_serial_port
*port
, int baudrate
, int *divisor
)
1429 dev_dbg(&port
->dev
, "%s - %d\n", __func__
, baudrate
);
1431 for (i
= 0; i
< ARRAY_SIZE(divisor_table
); i
++) {
1432 if (divisor_table
[i
].baudrate
== baudrate
) {
1433 *divisor
= divisor_table
[i
].divisor
;
1438 /* After trying for all the standard baud rates *
1439 * Try calculating the divisor for this baud rate */
1440 if (baudrate
> 75 && baudrate
< 230400) {
1441 /* get the divisor */
1442 custom
= (__u16
)(230400L / baudrate
);
1444 /* Check for round off */
1445 round1
= (__u16
)(2304000L / baudrate
);
1446 round
= (__u16
)(round1
- (custom
* 10));
1451 dev_dbg(&port
->dev
, "Baud %d = %d\n", baudrate
, custom
);
1455 dev_dbg(&port
->dev
, "Baud calculation Failed...\n");
1460 * send_cmd_write_baud_rate
1461 * this function sends the proper command to change the baud rate of the
1464 static int send_cmd_write_baud_rate(struct moschip_port
*mos7720_port
,
1467 struct usb_serial_port
*port
;
1468 struct usb_serial
*serial
;
1471 unsigned char number
;
1473 if (mos7720_port
== NULL
)
1476 port
= mos7720_port
->port
;
1477 serial
= port
->serial
;
1479 number
= port
->port_number
;
1480 dev_dbg(&port
->dev
, "%s - baud = %d\n", __func__
, baudrate
);
1482 /* Calculate the Divisor */
1483 status
= calc_baud_rate_divisor(port
, baudrate
, &divisor
);
1485 dev_err(&port
->dev
, "%s - bad baud rate\n", __func__
);
1489 /* Enable access to divisor latch */
1490 mos7720_port
->shadowLCR
= mos7720_port
->shadowLCR
| UART_LCR_DLAB
;
1491 write_mos_reg(serial
, number
, LCR
, mos7720_port
->shadowLCR
);
1493 /* Write the divisor */
1494 write_mos_reg(serial
, number
, DLL
, (__u8
)(divisor
& 0xff));
1495 write_mos_reg(serial
, number
, DLM
, (__u8
)((divisor
& 0xff00) >> 8));
1497 /* Disable access to divisor latch */
1498 mos7720_port
->shadowLCR
= mos7720_port
->shadowLCR
& ~UART_LCR_DLAB
;
1499 write_mos_reg(serial
, number
, LCR
, mos7720_port
->shadowLCR
);
1505 * change_port_settings
1506 * This routine is called to set the UART on the device to match
1507 * the specified new settings.
1509 static void change_port_settings(struct tty_struct
*tty
,
1510 struct moschip_port
*mos7720_port
,
1511 struct ktermios
*old_termios
)
1513 struct usb_serial_port
*port
;
1514 struct usb_serial
*serial
;
1525 if (mos7720_port
== NULL
)
1528 port
= mos7720_port
->port
;
1529 serial
= port
->serial
;
1530 port_number
= port
->port_number
;
1532 if (!mos7720_port
->open
) {
1533 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1537 lData
= UART_LCR_WLEN8
;
1538 lStop
= 0x00; /* 1 stop bit */
1539 lParity
= 0x00; /* No parity */
1541 cflag
= tty
->termios
.c_cflag
;
1542 iflag
= tty
->termios
.c_iflag
;
1544 /* Change the number of bits */
1545 switch (cflag
& CSIZE
) {
1547 lData
= UART_LCR_WLEN5
;
1552 lData
= UART_LCR_WLEN6
;
1557 lData
= UART_LCR_WLEN7
;
1562 lData
= UART_LCR_WLEN8
;
1566 /* Change the Parity bit */
1567 if (cflag
& PARENB
) {
1568 if (cflag
& PARODD
) {
1569 lParity
= UART_LCR_PARITY
;
1570 dev_dbg(&port
->dev
, "%s - parity = odd\n", __func__
);
1572 lParity
= (UART_LCR_EPAR
| UART_LCR_PARITY
);
1573 dev_dbg(&port
->dev
, "%s - parity = even\n", __func__
);
1577 dev_dbg(&port
->dev
, "%s - parity = none\n", __func__
);
1581 lParity
= lParity
| 0x20;
1583 /* Change the Stop bit */
1584 if (cflag
& CSTOPB
) {
1585 lStop
= UART_LCR_STOP
;
1586 dev_dbg(&port
->dev
, "%s - stop bits = 2\n", __func__
);
1589 dev_dbg(&port
->dev
, "%s - stop bits = 1\n", __func__
);
1592 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1593 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1594 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
1596 /* Update the LCR with the correct value */
1597 mos7720_port
->shadowLCR
&=
1598 ~(LCR_BITS_MASK
| LCR_STOP_MASK
| LCR_PAR_MASK
);
1599 mos7720_port
->shadowLCR
|= (lData
| lParity
| lStop
);
1602 /* Disable Interrupts */
1603 write_mos_reg(serial
, port_number
, IER
, 0x00);
1604 write_mos_reg(serial
, port_number
, FCR
, 0x00);
1605 write_mos_reg(serial
, port_number
, FCR
, 0xcf);
1607 /* Send the updated LCR value to the mos7720 */
1608 write_mos_reg(serial
, port_number
, LCR
, mos7720_port
->shadowLCR
);
1609 mos7720_port
->shadowMCR
= 0x0b;
1610 write_mos_reg(serial
, port_number
, MCR
, mos7720_port
->shadowMCR
);
1612 /* set up the MCR register and send it to the mos7720 */
1613 mos7720_port
->shadowMCR
= UART_MCR_OUT2
;
1615 mos7720_port
->shadowMCR
|= (UART_MCR_DTR
| UART_MCR_RTS
);
1617 if (cflag
& CRTSCTS
) {
1618 mos7720_port
->shadowMCR
|= (UART_MCR_XONANY
);
1619 /* To set hardware flow control to the specified *
1620 * serial port, in SP1/2_CONTROL_REG */
1622 write_mos_reg(serial
, dummy
, SP_CONTROL_REG
, 0x01);
1624 write_mos_reg(serial
, dummy
, SP_CONTROL_REG
, 0x02);
1627 mos7720_port
->shadowMCR
&= ~(UART_MCR_XONANY
);
1629 write_mos_reg(serial
, port_number
, MCR
, mos7720_port
->shadowMCR
);
1631 /* Determine divisor based on baud rate */
1632 baud
= tty_get_baud_rate(tty
);
1634 /* pick a default, any default... */
1635 dev_dbg(&port
->dev
, "Picked default baud...\n");
1639 if (baud
>= 230400) {
1640 set_higher_rates(mos7720_port
, baud
);
1641 /* Enable Interrupts */
1642 write_mos_reg(serial
, port_number
, IER
, 0x0c);
1646 dev_dbg(&port
->dev
, "%s - baud rate = %d\n", __func__
, baud
);
1647 status
= send_cmd_write_baud_rate(mos7720_port
, baud
);
1648 /* FIXME: needs to write actual resulting baud back not just
1651 tty_encode_baud_rate(tty
, baud
, baud
);
1652 /* Enable Interrupts */
1653 write_mos_reg(serial
, port_number
, IER
, 0x0c);
1655 if (port
->read_urb
->status
!= -EINPROGRESS
) {
1656 status
= usb_submit_urb(port
->read_urb
, GFP_KERNEL
);
1658 dev_dbg(&port
->dev
, "usb_submit_urb(read bulk) failed, status = %d\n", status
);
1663 * mos7720_set_termios
1664 * this function is called by the tty driver when it wants to change the
1665 * termios structure.
1667 static void mos7720_set_termios(struct tty_struct
*tty
,
1668 struct usb_serial_port
*port
, struct ktermios
*old_termios
)
1672 struct usb_serial
*serial
;
1673 struct moschip_port
*mos7720_port
;
1675 serial
= port
->serial
;
1677 mos7720_port
= usb_get_serial_port_data(port
);
1679 if (mos7720_port
== NULL
)
1682 if (!mos7720_port
->open
) {
1683 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1687 dev_dbg(&port
->dev
, "setting termios - ASPIRE\n");
1689 cflag
= tty
->termios
.c_cflag
;
1691 dev_dbg(&port
->dev
, "%s - cflag %08x iflag %08x\n", __func__
,
1692 tty
->termios
.c_cflag
, RELEVANT_IFLAG(tty
->termios
.c_iflag
));
1694 dev_dbg(&port
->dev
, "%s - old cflag %08x old iflag %08x\n", __func__
,
1695 old_termios
->c_cflag
, RELEVANT_IFLAG(old_termios
->c_iflag
));
1697 /* change the port settings to the new ones specified */
1698 change_port_settings(tty
, mos7720_port
, old_termios
);
1700 if (port
->read_urb
->status
!= -EINPROGRESS
) {
1701 status
= usb_submit_urb(port
->read_urb
, GFP_KERNEL
);
1703 dev_dbg(&port
->dev
, "usb_submit_urb(read bulk) failed, status = %d\n", status
);
1708 * get_lsr_info - get line status register info
1710 * Purpose: Let user call ioctl() to get info when the UART physically
1711 * is emptied. On bus types like RS485, the transmitter must
1712 * release the bus after transmitting. This must be done when
1713 * the transmit shift register is empty, not be done when the
1714 * transmit holding register is empty. This functionality
1715 * allows an RS485 driver to be written in user space.
1717 static int get_lsr_info(struct tty_struct
*tty
,
1718 struct moschip_port
*mos7720_port
, unsigned int __user
*value
)
1720 struct usb_serial_port
*port
= tty
->driver_data
;
1721 unsigned int result
= 0;
1722 unsigned char data
= 0;
1723 int port_number
= port
->port_number
;
1726 count
= mos7720_chars_in_buffer(tty
);
1728 read_mos_reg(port
->serial
, port_number
, LSR
, &data
);
1729 if ((data
& (UART_LSR_TEMT
| UART_LSR_THRE
))
1730 == (UART_LSR_TEMT
| UART_LSR_THRE
)) {
1731 dev_dbg(&port
->dev
, "%s -- Empty\n", __func__
);
1732 result
= TIOCSER_TEMT
;
1735 if (copy_to_user(value
, &result
, sizeof(int)))
1740 static int mos7720_tiocmget(struct tty_struct
*tty
)
1742 struct usb_serial_port
*port
= tty
->driver_data
;
1743 struct moschip_port
*mos7720_port
= usb_get_serial_port_data(port
);
1744 unsigned int result
= 0;
1748 mcr
= mos7720_port
->shadowMCR
;
1749 msr
= mos7720_port
->shadowMSR
;
1751 result
= ((mcr
& UART_MCR_DTR
) ? TIOCM_DTR
: 0) /* 0x002 */
1752 | ((mcr
& UART_MCR_RTS
) ? TIOCM_RTS
: 0) /* 0x004 */
1753 | ((msr
& UART_MSR_CTS
) ? TIOCM_CTS
: 0) /* 0x020 */
1754 | ((msr
& UART_MSR_DCD
) ? TIOCM_CAR
: 0) /* 0x040 */
1755 | ((msr
& UART_MSR_RI
) ? TIOCM_RI
: 0) /* 0x080 */
1756 | ((msr
& UART_MSR_DSR
) ? TIOCM_DSR
: 0); /* 0x100 */
1761 static int mos7720_tiocmset(struct tty_struct
*tty
,
1762 unsigned int set
, unsigned int clear
)
1764 struct usb_serial_port
*port
= tty
->driver_data
;
1765 struct moschip_port
*mos7720_port
= usb_get_serial_port_data(port
);
1768 mcr
= mos7720_port
->shadowMCR
;
1770 if (set
& TIOCM_RTS
)
1771 mcr
|= UART_MCR_RTS
;
1772 if (set
& TIOCM_DTR
)
1773 mcr
|= UART_MCR_DTR
;
1774 if (set
& TIOCM_LOOP
)
1775 mcr
|= UART_MCR_LOOP
;
1777 if (clear
& TIOCM_RTS
)
1778 mcr
&= ~UART_MCR_RTS
;
1779 if (clear
& TIOCM_DTR
)
1780 mcr
&= ~UART_MCR_DTR
;
1781 if (clear
& TIOCM_LOOP
)
1782 mcr
&= ~UART_MCR_LOOP
;
1784 mos7720_port
->shadowMCR
= mcr
;
1785 write_mos_reg(port
->serial
, port
->port_number
, MCR
,
1786 mos7720_port
->shadowMCR
);
1791 static int set_modem_info(struct moschip_port
*mos7720_port
, unsigned int cmd
,
1792 unsigned int __user
*value
)
1797 struct usb_serial_port
*port
;
1799 if (mos7720_port
== NULL
)
1802 port
= (struct usb_serial_port
*)mos7720_port
->port
;
1803 mcr
= mos7720_port
->shadowMCR
;
1805 if (copy_from_user(&arg
, value
, sizeof(int)))
1810 if (arg
& TIOCM_RTS
)
1811 mcr
|= UART_MCR_RTS
;
1812 if (arg
& TIOCM_DTR
)
1813 mcr
|= UART_MCR_RTS
;
1814 if (arg
& TIOCM_LOOP
)
1815 mcr
|= UART_MCR_LOOP
;
1819 if (arg
& TIOCM_RTS
)
1820 mcr
&= ~UART_MCR_RTS
;
1821 if (arg
& TIOCM_DTR
)
1822 mcr
&= ~UART_MCR_RTS
;
1823 if (arg
& TIOCM_LOOP
)
1824 mcr
&= ~UART_MCR_LOOP
;
1829 mos7720_port
->shadowMCR
= mcr
;
1830 write_mos_reg(port
->serial
, port
->port_number
, MCR
,
1831 mos7720_port
->shadowMCR
);
1836 static int get_serial_info(struct moschip_port
*mos7720_port
,
1837 struct serial_struct __user
*retinfo
)
1839 struct serial_struct tmp
;
1844 memset(&tmp
, 0, sizeof(tmp
));
1846 tmp
.type
= PORT_16550A
;
1847 tmp
.line
= mos7720_port
->port
->minor
;
1848 tmp
.port
= mos7720_port
->port
->port_number
;
1850 tmp
.flags
= ASYNC_SKIP_TEST
| ASYNC_AUTO_IRQ
;
1851 tmp
.xmit_fifo_size
= NUM_URBS
* URB_TRANSFER_BUFFER_SIZE
;
1852 tmp
.baud_base
= 9600;
1853 tmp
.close_delay
= 5*HZ
;
1854 tmp
.closing_wait
= 30*HZ
;
1856 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
1861 static int mos7720_ioctl(struct tty_struct
*tty
,
1862 unsigned int cmd
, unsigned long arg
)
1864 struct usb_serial_port
*port
= tty
->driver_data
;
1865 struct moschip_port
*mos7720_port
;
1867 mos7720_port
= usb_get_serial_port_data(port
);
1868 if (mos7720_port
== NULL
)
1873 dev_dbg(&port
->dev
, "%s TIOCSERGETLSR\n", __func__
);
1874 return get_lsr_info(tty
, mos7720_port
,
1875 (unsigned int __user
*)arg
);
1877 /* FIXME: These should be using the mode methods */
1880 dev_dbg(&port
->dev
, "%s TIOCMSET/TIOCMBIC/TIOCMSET\n", __func__
);
1881 return set_modem_info(mos7720_port
, cmd
,
1882 (unsigned int __user
*)arg
);
1885 dev_dbg(&port
->dev
, "%s TIOCGSERIAL\n", __func__
);
1886 return get_serial_info(mos7720_port
,
1887 (struct serial_struct __user
*)arg
);
1890 return -ENOIOCTLCMD
;
1893 static int mos7720_startup(struct usb_serial
*serial
)
1895 struct usb_device
*dev
;
1900 product
= le16_to_cpu(serial
->dev
->descriptor
.idProduct
);
1904 * The 7715 uses the first bulk in/out endpoint pair for the parallel
1905 * port, and the second for the serial port. Because the usbserial core
1906 * assumes both pairs are serial ports, we must engage in a bit of
1907 * subterfuge and swap the pointers for ports 0 and 1 in order to make
1908 * port 0 point to the serial port. However, both moschip devices use a
1909 * single interrupt-in endpoint for both ports (as mentioned a little
1910 * further down), and this endpoint was assigned to port 0. So after
1911 * the swap, we must copy the interrupt endpoint elements from port 1
1912 * (as newly assigned) to port 0, and null out port 1 pointers.
1914 if (product
== MOSCHIP_DEVICE_ID_7715
) {
1915 struct usb_serial_port
*tmp
= serial
->port
[0];
1916 serial
->port
[0] = serial
->port
[1];
1917 serial
->port
[1] = tmp
;
1918 serial
->port
[0]->interrupt_in_urb
= tmp
->interrupt_in_urb
;
1919 serial
->port
[0]->interrupt_in_buffer
= tmp
->interrupt_in_buffer
;
1920 serial
->port
[0]->interrupt_in_endpointAddress
=
1921 tmp
->interrupt_in_endpointAddress
;
1922 serial
->port
[1]->interrupt_in_urb
= NULL
;
1923 serial
->port
[1]->interrupt_in_buffer
= NULL
;
1926 /* setting configuration feature to one */
1927 usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
1928 (__u8
)0x03, 0x00, 0x01, 0x00, NULL
, 0x00, 5000);
1930 /* start the interrupt urb */
1931 ret_val
= usb_submit_urb(serial
->port
[0]->interrupt_in_urb
, GFP_KERNEL
);
1934 "%s - Error %d submitting control urb\n",
1937 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1938 if (product
== MOSCHIP_DEVICE_ID_7715
) {
1939 ret_val
= mos7715_parport_init(serial
);
1944 /* LSR For Port 1 */
1945 read_mos_reg(serial
, 0, LSR
, &data
);
1946 dev_dbg(&dev
->dev
, "LSR:%x\n", data
);
1951 static void mos7720_release(struct usb_serial
*serial
)
1953 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1954 /* close the parallel port */
1956 if (le16_to_cpu(serial
->dev
->descriptor
.idProduct
)
1957 == MOSCHIP_DEVICE_ID_7715
) {
1958 struct urbtracker
*urbtrack
;
1959 unsigned long flags
;
1960 struct mos7715_parport
*mos_parport
=
1961 usb_get_serial_data(serial
);
1963 /* prevent NULL ptr dereference in port callbacks */
1964 spin_lock(&release_lock
);
1965 mos_parport
->pp
->private_data
= NULL
;
1966 spin_unlock(&release_lock
);
1968 /* wait for synchronous usb calls to return */
1969 if (mos_parport
->msg_pending
)
1970 wait_for_completion_timeout(&mos_parport
->syncmsg_compl
,
1971 msecs_to_jiffies(MOS_WDR_TIMEOUT
));
1973 parport_remove_port(mos_parport
->pp
);
1974 usb_set_serial_data(serial
, NULL
);
1975 mos_parport
->serial
= NULL
;
1977 /* if tasklet currently scheduled, wait for it to complete */
1978 tasklet_kill(&mos_parport
->urb_tasklet
);
1980 /* unlink any urbs sent by the tasklet */
1981 spin_lock_irqsave(&mos_parport
->listlock
, flags
);
1982 list_for_each_entry(urbtrack
,
1983 &mos_parport
->active_urbs
,
1985 usb_unlink_urb(urbtrack
->urb
);
1986 spin_unlock_irqrestore(&mos_parport
->listlock
, flags
);
1988 kref_put(&mos_parport
->ref_count
, destroy_mos_parport
);
1993 static int mos7720_port_probe(struct usb_serial_port
*port
)
1995 struct moschip_port
*mos7720_port
;
1997 mos7720_port
= kzalloc(sizeof(*mos7720_port
), GFP_KERNEL
);
2001 /* Initialize all port interrupt end point to port 0 int endpoint.
2002 * Our device has only one interrupt endpoint common to all ports.
2004 port
->interrupt_in_endpointAddress
=
2005 port
->serial
->port
[0]->interrupt_in_endpointAddress
;
2006 mos7720_port
->port
= port
;
2008 usb_set_serial_port_data(port
, mos7720_port
);
2013 static int mos7720_port_remove(struct usb_serial_port
*port
)
2015 struct moschip_port
*mos7720_port
;
2017 mos7720_port
= usb_get_serial_port_data(port
);
2018 kfree(mos7720_port
);
2023 static struct usb_serial_driver moschip7720_2port_driver
= {
2025 .owner
= THIS_MODULE
,
2026 .name
= "moschip7720",
2028 .description
= "Moschip 2 port adapter",
2029 .id_table
= id_table
,
2030 .calc_num_ports
= mos77xx_calc_num_ports
,
2031 .open
= mos7720_open
,
2032 .close
= mos7720_close
,
2033 .throttle
= mos7720_throttle
,
2034 .unthrottle
= mos7720_unthrottle
,
2035 .probe
= mos77xx_probe
,
2036 .attach
= mos7720_startup
,
2037 .release
= mos7720_release
,
2038 .port_probe
= mos7720_port_probe
,
2039 .port_remove
= mos7720_port_remove
,
2040 .ioctl
= mos7720_ioctl
,
2041 .tiocmget
= mos7720_tiocmget
,
2042 .tiocmset
= mos7720_tiocmset
,
2043 .set_termios
= mos7720_set_termios
,
2044 .write
= mos7720_write
,
2045 .write_room
= mos7720_write_room
,
2046 .chars_in_buffer
= mos7720_chars_in_buffer
,
2047 .break_ctl
= mos7720_break
,
2048 .read_bulk_callback
= mos7720_bulk_in_callback
,
2049 .read_int_callback
= NULL
/* dynamically assigned in probe() */
2052 static struct usb_serial_driver
* const serial_drivers
[] = {
2053 &moschip7720_2port_driver
, NULL
2056 module_usb_serial_driver(serial_drivers
, id_table
);
2058 MODULE_AUTHOR(DRIVER_AUTHOR
);
2059 MODULE_DESCRIPTION(DRIVER_DESC
);
2060 MODULE_LICENSE("GPL");