1 // SPDX-License-Identifier: GPL-2.0
4 * Controls the Moschip 7720 usb to dual port serial converter
6 * Copyright 2006 Moschip Semiconductor Tech. Ltd.
9 * Vijaya Kumar <vijaykumar.gn@gmail.com>
10 * Ajay Kumar <naanuajay@yahoo.com>
11 * Gurudeva <ngurudeva@yahoo.com>
13 * Cleaned up from the original by:
14 * Greg Kroah-Hartman <gregkh@suse.de>
16 * Originally based on drivers/usb/serial/io_edgeport.c which is:
17 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
18 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/slab.h>
23 #include <linux/tty.h>
24 #include <linux/tty_driver.h>
25 #include <linux/tty_flip.h>
26 #include <linux/module.h>
27 #include <linux/spinlock.h>
28 #include <linux/serial.h>
29 #include <linux/serial_reg.h>
30 #include <linux/usb.h>
31 #include <linux/usb/serial.h>
32 #include <linux/uaccess.h>
33 #include <linux/parport.h>
35 #define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
36 #define DRIVER_DESC "Moschip USB Serial Driver"
38 /* default urb timeout */
39 #define MOS_WDR_TIMEOUT 5000
41 #define MOS_MAX_PORT 0x02
42 #define MOS_WRITE 0x0E
45 /* Interrupt Routines Defines */
46 #define SERIAL_IIR_RLS 0x06
47 #define SERIAL_IIR_RDA 0x04
48 #define SERIAL_IIR_CTI 0x0c
49 #define SERIAL_IIR_THR 0x02
50 #define SERIAL_IIR_MS 0x00
52 #define NUM_URBS 16 /* URB Count */
53 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
55 /* This structure holds all of the local serial port information */
57 __u8 shadowLCR
; /* last LCR value received */
58 __u8 shadowMCR
; /* last MCR value received */
59 __u8 shadowMSR
; /* last MSR value received */
61 struct usb_serial_port
*port
; /* loop back to the owner */
62 struct urb
*write_urb_pool
[NUM_URBS
];
65 #define USB_VENDOR_ID_MOSCHIP 0x9710
66 #define MOSCHIP_DEVICE_ID_7720 0x7720
67 #define MOSCHIP_DEVICE_ID_7715 0x7715
69 static const struct usb_device_id id_table
[] = {
70 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7720
) },
71 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7715
) },
72 { } /* terminating entry */
74 MODULE_DEVICE_TABLE(usb
, id_table
);
76 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
78 /* initial values for parport regs */
79 #define DCR_INIT_VAL 0x0c /* SLCTIN, nINIT */
80 #define ECR_INIT_VAL 0x00 /* SPP mode */
82 enum mos7715_pp_modes
{
84 PS2
= 1<<5, /* moschip calls this 'NIBBLE' mode */
85 PPF
= 2<<5, /* moschip calls this 'CB-FIFO mode */
88 struct mos7715_parport
{
89 struct parport
*pp
; /* back to containing struct */
90 struct kref ref_count
; /* to instance of this struct */
91 bool msg_pending
; /* usb sync call pending */
92 struct completion syncmsg_compl
; /* usb sync call completed */
93 struct work_struct work
; /* restore deferred writes */
94 struct usb_serial
*serial
; /* back to containing struct */
95 __u8 shadowECR
; /* parallel port regs... */
97 atomic_t shadowDSR
; /* updated in int-in callback */
100 /* lock guards against dereferencing NULL ptr in parport ops callbacks */
101 static DEFINE_SPINLOCK(release_lock
);
103 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
105 static const unsigned int dummy
; /* for clarity in register access fns */
108 MOS7720_THR
, /* serial port regs */
120 MOS7720_DPR
, /* parallel port regs */
124 MOS7720_SP1_REG
, /* device control regs */
125 MOS7720_SP2_REG
, /* serial port 2 (7720 only) */
127 MOS7720_SP_CONTROL_REG
,
131 * Return the correct value for the Windex field of the setup packet
132 * for a control endpoint message. See the 7715 datasheet.
134 static inline __u16
get_reg_index(enum mos_regs reg
)
136 static const __u16 mos7715_index_lookup_table
[] = {
137 0x00, /* MOS7720_THR */
138 0x00, /* MOS7720_RHR */
139 0x01, /* MOS7720_IER */
140 0x02, /* MOS7720_FCR */
141 0x02, /* MOS7720_ISR */
142 0x03, /* MOS7720_LCR */
143 0x04, /* MOS7720_MCR */
144 0x05, /* MOS7720_LSR */
145 0x06, /* MOS7720_MSR */
146 0x07, /* MOS7720_SPR */
147 0x00, /* MOS7720_DLL */
148 0x01, /* MOS7720_DLM */
149 0x00, /* MOS7720_DPR */
150 0x01, /* MOS7720_DSR */
151 0x02, /* MOS7720_DCR */
152 0x0a, /* MOS7720_ECR */
153 0x01, /* MOS7720_SP1_REG */
154 0x02, /* MOS7720_SP2_REG (7720 only) */
155 0x04, /* MOS7720_PP_REG (7715 only) */
156 0x08, /* MOS7720_SP_CONTROL_REG */
158 return mos7715_index_lookup_table
[reg
];
162 * Return the correct value for the upper byte of the Wvalue field of
163 * the setup packet for a control endpoint message.
165 static inline __u16
get_reg_value(enum mos_regs reg
,
166 unsigned int serial_portnum
)
168 if (reg
>= MOS7720_SP1_REG
) /* control reg */
171 else if (reg
>= MOS7720_DPR
) /* parallel port reg (7715 only) */
174 else /* serial port reg */
175 return (serial_portnum
+ 2) << 8;
179 * Write data byte to the specified device register. The data is embedded in
180 * the value field of the setup packet. serial_portnum is ignored for registers
181 * not specific to a particular serial port.
183 static int write_mos_reg(struct usb_serial
*serial
, unsigned int serial_portnum
,
184 enum mos_regs reg
, __u8 data
)
186 struct usb_device
*usbdev
= serial
->dev
;
187 unsigned int pipe
= usb_sndctrlpipe(usbdev
, 0);
188 __u8 request
= (__u8
)0x0e;
189 __u8 requesttype
= (__u8
)0x40;
190 __u16 index
= get_reg_index(reg
);
191 __u16 value
= get_reg_value(reg
, serial_portnum
) + data
;
192 int status
= usb_control_msg(usbdev
, pipe
, request
, requesttype
, value
,
193 index
, NULL
, 0, MOS_WDR_TIMEOUT
);
195 dev_err(&usbdev
->dev
,
196 "mos7720: usb_control_msg() failed: %d\n", status
);
201 * Read data byte from the specified device register. The data returned by the
202 * device is embedded in the value field of the setup packet. serial_portnum is
203 * ignored for registers that are not specific to a particular serial port.
205 static int read_mos_reg(struct usb_serial
*serial
, unsigned int serial_portnum
,
206 enum mos_regs reg
, __u8
*data
)
208 struct usb_device
*usbdev
= serial
->dev
;
209 unsigned int pipe
= usb_rcvctrlpipe(usbdev
, 0);
210 __u8 request
= (__u8
)0x0d;
211 __u8 requesttype
= (__u8
)0xc0;
212 __u16 index
= get_reg_index(reg
);
213 __u16 value
= get_reg_value(reg
, serial_portnum
);
217 buf
= kmalloc(1, GFP_KERNEL
);
221 status
= usb_control_msg(usbdev
, pipe
, request
, requesttype
, value
,
222 index
, buf
, 1, MOS_WDR_TIMEOUT
);
226 dev_err(&usbdev
->dev
,
227 "mos7720: usb_control_msg() failed: %d\n", status
);
238 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
240 static inline int mos7715_change_mode(struct mos7715_parport
*mos_parport
,
241 enum mos7715_pp_modes mode
)
243 mos_parport
->shadowECR
= mode
;
244 write_mos_reg(mos_parport
->serial
, dummy
, MOS7720_ECR
,
245 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
);
258 * This is the common top part of all parallel port callback operations that
259 * send synchronous messages to the device. This implements convoluted locking
260 * that avoids two scenarios: (1) a port operation is called after usbserial
261 * has called our release function, at which point struct mos7715_parport has
262 * been destroyed, and (2) the device has been disconnected, but usbserial has
263 * not called the release function yet because someone has a serial port open.
264 * The shared release_lock prevents the first, and the mutex and disconnected
265 * flag maintained by usbserial covers the second. We also use the msg_pending
266 * flag to ensure that all synchronous usb message calls have completed before
267 * our release function can return.
269 static int parport_prologue(struct parport
*pp
)
271 struct mos7715_parport
*mos_parport
;
273 spin_lock(&release_lock
);
274 mos_parport
= pp
->private_data
;
275 if (unlikely(mos_parport
== NULL
)) {
276 /* release fn called, port struct destroyed */
277 spin_unlock(&release_lock
);
280 mos_parport
->msg_pending
= true; /* synch usb call pending */
281 reinit_completion(&mos_parport
->syncmsg_compl
);
282 spin_unlock(&release_lock
);
284 /* ensure writes from restore are submitted before new requests */
285 if (work_pending(&mos_parport
->work
))
286 flush_work(&mos_parport
->work
);
288 mutex_lock(&mos_parport
->serial
->disc_mutex
);
289 if (mos_parport
->serial
->disconnected
) {
290 /* device disconnected */
291 mutex_unlock(&mos_parport
->serial
->disc_mutex
);
292 mos_parport
->msg_pending
= false;
293 complete(&mos_parport
->syncmsg_compl
);
301 * This is the common bottom part of all parallel port functions that send
302 * synchronous messages to the device.
304 static inline void parport_epilogue(struct parport
*pp
)
306 struct mos7715_parport
*mos_parport
= pp
->private_data
;
307 mutex_unlock(&mos_parport
->serial
->disc_mutex
);
308 mos_parport
->msg_pending
= false;
309 complete(&mos_parport
->syncmsg_compl
);
312 static void deferred_restore_writes(struct work_struct
*work
)
314 struct mos7715_parport
*mos_parport
;
316 mos_parport
= container_of(work
, struct mos7715_parport
, work
);
318 mutex_lock(&mos_parport
->serial
->disc_mutex
);
320 /* if device disconnected, game over */
321 if (mos_parport
->serial
->disconnected
)
324 write_mos_reg(mos_parport
->serial
, dummy
, MOS7720_DCR
,
325 mos_parport
->shadowDCR
);
326 write_mos_reg(mos_parport
->serial
, dummy
, MOS7720_ECR
,
327 mos_parport
->shadowECR
);
329 mutex_unlock(&mos_parport
->serial
->disc_mutex
);
332 static void parport_mos7715_write_data(struct parport
*pp
, unsigned char d
)
334 struct mos7715_parport
*mos_parport
= pp
->private_data
;
336 if (parport_prologue(pp
) < 0)
338 mos7715_change_mode(mos_parport
, SPP
);
339 write_mos_reg(mos_parport
->serial
, dummy
, MOS7720_DPR
, (__u8
)d
);
340 parport_epilogue(pp
);
343 static unsigned char parport_mos7715_read_data(struct parport
*pp
)
345 struct mos7715_parport
*mos_parport
= pp
->private_data
;
348 if (parport_prologue(pp
) < 0)
350 read_mos_reg(mos_parport
->serial
, dummy
, MOS7720_DPR
, &d
);
351 parport_epilogue(pp
);
355 static void parport_mos7715_write_control(struct parport
*pp
, unsigned char d
)
357 struct mos7715_parport
*mos_parport
= pp
->private_data
;
360 if (parport_prologue(pp
) < 0)
362 data
= ((__u8
)d
& 0x0f) | (mos_parport
->shadowDCR
& 0xf0);
363 write_mos_reg(mos_parport
->serial
, dummy
, MOS7720_DCR
, data
);
364 mos_parport
->shadowDCR
= data
;
365 parport_epilogue(pp
);
368 static unsigned char parport_mos7715_read_control(struct parport
*pp
)
370 struct mos7715_parport
*mos_parport
;
373 spin_lock(&release_lock
);
374 mos_parport
= pp
->private_data
;
375 if (unlikely(mos_parport
== NULL
)) {
376 spin_unlock(&release_lock
);
379 dcr
= mos_parport
->shadowDCR
& 0x0f;
380 spin_unlock(&release_lock
);
384 static unsigned char parport_mos7715_frob_control(struct parport
*pp
,
388 struct mos7715_parport
*mos_parport
= pp
->private_data
;
393 if (parport_prologue(pp
) < 0)
395 mos_parport
->shadowDCR
= (mos_parport
->shadowDCR
& (~mask
)) ^ val
;
396 write_mos_reg(mos_parport
->serial
, dummy
, MOS7720_DCR
,
397 mos_parport
->shadowDCR
);
398 dcr
= mos_parport
->shadowDCR
& 0x0f;
399 parport_epilogue(pp
);
403 static unsigned char parport_mos7715_read_status(struct parport
*pp
)
405 unsigned char status
;
406 struct mos7715_parport
*mos_parport
;
408 spin_lock(&release_lock
);
409 mos_parport
= pp
->private_data
;
410 if (unlikely(mos_parport
== NULL
)) { /* release called */
411 spin_unlock(&release_lock
);
414 status
= atomic_read(&mos_parport
->shadowDSR
) & 0xf8;
415 spin_unlock(&release_lock
);
419 static void parport_mos7715_enable_irq(struct parport
*pp
)
423 static void parport_mos7715_disable_irq(struct parport
*pp
)
427 static void parport_mos7715_data_forward(struct parport
*pp
)
429 struct mos7715_parport
*mos_parport
= pp
->private_data
;
431 if (parport_prologue(pp
) < 0)
433 mos7715_change_mode(mos_parport
, PS2
);
434 mos_parport
->shadowDCR
&= ~0x20;
435 write_mos_reg(mos_parport
->serial
, dummy
, MOS7720_DCR
,
436 mos_parport
->shadowDCR
);
437 parport_epilogue(pp
);
440 static void parport_mos7715_data_reverse(struct parport
*pp
)
442 struct mos7715_parport
*mos_parport
= pp
->private_data
;
444 if (parport_prologue(pp
) < 0)
446 mos7715_change_mode(mos_parport
, PS2
);
447 mos_parport
->shadowDCR
|= 0x20;
448 write_mos_reg(mos_parport
->serial
, dummy
, MOS7720_DCR
,
449 mos_parport
->shadowDCR
);
450 parport_epilogue(pp
);
453 static void parport_mos7715_init_state(struct pardevice
*dev
,
454 struct parport_state
*s
)
456 s
->u
.pc
.ctr
= DCR_INIT_VAL
;
457 s
->u
.pc
.ecr
= ECR_INIT_VAL
;
460 /* N.B. Parport core code requires that this function not block */
461 static void parport_mos7715_save_state(struct parport
*pp
,
462 struct parport_state
*s
)
464 struct mos7715_parport
*mos_parport
;
466 spin_lock(&release_lock
);
467 mos_parport
= pp
->private_data
;
468 if (unlikely(mos_parport
== NULL
)) { /* release called */
469 spin_unlock(&release_lock
);
472 s
->u
.pc
.ctr
= mos_parport
->shadowDCR
;
473 s
->u
.pc
.ecr
= mos_parport
->shadowECR
;
474 spin_unlock(&release_lock
);
477 /* N.B. Parport core code requires that this function not block */
478 static void parport_mos7715_restore_state(struct parport
*pp
,
479 struct parport_state
*s
)
481 struct mos7715_parport
*mos_parport
;
483 spin_lock(&release_lock
);
484 mos_parport
= pp
->private_data
;
485 if (unlikely(mos_parport
== NULL
)) { /* release called */
486 spin_unlock(&release_lock
);
489 mos_parport
->shadowDCR
= s
->u
.pc
.ctr
;
490 mos_parport
->shadowECR
= s
->u
.pc
.ecr
;
492 schedule_work(&mos_parport
->work
);
493 spin_unlock(&release_lock
);
496 static size_t parport_mos7715_write_compat(struct parport
*pp
,
498 size_t len
, int flags
)
501 struct mos7715_parport
*mos_parport
= pp
->private_data
;
504 if (parport_prologue(pp
) < 0)
506 mos7715_change_mode(mos_parport
, PPF
);
507 retval
= usb_bulk_msg(mos_parport
->serial
->dev
,
508 usb_sndbulkpipe(mos_parport
->serial
->dev
, 2),
509 (void *)buffer
, len
, &actual_len
,
511 parport_epilogue(pp
);
513 dev_err(&mos_parport
->serial
->dev
->dev
,
514 "mos7720: usb_bulk_msg() failed: %d\n", retval
);
520 static struct parport_operations parport_mos7715_ops
= {
521 .owner
= THIS_MODULE
,
522 .write_data
= parport_mos7715_write_data
,
523 .read_data
= parport_mos7715_read_data
,
525 .write_control
= parport_mos7715_write_control
,
526 .read_control
= parport_mos7715_read_control
,
527 .frob_control
= parport_mos7715_frob_control
,
529 .read_status
= parport_mos7715_read_status
,
531 .enable_irq
= parport_mos7715_enable_irq
,
532 .disable_irq
= parport_mos7715_disable_irq
,
534 .data_forward
= parport_mos7715_data_forward
,
535 .data_reverse
= parport_mos7715_data_reverse
,
537 .init_state
= parport_mos7715_init_state
,
538 .save_state
= parport_mos7715_save_state
,
539 .restore_state
= parport_mos7715_restore_state
,
541 .compat_write_data
= parport_mos7715_write_compat
,
543 .nibble_read_data
= parport_ieee1284_read_nibble
,
544 .byte_read_data
= parport_ieee1284_read_byte
,
548 * Allocate and initialize parallel port control struct, initialize
549 * the parallel port hardware device, and register with the parport subsystem.
551 static int mos7715_parport_init(struct usb_serial
*serial
)
553 struct mos7715_parport
*mos_parport
;
555 /* allocate and initialize parallel port control struct */
556 mos_parport
= kzalloc(sizeof(struct mos7715_parport
), GFP_KERNEL
);
560 mos_parport
->msg_pending
= false;
561 kref_init(&mos_parport
->ref_count
);
562 usb_set_serial_data(serial
, mos_parport
); /* hijack private pointer */
563 mos_parport
->serial
= serial
;
564 INIT_WORK(&mos_parport
->work
, deferred_restore_writes
);
565 init_completion(&mos_parport
->syncmsg_compl
);
567 /* cycle parallel port reset bit */
568 write_mos_reg(mos_parport
->serial
, dummy
, MOS7720_PP_REG
, (__u8
)0x80);
569 write_mos_reg(mos_parport
->serial
, dummy
, MOS7720_PP_REG
, (__u8
)0x00);
571 /* initialize device registers */
572 mos_parport
->shadowDCR
= DCR_INIT_VAL
;
573 write_mos_reg(mos_parport
->serial
, dummy
, MOS7720_DCR
,
574 mos_parport
->shadowDCR
);
575 mos_parport
->shadowECR
= ECR_INIT_VAL
;
576 write_mos_reg(mos_parport
->serial
, dummy
, MOS7720_ECR
,
577 mos_parport
->shadowECR
);
579 /* register with parport core */
580 mos_parport
->pp
= parport_register_port(0, PARPORT_IRQ_NONE
,
582 &parport_mos7715_ops
);
583 if (mos_parport
->pp
== NULL
) {
584 dev_err(&serial
->interface
->dev
,
585 "Could not register parport\n");
586 kref_put(&mos_parport
->ref_count
, destroy_mos_parport
);
589 mos_parport
->pp
->private_data
= mos_parport
;
590 mos_parport
->pp
->modes
= PARPORT_MODE_COMPAT
| PARPORT_MODE_PCSPP
;
591 mos_parport
->pp
->dev
= &serial
->interface
->dev
;
592 parport_announce_port(mos_parport
->pp
);
596 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
599 * mos7720_interrupt_callback
600 * this is the callback function for when we have received data on the
601 * interrupt endpoint.
603 static void mos7720_interrupt_callback(struct urb
*urb
)
607 int status
= urb
->status
;
608 struct device
*dev
= &urb
->dev
->dev
;
620 /* this urb is terminated, clean up */
621 dev_dbg(dev
, "%s - urb shutting down with status: %d\n", __func__
, status
);
624 dev_dbg(dev
, "%s - nonzero urb status received: %d\n", __func__
, status
);
628 length
= urb
->actual_length
;
629 data
= urb
->transfer_buffer
;
631 /* Moschip get 4 bytes
632 * Byte 1 IIR Port 1 (port.number is 0)
633 * Byte 2 IIR Port 2 (port.number is 1)
634 * Byte 3 --------------
635 * Byte 4 FIFO status for both */
637 /* the above description is inverted
638 * oneukum 2007-03-14 */
640 if (unlikely(length
!= 4)) {
641 dev_dbg(dev
, "Wrong data !!!\n");
648 if ((sp1
| sp2
) & 0x01) {
649 /* No Interrupt Pending in both the ports */
650 dev_dbg(dev
, "No Interrupt !!!\n");
652 switch (sp1
& 0x0f) {
654 dev_dbg(dev
, "Serial Port 1: Receiver status error or address bit detected in 9-bit mode\n");
657 dev_dbg(dev
, "Serial Port 1: Receiver time out\n");
660 /* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */
664 switch (sp2
& 0x0f) {
666 dev_dbg(dev
, "Serial Port 2: Receiver status error or address bit detected in 9-bit mode\n");
669 dev_dbg(dev
, "Serial Port 2: Receiver time out\n");
672 /* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */
678 result
= usb_submit_urb(urb
, GFP_ATOMIC
);
680 dev_err(dev
, "%s - Error %d submitting control urb\n", __func__
, result
);
684 * mos7715_interrupt_callback
685 * this is the 7715's callback function for when we have received data on
686 * the interrupt endpoint.
688 static void mos7715_interrupt_callback(struct urb
*urb
)
692 int status
= urb
->status
;
693 struct device
*dev
= &urb
->dev
->dev
;
705 /* this urb is terminated, clean up */
706 dev_dbg(dev
, "%s - urb shutting down with status: %d\n", __func__
, status
);
709 dev_dbg(dev
, "%s - nonzero urb status received: %d\n", __func__
, status
);
713 length
= urb
->actual_length
;
714 data
= urb
->transfer_buffer
;
716 /* Structure of data from 7715 device:
717 * Byte 1: IIR serial Port
719 * Byte 2: DSR parallel port
720 * Byte 4: FIFO status for both */
722 if (unlikely(length
!= 4)) {
723 dev_dbg(dev
, "Wrong data !!!\n");
728 if (!(iir
& 0x01)) { /* serial port interrupt pending */
729 switch (iir
& 0x0f) {
731 dev_dbg(dev
, "Serial Port: Receiver status error or address bit detected in 9-bit mode\n");
734 dev_dbg(dev
, "Serial Port: Receiver time out\n");
737 /* dev_dbg(dev, "Serial Port: Modem status change\n"); */
742 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
743 { /* update local copy of DSR reg */
744 struct usb_serial_port
*port
= urb
->context
;
745 struct mos7715_parport
*mos_parport
= port
->serial
->private;
746 if (unlikely(mos_parport
== NULL
))
748 atomic_set(&mos_parport
->shadowDSR
, data
[2]);
753 result
= usb_submit_urb(urb
, GFP_ATOMIC
);
755 dev_err(dev
, "%s - Error %d submitting control urb\n", __func__
, result
);
759 * mos7720_bulk_in_callback
760 * this is the callback function for when we have received data on the
763 static void mos7720_bulk_in_callback(struct urb
*urb
)
766 unsigned char *data
;
767 struct usb_serial_port
*port
;
768 int status
= urb
->status
;
771 dev_dbg(&urb
->dev
->dev
, "nonzero read bulk status received: %d\n", status
);
777 dev_dbg(&port
->dev
, "Entering...%s\n", __func__
);
779 data
= urb
->transfer_buffer
;
781 if (urb
->actual_length
) {
782 tty_insert_flip_string(&port
->port
, data
, urb
->actual_length
);
783 tty_flip_buffer_push(&port
->port
);
786 if (port
->read_urb
->status
!= -EINPROGRESS
) {
787 retval
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
789 dev_dbg(&port
->dev
, "usb_submit_urb(read bulk) failed, retval = %d\n", retval
);
794 * mos7720_bulk_out_data_callback
795 * this is the callback function for when we have finished sending serial
796 * data on the bulk out endpoint.
798 static void mos7720_bulk_out_data_callback(struct urb
*urb
)
800 struct moschip_port
*mos7720_port
;
801 int status
= urb
->status
;
804 dev_dbg(&urb
->dev
->dev
, "nonzero write bulk status received:%d\n", status
);
808 mos7720_port
= urb
->context
;
810 dev_dbg(&urb
->dev
->dev
, "NULL mos7720_port pointer\n");
814 if (mos7720_port
->open
)
815 tty_port_tty_wakeup(&mos7720_port
->port
->port
);
818 static int mos77xx_calc_num_ports(struct usb_serial
*serial
,
819 struct usb_serial_endpoints
*epds
)
821 u16 product
= le16_to_cpu(serial
->dev
->descriptor
.idProduct
);
823 if (product
== MOSCHIP_DEVICE_ID_7715
) {
825 * The 7715 uses the first bulk in/out endpoint pair for the
826 * parallel port, and the second for the serial port. We swap
827 * the endpoint descriptors here so that the the first and
828 * only registered port structure uses the serial-port
831 swap(epds
->bulk_in
[0], epds
->bulk_in
[1]);
832 swap(epds
->bulk_out
[0], epds
->bulk_out
[1]);
840 static int mos7720_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
842 struct usb_serial
*serial
;
844 struct moschip_port
*mos7720_port
;
848 int allocated_urbs
= 0;
851 serial
= port
->serial
;
853 mos7720_port
= usb_get_serial_port_data(port
);
854 if (mos7720_port
== NULL
)
857 usb_clear_halt(serial
->dev
, port
->write_urb
->pipe
);
858 usb_clear_halt(serial
->dev
, port
->read_urb
->pipe
);
860 /* Initialising the write urb pool */
861 for (j
= 0; j
< NUM_URBS
; ++j
) {
862 urb
= usb_alloc_urb(0, GFP_KERNEL
);
863 mos7720_port
->write_urb_pool
[j
] = urb
;
867 urb
->transfer_buffer
= kmalloc(URB_TRANSFER_BUFFER_SIZE
,
869 if (!urb
->transfer_buffer
) {
870 usb_free_urb(mos7720_port
->write_urb_pool
[j
]);
871 mos7720_port
->write_urb_pool
[j
] = NULL
;
880 /* Initialize MCS7720 -- Write Init values to corresponding Registers
883 * 0 : MOS7720_THR/MOS7720_RHR
892 * 0x08 : SP1/2 Control Reg
894 port_number
= port
->port_number
;
895 read_mos_reg(serial
, port_number
, MOS7720_LSR
, &data
);
897 dev_dbg(&port
->dev
, "SS::%p LSR:%x\n", mos7720_port
, data
);
899 write_mos_reg(serial
, dummy
, MOS7720_SP1_REG
, 0x02);
900 write_mos_reg(serial
, dummy
, MOS7720_SP2_REG
, 0x02);
902 write_mos_reg(serial
, port_number
, MOS7720_IER
, 0x00);
903 write_mos_reg(serial
, port_number
, MOS7720_FCR
, 0x00);
905 write_mos_reg(serial
, port_number
, MOS7720_FCR
, 0xcf);
906 mos7720_port
->shadowLCR
= 0x03;
907 write_mos_reg(serial
, port_number
, MOS7720_LCR
,
908 mos7720_port
->shadowLCR
);
909 mos7720_port
->shadowMCR
= 0x0b;
910 write_mos_reg(serial
, port_number
, MOS7720_MCR
,
911 mos7720_port
->shadowMCR
);
913 write_mos_reg(serial
, port_number
, MOS7720_SP_CONTROL_REG
, 0x00);
914 read_mos_reg(serial
, dummy
, MOS7720_SP_CONTROL_REG
, &data
);
915 data
= data
| (port
->port_number
+ 1);
916 write_mos_reg(serial
, dummy
, MOS7720_SP_CONTROL_REG
, data
);
917 mos7720_port
->shadowLCR
= 0x83;
918 write_mos_reg(serial
, port_number
, MOS7720_LCR
,
919 mos7720_port
->shadowLCR
);
920 write_mos_reg(serial
, port_number
, MOS7720_THR
, 0x0c);
921 write_mos_reg(serial
, port_number
, MOS7720_IER
, 0x00);
922 mos7720_port
->shadowLCR
= 0x03;
923 write_mos_reg(serial
, port_number
, MOS7720_LCR
,
924 mos7720_port
->shadowLCR
);
925 write_mos_reg(serial
, port_number
, MOS7720_IER
, 0x0c);
927 response
= usb_submit_urb(port
->read_urb
, GFP_KERNEL
);
929 dev_err(&port
->dev
, "%s - Error %d submitting read urb\n",
932 /* initialize our port settings */
933 mos7720_port
->shadowMCR
= UART_MCR_OUT2
; /* Must set to enable ints! */
935 /* send a open port command */
936 mos7720_port
->open
= 1;
942 * mos7720_chars_in_buffer
943 * this function is called by the tty driver when it wants to know how many
944 * bytes of data we currently have outstanding in the port (data that has
945 * been written, but hasn't made it out the port yet)
946 * If successful, we return the number of bytes left to be written in the
948 * Otherwise we return a negative error number.
950 static int mos7720_chars_in_buffer(struct tty_struct
*tty
)
952 struct usb_serial_port
*port
= tty
->driver_data
;
955 struct moschip_port
*mos7720_port
;
957 mos7720_port
= usb_get_serial_port_data(port
);
958 if (mos7720_port
== NULL
)
961 for (i
= 0; i
< NUM_URBS
; ++i
) {
962 if (mos7720_port
->write_urb_pool
[i
] &&
963 mos7720_port
->write_urb_pool
[i
]->status
== -EINPROGRESS
)
964 chars
+= URB_TRANSFER_BUFFER_SIZE
;
966 dev_dbg(&port
->dev
, "%s - returns %d\n", __func__
, chars
);
970 static void mos7720_close(struct usb_serial_port
*port
)
972 struct usb_serial
*serial
;
973 struct moschip_port
*mos7720_port
;
976 serial
= port
->serial
;
978 mos7720_port
= usb_get_serial_port_data(port
);
979 if (mos7720_port
== NULL
)
982 for (j
= 0; j
< NUM_URBS
; ++j
)
983 usb_kill_urb(mos7720_port
->write_urb_pool
[j
]);
985 /* Freeing Write URBs */
986 for (j
= 0; j
< NUM_URBS
; ++j
) {
987 if (mos7720_port
->write_urb_pool
[j
]) {
988 kfree(mos7720_port
->write_urb_pool
[j
]->transfer_buffer
);
989 usb_free_urb(mos7720_port
->write_urb_pool
[j
]);
993 /* While closing port, shutdown all bulk read, write *
994 * and interrupt read if they exists, otherwise nop */
995 usb_kill_urb(port
->write_urb
);
996 usb_kill_urb(port
->read_urb
);
998 write_mos_reg(serial
, port
->port_number
, MOS7720_MCR
, 0x00);
999 write_mos_reg(serial
, port
->port_number
, MOS7720_IER
, 0x00);
1001 mos7720_port
->open
= 0;
1004 static void mos7720_break(struct tty_struct
*tty
, int break_state
)
1006 struct usb_serial_port
*port
= tty
->driver_data
;
1008 struct usb_serial
*serial
;
1009 struct moschip_port
*mos7720_port
;
1011 serial
= port
->serial
;
1013 mos7720_port
= usb_get_serial_port_data(port
);
1014 if (mos7720_port
== NULL
)
1017 if (break_state
== -1)
1018 data
= mos7720_port
->shadowLCR
| UART_LCR_SBC
;
1020 data
= mos7720_port
->shadowLCR
& ~UART_LCR_SBC
;
1022 mos7720_port
->shadowLCR
= data
;
1023 write_mos_reg(serial
, port
->port_number
, MOS7720_LCR
,
1024 mos7720_port
->shadowLCR
);
1028 * mos7720_write_room
1029 * this function is called by the tty driver when it wants to know how many
1030 * bytes of data we can accept for a specific port.
1031 * If successful, we return the amount of room that we have for this port
1032 * Otherwise we return a negative error number.
1034 static int mos7720_write_room(struct tty_struct
*tty
)
1036 struct usb_serial_port
*port
= tty
->driver_data
;
1037 struct moschip_port
*mos7720_port
;
1041 mos7720_port
= usb_get_serial_port_data(port
);
1042 if (mos7720_port
== NULL
)
1045 /* FIXME: Locking */
1046 for (i
= 0; i
< NUM_URBS
; ++i
) {
1047 if (mos7720_port
->write_urb_pool
[i
] &&
1048 mos7720_port
->write_urb_pool
[i
]->status
!= -EINPROGRESS
)
1049 room
+= URB_TRANSFER_BUFFER_SIZE
;
1052 dev_dbg(&port
->dev
, "%s - returns %d\n", __func__
, room
);
1056 static int mos7720_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
1057 const unsigned char *data
, int count
)
1064 struct moschip_port
*mos7720_port
;
1065 struct usb_serial
*serial
;
1067 const unsigned char *current_position
= data
;
1069 serial
= port
->serial
;
1071 mos7720_port
= usb_get_serial_port_data(port
);
1072 if (mos7720_port
== NULL
)
1075 /* try to find a free urb in the list */
1078 for (i
= 0; i
< NUM_URBS
; ++i
) {
1079 if (mos7720_port
->write_urb_pool
[i
] &&
1080 mos7720_port
->write_urb_pool
[i
]->status
!= -EINPROGRESS
) {
1081 urb
= mos7720_port
->write_urb_pool
[i
];
1082 dev_dbg(&port
->dev
, "URB:%d\n", i
);
1088 dev_dbg(&port
->dev
, "%s - no more free urbs\n", __func__
);
1092 if (urb
->transfer_buffer
== NULL
) {
1093 urb
->transfer_buffer
= kmalloc(URB_TRANSFER_BUFFER_SIZE
,
1095 if (!urb
->transfer_buffer
)
1098 transfer_size
= min(count
, URB_TRANSFER_BUFFER_SIZE
);
1100 memcpy(urb
->transfer_buffer
, current_position
, transfer_size
);
1101 usb_serial_debug_data(&port
->dev
, __func__
, transfer_size
,
1102 urb
->transfer_buffer
);
1104 /* fill urb with data and submit */
1105 usb_fill_bulk_urb(urb
, serial
->dev
,
1106 usb_sndbulkpipe(serial
->dev
,
1107 port
->bulk_out_endpointAddress
),
1108 urb
->transfer_buffer
, transfer_size
,
1109 mos7720_bulk_out_data_callback
, mos7720_port
);
1111 /* send it down the pipe */
1112 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
1114 dev_err_console(port
, "%s - usb_submit_urb(write bulk) failed "
1115 "with status = %d\n", __func__
, status
);
1116 bytes_sent
= status
;
1119 bytes_sent
= transfer_size
;
1125 static void mos7720_throttle(struct tty_struct
*tty
)
1127 struct usb_serial_port
*port
= tty
->driver_data
;
1128 struct moschip_port
*mos7720_port
;
1131 mos7720_port
= usb_get_serial_port_data(port
);
1133 if (mos7720_port
== NULL
)
1136 if (!mos7720_port
->open
) {
1137 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1141 /* if we are implementing XON/XOFF, send the stop character */
1143 unsigned char stop_char
= STOP_CHAR(tty
);
1144 status
= mos7720_write(tty
, port
, &stop_char
, 1);
1149 /* if we are implementing RTS/CTS, toggle that line */
1150 if (C_CRTSCTS(tty
)) {
1151 mos7720_port
->shadowMCR
&= ~UART_MCR_RTS
;
1152 write_mos_reg(port
->serial
, port
->port_number
, MOS7720_MCR
,
1153 mos7720_port
->shadowMCR
);
1157 static void mos7720_unthrottle(struct tty_struct
*tty
)
1159 struct usb_serial_port
*port
= tty
->driver_data
;
1160 struct moschip_port
*mos7720_port
= usb_get_serial_port_data(port
);
1163 if (mos7720_port
== NULL
)
1166 if (!mos7720_port
->open
) {
1167 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1171 /* if we are implementing XON/XOFF, send the start character */
1173 unsigned char start_char
= START_CHAR(tty
);
1174 status
= mos7720_write(tty
, port
, &start_char
, 1);
1179 /* if we are implementing RTS/CTS, toggle that line */
1180 if (C_CRTSCTS(tty
)) {
1181 mos7720_port
->shadowMCR
|= UART_MCR_RTS
;
1182 write_mos_reg(port
->serial
, port
->port_number
, MOS7720_MCR
,
1183 mos7720_port
->shadowMCR
);
1187 /* FIXME: this function does not work */
1188 static int set_higher_rates(struct moschip_port
*mos7720_port
,
1191 struct usb_serial_port
*port
;
1192 struct usb_serial
*serial
;
1194 enum mos_regs sp_reg
;
1195 if (mos7720_port
== NULL
)
1198 port
= mos7720_port
->port
;
1199 serial
= port
->serial
;
1201 /***********************************************
1202 * Init Sequence for higher rates
1203 ***********************************************/
1204 dev_dbg(&port
->dev
, "Sending Setting Commands ..........\n");
1205 port_number
= port
->port_number
;
1207 write_mos_reg(serial
, port_number
, MOS7720_IER
, 0x00);
1208 write_mos_reg(serial
, port_number
, MOS7720_FCR
, 0x00);
1209 write_mos_reg(serial
, port_number
, MOS7720_FCR
, 0xcf);
1210 mos7720_port
->shadowMCR
= 0x0b;
1211 write_mos_reg(serial
, port_number
, MOS7720_MCR
,
1212 mos7720_port
->shadowMCR
);
1213 write_mos_reg(serial
, dummy
, MOS7720_SP_CONTROL_REG
, 0x00);
1215 /***********************************************
1216 * Set for higher rates *
1217 ***********************************************/
1218 /* writing baud rate verbatum into uart clock field clearly not right */
1219 if (port_number
== 0)
1220 sp_reg
= MOS7720_SP1_REG
;
1222 sp_reg
= MOS7720_SP2_REG
;
1223 write_mos_reg(serial
, dummy
, sp_reg
, baud
* 0x10);
1224 write_mos_reg(serial
, dummy
, MOS7720_SP_CONTROL_REG
, 0x03);
1225 mos7720_port
->shadowMCR
= 0x2b;
1226 write_mos_reg(serial
, port_number
, MOS7720_MCR
,
1227 mos7720_port
->shadowMCR
);
1229 /***********************************************
1231 ***********************************************/
1232 mos7720_port
->shadowLCR
= mos7720_port
->shadowLCR
| UART_LCR_DLAB
;
1233 write_mos_reg(serial
, port_number
, MOS7720_LCR
,
1234 mos7720_port
->shadowLCR
);
1235 write_mos_reg(serial
, port_number
, MOS7720_DLL
, 0x01);
1236 write_mos_reg(serial
, port_number
, MOS7720_DLM
, 0x00);
1237 mos7720_port
->shadowLCR
= mos7720_port
->shadowLCR
& ~UART_LCR_DLAB
;
1238 write_mos_reg(serial
, port_number
, MOS7720_LCR
,
1239 mos7720_port
->shadowLCR
);
1244 /* baud rate information */
1245 struct divisor_table_entry
{
1250 /* Define table of divisors for moschip 7720 hardware *
1251 * These assume a 3.6864MHz crystal, the standard /16, and *
1253 static const struct divisor_table_entry divisor_table
[] = {
1255 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
1256 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
1272 /*****************************************************************************
1273 * calc_baud_rate_divisor
1274 * this function calculates the proper baud rate divisor for the specified
1276 *****************************************************************************/
1277 static int calc_baud_rate_divisor(struct usb_serial_port
*port
, int baudrate
, int *divisor
)
1285 dev_dbg(&port
->dev
, "%s - %d\n", __func__
, baudrate
);
1287 for (i
= 0; i
< ARRAY_SIZE(divisor_table
); i
++) {
1288 if (divisor_table
[i
].baudrate
== baudrate
) {
1289 *divisor
= divisor_table
[i
].divisor
;
1294 /* After trying for all the standard baud rates *
1295 * Try calculating the divisor for this baud rate */
1296 if (baudrate
> 75 && baudrate
< 230400) {
1297 /* get the divisor */
1298 custom
= (__u16
)(230400L / baudrate
);
1300 /* Check for round off */
1301 round1
= (__u16
)(2304000L / baudrate
);
1302 round
= (__u16
)(round1
- (custom
* 10));
1307 dev_dbg(&port
->dev
, "Baud %d = %d\n", baudrate
, custom
);
1311 dev_dbg(&port
->dev
, "Baud calculation Failed...\n");
1316 * send_cmd_write_baud_rate
1317 * this function sends the proper command to change the baud rate of the
1320 static int send_cmd_write_baud_rate(struct moschip_port
*mos7720_port
,
1323 struct usb_serial_port
*port
;
1324 struct usb_serial
*serial
;
1327 unsigned char number
;
1329 if (mos7720_port
== NULL
)
1332 port
= mos7720_port
->port
;
1333 serial
= port
->serial
;
1335 number
= port
->port_number
;
1336 dev_dbg(&port
->dev
, "%s - baud = %d\n", __func__
, baudrate
);
1338 /* Calculate the Divisor */
1339 status
= calc_baud_rate_divisor(port
, baudrate
, &divisor
);
1341 dev_err(&port
->dev
, "%s - bad baud rate\n", __func__
);
1345 /* Enable access to divisor latch */
1346 mos7720_port
->shadowLCR
= mos7720_port
->shadowLCR
| UART_LCR_DLAB
;
1347 write_mos_reg(serial
, number
, MOS7720_LCR
, mos7720_port
->shadowLCR
);
1349 /* Write the divisor */
1350 write_mos_reg(serial
, number
, MOS7720_DLL
, (__u8
)(divisor
& 0xff));
1351 write_mos_reg(serial
, number
, MOS7720_DLM
,
1352 (__u8
)((divisor
& 0xff00) >> 8));
1354 /* Disable access to divisor latch */
1355 mos7720_port
->shadowLCR
= mos7720_port
->shadowLCR
& ~UART_LCR_DLAB
;
1356 write_mos_reg(serial
, number
, MOS7720_LCR
, mos7720_port
->shadowLCR
);
1362 * change_port_settings
1363 * This routine is called to set the UART on the device to match
1364 * the specified new settings.
1366 static void change_port_settings(struct tty_struct
*tty
,
1367 struct moschip_port
*mos7720_port
,
1368 struct ktermios
*old_termios
)
1370 struct usb_serial_port
*port
;
1371 struct usb_serial
*serial
;
1380 if (mos7720_port
== NULL
)
1383 port
= mos7720_port
->port
;
1384 serial
= port
->serial
;
1385 port_number
= port
->port_number
;
1387 if (!mos7720_port
->open
) {
1388 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1392 lData
= UART_LCR_WLEN8
;
1393 lStop
= 0x00; /* 1 stop bit */
1394 lParity
= 0x00; /* No parity */
1396 cflag
= tty
->termios
.c_cflag
;
1398 /* Change the number of bits */
1399 switch (cflag
& CSIZE
) {
1401 lData
= UART_LCR_WLEN5
;
1405 lData
= UART_LCR_WLEN6
;
1409 lData
= UART_LCR_WLEN7
;
1413 lData
= UART_LCR_WLEN8
;
1417 /* Change the Parity bit */
1418 if (cflag
& PARENB
) {
1419 if (cflag
& PARODD
) {
1420 lParity
= UART_LCR_PARITY
;
1421 dev_dbg(&port
->dev
, "%s - parity = odd\n", __func__
);
1423 lParity
= (UART_LCR_EPAR
| UART_LCR_PARITY
);
1424 dev_dbg(&port
->dev
, "%s - parity = even\n", __func__
);
1428 dev_dbg(&port
->dev
, "%s - parity = none\n", __func__
);
1432 lParity
= lParity
| 0x20;
1434 /* Change the Stop bit */
1435 if (cflag
& CSTOPB
) {
1436 lStop
= UART_LCR_STOP
;
1437 dev_dbg(&port
->dev
, "%s - stop bits = 2\n", __func__
);
1440 dev_dbg(&port
->dev
, "%s - stop bits = 1\n", __func__
);
1443 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1444 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1445 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
1447 /* Update the LCR with the correct value */
1448 mos7720_port
->shadowLCR
&=
1449 ~(LCR_BITS_MASK
| LCR_STOP_MASK
| LCR_PAR_MASK
);
1450 mos7720_port
->shadowLCR
|= (lData
| lParity
| lStop
);
1453 /* Disable Interrupts */
1454 write_mos_reg(serial
, port_number
, MOS7720_IER
, 0x00);
1455 write_mos_reg(serial
, port_number
, MOS7720_FCR
, 0x00);
1456 write_mos_reg(serial
, port_number
, MOS7720_FCR
, 0xcf);
1458 /* Send the updated LCR value to the mos7720 */
1459 write_mos_reg(serial
, port_number
, MOS7720_LCR
,
1460 mos7720_port
->shadowLCR
);
1461 mos7720_port
->shadowMCR
= 0x0b;
1462 write_mos_reg(serial
, port_number
, MOS7720_MCR
,
1463 mos7720_port
->shadowMCR
);
1465 /* set up the MCR register and send it to the mos7720 */
1466 mos7720_port
->shadowMCR
= UART_MCR_OUT2
;
1468 mos7720_port
->shadowMCR
|= (UART_MCR_DTR
| UART_MCR_RTS
);
1470 if (cflag
& CRTSCTS
) {
1471 mos7720_port
->shadowMCR
|= (UART_MCR_XONANY
);
1472 /* To set hardware flow control to the specified *
1473 * serial port, in SP1/2_CONTROL_REG */
1475 write_mos_reg(serial
, dummy
, MOS7720_SP_CONTROL_REG
,
1478 write_mos_reg(serial
, dummy
, MOS7720_SP_CONTROL_REG
,
1482 mos7720_port
->shadowMCR
&= ~(UART_MCR_XONANY
);
1484 write_mos_reg(serial
, port_number
, MOS7720_MCR
,
1485 mos7720_port
->shadowMCR
);
1487 /* Determine divisor based on baud rate */
1488 baud
= tty_get_baud_rate(tty
);
1490 /* pick a default, any default... */
1491 dev_dbg(&port
->dev
, "Picked default baud...\n");
1495 if (baud
>= 230400) {
1496 set_higher_rates(mos7720_port
, baud
);
1497 /* Enable Interrupts */
1498 write_mos_reg(serial
, port_number
, MOS7720_IER
, 0x0c);
1502 dev_dbg(&port
->dev
, "%s - baud rate = %d\n", __func__
, baud
);
1503 status
= send_cmd_write_baud_rate(mos7720_port
, baud
);
1504 /* FIXME: needs to write actual resulting baud back not just
1507 tty_encode_baud_rate(tty
, baud
, baud
);
1508 /* Enable Interrupts */
1509 write_mos_reg(serial
, port_number
, MOS7720_IER
, 0x0c);
1511 if (port
->read_urb
->status
!= -EINPROGRESS
) {
1512 status
= usb_submit_urb(port
->read_urb
, GFP_KERNEL
);
1514 dev_dbg(&port
->dev
, "usb_submit_urb(read bulk) failed, status = %d\n", status
);
1519 * mos7720_set_termios
1520 * this function is called by the tty driver when it wants to change the
1521 * termios structure.
1523 static void mos7720_set_termios(struct tty_struct
*tty
,
1524 struct usb_serial_port
*port
, struct ktermios
*old_termios
)
1527 struct moschip_port
*mos7720_port
;
1529 mos7720_port
= usb_get_serial_port_data(port
);
1531 if (mos7720_port
== NULL
)
1534 if (!mos7720_port
->open
) {
1535 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1539 /* change the port settings to the new ones specified */
1540 change_port_settings(tty
, mos7720_port
, old_termios
);
1542 if (port
->read_urb
->status
!= -EINPROGRESS
) {
1543 status
= usb_submit_urb(port
->read_urb
, GFP_KERNEL
);
1545 dev_dbg(&port
->dev
, "usb_submit_urb(read bulk) failed, status = %d\n", status
);
1550 * get_lsr_info - get line status register info
1552 * Purpose: Let user call ioctl() to get info when the UART physically
1553 * is emptied. On bus types like RS485, the transmitter must
1554 * release the bus after transmitting. This must be done when
1555 * the transmit shift register is empty, not be done when the
1556 * transmit holding register is empty. This functionality
1557 * allows an RS485 driver to be written in user space.
1559 static int get_lsr_info(struct tty_struct
*tty
,
1560 struct moschip_port
*mos7720_port
, unsigned int __user
*value
)
1562 struct usb_serial_port
*port
= tty
->driver_data
;
1563 unsigned int result
= 0;
1564 unsigned char data
= 0;
1565 int port_number
= port
->port_number
;
1568 count
= mos7720_chars_in_buffer(tty
);
1570 read_mos_reg(port
->serial
, port_number
, MOS7720_LSR
, &data
);
1571 if ((data
& (UART_LSR_TEMT
| UART_LSR_THRE
))
1572 == (UART_LSR_TEMT
| UART_LSR_THRE
)) {
1573 dev_dbg(&port
->dev
, "%s -- Empty\n", __func__
);
1574 result
= TIOCSER_TEMT
;
1577 if (copy_to_user(value
, &result
, sizeof(int)))
1582 static int mos7720_tiocmget(struct tty_struct
*tty
)
1584 struct usb_serial_port
*port
= tty
->driver_data
;
1585 struct moschip_port
*mos7720_port
= usb_get_serial_port_data(port
);
1586 unsigned int result
= 0;
1590 mcr
= mos7720_port
->shadowMCR
;
1591 msr
= mos7720_port
->shadowMSR
;
1593 result
= ((mcr
& UART_MCR_DTR
) ? TIOCM_DTR
: 0) /* 0x002 */
1594 | ((mcr
& UART_MCR_RTS
) ? TIOCM_RTS
: 0) /* 0x004 */
1595 | ((msr
& UART_MSR_CTS
) ? TIOCM_CTS
: 0) /* 0x020 */
1596 | ((msr
& UART_MSR_DCD
) ? TIOCM_CAR
: 0) /* 0x040 */
1597 | ((msr
& UART_MSR_RI
) ? TIOCM_RI
: 0) /* 0x080 */
1598 | ((msr
& UART_MSR_DSR
) ? TIOCM_DSR
: 0); /* 0x100 */
1603 static int mos7720_tiocmset(struct tty_struct
*tty
,
1604 unsigned int set
, unsigned int clear
)
1606 struct usb_serial_port
*port
= tty
->driver_data
;
1607 struct moschip_port
*mos7720_port
= usb_get_serial_port_data(port
);
1610 mcr
= mos7720_port
->shadowMCR
;
1612 if (set
& TIOCM_RTS
)
1613 mcr
|= UART_MCR_RTS
;
1614 if (set
& TIOCM_DTR
)
1615 mcr
|= UART_MCR_DTR
;
1616 if (set
& TIOCM_LOOP
)
1617 mcr
|= UART_MCR_LOOP
;
1619 if (clear
& TIOCM_RTS
)
1620 mcr
&= ~UART_MCR_RTS
;
1621 if (clear
& TIOCM_DTR
)
1622 mcr
&= ~UART_MCR_DTR
;
1623 if (clear
& TIOCM_LOOP
)
1624 mcr
&= ~UART_MCR_LOOP
;
1626 mos7720_port
->shadowMCR
= mcr
;
1627 write_mos_reg(port
->serial
, port
->port_number
, MOS7720_MCR
,
1628 mos7720_port
->shadowMCR
);
1633 static int get_serial_info(struct tty_struct
*tty
,
1634 struct serial_struct
*ss
)
1636 struct usb_serial_port
*port
= tty
->driver_data
;
1637 struct moschip_port
*mos7720_port
= usb_get_serial_port_data(port
);
1639 ss
->type
= PORT_16550A
;
1640 ss
->line
= mos7720_port
->port
->minor
;
1641 ss
->port
= mos7720_port
->port
->port_number
;
1643 ss
->xmit_fifo_size
= NUM_URBS
* URB_TRANSFER_BUFFER_SIZE
;
1644 ss
->baud_base
= 9600;
1645 ss
->close_delay
= 5*HZ
;
1646 ss
->closing_wait
= 30*HZ
;
1650 static int mos7720_ioctl(struct tty_struct
*tty
,
1651 unsigned int cmd
, unsigned long arg
)
1653 struct usb_serial_port
*port
= tty
->driver_data
;
1654 struct moschip_port
*mos7720_port
;
1656 mos7720_port
= usb_get_serial_port_data(port
);
1657 if (mos7720_port
== NULL
)
1662 dev_dbg(&port
->dev
, "%s TIOCSERGETLSR\n", __func__
);
1663 return get_lsr_info(tty
, mos7720_port
,
1664 (unsigned int __user
*)arg
);
1667 return -ENOIOCTLCMD
;
1670 static int mos7720_startup(struct usb_serial
*serial
)
1672 struct usb_device
*dev
;
1677 product
= le16_to_cpu(serial
->dev
->descriptor
.idProduct
);
1680 if (product
== MOSCHIP_DEVICE_ID_7715
) {
1681 struct urb
*urb
= serial
->port
[0]->interrupt_in_urb
;
1683 urb
->complete
= mos7715_interrupt_callback
;
1685 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1686 ret_val
= mos7715_parport_init(serial
);
1691 /* start the interrupt urb */
1692 ret_val
= usb_submit_urb(serial
->port
[0]->interrupt_in_urb
, GFP_KERNEL
);
1694 dev_err(&dev
->dev
, "failed to submit interrupt urb: %d\n",
1698 /* LSR For Port 1 */
1699 read_mos_reg(serial
, 0, MOS7720_LSR
, &data
);
1700 dev_dbg(&dev
->dev
, "LSR:%x\n", data
);
1705 static void mos7720_release(struct usb_serial
*serial
)
1707 usb_kill_urb(serial
->port
[0]->interrupt_in_urb
);
1709 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1710 /* close the parallel port */
1712 if (le16_to_cpu(serial
->dev
->descriptor
.idProduct
)
1713 == MOSCHIP_DEVICE_ID_7715
) {
1714 struct mos7715_parport
*mos_parport
=
1715 usb_get_serial_data(serial
);
1717 /* prevent NULL ptr dereference in port callbacks */
1718 spin_lock(&release_lock
);
1719 mos_parport
->pp
->private_data
= NULL
;
1720 spin_unlock(&release_lock
);
1722 /* wait for synchronous usb calls to return */
1723 if (mos_parport
->msg_pending
)
1724 wait_for_completion_timeout(&mos_parport
->syncmsg_compl
,
1725 msecs_to_jiffies(MOS_WDR_TIMEOUT
));
1727 * If delayed work is currently scheduled, wait for it to
1728 * complete. This also implies barriers that ensure the
1729 * below serial clearing is not hoisted above the ->work.
1731 cancel_work_sync(&mos_parport
->work
);
1733 parport_remove_port(mos_parport
->pp
);
1734 usb_set_serial_data(serial
, NULL
);
1735 mos_parport
->serial
= NULL
;
1737 parport_del_port(mos_parport
->pp
);
1739 kref_put(&mos_parport
->ref_count
, destroy_mos_parport
);
1744 static int mos7720_port_probe(struct usb_serial_port
*port
)
1746 struct moschip_port
*mos7720_port
;
1748 mos7720_port
= kzalloc(sizeof(*mos7720_port
), GFP_KERNEL
);
1752 mos7720_port
->port
= port
;
1754 usb_set_serial_port_data(port
, mos7720_port
);
1759 static int mos7720_port_remove(struct usb_serial_port
*port
)
1761 struct moschip_port
*mos7720_port
;
1763 mos7720_port
= usb_get_serial_port_data(port
);
1764 kfree(mos7720_port
);
1769 static struct usb_serial_driver moschip7720_2port_driver
= {
1771 .owner
= THIS_MODULE
,
1772 .name
= "moschip7720",
1774 .description
= "Moschip 2 port adapter",
1775 .id_table
= id_table
,
1778 .num_interrupt_in
= 1,
1779 .calc_num_ports
= mos77xx_calc_num_ports
,
1780 .open
= mos7720_open
,
1781 .close
= mos7720_close
,
1782 .throttle
= mos7720_throttle
,
1783 .unthrottle
= mos7720_unthrottle
,
1784 .attach
= mos7720_startup
,
1785 .release
= mos7720_release
,
1786 .port_probe
= mos7720_port_probe
,
1787 .port_remove
= mos7720_port_remove
,
1788 .ioctl
= mos7720_ioctl
,
1789 .tiocmget
= mos7720_tiocmget
,
1790 .tiocmset
= mos7720_tiocmset
,
1791 .get_serial
= get_serial_info
,
1792 .set_termios
= mos7720_set_termios
,
1793 .write
= mos7720_write
,
1794 .write_room
= mos7720_write_room
,
1795 .chars_in_buffer
= mos7720_chars_in_buffer
,
1796 .break_ctl
= mos7720_break
,
1797 .read_bulk_callback
= mos7720_bulk_in_callback
,
1798 .read_int_callback
= mos7720_interrupt_callback
,
1801 static struct usb_serial_driver
* const serial_drivers
[] = {
1802 &moschip7720_2port_driver
, NULL
1805 module_usb_serial_driver(serial_drivers
, id_table
);
1807 MODULE_AUTHOR(DRIVER_AUTHOR
);
1808 MODULE_DESCRIPTION(DRIVER_DESC
);
1809 MODULE_LICENSE("GPL v2");