Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / usb / serial / mos7720.c
blob41ee2984a0dff25fe75606e6ce11489efe578c47
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * mos7720.c
4 * Controls the Moschip 7720 usb to dual port serial converter
6 * Copyright 2006 Moschip Semiconductor Tech. Ltd.
8 * Developed by:
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
43 #define MOS_READ 0x0D
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 */
56 struct moschip_port {
57 __u8 shadowLCR; /* last LCR value received */
58 __u8 shadowMCR; /* last MCR value received */
59 __u8 shadowMSR; /* last MSR value received */
60 char open;
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 {
83 SPP = 0<<5,
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... */
96 __u8 shadowDCR;
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 */
107 enum mos_regs {
108 MOS7720_THR, /* serial port regs */
109 MOS7720_RHR,
110 MOS7720_IER,
111 MOS7720_FCR,
112 MOS7720_ISR,
113 MOS7720_LCR,
114 MOS7720_MCR,
115 MOS7720_LSR,
116 MOS7720_MSR,
117 MOS7720_SPR,
118 MOS7720_DLL,
119 MOS7720_DLM,
120 MOS7720_DPR, /* parallel port regs */
121 MOS7720_DSR,
122 MOS7720_DCR,
123 MOS7720_ECR,
124 MOS7720_SP1_REG, /* device control regs */
125 MOS7720_SP2_REG, /* serial port 2 (7720 only) */
126 MOS7720_PP_REG,
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 */
169 return 0x0000;
171 else if (reg >= MOS7720_DPR) /* parallel port reg (7715 only) */
172 return 0x0100;
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);
194 if (status < 0)
195 dev_err(&usbdev->dev,
196 "mos7720: usb_control_msg() failed: %d\n", status);
197 return 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);
214 u8 *buf;
215 int status;
217 buf = kmalloc(1, GFP_KERNEL);
218 if (!buf)
219 return -ENOMEM;
221 status = usb_control_msg(usbdev, pipe, request, requesttype, value,
222 index, buf, 1, MOS_WDR_TIMEOUT);
223 if (status == 1) {
224 *data = *buf;
225 } else {
226 dev_err(&usbdev->dev,
227 "mos7720: usb_control_msg() failed: %d\n", status);
228 if (status >= 0)
229 status = -EIO;
230 *data = 0;
233 kfree(buf);
235 return 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);
246 return 0;
249 static void destroy_mos_parport(struct kref *kref)
251 struct mos7715_parport *mos_parport =
252 container_of(kref, struct mos7715_parport, ref_count);
254 kfree(mos_parport);
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);
278 return -1;
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);
294 return -1;
297 return 0;
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)
322 goto done;
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);
328 done:
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)
337 return;
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;
346 unsigned char d;
348 if (parport_prologue(pp) < 0)
349 return 0;
350 read_mos_reg(mos_parport->serial, dummy, MOS7720_DPR, &d);
351 parport_epilogue(pp);
352 return d;
355 static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
357 struct mos7715_parport *mos_parport = pp->private_data;
358 __u8 data;
360 if (parport_prologue(pp) < 0)
361 return;
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;
371 __u8 dcr;
373 spin_lock(&release_lock);
374 mos_parport = pp->private_data;
375 if (unlikely(mos_parport == NULL)) {
376 spin_unlock(&release_lock);
377 return 0;
379 dcr = mos_parport->shadowDCR & 0x0f;
380 spin_unlock(&release_lock);
381 return dcr;
384 static unsigned char parport_mos7715_frob_control(struct parport *pp,
385 unsigned char mask,
386 unsigned char val)
388 struct mos7715_parport *mos_parport = pp->private_data;
389 __u8 dcr;
391 mask &= 0x0f;
392 val &= 0x0f;
393 if (parport_prologue(pp) < 0)
394 return 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);
400 return dcr;
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);
412 return 0;
414 status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
415 spin_unlock(&release_lock);
416 return status;
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)
432 return;
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)
445 return;
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);
470 return;
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);
487 return;
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,
497 const void *buffer,
498 size_t len, int flags)
500 int retval;
501 struct mos7715_parport *mos_parport = pp->private_data;
502 int actual_len;
504 if (parport_prologue(pp) < 0)
505 return 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,
510 MOS_WDR_TIMEOUT);
511 parport_epilogue(pp);
512 if (retval) {
513 dev_err(&mos_parport->serial->dev->dev,
514 "mos7720: usb_bulk_msg() failed: %d\n", retval);
515 return 0;
517 return actual_len;
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);
557 if (!mos_parport)
558 return -ENOMEM;
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,
581 PARPORT_DMA_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);
587 return -EIO;
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);
594 return 0;
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)
605 int result;
606 int length;
607 int status = urb->status;
608 struct device *dev = &urb->dev->dev;
609 __u8 *data;
610 __u8 sp1;
611 __u8 sp2;
613 switch (status) {
614 case 0:
615 /* success */
616 break;
617 case -ECONNRESET:
618 case -ENOENT:
619 case -ESHUTDOWN:
620 /* this urb is terminated, clean up */
621 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
622 return;
623 default:
624 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
625 goto exit;
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");
642 return;
645 sp1 = data[3];
646 sp2 = data[2];
648 if ((sp1 | sp2) & 0x01) {
649 /* No Interrupt Pending in both the ports */
650 dev_dbg(dev, "No Interrupt !!!\n");
651 } else {
652 switch (sp1 & 0x0f) {
653 case SERIAL_IIR_RLS:
654 dev_dbg(dev, "Serial Port 1: Receiver status error or address bit detected in 9-bit mode\n");
655 break;
656 case SERIAL_IIR_CTI:
657 dev_dbg(dev, "Serial Port 1: Receiver time out\n");
658 break;
659 case SERIAL_IIR_MS:
660 /* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */
661 break;
664 switch (sp2 & 0x0f) {
665 case SERIAL_IIR_RLS:
666 dev_dbg(dev, "Serial Port 2: Receiver status error or address bit detected in 9-bit mode\n");
667 break;
668 case SERIAL_IIR_CTI:
669 dev_dbg(dev, "Serial Port 2: Receiver time out\n");
670 break;
671 case SERIAL_IIR_MS:
672 /* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */
673 break;
677 exit:
678 result = usb_submit_urb(urb, GFP_ATOMIC);
679 if (result)
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)
690 int result;
691 int length;
692 int status = urb->status;
693 struct device *dev = &urb->dev->dev;
694 __u8 *data;
695 __u8 iir;
697 switch (status) {
698 case 0:
699 /* success */
700 break;
701 case -ECONNRESET:
702 case -ENOENT:
703 case -ESHUTDOWN:
704 case -ENODEV:
705 /* this urb is terminated, clean up */
706 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
707 return;
708 default:
709 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
710 goto exit;
713 length = urb->actual_length;
714 data = urb->transfer_buffer;
716 /* Structure of data from 7715 device:
717 * Byte 1: IIR serial Port
718 * Byte 2: unused
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");
724 return;
727 iir = data[0];
728 if (!(iir & 0x01)) { /* serial port interrupt pending */
729 switch (iir & 0x0f) {
730 case SERIAL_IIR_RLS:
731 dev_dbg(dev, "Serial Port: Receiver status error or address bit detected in 9-bit mode\n");
732 break;
733 case SERIAL_IIR_CTI:
734 dev_dbg(dev, "Serial Port: Receiver time out\n");
735 break;
736 case SERIAL_IIR_MS:
737 /* dev_dbg(dev, "Serial Port: Modem status change\n"); */
738 break;
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))
747 return;
748 atomic_set(&mos_parport->shadowDSR, data[2]);
750 #endif
752 exit:
753 result = usb_submit_urb(urb, GFP_ATOMIC);
754 if (result)
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
761 * bulk in endpoint.
763 static void mos7720_bulk_in_callback(struct urb *urb)
765 int retval;
766 unsigned char *data ;
767 struct usb_serial_port *port;
768 int status = urb->status;
770 if (status) {
771 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
772 return;
775 port = urb->context;
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);
788 if (retval)
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;
803 if (status) {
804 dev_dbg(&urb->dev->dev, "nonzero write bulk status received:%d\n", status);
805 return;
808 mos7720_port = urb->context;
809 if (!mos7720_port) {
810 dev_dbg(&urb->dev->dev, "NULL mos7720_port pointer\n");
811 return ;
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
829 * endpoints.
831 swap(epds->bulk_in[0], epds->bulk_in[1]);
832 swap(epds->bulk_out[0], epds->bulk_out[1]);
834 return 1;
837 return 2;
840 static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
842 struct usb_serial *serial;
843 struct urb *urb;
844 struct moschip_port *mos7720_port;
845 int response;
846 int port_number;
847 __u8 data;
848 int allocated_urbs = 0;
849 int j;
851 serial = port->serial;
853 mos7720_port = usb_get_serial_port_data(port);
854 if (mos7720_port == NULL)
855 return -ENODEV;
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;
864 if (!urb)
865 continue;
867 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
868 GFP_KERNEL);
869 if (!urb->transfer_buffer) {
870 usb_free_urb(mos7720_port->write_urb_pool[j]);
871 mos7720_port->write_urb_pool[j] = NULL;
872 continue;
874 allocated_urbs++;
877 if (!allocated_urbs)
878 return -ENOMEM;
880 /* Initialize MCS7720 -- Write Init values to corresponding Registers
882 * Register Index
883 * 0 : MOS7720_THR/MOS7720_RHR
884 * 1 : MOS7720_IER
885 * 2 : MOS7720_FCR
886 * 3 : MOS7720_LCR
887 * 4 : MOS7720_MCR
888 * 5 : MOS7720_LSR
889 * 6 : MOS7720_MSR
890 * 7 : MOS7720_SPR
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);
928 if (response)
929 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
930 __func__, response);
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;
938 return 0;
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
947 * system,
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;
953 int i;
954 int chars = 0;
955 struct moschip_port *mos7720_port;
957 mos7720_port = usb_get_serial_port_data(port);
958 if (mos7720_port == NULL)
959 return 0;
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);
967 return chars;
970 static void mos7720_close(struct usb_serial_port *port)
972 struct usb_serial *serial;
973 struct moschip_port *mos7720_port;
974 int j;
976 serial = port->serial;
978 mos7720_port = usb_get_serial_port_data(port);
979 if (mos7720_port == NULL)
980 return;
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;
1007 unsigned char 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)
1015 return;
1017 if (break_state == -1)
1018 data = mos7720_port->shadowLCR | UART_LCR_SBC;
1019 else
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;
1038 int room = 0;
1039 int i;
1041 mos7720_port = usb_get_serial_port_data(port);
1042 if (mos7720_port == NULL)
1043 return -ENODEV;
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);
1053 return room;
1056 static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
1057 const unsigned char *data, int count)
1059 int status;
1060 int i;
1061 int bytes_sent = 0;
1062 int transfer_size;
1064 struct moschip_port *mos7720_port;
1065 struct usb_serial *serial;
1066 struct urb *urb;
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)
1073 return -ENODEV;
1075 /* try to find a free urb in the list */
1076 urb = NULL;
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);
1083 break;
1087 if (urb == NULL) {
1088 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
1089 goto exit;
1092 if (urb->transfer_buffer == NULL) {
1093 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1094 GFP_ATOMIC);
1095 if (!urb->transfer_buffer)
1096 goto exit;
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);
1113 if (status) {
1114 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1115 "with status = %d\n", __func__, status);
1116 bytes_sent = status;
1117 goto exit;
1119 bytes_sent = transfer_size;
1121 exit:
1122 return bytes_sent;
1125 static void mos7720_throttle(struct tty_struct *tty)
1127 struct usb_serial_port *port = tty->driver_data;
1128 struct moschip_port *mos7720_port;
1129 int status;
1131 mos7720_port = usb_get_serial_port_data(port);
1133 if (mos7720_port == NULL)
1134 return;
1136 if (!mos7720_port->open) {
1137 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1138 return;
1141 /* if we are implementing XON/XOFF, send the stop character */
1142 if (I_IXOFF(tty)) {
1143 unsigned char stop_char = STOP_CHAR(tty);
1144 status = mos7720_write(tty, port, &stop_char, 1);
1145 if (status <= 0)
1146 return;
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);
1161 int status;
1163 if (mos7720_port == NULL)
1164 return;
1166 if (!mos7720_port->open) {
1167 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1168 return;
1171 /* if we are implementing XON/XOFF, send the start character */
1172 if (I_IXOFF(tty)) {
1173 unsigned char start_char = START_CHAR(tty);
1174 status = mos7720_write(tty, port, &start_char, 1);
1175 if (status <= 0)
1176 return;
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,
1189 unsigned int baud)
1191 struct usb_serial_port *port;
1192 struct usb_serial *serial;
1193 int port_number;
1194 enum mos_regs sp_reg;
1195 if (mos7720_port == NULL)
1196 return -EINVAL;
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;
1221 else
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 /***********************************************
1230 * Set DLL/DLM
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);
1241 return 0;
1244 /* baud rate information */
1245 struct divisor_table_entry {
1246 __u32 baudrate;
1247 __u16 divisor;
1250 /* Define table of divisors for moschip 7720 hardware *
1251 * These assume a 3.6864MHz crystal, the standard /16, and *
1252 * MCR.7 = 0. */
1253 static const struct divisor_table_entry divisor_table[] = {
1254 { 50, 2304},
1255 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
1256 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
1257 { 150, 768},
1258 { 300, 384},
1259 { 600, 192},
1260 { 1200, 96},
1261 { 1800, 64},
1262 { 2400, 48},
1263 { 4800, 24},
1264 { 7200, 16},
1265 { 9600, 12},
1266 { 19200, 6},
1267 { 38400, 3},
1268 { 57600, 2},
1269 { 115200, 1},
1272 /*****************************************************************************
1273 * calc_baud_rate_divisor
1274 * this function calculates the proper baud rate divisor for the specified
1275 * baud rate.
1276 *****************************************************************************/
1277 static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, int *divisor)
1279 int i;
1280 __u16 custom;
1281 __u16 round1;
1282 __u16 round;
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;
1290 return 0;
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));
1303 if (round > 4)
1304 custom++;
1305 *divisor = custom;
1307 dev_dbg(&port->dev, "Baud %d = %d\n", baudrate, custom);
1308 return 0;
1311 dev_dbg(&port->dev, "Baud calculation Failed...\n");
1312 return -EINVAL;
1316 * send_cmd_write_baud_rate
1317 * this function sends the proper command to change the baud rate of the
1318 * specified port.
1320 static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1321 int baudrate)
1323 struct usb_serial_port *port;
1324 struct usb_serial *serial;
1325 int divisor;
1326 int status;
1327 unsigned char number;
1329 if (mos7720_port == NULL)
1330 return -1;
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);
1340 if (status) {
1341 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1342 return status;
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);
1358 return status;
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;
1372 int baud;
1373 unsigned cflag;
1374 __u8 lData;
1375 __u8 lParity;
1376 __u8 lStop;
1377 int status;
1378 int port_number;
1380 if (mos7720_port == NULL)
1381 return ;
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__);
1389 return;
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) {
1400 case CS5:
1401 lData = UART_LCR_WLEN5;
1402 break;
1404 case CS6:
1405 lData = UART_LCR_WLEN6;
1406 break;
1408 case CS7:
1409 lData = UART_LCR_WLEN7;
1410 break;
1411 default:
1412 case CS8:
1413 lData = UART_LCR_WLEN8;
1414 break;
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__);
1422 } else {
1423 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
1424 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
1427 } else {
1428 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
1431 if (cflag & CMSPAR)
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__);
1438 } else {
1439 lStop = 0x00;
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;
1467 if (cflag & CBAUD)
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 */
1474 if (port_number)
1475 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG,
1476 0x01);
1477 else
1478 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG,
1479 0x02);
1481 } else
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);
1489 if (!baud) {
1490 /* pick a default, any default... */
1491 dev_dbg(&port->dev, "Picked default baud...\n");
1492 baud = 9600;
1495 if (baud >= 230400) {
1496 set_higher_rates(mos7720_port, baud);
1497 /* Enable Interrupts */
1498 write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
1499 return;
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
1505 blindly do so */
1506 if (cflag & CBAUD)
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);
1513 if (status)
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)
1526 int status;
1527 struct moschip_port *mos7720_port;
1529 mos7720_port = usb_get_serial_port_data(port);
1531 if (mos7720_port == NULL)
1532 return;
1534 if (!mos7720_port->open) {
1535 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1536 return;
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);
1544 if (status)
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;
1566 int count;
1568 count = mos7720_chars_in_buffer(tty);
1569 if (count == 0) {
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)))
1578 return -EFAULT;
1579 return 0;
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;
1587 unsigned int mcr ;
1588 unsigned int msr ;
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 */
1600 return result;
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);
1608 unsigned int mcr ;
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);
1630 return 0;
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;
1642 ss->irq = 0;
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;
1647 return 0;
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)
1658 return -ENODEV;
1660 switch (cmd) {
1661 case TIOCSERGETLSR:
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;
1673 char data;
1674 u16 product;
1675 int ret_val;
1677 product = le16_to_cpu(serial->dev->descriptor.idProduct);
1678 dev = serial->dev;
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);
1687 if (ret_val < 0)
1688 return ret_val;
1689 #endif
1691 /* start the interrupt urb */
1692 ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
1693 if (ret_val) {
1694 dev_err(&dev->dev, "failed to submit interrupt urb: %d\n",
1695 ret_val);
1698 /* LSR For Port 1 */
1699 read_mos_reg(serial, 0, MOS7720_LSR, &data);
1700 dev_dbg(&dev->dev, "LSR:%x\n", data);
1702 return 0;
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);
1741 #endif
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);
1749 if (!mos7720_port)
1750 return -ENOMEM;
1752 mos7720_port->port = port;
1754 usb_set_serial_port_data(port, mos7720_port);
1756 return 0;
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);
1766 return 0;
1769 static struct usb_serial_driver moschip7720_2port_driver = {
1770 .driver = {
1771 .owner = THIS_MODULE,
1772 .name = "moschip7720",
1774 .description = "Moschip 2 port adapter",
1775 .id_table = id_table,
1776 .num_bulk_in = 2,
1777 .num_bulk_out = 2,
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");