x86/topology: Fix function name in documentation
[cris-mirror.git] / drivers / usb / serial / mos7720.c
blobbd57630e67e2bbd2fcb26ede6b834a6074f88fbf
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 struct urbtracker {
83 struct mos7715_parport *mos_parport;
84 struct list_head urblist_entry;
85 struct kref ref_count;
86 struct urb *urb;
87 struct usb_ctrlrequest *setup;
90 enum mos7715_pp_modes {
91 SPP = 0<<5,
92 PS2 = 1<<5, /* moschip calls this 'NIBBLE' mode */
93 PPF = 2<<5, /* moschip calls this 'CB-FIFO mode */
96 struct mos7715_parport {
97 struct parport *pp; /* back to containing struct */
98 struct kref ref_count; /* to instance of this struct */
99 struct list_head deferred_urbs; /* list deferred async urbs */
100 struct list_head active_urbs; /* list async urbs in flight */
101 spinlock_t listlock; /* protects list access */
102 bool msg_pending; /* usb sync call pending */
103 struct completion syncmsg_compl; /* usb sync call completed */
104 struct tasklet_struct urb_tasklet; /* for sending deferred urbs */
105 struct usb_serial *serial; /* back to containing struct */
106 __u8 shadowECR; /* parallel port regs... */
107 __u8 shadowDCR;
108 atomic_t shadowDSR; /* updated in int-in callback */
111 /* lock guards against dereferencing NULL ptr in parport ops callbacks */
112 static DEFINE_SPINLOCK(release_lock);
114 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
116 static const unsigned int dummy; /* for clarity in register access fns */
118 enum mos_regs {
119 MOS7720_THR, /* serial port regs */
120 MOS7720_RHR,
121 MOS7720_IER,
122 MOS7720_FCR,
123 MOS7720_ISR,
124 MOS7720_LCR,
125 MOS7720_MCR,
126 MOS7720_LSR,
127 MOS7720_MSR,
128 MOS7720_SPR,
129 MOS7720_DLL,
130 MOS7720_DLM,
131 MOS7720_DPR, /* parallel port regs */
132 MOS7720_DSR,
133 MOS7720_DCR,
134 MOS7720_ECR,
135 MOS7720_SP1_REG, /* device control regs */
136 MOS7720_SP2_REG, /* serial port 2 (7720 only) */
137 MOS7720_PP_REG,
138 MOS7720_SP_CONTROL_REG,
142 * Return the correct value for the Windex field of the setup packet
143 * for a control endpoint message. See the 7715 datasheet.
145 static inline __u16 get_reg_index(enum mos_regs reg)
147 static const __u16 mos7715_index_lookup_table[] = {
148 0x00, /* MOS7720_THR */
149 0x00, /* MOS7720_RHR */
150 0x01, /* MOS7720_IER */
151 0x02, /* MOS7720_FCR */
152 0x02, /* MOS7720_ISR */
153 0x03, /* MOS7720_LCR */
154 0x04, /* MOS7720_MCR */
155 0x05, /* MOS7720_LSR */
156 0x06, /* MOS7720_MSR */
157 0x07, /* MOS7720_SPR */
158 0x00, /* MOS7720_DLL */
159 0x01, /* MOS7720_DLM */
160 0x00, /* MOS7720_DPR */
161 0x01, /* MOS7720_DSR */
162 0x02, /* MOS7720_DCR */
163 0x0a, /* MOS7720_ECR */
164 0x01, /* MOS7720_SP1_REG */
165 0x02, /* MOS7720_SP2_REG (7720 only) */
166 0x04, /* MOS7720_PP_REG (7715 only) */
167 0x08, /* MOS7720_SP_CONTROL_REG */
169 return mos7715_index_lookup_table[reg];
173 * Return the correct value for the upper byte of the Wvalue field of
174 * the setup packet for a control endpoint message.
176 static inline __u16 get_reg_value(enum mos_regs reg,
177 unsigned int serial_portnum)
179 if (reg >= MOS7720_SP1_REG) /* control reg */
180 return 0x0000;
182 else if (reg >= MOS7720_DPR) /* parallel port reg (7715 only) */
183 return 0x0100;
185 else /* serial port reg */
186 return (serial_portnum + 2) << 8;
190 * Write data byte to the specified device register. The data is embedded in
191 * the value field of the setup packet. serial_portnum is ignored for registers
192 * not specific to a particular serial port.
194 static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
195 enum mos_regs reg, __u8 data)
197 struct usb_device *usbdev = serial->dev;
198 unsigned int pipe = usb_sndctrlpipe(usbdev, 0);
199 __u8 request = (__u8)0x0e;
200 __u8 requesttype = (__u8)0x40;
201 __u16 index = get_reg_index(reg);
202 __u16 value = get_reg_value(reg, serial_portnum) + data;
203 int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
204 index, NULL, 0, MOS_WDR_TIMEOUT);
205 if (status < 0)
206 dev_err(&usbdev->dev,
207 "mos7720: usb_control_msg() failed: %d\n", status);
208 return status;
212 * Read data byte from the specified device register. The data returned by the
213 * device is embedded in the value field of the setup packet. serial_portnum is
214 * ignored for registers that are not specific to a particular serial port.
216 static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
217 enum mos_regs reg, __u8 *data)
219 struct usb_device *usbdev = serial->dev;
220 unsigned int pipe = usb_rcvctrlpipe(usbdev, 0);
221 __u8 request = (__u8)0x0d;
222 __u8 requesttype = (__u8)0xc0;
223 __u16 index = get_reg_index(reg);
224 __u16 value = get_reg_value(reg, serial_portnum);
225 u8 *buf;
226 int status;
228 buf = kmalloc(1, GFP_KERNEL);
229 if (!buf)
230 return -ENOMEM;
232 status = usb_control_msg(usbdev, pipe, request, requesttype, value,
233 index, buf, 1, MOS_WDR_TIMEOUT);
234 if (status == 1) {
235 *data = *buf;
236 } else {
237 dev_err(&usbdev->dev,
238 "mos7720: usb_control_msg() failed: %d\n", status);
239 if (status >= 0)
240 status = -EIO;
241 *data = 0;
244 kfree(buf);
246 return status;
249 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
251 static inline int mos7715_change_mode(struct mos7715_parport *mos_parport,
252 enum mos7715_pp_modes mode)
254 mos_parport->shadowECR = mode;
255 write_mos_reg(mos_parport->serial, dummy, MOS7720_ECR,
256 mos_parport->shadowECR);
257 return 0;
260 static void destroy_mos_parport(struct kref *kref)
262 struct mos7715_parport *mos_parport =
263 container_of(kref, struct mos7715_parport, ref_count);
265 kfree(mos_parport);
268 static void destroy_urbtracker(struct kref *kref)
270 struct urbtracker *urbtrack =
271 container_of(kref, struct urbtracker, ref_count);
272 struct mos7715_parport *mos_parport = urbtrack->mos_parport;
274 usb_free_urb(urbtrack->urb);
275 kfree(urbtrack->setup);
276 kfree(urbtrack);
277 kref_put(&mos_parport->ref_count, destroy_mos_parport);
281 * This runs as a tasklet when sending an urb in a non-blocking parallel
282 * port callback had to be deferred because the disconnect mutex could not be
283 * obtained at the time.
285 static void send_deferred_urbs(unsigned long _mos_parport)
287 int ret_val;
288 unsigned long flags;
289 struct mos7715_parport *mos_parport = (void *)_mos_parport;
290 struct urbtracker *urbtrack, *tmp;
291 struct list_head *cursor, *next;
292 struct device *dev;
294 /* if release function ran, game over */
295 if (unlikely(mos_parport->serial == NULL))
296 return;
298 dev = &mos_parport->serial->dev->dev;
300 /* try again to get the mutex */
301 if (!mutex_trylock(&mos_parport->serial->disc_mutex)) {
302 dev_dbg(dev, "%s: rescheduling tasklet\n", __func__);
303 tasklet_schedule(&mos_parport->urb_tasklet);
304 return;
307 /* if device disconnected, game over */
308 if (unlikely(mos_parport->serial->disconnected)) {
309 mutex_unlock(&mos_parport->serial->disc_mutex);
310 return;
313 spin_lock_irqsave(&mos_parport->listlock, flags);
314 if (list_empty(&mos_parport->deferred_urbs)) {
315 spin_unlock_irqrestore(&mos_parport->listlock, flags);
316 mutex_unlock(&mos_parport->serial->disc_mutex);
317 dev_dbg(dev, "%s: deferred_urbs list empty\n", __func__);
318 return;
321 /* move contents of deferred_urbs list to active_urbs list and submit */
322 list_for_each_safe(cursor, next, &mos_parport->deferred_urbs)
323 list_move_tail(cursor, &mos_parport->active_urbs);
324 list_for_each_entry_safe(urbtrack, tmp, &mos_parport->active_urbs,
325 urblist_entry) {
326 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
327 dev_dbg(dev, "%s: urb submitted\n", __func__);
328 if (ret_val) {
329 dev_err(dev, "usb_submit_urb() failed: %d\n", ret_val);
330 list_del(&urbtrack->urblist_entry);
331 kref_put(&urbtrack->ref_count, destroy_urbtracker);
334 spin_unlock_irqrestore(&mos_parport->listlock, flags);
335 mutex_unlock(&mos_parport->serial->disc_mutex);
338 /* callback for parallel port control urbs submitted asynchronously */
339 static void async_complete(struct urb *urb)
341 struct urbtracker *urbtrack = urb->context;
342 int status = urb->status;
344 if (unlikely(status))
345 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
347 /* remove the urbtracker from the active_urbs list */
348 spin_lock(&urbtrack->mos_parport->listlock);
349 list_del(&urbtrack->urblist_entry);
350 spin_unlock(&urbtrack->mos_parport->listlock);
351 kref_put(&urbtrack->ref_count, destroy_urbtracker);
354 static int write_parport_reg_nonblock(struct mos7715_parport *mos_parport,
355 enum mos_regs reg, __u8 data)
357 struct urbtracker *urbtrack;
358 int ret_val;
359 unsigned long flags;
360 struct usb_serial *serial = mos_parport->serial;
361 struct usb_device *usbdev = serial->dev;
363 /* create and initialize the control urb and containing urbtracker */
364 urbtrack = kmalloc(sizeof(struct urbtracker), GFP_ATOMIC);
365 if (!urbtrack)
366 return -ENOMEM;
368 kref_get(&mos_parport->ref_count);
369 urbtrack->mos_parport = mos_parport;
370 urbtrack->urb = usb_alloc_urb(0, GFP_ATOMIC);
371 if (!urbtrack->urb) {
372 kfree(urbtrack);
373 return -ENOMEM;
375 urbtrack->setup = kmalloc(sizeof(*urbtrack->setup), GFP_ATOMIC);
376 if (!urbtrack->setup) {
377 usb_free_urb(urbtrack->urb);
378 kfree(urbtrack);
379 return -ENOMEM;
381 urbtrack->setup->bRequestType = (__u8)0x40;
382 urbtrack->setup->bRequest = (__u8)0x0e;
383 urbtrack->setup->wValue = cpu_to_le16(get_reg_value(reg, dummy));
384 urbtrack->setup->wIndex = cpu_to_le16(get_reg_index(reg));
385 urbtrack->setup->wLength = 0;
386 usb_fill_control_urb(urbtrack->urb, usbdev,
387 usb_sndctrlpipe(usbdev, 0),
388 (unsigned char *)urbtrack->setup,
389 NULL, 0, async_complete, urbtrack);
390 kref_init(&urbtrack->ref_count);
391 INIT_LIST_HEAD(&urbtrack->urblist_entry);
394 * get the disconnect mutex, or add tracker to the deferred_urbs list
395 * and schedule a tasklet to try again later
397 if (!mutex_trylock(&serial->disc_mutex)) {
398 spin_lock_irqsave(&mos_parport->listlock, flags);
399 list_add_tail(&urbtrack->urblist_entry,
400 &mos_parport->deferred_urbs);
401 spin_unlock_irqrestore(&mos_parport->listlock, flags);
402 tasklet_schedule(&mos_parport->urb_tasklet);
403 dev_dbg(&usbdev->dev, "tasklet scheduled\n");
404 return 0;
407 /* bail if device disconnected */
408 if (serial->disconnected) {
409 kref_put(&urbtrack->ref_count, destroy_urbtracker);
410 mutex_unlock(&serial->disc_mutex);
411 return -ENODEV;
414 /* add the tracker to the active_urbs list and submit */
415 spin_lock_irqsave(&mos_parport->listlock, flags);
416 list_add_tail(&urbtrack->urblist_entry, &mos_parport->active_urbs);
417 spin_unlock_irqrestore(&mos_parport->listlock, flags);
418 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
419 mutex_unlock(&serial->disc_mutex);
420 if (ret_val) {
421 dev_err(&usbdev->dev,
422 "%s: submit_urb() failed: %d\n", __func__, ret_val);
423 spin_lock_irqsave(&mos_parport->listlock, flags);
424 list_del(&urbtrack->urblist_entry);
425 spin_unlock_irqrestore(&mos_parport->listlock, flags);
426 kref_put(&urbtrack->ref_count, destroy_urbtracker);
427 return ret_val;
429 return 0;
433 * This is the the common top part of all parallel port callback operations that
434 * send synchronous messages to the device. This implements convoluted locking
435 * that avoids two scenarios: (1) a port operation is called after usbserial
436 * has called our release function, at which point struct mos7715_parport has
437 * been destroyed, and (2) the device has been disconnected, but usbserial has
438 * not called the release function yet because someone has a serial port open.
439 * The shared release_lock prevents the first, and the mutex and disconnected
440 * flag maintained by usbserial covers the second. We also use the msg_pending
441 * flag to ensure that all synchronous usb message calls have completed before
442 * our release function can return.
444 static int parport_prologue(struct parport *pp)
446 struct mos7715_parport *mos_parport;
448 spin_lock(&release_lock);
449 mos_parport = pp->private_data;
450 if (unlikely(mos_parport == NULL)) {
451 /* release fn called, port struct destroyed */
452 spin_unlock(&release_lock);
453 return -1;
455 mos_parport->msg_pending = true; /* synch usb call pending */
456 reinit_completion(&mos_parport->syncmsg_compl);
457 spin_unlock(&release_lock);
459 mutex_lock(&mos_parport->serial->disc_mutex);
460 if (mos_parport->serial->disconnected) {
461 /* device disconnected */
462 mutex_unlock(&mos_parport->serial->disc_mutex);
463 mos_parport->msg_pending = false;
464 complete(&mos_parport->syncmsg_compl);
465 return -1;
468 return 0;
472 * This is the common bottom part of all parallel port functions that send
473 * synchronous messages to the device.
475 static inline void parport_epilogue(struct parport *pp)
477 struct mos7715_parport *mos_parport = pp->private_data;
478 mutex_unlock(&mos_parport->serial->disc_mutex);
479 mos_parport->msg_pending = false;
480 complete(&mos_parport->syncmsg_compl);
483 static void parport_mos7715_write_data(struct parport *pp, unsigned char d)
485 struct mos7715_parport *mos_parport = pp->private_data;
487 if (parport_prologue(pp) < 0)
488 return;
489 mos7715_change_mode(mos_parport, SPP);
490 write_mos_reg(mos_parport->serial, dummy, MOS7720_DPR, (__u8)d);
491 parport_epilogue(pp);
494 static unsigned char parport_mos7715_read_data(struct parport *pp)
496 struct mos7715_parport *mos_parport = pp->private_data;
497 unsigned char d;
499 if (parport_prologue(pp) < 0)
500 return 0;
501 read_mos_reg(mos_parport->serial, dummy, MOS7720_DPR, &d);
502 parport_epilogue(pp);
503 return d;
506 static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
508 struct mos7715_parport *mos_parport = pp->private_data;
509 __u8 data;
511 if (parport_prologue(pp) < 0)
512 return;
513 data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
514 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR, data);
515 mos_parport->shadowDCR = data;
516 parport_epilogue(pp);
519 static unsigned char parport_mos7715_read_control(struct parport *pp)
521 struct mos7715_parport *mos_parport;
522 __u8 dcr;
524 spin_lock(&release_lock);
525 mos_parport = pp->private_data;
526 if (unlikely(mos_parport == NULL)) {
527 spin_unlock(&release_lock);
528 return 0;
530 dcr = mos_parport->shadowDCR & 0x0f;
531 spin_unlock(&release_lock);
532 return dcr;
535 static unsigned char parport_mos7715_frob_control(struct parport *pp,
536 unsigned char mask,
537 unsigned char val)
539 struct mos7715_parport *mos_parport = pp->private_data;
540 __u8 dcr;
542 mask &= 0x0f;
543 val &= 0x0f;
544 if (parport_prologue(pp) < 0)
545 return 0;
546 mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
547 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
548 mos_parport->shadowDCR);
549 dcr = mos_parport->shadowDCR & 0x0f;
550 parport_epilogue(pp);
551 return dcr;
554 static unsigned char parport_mos7715_read_status(struct parport *pp)
556 unsigned char status;
557 struct mos7715_parport *mos_parport;
559 spin_lock(&release_lock);
560 mos_parport = pp->private_data;
561 if (unlikely(mos_parport == NULL)) { /* release called */
562 spin_unlock(&release_lock);
563 return 0;
565 status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
566 spin_unlock(&release_lock);
567 return status;
570 static void parport_mos7715_enable_irq(struct parport *pp)
574 static void parport_mos7715_disable_irq(struct parport *pp)
578 static void parport_mos7715_data_forward(struct parport *pp)
580 struct mos7715_parport *mos_parport = pp->private_data;
582 if (parport_prologue(pp) < 0)
583 return;
584 mos7715_change_mode(mos_parport, PS2);
585 mos_parport->shadowDCR &= ~0x20;
586 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
587 mos_parport->shadowDCR);
588 parport_epilogue(pp);
591 static void parport_mos7715_data_reverse(struct parport *pp)
593 struct mos7715_parport *mos_parport = pp->private_data;
595 if (parport_prologue(pp) < 0)
596 return;
597 mos7715_change_mode(mos_parport, PS2);
598 mos_parport->shadowDCR |= 0x20;
599 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
600 mos_parport->shadowDCR);
601 parport_epilogue(pp);
604 static void parport_mos7715_init_state(struct pardevice *dev,
605 struct parport_state *s)
607 s->u.pc.ctr = DCR_INIT_VAL;
608 s->u.pc.ecr = ECR_INIT_VAL;
611 /* N.B. Parport core code requires that this function not block */
612 static void parport_mos7715_save_state(struct parport *pp,
613 struct parport_state *s)
615 struct mos7715_parport *mos_parport;
617 spin_lock(&release_lock);
618 mos_parport = pp->private_data;
619 if (unlikely(mos_parport == NULL)) { /* release called */
620 spin_unlock(&release_lock);
621 return;
623 s->u.pc.ctr = mos_parport->shadowDCR;
624 s->u.pc.ecr = mos_parport->shadowECR;
625 spin_unlock(&release_lock);
628 /* N.B. Parport core code requires that this function not block */
629 static void parport_mos7715_restore_state(struct parport *pp,
630 struct parport_state *s)
632 struct mos7715_parport *mos_parport;
634 spin_lock(&release_lock);
635 mos_parport = pp->private_data;
636 if (unlikely(mos_parport == NULL)) { /* release called */
637 spin_unlock(&release_lock);
638 return;
640 write_parport_reg_nonblock(mos_parport, MOS7720_DCR,
641 mos_parport->shadowDCR);
642 write_parport_reg_nonblock(mos_parport, MOS7720_ECR,
643 mos_parport->shadowECR);
644 spin_unlock(&release_lock);
647 static size_t parport_mos7715_write_compat(struct parport *pp,
648 const void *buffer,
649 size_t len, int flags)
651 int retval;
652 struct mos7715_parport *mos_parport = pp->private_data;
653 int actual_len;
655 if (parport_prologue(pp) < 0)
656 return 0;
657 mos7715_change_mode(mos_parport, PPF);
658 retval = usb_bulk_msg(mos_parport->serial->dev,
659 usb_sndbulkpipe(mos_parport->serial->dev, 2),
660 (void *)buffer, len, &actual_len,
661 MOS_WDR_TIMEOUT);
662 parport_epilogue(pp);
663 if (retval) {
664 dev_err(&mos_parport->serial->dev->dev,
665 "mos7720: usb_bulk_msg() failed: %d\n", retval);
666 return 0;
668 return actual_len;
671 static struct parport_operations parport_mos7715_ops = {
672 .owner = THIS_MODULE,
673 .write_data = parport_mos7715_write_data,
674 .read_data = parport_mos7715_read_data,
676 .write_control = parport_mos7715_write_control,
677 .read_control = parport_mos7715_read_control,
678 .frob_control = parport_mos7715_frob_control,
680 .read_status = parport_mos7715_read_status,
682 .enable_irq = parport_mos7715_enable_irq,
683 .disable_irq = parport_mos7715_disable_irq,
685 .data_forward = parport_mos7715_data_forward,
686 .data_reverse = parport_mos7715_data_reverse,
688 .init_state = parport_mos7715_init_state,
689 .save_state = parport_mos7715_save_state,
690 .restore_state = parport_mos7715_restore_state,
692 .compat_write_data = parport_mos7715_write_compat,
694 .nibble_read_data = parport_ieee1284_read_nibble,
695 .byte_read_data = parport_ieee1284_read_byte,
699 * Allocate and initialize parallel port control struct, initialize
700 * the parallel port hardware device, and register with the parport subsystem.
702 static int mos7715_parport_init(struct usb_serial *serial)
704 struct mos7715_parport *mos_parport;
706 /* allocate and initialize parallel port control struct */
707 mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
708 if (!mos_parport)
709 return -ENOMEM;
711 mos_parport->msg_pending = false;
712 kref_init(&mos_parport->ref_count);
713 spin_lock_init(&mos_parport->listlock);
714 INIT_LIST_HEAD(&mos_parport->active_urbs);
715 INIT_LIST_HEAD(&mos_parport->deferred_urbs);
716 usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
717 mos_parport->serial = serial;
718 tasklet_init(&mos_parport->urb_tasklet, send_deferred_urbs,
719 (unsigned long) mos_parport);
720 init_completion(&mos_parport->syncmsg_compl);
722 /* cycle parallel port reset bit */
723 write_mos_reg(mos_parport->serial, dummy, MOS7720_PP_REG, (__u8)0x80);
724 write_mos_reg(mos_parport->serial, dummy, MOS7720_PP_REG, (__u8)0x00);
726 /* initialize device registers */
727 mos_parport->shadowDCR = DCR_INIT_VAL;
728 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
729 mos_parport->shadowDCR);
730 mos_parport->shadowECR = ECR_INIT_VAL;
731 write_mos_reg(mos_parport->serial, dummy, MOS7720_ECR,
732 mos_parport->shadowECR);
734 /* register with parport core */
735 mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE,
736 PARPORT_DMA_NONE,
737 &parport_mos7715_ops);
738 if (mos_parport->pp == NULL) {
739 dev_err(&serial->interface->dev,
740 "Could not register parport\n");
741 kref_put(&mos_parport->ref_count, destroy_mos_parport);
742 return -EIO;
744 mos_parport->pp->private_data = mos_parport;
745 mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP;
746 mos_parport->pp->dev = &serial->interface->dev;
747 parport_announce_port(mos_parport->pp);
749 return 0;
751 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
754 * mos7720_interrupt_callback
755 * this is the callback function for when we have received data on the
756 * interrupt endpoint.
758 static void mos7720_interrupt_callback(struct urb *urb)
760 int result;
761 int length;
762 int status = urb->status;
763 struct device *dev = &urb->dev->dev;
764 __u8 *data;
765 __u8 sp1;
766 __u8 sp2;
768 switch (status) {
769 case 0:
770 /* success */
771 break;
772 case -ECONNRESET:
773 case -ENOENT:
774 case -ESHUTDOWN:
775 /* this urb is terminated, clean up */
776 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
777 return;
778 default:
779 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
780 goto exit;
783 length = urb->actual_length;
784 data = urb->transfer_buffer;
786 /* Moschip get 4 bytes
787 * Byte 1 IIR Port 1 (port.number is 0)
788 * Byte 2 IIR Port 2 (port.number is 1)
789 * Byte 3 --------------
790 * Byte 4 FIFO status for both */
792 /* the above description is inverted
793 * oneukum 2007-03-14 */
795 if (unlikely(length != 4)) {
796 dev_dbg(dev, "Wrong data !!!\n");
797 return;
800 sp1 = data[3];
801 sp2 = data[2];
803 if ((sp1 | sp2) & 0x01) {
804 /* No Interrupt Pending in both the ports */
805 dev_dbg(dev, "No Interrupt !!!\n");
806 } else {
807 switch (sp1 & 0x0f) {
808 case SERIAL_IIR_RLS:
809 dev_dbg(dev, "Serial Port 1: Receiver status error or address bit detected in 9-bit mode\n");
810 break;
811 case SERIAL_IIR_CTI:
812 dev_dbg(dev, "Serial Port 1: Receiver time out\n");
813 break;
814 case SERIAL_IIR_MS:
815 /* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */
816 break;
819 switch (sp2 & 0x0f) {
820 case SERIAL_IIR_RLS:
821 dev_dbg(dev, "Serial Port 2: Receiver status error or address bit detected in 9-bit mode\n");
822 break;
823 case SERIAL_IIR_CTI:
824 dev_dbg(dev, "Serial Port 2: Receiver time out\n");
825 break;
826 case SERIAL_IIR_MS:
827 /* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */
828 break;
832 exit:
833 result = usb_submit_urb(urb, GFP_ATOMIC);
834 if (result)
835 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
839 * mos7715_interrupt_callback
840 * this is the 7715's callback function for when we have received data on
841 * the interrupt endpoint.
843 static void mos7715_interrupt_callback(struct urb *urb)
845 int result;
846 int length;
847 int status = urb->status;
848 struct device *dev = &urb->dev->dev;
849 __u8 *data;
850 __u8 iir;
852 switch (status) {
853 case 0:
854 /* success */
855 break;
856 case -ECONNRESET:
857 case -ENOENT:
858 case -ESHUTDOWN:
859 case -ENODEV:
860 /* this urb is terminated, clean up */
861 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
862 return;
863 default:
864 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
865 goto exit;
868 length = urb->actual_length;
869 data = urb->transfer_buffer;
871 /* Structure of data from 7715 device:
872 * Byte 1: IIR serial Port
873 * Byte 2: unused
874 * Byte 2: DSR parallel port
875 * Byte 4: FIFO status for both */
877 if (unlikely(length != 4)) {
878 dev_dbg(dev, "Wrong data !!!\n");
879 return;
882 iir = data[0];
883 if (!(iir & 0x01)) { /* serial port interrupt pending */
884 switch (iir & 0x0f) {
885 case SERIAL_IIR_RLS:
886 dev_dbg(dev, "Serial Port: Receiver status error or address bit detected in 9-bit mode\n");
887 break;
888 case SERIAL_IIR_CTI:
889 dev_dbg(dev, "Serial Port: Receiver time out\n");
890 break;
891 case SERIAL_IIR_MS:
892 /* dev_dbg(dev, "Serial Port: Modem status change\n"); */
893 break;
897 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
898 { /* update local copy of DSR reg */
899 struct usb_serial_port *port = urb->context;
900 struct mos7715_parport *mos_parport = port->serial->private;
901 if (unlikely(mos_parport == NULL))
902 return;
903 atomic_set(&mos_parport->shadowDSR, data[2]);
905 #endif
907 exit:
908 result = usb_submit_urb(urb, GFP_ATOMIC);
909 if (result)
910 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
914 * mos7720_bulk_in_callback
915 * this is the callback function for when we have received data on the
916 * bulk in endpoint.
918 static void mos7720_bulk_in_callback(struct urb *urb)
920 int retval;
921 unsigned char *data ;
922 struct usb_serial_port *port;
923 int status = urb->status;
925 if (status) {
926 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
927 return;
930 port = urb->context;
932 dev_dbg(&port->dev, "Entering...%s\n", __func__);
934 data = urb->transfer_buffer;
936 if (urb->actual_length) {
937 tty_insert_flip_string(&port->port, data, urb->actual_length);
938 tty_flip_buffer_push(&port->port);
941 if (port->read_urb->status != -EINPROGRESS) {
942 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
943 if (retval)
944 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
949 * mos7720_bulk_out_data_callback
950 * this is the callback function for when we have finished sending serial
951 * data on the bulk out endpoint.
953 static void mos7720_bulk_out_data_callback(struct urb *urb)
955 struct moschip_port *mos7720_port;
956 int status = urb->status;
958 if (status) {
959 dev_dbg(&urb->dev->dev, "nonzero write bulk status received:%d\n", status);
960 return;
963 mos7720_port = urb->context;
964 if (!mos7720_port) {
965 dev_dbg(&urb->dev->dev, "NULL mos7720_port pointer\n");
966 return ;
969 if (mos7720_port->open)
970 tty_port_tty_wakeup(&mos7720_port->port->port);
973 static int mos77xx_calc_num_ports(struct usb_serial *serial,
974 struct usb_serial_endpoints *epds)
976 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
978 if (product == MOSCHIP_DEVICE_ID_7715) {
980 * The 7715 uses the first bulk in/out endpoint pair for the
981 * parallel port, and the second for the serial port. We swap
982 * the endpoint descriptors here so that the the first and
983 * only registered port structure uses the serial-port
984 * endpoints.
986 swap(epds->bulk_in[0], epds->bulk_in[1]);
987 swap(epds->bulk_out[0], epds->bulk_out[1]);
989 return 1;
992 return 2;
995 static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
997 struct usb_serial *serial;
998 struct urb *urb;
999 struct moschip_port *mos7720_port;
1000 int response;
1001 int port_number;
1002 __u8 data;
1003 int allocated_urbs = 0;
1004 int j;
1006 serial = port->serial;
1008 mos7720_port = usb_get_serial_port_data(port);
1009 if (mos7720_port == NULL)
1010 return -ENODEV;
1012 usb_clear_halt(serial->dev, port->write_urb->pipe);
1013 usb_clear_halt(serial->dev, port->read_urb->pipe);
1015 /* Initialising the write urb pool */
1016 for (j = 0; j < NUM_URBS; ++j) {
1017 urb = usb_alloc_urb(0, GFP_KERNEL);
1018 mos7720_port->write_urb_pool[j] = urb;
1019 if (!urb)
1020 continue;
1022 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1023 GFP_KERNEL);
1024 if (!urb->transfer_buffer) {
1025 usb_free_urb(mos7720_port->write_urb_pool[j]);
1026 mos7720_port->write_urb_pool[j] = NULL;
1027 continue;
1029 allocated_urbs++;
1032 if (!allocated_urbs)
1033 return -ENOMEM;
1035 /* Initialize MCS7720 -- Write Init values to corresponding Registers
1037 * Register Index
1038 * 0 : MOS7720_THR/MOS7720_RHR
1039 * 1 : MOS7720_IER
1040 * 2 : MOS7720_FCR
1041 * 3 : MOS7720_LCR
1042 * 4 : MOS7720_MCR
1043 * 5 : MOS7720_LSR
1044 * 6 : MOS7720_MSR
1045 * 7 : MOS7720_SPR
1047 * 0x08 : SP1/2 Control Reg
1049 port_number = port->port_number;
1050 read_mos_reg(serial, port_number, MOS7720_LSR, &data);
1052 dev_dbg(&port->dev, "SS::%p LSR:%x\n", mos7720_port, data);
1054 write_mos_reg(serial, dummy, MOS7720_SP1_REG, 0x02);
1055 write_mos_reg(serial, dummy, MOS7720_SP2_REG, 0x02);
1057 write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
1058 write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
1060 write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
1061 mos7720_port->shadowLCR = 0x03;
1062 write_mos_reg(serial, port_number, MOS7720_LCR,
1063 mos7720_port->shadowLCR);
1064 mos7720_port->shadowMCR = 0x0b;
1065 write_mos_reg(serial, port_number, MOS7720_MCR,
1066 mos7720_port->shadowMCR);
1068 write_mos_reg(serial, port_number, MOS7720_SP_CONTROL_REG, 0x00);
1069 read_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, &data);
1070 data = data | (port->port_number + 1);
1071 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, data);
1072 mos7720_port->shadowLCR = 0x83;
1073 write_mos_reg(serial, port_number, MOS7720_LCR,
1074 mos7720_port->shadowLCR);
1075 write_mos_reg(serial, port_number, MOS7720_THR, 0x0c);
1076 write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
1077 mos7720_port->shadowLCR = 0x03;
1078 write_mos_reg(serial, port_number, MOS7720_LCR,
1079 mos7720_port->shadowLCR);
1080 write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
1082 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
1083 if (response)
1084 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
1085 __func__, response);
1087 /* initialize our port settings */
1088 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
1090 /* send a open port command */
1091 mos7720_port->open = 1;
1093 return 0;
1097 * mos7720_chars_in_buffer
1098 * this function is called by the tty driver when it wants to know how many
1099 * bytes of data we currently have outstanding in the port (data that has
1100 * been written, but hasn't made it out the port yet)
1101 * If successful, we return the number of bytes left to be written in the
1102 * system,
1103 * Otherwise we return a negative error number.
1105 static int mos7720_chars_in_buffer(struct tty_struct *tty)
1107 struct usb_serial_port *port = tty->driver_data;
1108 int i;
1109 int chars = 0;
1110 struct moschip_port *mos7720_port;
1112 mos7720_port = usb_get_serial_port_data(port);
1113 if (mos7720_port == NULL)
1114 return 0;
1116 for (i = 0; i < NUM_URBS; ++i) {
1117 if (mos7720_port->write_urb_pool[i] &&
1118 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
1119 chars += URB_TRANSFER_BUFFER_SIZE;
1121 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
1122 return chars;
1125 static void mos7720_close(struct usb_serial_port *port)
1127 struct usb_serial *serial;
1128 struct moschip_port *mos7720_port;
1129 int j;
1131 serial = port->serial;
1133 mos7720_port = usb_get_serial_port_data(port);
1134 if (mos7720_port == NULL)
1135 return;
1137 for (j = 0; j < NUM_URBS; ++j)
1138 usb_kill_urb(mos7720_port->write_urb_pool[j]);
1140 /* Freeing Write URBs */
1141 for (j = 0; j < NUM_URBS; ++j) {
1142 if (mos7720_port->write_urb_pool[j]) {
1143 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
1144 usb_free_urb(mos7720_port->write_urb_pool[j]);
1148 /* While closing port, shutdown all bulk read, write *
1149 * and interrupt read if they exists, otherwise nop */
1150 usb_kill_urb(port->write_urb);
1151 usb_kill_urb(port->read_urb);
1153 write_mos_reg(serial, port->port_number, MOS7720_MCR, 0x00);
1154 write_mos_reg(serial, port->port_number, MOS7720_IER, 0x00);
1156 mos7720_port->open = 0;
1159 static void mos7720_break(struct tty_struct *tty, int break_state)
1161 struct usb_serial_port *port = tty->driver_data;
1162 unsigned char data;
1163 struct usb_serial *serial;
1164 struct moschip_port *mos7720_port;
1166 serial = port->serial;
1168 mos7720_port = usb_get_serial_port_data(port);
1169 if (mos7720_port == NULL)
1170 return;
1172 if (break_state == -1)
1173 data = mos7720_port->shadowLCR | UART_LCR_SBC;
1174 else
1175 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
1177 mos7720_port->shadowLCR = data;
1178 write_mos_reg(serial, port->port_number, MOS7720_LCR,
1179 mos7720_port->shadowLCR);
1183 * mos7720_write_room
1184 * this function is called by the tty driver when it wants to know how many
1185 * bytes of data we can accept for a specific port.
1186 * If successful, we return the amount of room that we have for this port
1187 * Otherwise we return a negative error number.
1189 static int mos7720_write_room(struct tty_struct *tty)
1191 struct usb_serial_port *port = tty->driver_data;
1192 struct moschip_port *mos7720_port;
1193 int room = 0;
1194 int i;
1196 mos7720_port = usb_get_serial_port_data(port);
1197 if (mos7720_port == NULL)
1198 return -ENODEV;
1200 /* FIXME: Locking */
1201 for (i = 0; i < NUM_URBS; ++i) {
1202 if (mos7720_port->write_urb_pool[i] &&
1203 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
1204 room += URB_TRANSFER_BUFFER_SIZE;
1207 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
1208 return room;
1211 static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
1212 const unsigned char *data, int count)
1214 int status;
1215 int i;
1216 int bytes_sent = 0;
1217 int transfer_size;
1219 struct moschip_port *mos7720_port;
1220 struct usb_serial *serial;
1221 struct urb *urb;
1222 const unsigned char *current_position = data;
1224 serial = port->serial;
1226 mos7720_port = usb_get_serial_port_data(port);
1227 if (mos7720_port == NULL)
1228 return -ENODEV;
1230 /* try to find a free urb in the list */
1231 urb = NULL;
1233 for (i = 0; i < NUM_URBS; ++i) {
1234 if (mos7720_port->write_urb_pool[i] &&
1235 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
1236 urb = mos7720_port->write_urb_pool[i];
1237 dev_dbg(&port->dev, "URB:%d\n", i);
1238 break;
1242 if (urb == NULL) {
1243 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
1244 goto exit;
1247 if (urb->transfer_buffer == NULL) {
1248 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1249 GFP_ATOMIC);
1250 if (!urb->transfer_buffer)
1251 goto exit;
1253 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1255 memcpy(urb->transfer_buffer, current_position, transfer_size);
1256 usb_serial_debug_data(&port->dev, __func__, transfer_size,
1257 urb->transfer_buffer);
1259 /* fill urb with data and submit */
1260 usb_fill_bulk_urb(urb, serial->dev,
1261 usb_sndbulkpipe(serial->dev,
1262 port->bulk_out_endpointAddress),
1263 urb->transfer_buffer, transfer_size,
1264 mos7720_bulk_out_data_callback, mos7720_port);
1266 /* send it down the pipe */
1267 status = usb_submit_urb(urb, GFP_ATOMIC);
1268 if (status) {
1269 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1270 "with status = %d\n", __func__, status);
1271 bytes_sent = status;
1272 goto exit;
1274 bytes_sent = transfer_size;
1276 exit:
1277 return bytes_sent;
1280 static void mos7720_throttle(struct tty_struct *tty)
1282 struct usb_serial_port *port = tty->driver_data;
1283 struct moschip_port *mos7720_port;
1284 int status;
1286 mos7720_port = usb_get_serial_port_data(port);
1288 if (mos7720_port == NULL)
1289 return;
1291 if (!mos7720_port->open) {
1292 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1293 return;
1296 /* if we are implementing XON/XOFF, send the stop character */
1297 if (I_IXOFF(tty)) {
1298 unsigned char stop_char = STOP_CHAR(tty);
1299 status = mos7720_write(tty, port, &stop_char, 1);
1300 if (status <= 0)
1301 return;
1304 /* if we are implementing RTS/CTS, toggle that line */
1305 if (C_CRTSCTS(tty)) {
1306 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
1307 write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
1308 mos7720_port->shadowMCR);
1312 static void mos7720_unthrottle(struct tty_struct *tty)
1314 struct usb_serial_port *port = tty->driver_data;
1315 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1316 int status;
1318 if (mos7720_port == NULL)
1319 return;
1321 if (!mos7720_port->open) {
1322 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1323 return;
1326 /* if we are implementing XON/XOFF, send the start character */
1327 if (I_IXOFF(tty)) {
1328 unsigned char start_char = START_CHAR(tty);
1329 status = mos7720_write(tty, port, &start_char, 1);
1330 if (status <= 0)
1331 return;
1334 /* if we are implementing RTS/CTS, toggle that line */
1335 if (C_CRTSCTS(tty)) {
1336 mos7720_port->shadowMCR |= UART_MCR_RTS;
1337 write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
1338 mos7720_port->shadowMCR);
1342 /* FIXME: this function does not work */
1343 static int set_higher_rates(struct moschip_port *mos7720_port,
1344 unsigned int baud)
1346 struct usb_serial_port *port;
1347 struct usb_serial *serial;
1348 int port_number;
1349 enum mos_regs sp_reg;
1350 if (mos7720_port == NULL)
1351 return -EINVAL;
1353 port = mos7720_port->port;
1354 serial = port->serial;
1356 /***********************************************
1357 * Init Sequence for higher rates
1358 ***********************************************/
1359 dev_dbg(&port->dev, "Sending Setting Commands ..........\n");
1360 port_number = port->port_number;
1362 write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
1363 write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
1364 write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
1365 mos7720_port->shadowMCR = 0x0b;
1366 write_mos_reg(serial, port_number, MOS7720_MCR,
1367 mos7720_port->shadowMCR);
1368 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, 0x00);
1370 /***********************************************
1371 * Set for higher rates *
1372 ***********************************************/
1373 /* writing baud rate verbatum into uart clock field clearly not right */
1374 if (port_number == 0)
1375 sp_reg = MOS7720_SP1_REG;
1376 else
1377 sp_reg = MOS7720_SP2_REG;
1378 write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
1379 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, 0x03);
1380 mos7720_port->shadowMCR = 0x2b;
1381 write_mos_reg(serial, port_number, MOS7720_MCR,
1382 mos7720_port->shadowMCR);
1384 /***********************************************
1385 * Set DLL/DLM
1386 ***********************************************/
1387 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1388 write_mos_reg(serial, port_number, MOS7720_LCR,
1389 mos7720_port->shadowLCR);
1390 write_mos_reg(serial, port_number, MOS7720_DLL, 0x01);
1391 write_mos_reg(serial, port_number, MOS7720_DLM, 0x00);
1392 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1393 write_mos_reg(serial, port_number, MOS7720_LCR,
1394 mos7720_port->shadowLCR);
1396 return 0;
1399 /* baud rate information */
1400 struct divisor_table_entry {
1401 __u32 baudrate;
1402 __u16 divisor;
1405 /* Define table of divisors for moschip 7720 hardware *
1406 * These assume a 3.6864MHz crystal, the standard /16, and *
1407 * MCR.7 = 0. */
1408 static const struct divisor_table_entry divisor_table[] = {
1409 { 50, 2304},
1410 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
1411 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
1412 { 150, 768},
1413 { 300, 384},
1414 { 600, 192},
1415 { 1200, 96},
1416 { 1800, 64},
1417 { 2400, 48},
1418 { 4800, 24},
1419 { 7200, 16},
1420 { 9600, 12},
1421 { 19200, 6},
1422 { 38400, 3},
1423 { 57600, 2},
1424 { 115200, 1},
1427 /*****************************************************************************
1428 * calc_baud_rate_divisor
1429 * this function calculates the proper baud rate divisor for the specified
1430 * baud rate.
1431 *****************************************************************************/
1432 static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, int *divisor)
1434 int i;
1435 __u16 custom;
1436 __u16 round1;
1437 __u16 round;
1440 dev_dbg(&port->dev, "%s - %d\n", __func__, baudrate);
1442 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
1443 if (divisor_table[i].baudrate == baudrate) {
1444 *divisor = divisor_table[i].divisor;
1445 return 0;
1449 /* After trying for all the standard baud rates *
1450 * Try calculating the divisor for this baud rate */
1451 if (baudrate > 75 && baudrate < 230400) {
1452 /* get the divisor */
1453 custom = (__u16)(230400L / baudrate);
1455 /* Check for round off */
1456 round1 = (__u16)(2304000L / baudrate);
1457 round = (__u16)(round1 - (custom * 10));
1458 if (round > 4)
1459 custom++;
1460 *divisor = custom;
1462 dev_dbg(&port->dev, "Baud %d = %d\n", baudrate, custom);
1463 return 0;
1466 dev_dbg(&port->dev, "Baud calculation Failed...\n");
1467 return -EINVAL;
1471 * send_cmd_write_baud_rate
1472 * this function sends the proper command to change the baud rate of the
1473 * specified port.
1475 static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1476 int baudrate)
1478 struct usb_serial_port *port;
1479 struct usb_serial *serial;
1480 int divisor;
1481 int status;
1482 unsigned char number;
1484 if (mos7720_port == NULL)
1485 return -1;
1487 port = mos7720_port->port;
1488 serial = port->serial;
1490 number = port->port_number;
1491 dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudrate);
1493 /* Calculate the Divisor */
1494 status = calc_baud_rate_divisor(port, baudrate, &divisor);
1495 if (status) {
1496 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1497 return status;
1500 /* Enable access to divisor latch */
1501 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1502 write_mos_reg(serial, number, MOS7720_LCR, mos7720_port->shadowLCR);
1504 /* Write the divisor */
1505 write_mos_reg(serial, number, MOS7720_DLL, (__u8)(divisor & 0xff));
1506 write_mos_reg(serial, number, MOS7720_DLM,
1507 (__u8)((divisor & 0xff00) >> 8));
1509 /* Disable access to divisor latch */
1510 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1511 write_mos_reg(serial, number, MOS7720_LCR, mos7720_port->shadowLCR);
1513 return status;
1517 * change_port_settings
1518 * This routine is called to set the UART on the device to match
1519 * the specified new settings.
1521 static void change_port_settings(struct tty_struct *tty,
1522 struct moschip_port *mos7720_port,
1523 struct ktermios *old_termios)
1525 struct usb_serial_port *port;
1526 struct usb_serial *serial;
1527 int baud;
1528 unsigned cflag;
1529 unsigned iflag;
1530 __u8 mask = 0xff;
1531 __u8 lData;
1532 __u8 lParity;
1533 __u8 lStop;
1534 int status;
1535 int port_number;
1537 if (mos7720_port == NULL)
1538 return ;
1540 port = mos7720_port->port;
1541 serial = port->serial;
1542 port_number = port->port_number;
1544 if (!mos7720_port->open) {
1545 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1546 return;
1549 lData = UART_LCR_WLEN8;
1550 lStop = 0x00; /* 1 stop bit */
1551 lParity = 0x00; /* No parity */
1553 cflag = tty->termios.c_cflag;
1554 iflag = tty->termios.c_iflag;
1556 /* Change the number of bits */
1557 switch (cflag & CSIZE) {
1558 case CS5:
1559 lData = UART_LCR_WLEN5;
1560 mask = 0x1f;
1561 break;
1563 case CS6:
1564 lData = UART_LCR_WLEN6;
1565 mask = 0x3f;
1566 break;
1568 case CS7:
1569 lData = UART_LCR_WLEN7;
1570 mask = 0x7f;
1571 break;
1572 default:
1573 case CS8:
1574 lData = UART_LCR_WLEN8;
1575 break;
1578 /* Change the Parity bit */
1579 if (cflag & PARENB) {
1580 if (cflag & PARODD) {
1581 lParity = UART_LCR_PARITY;
1582 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
1583 } else {
1584 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
1585 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
1588 } else {
1589 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
1592 if (cflag & CMSPAR)
1593 lParity = lParity | 0x20;
1595 /* Change the Stop bit */
1596 if (cflag & CSTOPB) {
1597 lStop = UART_LCR_STOP;
1598 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
1599 } else {
1600 lStop = 0x00;
1601 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
1604 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1605 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1606 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
1608 /* Update the LCR with the correct value */
1609 mos7720_port->shadowLCR &=
1610 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1611 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1614 /* Disable Interrupts */
1615 write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
1616 write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
1617 write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
1619 /* Send the updated LCR value to the mos7720 */
1620 write_mos_reg(serial, port_number, MOS7720_LCR,
1621 mos7720_port->shadowLCR);
1622 mos7720_port->shadowMCR = 0x0b;
1623 write_mos_reg(serial, port_number, MOS7720_MCR,
1624 mos7720_port->shadowMCR);
1626 /* set up the MCR register and send it to the mos7720 */
1627 mos7720_port->shadowMCR = UART_MCR_OUT2;
1628 if (cflag & CBAUD)
1629 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1631 if (cflag & CRTSCTS) {
1632 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
1633 /* To set hardware flow control to the specified *
1634 * serial port, in SP1/2_CONTROL_REG */
1635 if (port_number)
1636 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG,
1637 0x01);
1638 else
1639 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG,
1640 0x02);
1642 } else
1643 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1645 write_mos_reg(serial, port_number, MOS7720_MCR,
1646 mos7720_port->shadowMCR);
1648 /* Determine divisor based on baud rate */
1649 baud = tty_get_baud_rate(tty);
1650 if (!baud) {
1651 /* pick a default, any default... */
1652 dev_dbg(&port->dev, "Picked default baud...\n");
1653 baud = 9600;
1656 if (baud >= 230400) {
1657 set_higher_rates(mos7720_port, baud);
1658 /* Enable Interrupts */
1659 write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
1660 return;
1663 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
1664 status = send_cmd_write_baud_rate(mos7720_port, baud);
1665 /* FIXME: needs to write actual resulting baud back not just
1666 blindly do so */
1667 if (cflag & CBAUD)
1668 tty_encode_baud_rate(tty, baud, baud);
1669 /* Enable Interrupts */
1670 write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
1672 if (port->read_urb->status != -EINPROGRESS) {
1673 status = usb_submit_urb(port->read_urb, GFP_KERNEL);
1674 if (status)
1675 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
1680 * mos7720_set_termios
1681 * this function is called by the tty driver when it wants to change the
1682 * termios structure.
1684 static void mos7720_set_termios(struct tty_struct *tty,
1685 struct usb_serial_port *port, struct ktermios *old_termios)
1687 int status;
1688 struct usb_serial *serial;
1689 struct moschip_port *mos7720_port;
1691 serial = port->serial;
1693 mos7720_port = usb_get_serial_port_data(port);
1695 if (mos7720_port == NULL)
1696 return;
1698 if (!mos7720_port->open) {
1699 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1700 return;
1703 /* change the port settings to the new ones specified */
1704 change_port_settings(tty, mos7720_port, old_termios);
1706 if (port->read_urb->status != -EINPROGRESS) {
1707 status = usb_submit_urb(port->read_urb, GFP_KERNEL);
1708 if (status)
1709 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
1714 * get_lsr_info - get line status register info
1716 * Purpose: Let user call ioctl() to get info when the UART physically
1717 * is emptied. On bus types like RS485, the transmitter must
1718 * release the bus after transmitting. This must be done when
1719 * the transmit shift register is empty, not be done when the
1720 * transmit holding register is empty. This functionality
1721 * allows an RS485 driver to be written in user space.
1723 static int get_lsr_info(struct tty_struct *tty,
1724 struct moschip_port *mos7720_port, unsigned int __user *value)
1726 struct usb_serial_port *port = tty->driver_data;
1727 unsigned int result = 0;
1728 unsigned char data = 0;
1729 int port_number = port->port_number;
1730 int count;
1732 count = mos7720_chars_in_buffer(tty);
1733 if (count == 0) {
1734 read_mos_reg(port->serial, port_number, MOS7720_LSR, &data);
1735 if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1736 == (UART_LSR_TEMT | UART_LSR_THRE)) {
1737 dev_dbg(&port->dev, "%s -- Empty\n", __func__);
1738 result = TIOCSER_TEMT;
1741 if (copy_to_user(value, &result, sizeof(int)))
1742 return -EFAULT;
1743 return 0;
1746 static int mos7720_tiocmget(struct tty_struct *tty)
1748 struct usb_serial_port *port = tty->driver_data;
1749 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1750 unsigned int result = 0;
1751 unsigned int mcr ;
1752 unsigned int msr ;
1754 mcr = mos7720_port->shadowMCR;
1755 msr = mos7720_port->shadowMSR;
1757 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1758 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1759 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1760 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
1761 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1762 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
1764 return result;
1767 static int mos7720_tiocmset(struct tty_struct *tty,
1768 unsigned int set, unsigned int clear)
1770 struct usb_serial_port *port = tty->driver_data;
1771 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1772 unsigned int mcr ;
1774 mcr = mos7720_port->shadowMCR;
1776 if (set & TIOCM_RTS)
1777 mcr |= UART_MCR_RTS;
1778 if (set & TIOCM_DTR)
1779 mcr |= UART_MCR_DTR;
1780 if (set & TIOCM_LOOP)
1781 mcr |= UART_MCR_LOOP;
1783 if (clear & TIOCM_RTS)
1784 mcr &= ~UART_MCR_RTS;
1785 if (clear & TIOCM_DTR)
1786 mcr &= ~UART_MCR_DTR;
1787 if (clear & TIOCM_LOOP)
1788 mcr &= ~UART_MCR_LOOP;
1790 mos7720_port->shadowMCR = mcr;
1791 write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
1792 mos7720_port->shadowMCR);
1794 return 0;
1797 static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1798 unsigned int __user *value)
1800 unsigned int mcr;
1801 unsigned int arg;
1803 struct usb_serial_port *port;
1805 if (mos7720_port == NULL)
1806 return -1;
1808 port = (struct usb_serial_port *)mos7720_port->port;
1809 mcr = mos7720_port->shadowMCR;
1811 if (copy_from_user(&arg, value, sizeof(int)))
1812 return -EFAULT;
1814 switch (cmd) {
1815 case TIOCMBIS:
1816 if (arg & TIOCM_RTS)
1817 mcr |= UART_MCR_RTS;
1818 if (arg & TIOCM_DTR)
1819 mcr |= UART_MCR_RTS;
1820 if (arg & TIOCM_LOOP)
1821 mcr |= UART_MCR_LOOP;
1822 break;
1824 case TIOCMBIC:
1825 if (arg & TIOCM_RTS)
1826 mcr &= ~UART_MCR_RTS;
1827 if (arg & TIOCM_DTR)
1828 mcr &= ~UART_MCR_RTS;
1829 if (arg & TIOCM_LOOP)
1830 mcr &= ~UART_MCR_LOOP;
1831 break;
1835 mos7720_port->shadowMCR = mcr;
1836 write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
1837 mos7720_port->shadowMCR);
1839 return 0;
1842 static int get_serial_info(struct moschip_port *mos7720_port,
1843 struct serial_struct __user *retinfo)
1845 struct serial_struct tmp;
1847 memset(&tmp, 0, sizeof(tmp));
1849 tmp.type = PORT_16550A;
1850 tmp.line = mos7720_port->port->minor;
1851 tmp.port = mos7720_port->port->port_number;
1852 tmp.irq = 0;
1853 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1854 tmp.baud_base = 9600;
1855 tmp.close_delay = 5*HZ;
1856 tmp.closing_wait = 30*HZ;
1858 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1859 return -EFAULT;
1860 return 0;
1863 static int mos7720_ioctl(struct tty_struct *tty,
1864 unsigned int cmd, unsigned long arg)
1866 struct usb_serial_port *port = tty->driver_data;
1867 struct moschip_port *mos7720_port;
1869 mos7720_port = usb_get_serial_port_data(port);
1870 if (mos7720_port == NULL)
1871 return -ENODEV;
1873 switch (cmd) {
1874 case TIOCSERGETLSR:
1875 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
1876 return get_lsr_info(tty, mos7720_port,
1877 (unsigned int __user *)arg);
1879 /* FIXME: These should be using the mode methods */
1880 case TIOCMBIS:
1881 case TIOCMBIC:
1882 dev_dbg(&port->dev, "%s TIOCMSET/TIOCMBIC/TIOCMSET\n", __func__);
1883 return set_modem_info(mos7720_port, cmd,
1884 (unsigned int __user *)arg);
1886 case TIOCGSERIAL:
1887 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
1888 return get_serial_info(mos7720_port,
1889 (struct serial_struct __user *)arg);
1892 return -ENOIOCTLCMD;
1895 static int mos7720_startup(struct usb_serial *serial)
1897 struct usb_device *dev;
1898 char data;
1899 u16 product;
1900 int ret_val;
1902 product = le16_to_cpu(serial->dev->descriptor.idProduct);
1903 dev = serial->dev;
1905 /* setting configuration feature to one */
1906 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1907 (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5000);
1909 if (product == MOSCHIP_DEVICE_ID_7715) {
1910 struct urb *urb = serial->port[0]->interrupt_in_urb;
1912 urb->complete = mos7715_interrupt_callback;
1914 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1915 ret_val = mos7715_parport_init(serial);
1916 if (ret_val < 0)
1917 return ret_val;
1918 #endif
1920 /* start the interrupt urb */
1921 ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
1922 if (ret_val) {
1923 dev_err(&dev->dev, "failed to submit interrupt urb: %d\n",
1924 ret_val);
1927 /* LSR For Port 1 */
1928 read_mos_reg(serial, 0, MOS7720_LSR, &data);
1929 dev_dbg(&dev->dev, "LSR:%x\n", data);
1931 return 0;
1934 static void mos7720_release(struct usb_serial *serial)
1936 usb_kill_urb(serial->port[0]->interrupt_in_urb);
1938 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1939 /* close the parallel port */
1941 if (le16_to_cpu(serial->dev->descriptor.idProduct)
1942 == MOSCHIP_DEVICE_ID_7715) {
1943 struct urbtracker *urbtrack;
1944 unsigned long flags;
1945 struct mos7715_parport *mos_parport =
1946 usb_get_serial_data(serial);
1948 /* prevent NULL ptr dereference in port callbacks */
1949 spin_lock(&release_lock);
1950 mos_parport->pp->private_data = NULL;
1951 spin_unlock(&release_lock);
1953 /* wait for synchronous usb calls to return */
1954 if (mos_parport->msg_pending)
1955 wait_for_completion_timeout(&mos_parport->syncmsg_compl,
1956 msecs_to_jiffies(MOS_WDR_TIMEOUT));
1958 parport_remove_port(mos_parport->pp);
1959 usb_set_serial_data(serial, NULL);
1960 mos_parport->serial = NULL;
1962 /* if tasklet currently scheduled, wait for it to complete */
1963 tasklet_kill(&mos_parport->urb_tasklet);
1965 /* unlink any urbs sent by the tasklet */
1966 spin_lock_irqsave(&mos_parport->listlock, flags);
1967 list_for_each_entry(urbtrack,
1968 &mos_parport->active_urbs,
1969 urblist_entry)
1970 usb_unlink_urb(urbtrack->urb);
1971 spin_unlock_irqrestore(&mos_parport->listlock, flags);
1972 parport_del_port(mos_parport->pp);
1974 kref_put(&mos_parport->ref_count, destroy_mos_parport);
1976 #endif
1979 static int mos7720_port_probe(struct usb_serial_port *port)
1981 struct moschip_port *mos7720_port;
1983 mos7720_port = kzalloc(sizeof(*mos7720_port), GFP_KERNEL);
1984 if (!mos7720_port)
1985 return -ENOMEM;
1987 mos7720_port->port = port;
1989 usb_set_serial_port_data(port, mos7720_port);
1991 return 0;
1994 static int mos7720_port_remove(struct usb_serial_port *port)
1996 struct moschip_port *mos7720_port;
1998 mos7720_port = usb_get_serial_port_data(port);
1999 kfree(mos7720_port);
2001 return 0;
2004 static struct usb_serial_driver moschip7720_2port_driver = {
2005 .driver = {
2006 .owner = THIS_MODULE,
2007 .name = "moschip7720",
2009 .description = "Moschip 2 port adapter",
2010 .id_table = id_table,
2011 .num_bulk_in = 2,
2012 .num_bulk_out = 2,
2013 .num_interrupt_in = 1,
2014 .calc_num_ports = mos77xx_calc_num_ports,
2015 .open = mos7720_open,
2016 .close = mos7720_close,
2017 .throttle = mos7720_throttle,
2018 .unthrottle = mos7720_unthrottle,
2019 .attach = mos7720_startup,
2020 .release = mos7720_release,
2021 .port_probe = mos7720_port_probe,
2022 .port_remove = mos7720_port_remove,
2023 .ioctl = mos7720_ioctl,
2024 .tiocmget = mos7720_tiocmget,
2025 .tiocmset = mos7720_tiocmset,
2026 .set_termios = mos7720_set_termios,
2027 .write = mos7720_write,
2028 .write_room = mos7720_write_room,
2029 .chars_in_buffer = mos7720_chars_in_buffer,
2030 .break_ctl = mos7720_break,
2031 .read_bulk_callback = mos7720_bulk_in_callback,
2032 .read_int_callback = mos7720_interrupt_callback,
2035 static struct usb_serial_driver * const serial_drivers[] = {
2036 &moschip7720_2port_driver, NULL
2039 module_usb_serial_driver(serial_drivers, id_table);
2041 MODULE_AUTHOR(DRIVER_AUTHOR);
2042 MODULE_DESCRIPTION(DRIVER_DESC);
2043 MODULE_LICENSE("GPL v2");