blob: e0ebec3b5d6ae077aa4ac0347e8a33470f8dc6c3 [file] [log] [blame]
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001/*
2 * mos7720.c
3 * Controls the Moschip 7720 usb to dual port serial convertor
4 *
5 * Copyright 2006 Moschip Semiconductor Tech. Ltd.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, version 2 of the License.
10 *
11 * Developed by:
Greg Kroah-Hartman50d2dc72007-06-25 01:08:01 -070012 * Vijaya Kumar <vijaykumar.gn@gmail.com>
13 * Ajay Kumar <naanuajay@yahoo.com>
14 * Gurudeva <ngurudeva@yahoo.com>
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070015 *
16 * Cleaned up from the original by:
17 * Greg Kroah-Hartman <gregkh@suse.de>
18 *
19 * Originally based on drivers/usb/serial/io_edgeport.c which is:
20 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
21 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
22 */
23#include <linux/kernel.h>
24#include <linux/errno.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/tty.h>
28#include <linux/tty_driver.h>
29#include <linux/tty_flip.h>
30#include <linux/module.h>
31#include <linux/spinlock.h>
32#include <linux/serial.h>
33#include <linux/serial_reg.h>
34#include <linux/usb.h>
35#include <linux/usb/serial.h>
Alan Cox4da1a172008-07-22 11:16:21 +010036#include <linux/uaccess.h>
Mike Dunnb69578d2010-04-15 17:01:33 -040037#include <linux/parport.h>
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070038
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070039#define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
40#define DRIVER_DESC "Moschip USB Serial Driver"
41
42/* default urb timeout */
43#define MOS_WDR_TIMEOUT (HZ * 5)
44
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070045#define MOS_MAX_PORT 0x02
46#define MOS_WRITE 0x0E
47#define MOS_READ 0x0D
48
49/* Interrupt Rotinue Defines */
50#define SERIAL_IIR_RLS 0x06
51#define SERIAL_IIR_RDA 0x04
52#define SERIAL_IIR_CTI 0x0c
53#define SERIAL_IIR_THR 0x02
54#define SERIAL_IIR_MS 0x00
55
56#define NUM_URBS 16 /* URB Count */
57#define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
58
Mike Dunnb69578d2010-04-15 17:01:33 -040059/* This structure holds all of the local serial port information */
Alan Cox4da1a172008-07-22 11:16:21 +010060struct moschip_port {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070061 __u8 shadowLCR; /* last LCR value received */
62 __u8 shadowMCR; /* last MCR value received */
63 __u8 shadowMSR; /* last MSR value received */
64 char open;
65 struct async_icount icount;
66 struct usb_serial_port *port; /* loop back to the owner */
67 struct urb *write_urb_pool[NUM_URBS];
68};
69
Mike Dunnfb088e32010-01-26 12:12:12 -050070static struct usb_serial_driver moschip7720_2port_driver;
71
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070072#define USB_VENDOR_ID_MOSCHIP 0x9710
73#define MOSCHIP_DEVICE_ID_7720 0x7720
74#define MOSCHIP_DEVICE_ID_7715 0x7715
75
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -070076static const struct usb_device_id id_table[] = {
Alan Cox4da1a172008-07-22 11:16:21 +010077 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
Mike Dunnfb088e32010-01-26 12:12:12 -050078 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) },
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070079 { } /* terminating entry */
80};
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -070081MODULE_DEVICE_TABLE(usb, id_table);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070082
Mike Dunnb69578d2010-04-15 17:01:33 -040083#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
84
85/* initial values for parport regs */
86#define DCR_INIT_VAL 0x0c /* SLCTIN, nINIT */
87#define ECR_INIT_VAL 0x00 /* SPP mode */
88
89struct urbtracker {
90 struct mos7715_parport *mos_parport;
91 struct list_head urblist_entry;
92 struct kref ref_count;
93 struct urb *urb;
94};
95
96enum mos7715_pp_modes {
97 SPP = 0<<5,
98 PS2 = 1<<5, /* moschip calls this 'NIBBLE' mode */
99 PPF = 2<<5, /* moschip calls this 'CB-FIFO mode */
100};
101
102struct mos7715_parport {
103 struct parport *pp; /* back to containing struct */
104 struct kref ref_count; /* to instance of this struct */
105 struct list_head deferred_urbs; /* list deferred async urbs */
106 struct list_head active_urbs; /* list async urbs in flight */
107 spinlock_t listlock; /* protects list access */
108 bool msg_pending; /* usb sync call pending */
109 struct completion syncmsg_compl; /* usb sync call completed */
110 struct tasklet_struct urb_tasklet; /* for sending deferred urbs */
111 struct usb_serial *serial; /* back to containing struct */
112 __u8 shadowECR; /* parallel port regs... */
113 __u8 shadowDCR;
114 atomic_t shadowDSR; /* updated in int-in callback */
115};
116
117/* lock guards against dereferencing NULL ptr in parport ops callbacks */
118static DEFINE_SPINLOCK(release_lock);
119
Mike Dunn63b91762010-04-15 17:02:09 -0400120#endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
121
122static const unsigned int dummy; /* for clarity in register access fns */
123
Mike Dunnb69578d2010-04-15 17:01:33 -0400124enum mos_regs {
125 THR, /* serial port regs */
126 RHR,
127 IER,
128 FCR,
129 ISR,
130 LCR,
131 MCR,
132 LSR,
133 MSR,
134 SPR,
135 DLL,
136 DLM,
137 DPR, /* parallel port regs */
138 DSR,
139 DCR,
140 ECR,
141 SP1_REG, /* device control regs */
142 SP2_REG, /* serial port 2 (7720 only) */
143 PP_REG,
144 SP_CONTROL_REG,
145};
146
147/*
148 * Return the correct value for the Windex field of the setup packet
149 * for a control endpoint message. See the 7715 datasheet.
150 */
151static inline __u16 get_reg_index(enum mos_regs reg)
152{
153 static const __u16 mos7715_index_lookup_table[] = {
154 0x00, /* THR */
155 0x00, /* RHR */
156 0x01, /* IER */
157 0x02, /* FCR */
158 0x02, /* ISR */
159 0x03, /* LCR */
160 0x04, /* MCR */
161 0x05, /* LSR */
162 0x06, /* MSR */
163 0x07, /* SPR */
164 0x00, /* DLL */
165 0x01, /* DLM */
166 0x00, /* DPR */
167 0x01, /* DSR */
168 0x02, /* DCR */
169 0x0a, /* ECR */
170 0x01, /* SP1_REG */
171 0x02, /* SP2_REG (7720 only) */
172 0x04, /* PP_REG (7715 only) */
173 0x08, /* SP_CONTROL_REG */
174 };
175 return mos7715_index_lookup_table[reg];
176}
177
178/*
179 * Return the correct value for the upper byte of the Wvalue field of
180 * the setup packet for a control endpoint message.
181 */
Mike Dunn63b91762010-04-15 17:02:09 -0400182static inline __u16 get_reg_value(enum mos_regs reg,
183 unsigned int serial_portnum)
Mike Dunnb69578d2010-04-15 17:01:33 -0400184{
185 if (reg >= SP1_REG) /* control reg */
186 return 0x0000;
Mike Dunn63b91762010-04-15 17:02:09 -0400187
188 else if (reg >= DPR) /* parallel port reg (7715 only) */
Mike Dunnb69578d2010-04-15 17:01:33 -0400189 return 0x0100;
Mike Dunn63b91762010-04-15 17:02:09 -0400190
191 else /* serial port reg */
192 return (serial_portnum + 2) << 8;
Mike Dunnb69578d2010-04-15 17:01:33 -0400193}
194
195/*
196 * Write data byte to the specified device register. The data is embedded in
Mike Dunn63b91762010-04-15 17:02:09 -0400197 * the value field of the setup packet. serial_portnum is ignored for registers
198 * not specific to a particular serial port.
Mike Dunnb69578d2010-04-15 17:01:33 -0400199 */
Mike Dunn63b91762010-04-15 17:02:09 -0400200static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
201 enum mos_regs reg, __u8 data)
Mike Dunnb69578d2010-04-15 17:01:33 -0400202{
Mike Dunnb69578d2010-04-15 17:01:33 -0400203 struct usb_device *usbdev = serial->dev;
204 unsigned int pipe = usb_sndctrlpipe(usbdev, 0);
205 __u8 request = (__u8)0x0e;
206 __u8 requesttype = (__u8)0x40;
Mike Dunnb69578d2010-04-15 17:01:33 -0400207 __u16 index = get_reg_index(reg);
Mike Dunn63b91762010-04-15 17:02:09 -0400208 __u16 value = get_reg_value(reg, serial_portnum) + data;
209 int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
210 index, NULL, 0, MOS_WDR_TIMEOUT);
Mike Dunnb69578d2010-04-15 17:01:33 -0400211 if (status < 0)
212 dev_err(&usbdev->dev,
213 "mos7720: usb_control_msg() failed: %d", status);
214 return status;
215}
216
217/*
218 * Read data byte from the specified device register. The data returned by the
Mike Dunn63b91762010-04-15 17:02:09 -0400219 * device is embedded in the value field of the setup packet. serial_portnum is
220 * ignored for registers that are not specific to a particular serial port.
Mike Dunnb69578d2010-04-15 17:01:33 -0400221 */
Mike Dunn63b91762010-04-15 17:02:09 -0400222static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
223 enum mos_regs reg, __u8 *data)
Mike Dunnb69578d2010-04-15 17:01:33 -0400224{
Mike Dunn63b91762010-04-15 17:02:09 -0400225 struct usb_device *usbdev = serial->dev;
Mike Dunnb69578d2010-04-15 17:01:33 -0400226 unsigned int pipe = usb_rcvctrlpipe(usbdev, 0);
227 __u8 request = (__u8)0x0d;
228 __u8 requesttype = (__u8)0xc0;
Mike Dunnb69578d2010-04-15 17:01:33 -0400229 __u16 index = get_reg_index(reg);
Mike Dunn63b91762010-04-15 17:02:09 -0400230 __u16 value = get_reg_value(reg, serial_portnum);
Mike Dunnb69578d2010-04-15 17:01:33 -0400231 int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
Mike Dunn63b91762010-04-15 17:02:09 -0400232 index, data, 1, MOS_WDR_TIMEOUT);
Mike Dunnb69578d2010-04-15 17:01:33 -0400233 if (status < 0)
234 dev_err(&usbdev->dev,
235 "mos7720: usb_control_msg() failed: %d", status);
236 return status;
237}
238
Mike Dunn63b91762010-04-15 17:02:09 -0400239#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
240
Mike Dunnb69578d2010-04-15 17:01:33 -0400241static inline int mos7715_change_mode(struct mos7715_parport *mos_parport,
242 enum mos7715_pp_modes mode)
243{
244 mos_parport->shadowECR = mode;
Mike Dunn63b91762010-04-15 17:02:09 -0400245 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400246 return 0;
247}
248
249static void destroy_mos_parport(struct kref *kref)
250{
251 struct mos7715_parport *mos_parport =
252 container_of(kref, struct mos7715_parport, ref_count);
253
Mike Dunnb69578d2010-04-15 17:01:33 -0400254 kfree(mos_parport);
255}
256
257static void destroy_urbtracker(struct kref *kref)
258{
259 struct urbtracker *urbtrack =
260 container_of(kref, struct urbtracker, ref_count);
261 struct mos7715_parport *mos_parport = urbtrack->mos_parport;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700262
Mike Dunnb69578d2010-04-15 17:01:33 -0400263 usb_free_urb(urbtrack->urb);
264 kfree(urbtrack);
265 kref_put(&mos_parport->ref_count, destroy_mos_parport);
266}
267
268/*
269 * This runs as a tasklet when sending an urb in a non-blocking parallel
270 * port callback had to be deferred because the disconnect mutex could not be
271 * obtained at the time.
272 */
273static void send_deferred_urbs(unsigned long _mos_parport)
274{
275 int ret_val;
276 unsigned long flags;
277 struct mos7715_parport *mos_parport = (void *)_mos_parport;
Wei Yongjun67990472012-08-21 11:28:45 +0800278 struct urbtracker *urbtrack, *tmp;
Mike Dunnb69578d2010-04-15 17:01:33 -0400279 struct list_head *cursor, *next;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700280 struct device *dev;
Mike Dunnb69578d2010-04-15 17:01:33 -0400281
Mike Dunnb69578d2010-04-15 17:01:33 -0400282 /* if release function ran, game over */
283 if (unlikely(mos_parport->serial == NULL))
284 return;
285
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700286 dev = &mos_parport->serial->dev->dev;
287
Mike Dunnb69578d2010-04-15 17:01:33 -0400288 /* try again to get the mutex */
289 if (!mutex_trylock(&mos_parport->serial->disc_mutex)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700290 dev_dbg(dev, "%s: rescheduling tasklet\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400291 tasklet_schedule(&mos_parport->urb_tasklet);
292 return;
293 }
294
295 /* if device disconnected, game over */
296 if (unlikely(mos_parport->serial->disconnected)) {
297 mutex_unlock(&mos_parport->serial->disc_mutex);
298 return;
299 }
300
301 spin_lock_irqsave(&mos_parport->listlock, flags);
302 if (list_empty(&mos_parport->deferred_urbs)) {
303 spin_unlock_irqrestore(&mos_parport->listlock, flags);
304 mutex_unlock(&mos_parport->serial->disc_mutex);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700305 dev_dbg(dev, "%s: deferred_urbs list empty\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400306 return;
307 }
308
309 /* move contents of deferred_urbs list to active_urbs list and submit */
310 list_for_each_safe(cursor, next, &mos_parport->deferred_urbs)
311 list_move_tail(cursor, &mos_parport->active_urbs);
Wei Yongjun67990472012-08-21 11:28:45 +0800312 list_for_each_entry_safe(urbtrack, tmp, &mos_parport->active_urbs,
Mike Dunnb69578d2010-04-15 17:01:33 -0400313 urblist_entry) {
314 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700315 dev_dbg(dev, "%s: urb submitted\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400316 if (ret_val) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700317 dev_err(dev, "usb_submit_urb() failed: %d\n", ret_val);
Mike Dunnb69578d2010-04-15 17:01:33 -0400318 list_del(&urbtrack->urblist_entry);
319 kref_put(&urbtrack->ref_count, destroy_urbtracker);
320 }
321 }
322 spin_unlock_irqrestore(&mos_parport->listlock, flags);
323 mutex_unlock(&mos_parport->serial->disc_mutex);
324}
325
326/* callback for parallel port control urbs submitted asynchronously */
327static void async_complete(struct urb *urb)
328{
329 struct urbtracker *urbtrack = urb->context;
330 int status = urb->status;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700331
Mike Dunnb69578d2010-04-15 17:01:33 -0400332 if (unlikely(status))
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700333 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
Mike Dunnb69578d2010-04-15 17:01:33 -0400334
335 /* remove the urbtracker from the active_urbs list */
336 spin_lock(&urbtrack->mos_parport->listlock);
337 list_del(&urbtrack->urblist_entry);
338 spin_unlock(&urbtrack->mos_parport->listlock);
339 kref_put(&urbtrack->ref_count, destroy_urbtracker);
340}
341
342static int write_parport_reg_nonblock(struct mos7715_parport *mos_parport,
343 enum mos_regs reg, __u8 data)
344{
345 struct urbtracker *urbtrack;
346 int ret_val;
347 unsigned long flags;
348 struct usb_ctrlrequest setup;
349 struct usb_serial *serial = mos_parport->serial;
350 struct usb_device *usbdev = serial->dev;
Mike Dunnb69578d2010-04-15 17:01:33 -0400351
352 /* create and initialize the control urb and containing urbtracker */
353 urbtrack = kmalloc(sizeof(struct urbtracker), GFP_ATOMIC);
354 if (urbtrack == NULL) {
355 dev_err(&usbdev->dev, "out of memory");
356 return -ENOMEM;
357 }
358 kref_get(&mos_parport->ref_count);
359 urbtrack->mos_parport = mos_parport;
360 urbtrack->urb = usb_alloc_urb(0, GFP_ATOMIC);
361 if (urbtrack->urb == NULL) {
362 dev_err(&usbdev->dev, "out of urbs");
363 kfree(urbtrack);
364 return -ENOMEM;
365 }
366 setup.bRequestType = (__u8)0x40;
367 setup.bRequest = (__u8)0x0e;
Mike Dunn63b91762010-04-15 17:02:09 -0400368 setup.wValue = get_reg_value(reg, dummy);
Mike Dunnb69578d2010-04-15 17:01:33 -0400369 setup.wIndex = get_reg_index(reg);
370 setup.wLength = 0;
371 usb_fill_control_urb(urbtrack->urb, usbdev,
372 usb_sndctrlpipe(usbdev, 0),
373 (unsigned char *)&setup,
374 NULL, 0, async_complete, urbtrack);
375 kref_init(&urbtrack->ref_count);
376 INIT_LIST_HEAD(&urbtrack->urblist_entry);
377
378 /*
379 * get the disconnect mutex, or add tracker to the deferred_urbs list
380 * and schedule a tasklet to try again later
381 */
382 if (!mutex_trylock(&serial->disc_mutex)) {
383 spin_lock_irqsave(&mos_parport->listlock, flags);
384 list_add_tail(&urbtrack->urblist_entry,
385 &mos_parport->deferred_urbs);
386 spin_unlock_irqrestore(&mos_parport->listlock, flags);
387 tasklet_schedule(&mos_parport->urb_tasklet);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700388 dev_dbg(&usbdev->dev, "tasklet scheduled");
Mike Dunnb69578d2010-04-15 17:01:33 -0400389 return 0;
390 }
391
392 /* bail if device disconnected */
393 if (serial->disconnected) {
394 kref_put(&urbtrack->ref_count, destroy_urbtracker);
395 mutex_unlock(&serial->disc_mutex);
396 return -ENODEV;
397 }
398
399 /* add the tracker to the active_urbs list and submit */
400 spin_lock_irqsave(&mos_parport->listlock, flags);
401 list_add_tail(&urbtrack->urblist_entry, &mos_parport->active_urbs);
402 spin_unlock_irqrestore(&mos_parport->listlock, flags);
403 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
404 mutex_unlock(&serial->disc_mutex);
405 if (ret_val) {
406 dev_err(&usbdev->dev,
407 "%s: submit_urb() failed: %d", __func__, ret_val);
408 spin_lock_irqsave(&mos_parport->listlock, flags);
409 list_del(&urbtrack->urblist_entry);
410 spin_unlock_irqrestore(&mos_parport->listlock, flags);
411 kref_put(&urbtrack->ref_count, destroy_urbtracker);
412 return ret_val;
413 }
414 return 0;
415}
416
417/*
418 * This is the the common top part of all parallel port callback operations that
419 * send synchronous messages to the device. This implements convoluted locking
420 * that avoids two scenarios: (1) a port operation is called after usbserial
421 * has called our release function, at which point struct mos7715_parport has
422 * been destroyed, and (2) the device has been disconnected, but usbserial has
423 * not called the release function yet because someone has a serial port open.
424 * The shared release_lock prevents the first, and the mutex and disconnected
425 * flag maintained by usbserial covers the second. We also use the msg_pending
426 * flag to ensure that all synchronous usb messgage calls have completed before
427 * our release function can return.
428 */
429static int parport_prologue(struct parport *pp)
430{
431 struct mos7715_parport *mos_parport;
432
433 spin_lock(&release_lock);
434 mos_parport = pp->private_data;
435 if (unlikely(mos_parport == NULL)) {
436 /* release fn called, port struct destroyed */
437 spin_unlock(&release_lock);
438 return -1;
439 }
440 mos_parport->msg_pending = true; /* synch usb call pending */
441 INIT_COMPLETION(mos_parport->syncmsg_compl);
442 spin_unlock(&release_lock);
443
444 mutex_lock(&mos_parport->serial->disc_mutex);
445 if (mos_parport->serial->disconnected) {
446 /* device disconnected */
447 mutex_unlock(&mos_parport->serial->disc_mutex);
448 mos_parport->msg_pending = false;
449 complete(&mos_parport->syncmsg_compl);
450 return -1;
451 }
452
453 return 0;
454}
455
456/*
457 * This is the the common bottom part of all parallel port functions that send
458 * synchronous messages to the device.
459 */
460static inline void parport_epilogue(struct parport *pp)
461{
462 struct mos7715_parport *mos_parport = pp->private_data;
463 mutex_unlock(&mos_parport->serial->disc_mutex);
464 mos_parport->msg_pending = false;
465 complete(&mos_parport->syncmsg_compl);
466}
467
468static void parport_mos7715_write_data(struct parport *pp, unsigned char d)
469{
470 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700471
Mike Dunnb69578d2010-04-15 17:01:33 -0400472 if (parport_prologue(pp) < 0)
473 return;
474 mos7715_change_mode(mos_parport, SPP);
Mike Dunn63b91762010-04-15 17:02:09 -0400475 write_mos_reg(mos_parport->serial, dummy, DPR, (__u8)d);
Mike Dunnb69578d2010-04-15 17:01:33 -0400476 parport_epilogue(pp);
477}
478
479static unsigned char parport_mos7715_read_data(struct parport *pp)
480{
481 struct mos7715_parport *mos_parport = pp->private_data;
482 unsigned char d;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700483
Mike Dunnb69578d2010-04-15 17:01:33 -0400484 if (parport_prologue(pp) < 0)
485 return 0;
Mike Dunn63b91762010-04-15 17:02:09 -0400486 read_mos_reg(mos_parport->serial, dummy, DPR, &d);
Mike Dunnb69578d2010-04-15 17:01:33 -0400487 parport_epilogue(pp);
488 return d;
489}
490
491static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
492{
493 struct mos7715_parport *mos_parport = pp->private_data;
494 __u8 data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700495
Mike Dunnb69578d2010-04-15 17:01:33 -0400496 if (parport_prologue(pp) < 0)
497 return;
498 data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
Mike Dunn63b91762010-04-15 17:02:09 -0400499 write_mos_reg(mos_parport->serial, dummy, DCR, data);
Mike Dunnb69578d2010-04-15 17:01:33 -0400500 mos_parport->shadowDCR = data;
501 parport_epilogue(pp);
502}
503
504static unsigned char parport_mos7715_read_control(struct parport *pp)
505{
506 struct mos7715_parport *mos_parport = pp->private_data;
507 __u8 dcr;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700508
Mike Dunnb69578d2010-04-15 17:01:33 -0400509 spin_lock(&release_lock);
510 mos_parport = pp->private_data;
511 if (unlikely(mos_parport == NULL)) {
512 spin_unlock(&release_lock);
513 return 0;
514 }
515 dcr = mos_parport->shadowDCR & 0x0f;
516 spin_unlock(&release_lock);
517 return dcr;
518}
519
520static unsigned char parport_mos7715_frob_control(struct parport *pp,
521 unsigned char mask,
522 unsigned char val)
523{
524 struct mos7715_parport *mos_parport = pp->private_data;
525 __u8 dcr;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700526
Mike Dunnb69578d2010-04-15 17:01:33 -0400527 mask &= 0x0f;
528 val &= 0x0f;
529 if (parport_prologue(pp) < 0)
530 return 0;
531 mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
Mike Dunn63b91762010-04-15 17:02:09 -0400532 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400533 dcr = mos_parport->shadowDCR & 0x0f;
534 parport_epilogue(pp);
535 return dcr;
536}
537
538static unsigned char parport_mos7715_read_status(struct parport *pp)
539{
540 unsigned char status;
541 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700542
Mike Dunnb69578d2010-04-15 17:01:33 -0400543 spin_lock(&release_lock);
544 mos_parport = pp->private_data;
545 if (unlikely(mos_parport == NULL)) { /* release called */
546 spin_unlock(&release_lock);
547 return 0;
548 }
549 status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
550 spin_unlock(&release_lock);
551 return status;
552}
553
554static void parport_mos7715_enable_irq(struct parport *pp)
555{
Mike Dunnb69578d2010-04-15 17:01:33 -0400556}
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700557
Mike Dunnb69578d2010-04-15 17:01:33 -0400558static void parport_mos7715_disable_irq(struct parport *pp)
559{
Mike Dunnb69578d2010-04-15 17:01:33 -0400560}
561
562static void parport_mos7715_data_forward(struct parport *pp)
563{
564 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700565
Mike Dunnb69578d2010-04-15 17:01:33 -0400566 if (parport_prologue(pp) < 0)
567 return;
568 mos7715_change_mode(mos_parport, PS2);
569 mos_parport->shadowDCR &= ~0x20;
Mike Dunn63b91762010-04-15 17:02:09 -0400570 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400571 parport_epilogue(pp);
572}
573
574static void parport_mos7715_data_reverse(struct parport *pp)
575{
576 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700577
Mike Dunnb69578d2010-04-15 17:01:33 -0400578 if (parport_prologue(pp) < 0)
579 return;
580 mos7715_change_mode(mos_parport, PS2);
581 mos_parport->shadowDCR |= 0x20;
Mike Dunn63b91762010-04-15 17:02:09 -0400582 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400583 parport_epilogue(pp);
584}
585
586static void parport_mos7715_init_state(struct pardevice *dev,
587 struct parport_state *s)
588{
Mike Dunnb69578d2010-04-15 17:01:33 -0400589 s->u.pc.ctr = DCR_INIT_VAL;
590 s->u.pc.ecr = ECR_INIT_VAL;
591}
592
593/* N.B. Parport core code requires that this function not block */
594static void parport_mos7715_save_state(struct parport *pp,
595 struct parport_state *s)
596{
597 struct mos7715_parport *mos_parport;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700598
Mike Dunnb69578d2010-04-15 17:01:33 -0400599 spin_lock(&release_lock);
600 mos_parport = pp->private_data;
601 if (unlikely(mos_parport == NULL)) { /* release called */
602 spin_unlock(&release_lock);
603 return;
604 }
605 s->u.pc.ctr = mos_parport->shadowDCR;
606 s->u.pc.ecr = mos_parport->shadowECR;
607 spin_unlock(&release_lock);
608}
609
610/* N.B. Parport core code requires that this function not block */
611static void parport_mos7715_restore_state(struct parport *pp,
612 struct parport_state *s)
613{
614 struct mos7715_parport *mos_parport;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700615
Mike Dunnb69578d2010-04-15 17:01:33 -0400616 spin_lock(&release_lock);
617 mos_parport = pp->private_data;
618 if (unlikely(mos_parport == NULL)) { /* release called */
619 spin_unlock(&release_lock);
620 return;
621 }
622 write_parport_reg_nonblock(mos_parport, DCR, mos_parport->shadowDCR);
623 write_parport_reg_nonblock(mos_parport, ECR, mos_parport->shadowECR);
624 spin_unlock(&release_lock);
625}
626
627static size_t parport_mos7715_write_compat(struct parport *pp,
628 const void *buffer,
629 size_t len, int flags)
630{
631 int retval;
632 struct mos7715_parport *mos_parport = pp->private_data;
633 int actual_len;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700634
Mike Dunnb69578d2010-04-15 17:01:33 -0400635 if (parport_prologue(pp) < 0)
636 return 0;
637 mos7715_change_mode(mos_parport, PPF);
638 retval = usb_bulk_msg(mos_parport->serial->dev,
639 usb_sndbulkpipe(mos_parport->serial->dev, 2),
640 (void *)buffer, len, &actual_len,
641 MOS_WDR_TIMEOUT);
642 parport_epilogue(pp);
643 if (retval) {
644 dev_err(&mos_parport->serial->dev->dev,
645 "mos7720: usb_bulk_msg() failed: %d", retval);
646 return 0;
647 }
648 return actual_len;
649}
650
651static struct parport_operations parport_mos7715_ops = {
652 .owner = THIS_MODULE,
653 .write_data = parport_mos7715_write_data,
654 .read_data = parport_mos7715_read_data,
655
656 .write_control = parport_mos7715_write_control,
657 .read_control = parport_mos7715_read_control,
658 .frob_control = parport_mos7715_frob_control,
659
660 .read_status = parport_mos7715_read_status,
661
662 .enable_irq = parport_mos7715_enable_irq,
663 .disable_irq = parport_mos7715_disable_irq,
664
665 .data_forward = parport_mos7715_data_forward,
666 .data_reverse = parport_mos7715_data_reverse,
667
668 .init_state = parport_mos7715_init_state,
669 .save_state = parport_mos7715_save_state,
670 .restore_state = parport_mos7715_restore_state,
671
672 .compat_write_data = parport_mos7715_write_compat,
673
674 .nibble_read_data = parport_ieee1284_read_nibble,
675 .byte_read_data = parport_ieee1284_read_byte,
676};
677
678/*
679 * Allocate and initialize parallel port control struct, initialize
680 * the parallel port hardware device, and register with the parport subsystem.
681 */
682static int mos7715_parport_init(struct usb_serial *serial)
683{
684 struct mos7715_parport *mos_parport;
685
686 /* allocate and initialize parallel port control struct */
687 mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
688 if (mos_parport == NULL) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700689 dev_dbg(&serial->dev->dev, "%s: kzalloc failed\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400690 return -ENOMEM;
691 }
692 mos_parport->msg_pending = false;
693 kref_init(&mos_parport->ref_count);
694 spin_lock_init(&mos_parport->listlock);
695 INIT_LIST_HEAD(&mos_parport->active_urbs);
696 INIT_LIST_HEAD(&mos_parport->deferred_urbs);
697 usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
698 mos_parport->serial = serial;
699 tasklet_init(&mos_parport->urb_tasklet, send_deferred_urbs,
700 (unsigned long) mos_parport);
701 init_completion(&mos_parport->syncmsg_compl);
702
703 /* cycle parallel port reset bit */
Mike Dunn63b91762010-04-15 17:02:09 -0400704 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x80);
705 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x00);
Mike Dunnb69578d2010-04-15 17:01:33 -0400706
707 /* initialize device registers */
708 mos_parport->shadowDCR = DCR_INIT_VAL;
Mike Dunn63b91762010-04-15 17:02:09 -0400709 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400710 mos_parport->shadowECR = ECR_INIT_VAL;
Mike Dunn63b91762010-04-15 17:02:09 -0400711 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400712
713 /* register with parport core */
714 mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE,
715 PARPORT_DMA_NONE,
716 &parport_mos7715_ops);
717 if (mos_parport->pp == NULL) {
718 dev_err(&serial->interface->dev,
719 "Could not register parport\n");
720 kref_put(&mos_parport->ref_count, destroy_mos_parport);
721 return -EIO;
722 }
723 mos_parport->pp->private_data = mos_parport;
724 mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP;
725 mos_parport->pp->dev = &serial->interface->dev;
726 parport_announce_port(mos_parport->pp);
727
728 return 0;
729}
730#endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700731
732/*
733 * mos7720_interrupt_callback
734 * this is the callback function for when we have received data on the
735 * interrupt endpoint.
736 */
737static void mos7720_interrupt_callback(struct urb *urb)
738{
739 int result;
740 int length;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700741 int status = urb->status;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700742 struct device *dev = &urb->dev->dev;
Oliver Neukum325b70c2007-03-19 13:58:29 +0100743 __u8 *data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700744 __u8 sp1;
745 __u8 sp2;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700746
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700747 switch (status) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700748 case 0:
749 /* success */
750 break;
751 case -ECONNRESET:
752 case -ENOENT:
753 case -ESHUTDOWN:
754 /* this urb is terminated, clean up */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700755 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700756 return;
757 default:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700758 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700759 goto exit;
760 }
761
762 length = urb->actual_length;
763 data = urb->transfer_buffer;
764
765 /* Moschip get 4 bytes
766 * Byte 1 IIR Port 1 (port.number is 0)
767 * Byte 2 IIR Port 2 (port.number is 1)
768 * Byte 3 --------------
769 * Byte 4 FIFO status for both */
Oliver Neukum325b70c2007-03-19 13:58:29 +0100770
771 /* the above description is inverted
772 * oneukum 2007-03-14 */
773
774 if (unlikely(length != 4)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700775 dev_dbg(dev, "Wrong data !!!\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700776 return;
777 }
778
Oliver Neukum325b70c2007-03-19 13:58:29 +0100779 sp1 = data[3];
780 sp2 = data[2];
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700781
Oliver Neukum325b70c2007-03-19 13:58:29 +0100782 if ((sp1 | sp2) & 0x01) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700783 /* No Interrupt Pending in both the ports */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700784 dev_dbg(dev, "No Interrupt !!!\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700785 } else {
786 switch (sp1 & 0x0f) {
787 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700788 dev_dbg(dev, "Serial Port 1: Receiver status error or address bit detected in 9-bit mode\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700789 break;
790 case SERIAL_IIR_CTI:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700791 dev_dbg(dev, "Serial Port 1: Receiver time out\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700792 break;
793 case SERIAL_IIR_MS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700794 /* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700795 break;
796 }
797
798 switch (sp2 & 0x0f) {
799 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700800 dev_dbg(dev, "Serial Port 2: Receiver status error or address bit detected in 9-bit mode\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700801 break;
802 case SERIAL_IIR_CTI:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700803 dev_dbg(dev, "Serial Port 2: Receiver time out\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700804 break;
805 case SERIAL_IIR_MS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700806 /* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700807 break;
808 }
809 }
810
811exit:
812 result = usb_submit_urb(urb, GFP_ATOMIC);
813 if (result)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700814 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700815}
816
817/*
Mike Dunnfb088e32010-01-26 12:12:12 -0500818 * mos7715_interrupt_callback
819 * this is the 7715's callback function for when we have received data on
820 * the interrupt endpoint.
821 */
822static void mos7715_interrupt_callback(struct urb *urb)
823{
824 int result;
825 int length;
826 int status = urb->status;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700827 struct device *dev = &urb->dev->dev;
Mike Dunnfb088e32010-01-26 12:12:12 -0500828 __u8 *data;
829 __u8 iir;
830
831 switch (status) {
832 case 0:
833 /* success */
834 break;
835 case -ECONNRESET:
836 case -ENOENT:
837 case -ESHUTDOWN:
Mike Dunnb69578d2010-04-15 17:01:33 -0400838 case -ENODEV:
Mike Dunnfb088e32010-01-26 12:12:12 -0500839 /* this urb is terminated, clean up */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700840 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
Mike Dunnfb088e32010-01-26 12:12:12 -0500841 return;
842 default:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700843 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
Mike Dunnfb088e32010-01-26 12:12:12 -0500844 goto exit;
845 }
846
847 length = urb->actual_length;
848 data = urb->transfer_buffer;
849
850 /* Structure of data from 7715 device:
851 * Byte 1: IIR serial Port
852 * Byte 2: unused
853 * Byte 2: DSR parallel port
854 * Byte 4: FIFO status for both */
855
856 if (unlikely(length != 4)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700857 dev_dbg(dev, "Wrong data !!!\n");
Mike Dunnfb088e32010-01-26 12:12:12 -0500858 return;
859 }
860
861 iir = data[0];
862 if (!(iir & 0x01)) { /* serial port interrupt pending */
863 switch (iir & 0x0f) {
864 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700865 dev_dbg(dev, "Serial Port: Receiver status error or address bit detected in 9-bit mode\n\n");
Mike Dunnfb088e32010-01-26 12:12:12 -0500866 break;
867 case SERIAL_IIR_CTI:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700868 dev_dbg(dev, "Serial Port: Receiver time out\n");
Mike Dunnfb088e32010-01-26 12:12:12 -0500869 break;
870 case SERIAL_IIR_MS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700871 /* dev_dbg(dev, "Serial Port: Modem status change\n"); */
Mike Dunnfb088e32010-01-26 12:12:12 -0500872 break;
873 }
874 }
875
Mike Dunnb69578d2010-04-15 17:01:33 -0400876#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
877 { /* update local copy of DSR reg */
878 struct usb_serial_port *port = urb->context;
879 struct mos7715_parport *mos_parport = port->serial->private;
880 if (unlikely(mos_parport == NULL))
881 return;
882 atomic_set(&mos_parport->shadowDSR, data[2]);
883 }
884#endif
885
Mike Dunnfb088e32010-01-26 12:12:12 -0500886exit:
887 result = usb_submit_urb(urb, GFP_ATOMIC);
888 if (result)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700889 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
Mike Dunnfb088e32010-01-26 12:12:12 -0500890}
891
892/*
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700893 * mos7720_bulk_in_callback
894 * this is the callback function for when we have received data on the
895 * bulk in endpoint.
896 */
897static void mos7720_bulk_in_callback(struct urb *urb)
898{
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700899 int retval;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700900 unsigned char *data ;
901 struct usb_serial_port *port;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700902 int status = urb->status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700903
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700904 if (status) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700905 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700906 return;
907 }
908
Mike Dunnb69578d2010-04-15 17:01:33 -0400909 port = urb->context;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700910
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700911 dev_dbg(&port->dev, "Entering...%s\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700912
913 data = urb->transfer_buffer;
914
Jiri Slaby2e124b42013-01-03 15:53:06 +0100915 if (urb->actual_length) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100916 tty_insert_flip_string(&port->port, data, urb->actual_length);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100917 tty_flip_buffer_push(&port->port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700918 }
919
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700920 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700921 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
922 if (retval)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700923 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700924 }
925}
926
927/*
928 * mos7720_bulk_out_data_callback
929 * this is the callback function for when we have finished sending serial
930 * data on the bulk out endpoint.
931 */
932static void mos7720_bulk_out_data_callback(struct urb *urb)
933{
934 struct moschip_port *mos7720_port;
935 struct tty_struct *tty;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700936 int status = urb->status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700937
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700938 if (status) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700939 dev_dbg(&urb->dev->dev, "nonzero write bulk status received:%d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700940 return;
941 }
942
943 mos7720_port = urb->context;
944 if (!mos7720_port) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700945 dev_dbg(&urb->dev->dev, "NULL mos7720_port pointer\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700946 return ;
947 }
948
Alan Cox4a90f092008-10-13 10:39:46 +0100949 tty = tty_port_tty_get(&mos7720_port->port->port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700950
Jiri Slabyb963a842007-02-10 01:44:55 -0800951 if (tty && mos7720_port->open)
952 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100953 tty_kref_put(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700954}
955
956/*
Mike Dunnfb088e32010-01-26 12:12:12 -0500957 * mos77xx_probe
958 * this function installs the appropriate read interrupt endpoint callback
959 * depending on whether the device is a 7720 or 7715, thus avoiding costly
960 * run-time checks in the high-frequency callback routine itself.
961 */
962static int mos77xx_probe(struct usb_serial *serial,
963 const struct usb_device_id *id)
964{
965 if (id->idProduct == MOSCHIP_DEVICE_ID_7715)
966 moschip7720_2port_driver.read_int_callback =
967 mos7715_interrupt_callback;
968 else
969 moschip7720_2port_driver.read_int_callback =
970 mos7720_interrupt_callback;
971
972 return 0;
973}
974
975static int mos77xx_calc_num_ports(struct usb_serial *serial)
976{
977 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
978 if (product == MOSCHIP_DEVICE_ID_7715)
979 return 1;
980
981 return 2;
982}
983
Alan Coxa509a7e2009-09-19 13:13:26 -0700984static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700985{
986 struct usb_serial *serial;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700987 struct urb *urb;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700988 struct moschip_port *mos7720_port;
989 int response;
990 int port_number;
Mike Dunn63b91762010-04-15 17:02:09 -0400991 __u8 data;
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100992 int allocated_urbs = 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700993 int j;
994
995 serial = port->serial;
996
997 mos7720_port = usb_get_serial_port_data(port);
998 if (mos7720_port == NULL)
999 return -ENODEV;
1000
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001001 usb_clear_halt(serial->dev, port->write_urb->pipe);
1002 usb_clear_halt(serial->dev, port->read_urb->pipe);
1003
1004 /* Initialising the write urb pool */
1005 for (j = 0; j < NUM_URBS; ++j) {
Alan Cox4da1a172008-07-22 11:16:21 +01001006 urb = usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001007 mos7720_port->write_urb_pool[j] = urb;
1008
1009 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001010 dev_err(&port->dev, "No more urbs???\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001011 continue;
1012 }
1013
1014 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1015 GFP_KERNEL);
1016 if (!urb->transfer_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001017 dev_err(&port->dev,
1018 "%s-out of memory for urb buffers.\n",
1019 __func__);
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001020 usb_free_urb(mos7720_port->write_urb_pool[j]);
1021 mos7720_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001022 continue;
1023 }
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001024 allocated_urbs++;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001025 }
1026
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001027 if (!allocated_urbs)
1028 return -ENOMEM;
1029
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001030 /* Initialize MCS7720 -- Write Init values to corresponding Registers
1031 *
1032 * Register Index
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001033 * 0 : THR/RHR
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001034 * 1 : IER
1035 * 2 : FCR
1036 * 3 : LCR
1037 * 4 : MCR
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001038 * 5 : LSR
1039 * 6 : MSR
1040 * 7 : SPR
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001041 *
1042 * 0x08 : SP1/2 Control Reg
1043 */
1044 port_number = port->number - port->serial->minor;
Mike Dunn63b91762010-04-15 17:02:09 -04001045 read_mos_reg(serial, port_number, LSR, &data);
1046
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001047 dev_dbg(&port->dev, "SS::%p LSR:%x\n", mos7720_port, data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001048
Mike Dunn63b91762010-04-15 17:02:09 -04001049 write_mos_reg(serial, dummy, SP1_REG, 0x02);
1050 write_mos_reg(serial, dummy, SP2_REG, 0x02);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001051
Mike Dunn63b91762010-04-15 17:02:09 -04001052 write_mos_reg(serial, port_number, IER, 0x00);
1053 write_mos_reg(serial, port_number, FCR, 0x00);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001054
Mike Dunn63b91762010-04-15 17:02:09 -04001055 write_mos_reg(serial, port_number, FCR, 0xcf);
1056 mos7720_port->shadowLCR = 0x03;
1057 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1058 mos7720_port->shadowMCR = 0x0b;
1059 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001060
Mike Dunn63b91762010-04-15 17:02:09 -04001061 write_mos_reg(serial, port_number, SP_CONTROL_REG, 0x00);
1062 read_mos_reg(serial, dummy, SP_CONTROL_REG, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001063 data = data | (port->number - port->serial->minor + 1);
Mike Dunn63b91762010-04-15 17:02:09 -04001064 write_mos_reg(serial, dummy, SP_CONTROL_REG, data);
1065 mos7720_port->shadowLCR = 0x83;
1066 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1067 write_mos_reg(serial, port_number, THR, 0x0c);
1068 write_mos_reg(serial, port_number, IER, 0x00);
1069 mos7720_port->shadowLCR = 0x03;
1070 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1071 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001072
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001073 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
1074 if (response)
Alan Cox4da1a172008-07-22 11:16:21 +01001075 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
1076 __func__, response);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001077
1078 /* initialize our icount structure */
1079 memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
1080
1081 /* initialize our port settings */
1082 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
1083
1084 /* send a open port command */
1085 mos7720_port->open = 1;
1086
1087 return 0;
1088}
1089
1090/*
1091 * mos7720_chars_in_buffer
1092 * this function is called by the tty driver when it wants to know how many
1093 * bytes of data we currently have outstanding in the port (data that has
1094 * been written, but hasn't made it out the port yet)
1095 * If successful, we return the number of bytes left to be written in the
1096 * system,
1097 * Otherwise we return a negative error number.
1098 */
Alan Cox95da3102008-07-22 11:09:07 +01001099static int mos7720_chars_in_buffer(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001100{
Alan Cox95da3102008-07-22 11:09:07 +01001101 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001102 int i;
1103 int chars = 0;
1104 struct moschip_port *mos7720_port;
1105
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001106 mos7720_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001107 if (mos7720_port == NULL)
Alan Cox23198fd2009-07-20 16:05:27 +01001108 return 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001109
1110 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001111 if (mos7720_port->write_urb_pool[i] &&
1112 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001113 chars += URB_TRANSFER_BUFFER_SIZE;
1114 }
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001115 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001116 return chars;
1117}
1118
Alan Cox335f8512009-06-11 12:26:29 +01001119static void mos7720_close(struct usb_serial_port *port)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001120{
1121 struct usb_serial *serial;
1122 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001123 int j;
1124
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001125 serial = port->serial;
1126
1127 mos7720_port = usb_get_serial_port_data(port);
1128 if (mos7720_port == NULL)
1129 return;
1130
1131 for (j = 0; j < NUM_URBS; ++j)
1132 usb_kill_urb(mos7720_port->write_urb_pool[j]);
1133
1134 /* Freeing Write URBs */
1135 for (j = 0; j < NUM_URBS; ++j) {
1136 if (mos7720_port->write_urb_pool[j]) {
1137 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
1138 usb_free_urb(mos7720_port->write_urb_pool[j]);
1139 }
1140 }
1141
1142 /* While closing port, shutdown all bulk read, write *
Oliver Neukuma1cd7e92008-01-16 17:18:52 +01001143 * and interrupt read if they exists, otherwise nop */
Oliver Neukuma1cd7e92008-01-16 17:18:52 +01001144 usb_kill_urb(port->write_urb);
Oliver Neukuma1cd7e92008-01-16 17:18:52 +01001145 usb_kill_urb(port->read_urb);
1146
1147 mutex_lock(&serial->disc_mutex);
1148 /* these commands must not be issued if the device has
1149 * been disconnected */
1150 if (!serial->disconnected) {
Mike Dunn63b91762010-04-15 17:02:09 -04001151 write_mos_reg(serial, port->number - port->serial->minor,
1152 MCR, 0x00);
1153 write_mos_reg(serial, port->number - port->serial->minor,
1154 IER, 0x00);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001155 }
Oliver Neukuma1cd7e92008-01-16 17:18:52 +01001156 mutex_unlock(&serial->disc_mutex);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001157 mos7720_port->open = 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001158}
1159
Alan Cox95da3102008-07-22 11:09:07 +01001160static void mos7720_break(struct tty_struct *tty, int break_state)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001161{
Alan Cox95da3102008-07-22 11:09:07 +01001162 struct usb_serial_port *port = tty->driver_data;
Alan Cox4da1a172008-07-22 11:16:21 +01001163 unsigned char data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001164 struct usb_serial *serial;
1165 struct moschip_port *mos7720_port;
1166
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001167 serial = port->serial;
1168
1169 mos7720_port = usb_get_serial_port_data(port);
1170 if (mos7720_port == NULL)
1171 return;
1172
1173 if (break_state == -1)
1174 data = mos7720_port->shadowLCR | UART_LCR_SBC;
1175 else
1176 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
1177
1178 mos7720_port->shadowLCR = data;
Mike Dunn63b91762010-04-15 17:02:09 -04001179 write_mos_reg(serial, port->number - port->serial->minor,
1180 LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001181}
1182
1183/*
1184 * mos7720_write_room
1185 * this function is called by the tty driver when it wants to know how many
1186 * bytes of data we can accept for a specific port.
1187 * If successful, we return the amount of room that we have for this port
1188 * Otherwise we return a negative error number.
1189 */
Alan Cox95da3102008-07-22 11:09:07 +01001190static int mos7720_write_room(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001191{
Alan Cox95da3102008-07-22 11:09:07 +01001192 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001193 struct moschip_port *mos7720_port;
1194 int room = 0;
1195 int i;
1196
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001197 mos7720_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001198 if (mos7720_port == NULL)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001199 return -ENODEV;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001200
Alan Coxa5b6f602008-04-08 17:16:06 +01001201 /* FIXME: Locking */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001202 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001203 if (mos7720_port->write_urb_pool[i] &&
1204 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001205 room += URB_TRANSFER_BUFFER_SIZE;
1206 }
1207
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001208 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001209 return room;
1210}
1211
Alan Cox95da3102008-07-22 11:09:07 +01001212static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
1213 const unsigned char *data, int count)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001214{
1215 int status;
1216 int i;
1217 int bytes_sent = 0;
1218 int transfer_size;
1219
1220 struct moschip_port *mos7720_port;
1221 struct usb_serial *serial;
1222 struct urb *urb;
1223 const unsigned char *current_position = data;
1224
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001225 serial = port->serial;
1226
1227 mos7720_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001228 if (mos7720_port == NULL)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001229 return -ENODEV;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001230
1231 /* try to find a free urb in the list */
1232 urb = NULL;
1233
1234 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001235 if (mos7720_port->write_urb_pool[i] &&
1236 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001237 urb = mos7720_port->write_urb_pool[i];
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001238 dev_dbg(&port->dev, "URB:%d\n", i);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001239 break;
1240 }
1241 }
1242
1243 if (urb == NULL) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001244 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001245 goto exit;
1246 }
1247
1248 if (urb->transfer_buffer == NULL) {
1249 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1250 GFP_KERNEL);
1251 if (urb->transfer_buffer == NULL) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001252 dev_err_console(port, "%s no more kernel memory...\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001253 __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001254 goto exit;
1255 }
1256 }
Alan Cox4da1a172008-07-22 11:16:21 +01001257 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001258
1259 memcpy(urb->transfer_buffer, current_position, transfer_size);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001260 usb_serial_debug_data(&port->dev, __func__, transfer_size,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001261 urb->transfer_buffer);
1262
1263 /* fill urb with data and submit */
1264 usb_fill_bulk_urb(urb, serial->dev,
1265 usb_sndbulkpipe(serial->dev,
Alan Cox4da1a172008-07-22 11:16:21 +01001266 port->bulk_out_endpointAddress),
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001267 urb->transfer_buffer, transfer_size,
1268 mos7720_bulk_out_data_callback, mos7720_port);
1269
1270 /* send it down the pipe */
Alan Cox4da1a172008-07-22 11:16:21 +01001271 status = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001272 if (status) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001273 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001274 "with status = %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001275 bytes_sent = status;
1276 goto exit;
1277 }
1278 bytes_sent = transfer_size;
1279
1280exit:
1281 return bytes_sent;
1282}
1283
Alan Cox95da3102008-07-22 11:09:07 +01001284static void mos7720_throttle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001285{
Alan Cox95da3102008-07-22 11:09:07 +01001286 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001287 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001288 int status;
1289
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001290 mos7720_port = usb_get_serial_port_data(port);
1291
1292 if (mos7720_port == NULL)
1293 return;
1294
1295 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001296 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001297 return;
1298 }
1299
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001300 /* if we are implementing XON/XOFF, send the stop character */
1301 if (I_IXOFF(tty)) {
1302 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001303 status = mos7720_write(tty, port, &stop_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001304 if (status <= 0)
1305 return;
1306 }
1307
1308 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001309 if (tty->termios.c_cflag & CRTSCTS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001310 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
Mike Dunn63b91762010-04-15 17:02:09 -04001311 write_mos_reg(port->serial, port->number - port->serial->minor,
1312 MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001313 if (status != 0)
1314 return;
1315 }
1316}
1317
Alan Cox95da3102008-07-22 11:09:07 +01001318static void mos7720_unthrottle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001319{
Alan Cox95da3102008-07-22 11:09:07 +01001320 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001321 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +01001322 int status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001323
1324 if (mos7720_port == NULL)
1325 return;
1326
1327 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001328 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001329 return;
1330 }
1331
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001332 /* if we are implementing XON/XOFF, send the start character */
1333 if (I_IXOFF(tty)) {
1334 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001335 status = mos7720_write(tty, port, &start_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001336 if (status <= 0)
1337 return;
1338 }
1339
1340 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001341 if (tty->termios.c_cflag & CRTSCTS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001342 mos7720_port->shadowMCR |= UART_MCR_RTS;
Mike Dunn63b91762010-04-15 17:02:09 -04001343 write_mos_reg(port->serial, port->number - port->serial->minor,
1344 MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001345 if (status != 0)
1346 return;
1347 }
1348}
1349
Mike Dunnb69578d2010-04-15 17:01:33 -04001350/* FIXME: this function does not work */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001351static int set_higher_rates(struct moschip_port *mos7720_port,
1352 unsigned int baud)
1353{
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001354 struct usb_serial_port *port;
1355 struct usb_serial *serial;
1356 int port_number;
Mike Dunn63b91762010-04-15 17:02:09 -04001357 enum mos_regs sp_reg;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001358 if (mos7720_port == NULL)
1359 return -EINVAL;
1360
1361 port = mos7720_port->port;
1362 serial = port->serial;
1363
Alan Cox4da1a172008-07-22 11:16:21 +01001364 /***********************************************
1365 * Init Sequence for higher rates
1366 ***********************************************/
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001367 dev_dbg(&port->dev, "Sending Setting Commands ..........\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001368 port_number = port->number - port->serial->minor;
1369
Mike Dunn63b91762010-04-15 17:02:09 -04001370 write_mos_reg(serial, port_number, IER, 0x00);
1371 write_mos_reg(serial, port_number, FCR, 0x00);
1372 write_mos_reg(serial, port_number, FCR, 0xcf);
1373 mos7720_port->shadowMCR = 0x0b;
1374 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1375 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x00);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001376
Alan Cox4da1a172008-07-22 11:16:21 +01001377 /***********************************************
1378 * Set for higher rates *
1379 ***********************************************/
Mike Dunnb69578d2010-04-15 17:01:33 -04001380 /* writing baud rate verbatum into uart clock field clearly not right */
Mike Dunn63b91762010-04-15 17:02:09 -04001381 if (port_number == 0)
1382 sp_reg = SP1_REG;
1383 else
1384 sp_reg = SP2_REG;
1385 write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
1386 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x03);
1387 mos7720_port->shadowMCR = 0x2b;
1388 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001389
Alan Cox4da1a172008-07-22 11:16:21 +01001390 /***********************************************
1391 * Set DLL/DLM
1392 ***********************************************/
Mike Dunn63b91762010-04-15 17:02:09 -04001393 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1394 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1395 write_mos_reg(serial, port_number, DLL, 0x01);
1396 write_mos_reg(serial, port_number, DLM, 0x00);
1397 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1398 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001399
1400 return 0;
1401}
1402
1403/* baud rate information */
Alan Cox4da1a172008-07-22 11:16:21 +01001404struct divisor_table_entry {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001405 __u32 baudrate;
1406 __u16 divisor;
1407};
1408
1409/* Define table of divisors for moschip 7720 hardware *
1410 * These assume a 3.6864MHz crystal, the standard /16, and *
1411 * MCR.7 = 0. */
1412static struct divisor_table_entry divisor_table[] = {
1413 { 50, 2304},
1414 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
1415 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
1416 { 150, 768},
1417 { 300, 384},
1418 { 600, 192},
1419 { 1200, 96},
1420 { 1800, 64},
1421 { 2400, 48},
1422 { 4800, 24},
1423 { 7200, 16},
1424 { 9600, 12},
1425 { 19200, 6},
1426 { 38400, 3},
1427 { 57600, 2},
1428 { 115200, 1},
1429};
1430
1431/*****************************************************************************
1432 * calc_baud_rate_divisor
1433 * this function calculates the proper baud rate divisor for the specified
1434 * baud rate.
1435 *****************************************************************************/
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001436static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, int *divisor)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001437{
1438 int i;
1439 __u16 custom;
1440 __u16 round1;
1441 __u16 round;
1442
1443
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001444 dev_dbg(&port->dev, "%s - %d\n", __func__, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001445
1446 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
1447 if (divisor_table[i].baudrate == baudrate) {
1448 *divisor = divisor_table[i].divisor;
1449 return 0;
1450 }
1451 }
1452
Alan Cox4da1a172008-07-22 11:16:21 +01001453 /* After trying for all the standard baud rates *
1454 * Try calculating the divisor for this baud rate */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001455 if (baudrate > 75 && baudrate < 230400) {
1456 /* get the divisor */
1457 custom = (__u16)(230400L / baudrate);
1458
1459 /* Check for round off */
1460 round1 = (__u16)(2304000L / baudrate);
1461 round = (__u16)(round1 - (custom * 10));
1462 if (round > 4)
1463 custom++;
1464 *divisor = custom;
1465
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001466 dev_dbg(&port->dev, "Baud %d = %d\n", baudrate, custom);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001467 return 0;
1468 }
1469
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001470 dev_dbg(&port->dev, "Baud calculation Failed...\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001471 return -EINVAL;
1472}
1473
1474/*
1475 * send_cmd_write_baud_rate
1476 * this function sends the proper command to change the baud rate of the
1477 * specified port.
1478 */
1479static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1480 int baudrate)
1481{
1482 struct usb_serial_port *port;
1483 struct usb_serial *serial;
1484 int divisor;
1485 int status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001486 unsigned char number;
1487
1488 if (mos7720_port == NULL)
1489 return -1;
1490
1491 port = mos7720_port->port;
1492 serial = port->serial;
1493
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001494 number = port->number - port->serial->minor;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001495 dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001496
Alan Cox4da1a172008-07-22 11:16:21 +01001497 /* Calculate the Divisor */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001498 status = calc_baud_rate_divisor(port, baudrate, &divisor);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001499 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001500 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001501 return status;
1502 }
1503
Alan Cox4da1a172008-07-22 11:16:21 +01001504 /* Enable access to divisor latch */
Mike Dunn63b91762010-04-15 17:02:09 -04001505 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1506 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001507
1508 /* Write the divisor */
Mike Dunn63b91762010-04-15 17:02:09 -04001509 write_mos_reg(serial, number, DLL, (__u8)(divisor & 0xff));
1510 write_mos_reg(serial, number, DLM, (__u8)((divisor & 0xff00) >> 8));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001511
Alan Cox4da1a172008-07-22 11:16:21 +01001512 /* Disable access to divisor latch */
Mike Dunn63b91762010-04-15 17:02:09 -04001513 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1514 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001515
1516 return status;
1517}
1518
1519/*
1520 * change_port_settings
1521 * This routine is called to set the UART on the device to match
1522 * the specified new settings.
1523 */
Alan Cox95da3102008-07-22 11:09:07 +01001524static void change_port_settings(struct tty_struct *tty,
1525 struct moschip_port *mos7720_port,
Alan Cox606d0992006-12-08 02:38:45 -08001526 struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001527{
1528 struct usb_serial_port *port;
1529 struct usb_serial *serial;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001530 int baud;
1531 unsigned cflag;
1532 unsigned iflag;
1533 __u8 mask = 0xff;
1534 __u8 lData;
1535 __u8 lParity;
1536 __u8 lStop;
1537 int status;
1538 int port_number;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001539
1540 if (mos7720_port == NULL)
1541 return ;
1542
1543 port = mos7720_port->port;
1544 serial = port->serial;
1545 port_number = port->number - port->serial->minor;
1546
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001547 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001548 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001549 return;
1550 }
1551
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001552 lData = UART_LCR_WLEN8;
1553 lStop = 0x00; /* 1 stop bit */
1554 lParity = 0x00; /* No parity */
1555
Alan Coxadc8d742012-07-14 15:31:47 +01001556 cflag = tty->termios.c_cflag;
1557 iflag = tty->termios.c_iflag;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001558
1559 /* Change the number of bits */
1560 switch (cflag & CSIZE) {
1561 case CS5:
1562 lData = UART_LCR_WLEN5;
1563 mask = 0x1f;
1564 break;
1565
1566 case CS6:
1567 lData = UART_LCR_WLEN6;
1568 mask = 0x3f;
1569 break;
1570
1571 case CS7:
1572 lData = UART_LCR_WLEN7;
1573 mask = 0x7f;
1574 break;
1575 default:
1576 case CS8:
1577 lData = UART_LCR_WLEN8;
1578 break;
1579 }
1580
1581 /* Change the Parity bit */
1582 if (cflag & PARENB) {
1583 if (cflag & PARODD) {
1584 lParity = UART_LCR_PARITY;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001585 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001586 } else {
1587 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001588 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001589 }
1590
1591 } else {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001592 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001593 }
1594
1595 if (cflag & CMSPAR)
1596 lParity = lParity | 0x20;
1597
1598 /* Change the Stop bit */
1599 if (cflag & CSTOPB) {
1600 lStop = UART_LCR_STOP;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001601 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001602 } else {
1603 lStop = 0x00;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001604 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001605 }
1606
1607#define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1608#define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1609#define LCR_PAR_MASK 0x38 /* Mask for parity field */
1610
1611 /* Update the LCR with the correct value */
Alan Cox4da1a172008-07-22 11:16:21 +01001612 mos7720_port->shadowLCR &=
Mike Dunn63b91762010-04-15 17:02:09 -04001613 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001614 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1615
1616
1617 /* Disable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001618 write_mos_reg(serial, port_number, IER, 0x00);
1619 write_mos_reg(serial, port_number, FCR, 0x00);
1620 write_mos_reg(serial, port_number, FCR, 0xcf);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001621
1622 /* Send the updated LCR value to the mos7720 */
Mike Dunn63b91762010-04-15 17:02:09 -04001623 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1624 mos7720_port->shadowMCR = 0x0b;
1625 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001626
1627 /* set up the MCR register and send it to the mos7720 */
1628 mos7720_port->shadowMCR = UART_MCR_OUT2;
1629 if (cflag & CBAUD)
1630 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1631
1632 if (cflag & CRTSCTS) {
1633 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
Alan Cox4da1a172008-07-22 11:16:21 +01001634 /* To set hardware flow control to the specified *
1635 * serial port, in SP1/2_CONTROL_REG */
Mike Dunn63b91762010-04-15 17:02:09 -04001636 if (port->number)
1637 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x01);
1638 else
1639 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x02);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001640
Mike Dunn63b91762010-04-15 17:02:09 -04001641 } else
1642 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1643
1644 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001645
1646 /* Determine divisor based on baud rate */
1647 baud = tty_get_baud_rate(tty);
1648 if (!baud) {
1649 /* pick a default, any default... */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001650 dev_dbg(&port->dev, "Picked default baud...\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001651 baud = 9600;
1652 }
1653
1654 if (baud >= 230400) {
1655 set_higher_rates(mos7720_port, baud);
1656 /* Enable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001657 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001658 return;
1659 }
1660
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001661 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001662 status = send_cmd_write_baud_rate(mos7720_port, baud);
Alan Cox65d063ab2008-01-03 17:01:18 +00001663 /* FIXME: needs to write actual resulting baud back not just
1664 blindly do so */
1665 if (cflag & CBAUD)
1666 tty_encode_baud_rate(tty, baud, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001667 /* Enable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001668 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001669
1670 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001671 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1672 if (status)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001673 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001674 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001675}
1676
1677/*
1678 * mos7720_set_termios
1679 * this function is called by the tty driver when it wants to change the
1680 * termios structure.
1681 */
Alan Cox95da3102008-07-22 11:09:07 +01001682static void mos7720_set_termios(struct tty_struct *tty,
1683 struct usb_serial_port *port, struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001684{
1685 int status;
1686 unsigned int cflag;
1687 struct usb_serial *serial;
1688 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001689
1690 serial = port->serial;
1691
1692 mos7720_port = usb_get_serial_port_data(port);
1693
1694 if (mos7720_port == NULL)
1695 return;
1696
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001697 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001698 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001699 return;
1700 }
1701
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001702 dev_dbg(&port->dev, "setting termios - ASPIRE\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001703
Alan Coxadc8d742012-07-14 15:31:47 +01001704 cflag = tty->termios.c_cflag;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001705
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001706 dev_dbg(&port->dev, "%s - cflag %08x iflag %08x\n", __func__,
Linus Torvaldsd9a80742012-10-01 13:23:01 -07001707 tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001708
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001709 dev_dbg(&port->dev, "%s - old cflag %08x old iflag %08x\n", __func__,
1710 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001711
1712 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001713 change_port_settings(tty, mos7720_port, old_termios);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001714
Alan Cox4da1a172008-07-22 11:16:21 +01001715 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001716 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1717 if (status)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001718 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001719 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001720}
1721
1722/*
1723 * get_lsr_info - get line status register info
1724 *
1725 * Purpose: Let user call ioctl() to get info when the UART physically
1726 * is emptied. On bus types like RS485, the transmitter must
1727 * release the bus after transmitting. This must be done when
1728 * the transmit shift register is empty, not be done when the
1729 * transmit holding register is empty. This functionality
1730 * allows an RS485 driver to be written in user space.
1731 */
Alan Cox4da1a172008-07-22 11:16:21 +01001732static int get_lsr_info(struct tty_struct *tty,
1733 struct moschip_port *mos7720_port, unsigned int __user *value)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001734{
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001735 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001736 unsigned int result = 0;
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001737 unsigned char data = 0;
1738 int port_number = port->number - port->serial->minor;
1739 int count;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001740
Alan Cox95da3102008-07-22 11:09:07 +01001741 count = mos7720_chars_in_buffer(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001742 if (count == 0) {
Mike Dunn63b91762010-04-15 17:02:09 -04001743 read_mos_reg(port->serial, port_number, LSR, &data);
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001744 if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1745 == (UART_LSR_TEMT | UART_LSR_THRE)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001746 dev_dbg(&port->dev, "%s -- Empty\n", __func__);
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001747 result = TIOCSER_TEMT;
1748 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001749 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001750 if (copy_to_user(value, &result, sizeof(int)))
1751 return -EFAULT;
1752 return 0;
1753}
1754
Alan Cox60b33c12011-02-14 16:26:14 +00001755static int mos7720_tiocmget(struct tty_struct *tty)
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001756{
1757 struct usb_serial_port *port = tty->driver_data;
1758 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1759 unsigned int result = 0;
1760 unsigned int mcr ;
1761 unsigned int msr ;
1762
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001763 mcr = mos7720_port->shadowMCR;
1764 msr = mos7720_port->shadowMSR;
1765
1766 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1767 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1768 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1769 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
1770 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1771 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
1772
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001773 return result;
1774}
1775
Alan Cox20b9d172011-02-14 16:26:50 +00001776static int mos7720_tiocmset(struct tty_struct *tty,
Mike Dunn63b91762010-04-15 17:02:09 -04001777 unsigned int set, unsigned int clear)
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001778{
1779 struct usb_serial_port *port = tty->driver_data;
1780 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1781 unsigned int mcr ;
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001782
1783 mcr = mos7720_port->shadowMCR;
1784
1785 if (set & TIOCM_RTS)
1786 mcr |= UART_MCR_RTS;
1787 if (set & TIOCM_DTR)
1788 mcr |= UART_MCR_DTR;
1789 if (set & TIOCM_LOOP)
1790 mcr |= UART_MCR_LOOP;
1791
1792 if (clear & TIOCM_RTS)
1793 mcr &= ~UART_MCR_RTS;
1794 if (clear & TIOCM_DTR)
1795 mcr &= ~UART_MCR_DTR;
1796 if (clear & TIOCM_LOOP)
1797 mcr &= ~UART_MCR_LOOP;
1798
1799 mos7720_port->shadowMCR = mcr;
Mike Dunn63b91762010-04-15 17:02:09 -04001800 write_mos_reg(port->serial, port->number - port->serial->minor,
1801 MCR, mos7720_port->shadowMCR);
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001802
1803 return 0;
1804}
1805
Alan Cox0bca1b92010-09-16 18:21:40 +01001806static int mos7720_get_icount(struct tty_struct *tty,
1807 struct serial_icounter_struct *icount)
1808{
1809 struct usb_serial_port *port = tty->driver_data;
1810 struct moschip_port *mos7720_port;
1811 struct async_icount cnow;
1812
1813 mos7720_port = usb_get_serial_port_data(port);
1814 cnow = mos7720_port->icount;
1815
1816 icount->cts = cnow.cts;
1817 icount->dsr = cnow.dsr;
1818 icount->rng = cnow.rng;
1819 icount->dcd = cnow.dcd;
1820 icount->rx = cnow.rx;
1821 icount->tx = cnow.tx;
1822 icount->frame = cnow.frame;
1823 icount->overrun = cnow.overrun;
1824 icount->parity = cnow.parity;
1825 icount->brk = cnow.brk;
1826 icount->buf_overrun = cnow.buf_overrun;
1827
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001828 dev_dbg(&port->dev, "%s TIOCGICOUNT RX=%d, TX=%d\n", __func__,
1829 icount->rx, icount->tx);
Alan Cox0bca1b92010-09-16 18:21:40 +01001830 return 0;
1831}
1832
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001833static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1834 unsigned int __user *value)
1835{
Alan Cox0bca1b92010-09-16 18:21:40 +01001836 unsigned int mcr;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001837 unsigned int arg;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001838
1839 struct usb_serial_port *port;
1840
1841 if (mos7720_port == NULL)
1842 return -1;
1843
Alan Cox4da1a172008-07-22 11:16:21 +01001844 port = (struct usb_serial_port *)mos7720_port->port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001845 mcr = mos7720_port->shadowMCR;
1846
1847 if (copy_from_user(&arg, value, sizeof(int)))
1848 return -EFAULT;
1849
1850 switch (cmd) {
1851 case TIOCMBIS:
1852 if (arg & TIOCM_RTS)
1853 mcr |= UART_MCR_RTS;
1854 if (arg & TIOCM_DTR)
1855 mcr |= UART_MCR_RTS;
1856 if (arg & TIOCM_LOOP)
1857 mcr |= UART_MCR_LOOP;
1858 break;
1859
1860 case TIOCMBIC:
1861 if (arg & TIOCM_RTS)
1862 mcr &= ~UART_MCR_RTS;
1863 if (arg & TIOCM_DTR)
1864 mcr &= ~UART_MCR_RTS;
1865 if (arg & TIOCM_LOOP)
1866 mcr &= ~UART_MCR_LOOP;
1867 break;
1868
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001869 }
1870
1871 mos7720_port->shadowMCR = mcr;
Mike Dunn63b91762010-04-15 17:02:09 -04001872 write_mos_reg(port->serial, port->number - port->serial->minor,
1873 MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001874
1875 return 0;
1876}
1877
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001878static int get_serial_info(struct moschip_port *mos7720_port,
1879 struct serial_struct __user *retinfo)
1880{
1881 struct serial_struct tmp;
1882
1883 if (!retinfo)
1884 return -EFAULT;
1885
1886 memset(&tmp, 0, sizeof(tmp));
1887
1888 tmp.type = PORT_16550A;
1889 tmp.line = mos7720_port->port->serial->minor;
1890 tmp.port = mos7720_port->port->number;
1891 tmp.irq = 0;
1892 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
Alan Cox4da1a172008-07-22 11:16:21 +01001893 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001894 tmp.baud_base = 9600;
1895 tmp.close_delay = 5*HZ;
1896 tmp.closing_wait = 30*HZ;
1897
1898 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1899 return -EFAULT;
1900 return 0;
1901}
1902
Alan Cox00a0d0d2011-02-14 16:27:06 +00001903static int mos7720_ioctl(struct tty_struct *tty,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001904 unsigned int cmd, unsigned long arg)
1905{
Alan Cox95da3102008-07-22 11:09:07 +01001906 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001907 struct moschip_port *mos7720_port;
1908 struct async_icount cnow;
1909 struct async_icount cprev;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001910
1911 mos7720_port = usb_get_serial_port_data(port);
1912 if (mos7720_port == NULL)
1913 return -ENODEV;
1914
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001915 dev_dbg(&port->dev, "%s - cmd = 0x%x", __func__, cmd);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001916
1917 switch (cmd) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001918 case TIOCSERGETLSR:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001919 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
Alan Cox4da1a172008-07-22 11:16:21 +01001920 return get_lsr_info(tty, mos7720_port,
1921 (unsigned int __user *)arg);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001922
Alan Cox95da3102008-07-22 11:09:07 +01001923 /* FIXME: These should be using the mode methods */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001924 case TIOCMBIS:
1925 case TIOCMBIC:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001926 dev_dbg(&port->dev, "%s TIOCMSET/TIOCMBIC/TIOCMSET\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001927 return set_modem_info(mos7720_port, cmd,
1928 (unsigned int __user *)arg);
1929
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001930 case TIOCGSERIAL:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001931 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001932 return get_serial_info(mos7720_port,
1933 (struct serial_struct __user *)arg);
1934
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001935 case TIOCMIWAIT:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001936 dev_dbg(&port->dev, "%s TIOCMIWAIT\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001937 cprev = mos7720_port->icount;
1938 while (1) {
1939 if (signal_pending(current))
1940 return -ERESTARTSYS;
1941 cnow = mos7720_port->icount;
1942 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1943 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1944 return -EIO; /* no change => error */
1945 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1946 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1947 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
Alan Cox4da1a172008-07-22 11:16:21 +01001948 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001949 return 0;
1950 }
1951 cprev = cnow;
1952 }
1953 /* NOTREACHED */
1954 break;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001955 }
1956
1957 return -ENOIOCTLCMD;
1958}
1959
1960static int mos7720_startup(struct usb_serial *serial)
1961{
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001962 struct usb_device *dev;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001963 char data;
Huzaifa Sidhpurwala91f58ae2011-02-21 12:58:45 +05301964 u16 product;
Mike Dunnb69578d2010-04-15 17:01:33 -04001965 int ret_val;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001966
Huzaifa Sidhpurwala91f58ae2011-02-21 12:58:45 +05301967 product = le16_to_cpu(serial->dev->descriptor.idProduct);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001968 dev = serial->dev;
1969
Mike Dunnfb088e32010-01-26 12:12:12 -05001970 /*
1971 * The 7715 uses the first bulk in/out endpoint pair for the parallel
1972 * port, and the second for the serial port. Because the usbserial core
1973 * assumes both pairs are serial ports, we must engage in a bit of
1974 * subterfuge and swap the pointers for ports 0 and 1 in order to make
1975 * port 0 point to the serial port. However, both moschip devices use a
1976 * single interrupt-in endpoint for both ports (as mentioned a little
1977 * further down), and this endpoint was assigned to port 0. So after
1978 * the swap, we must copy the interrupt endpoint elements from port 1
1979 * (as newly assigned) to port 0, and null out port 1 pointers.
1980 */
1981 if (product == MOSCHIP_DEVICE_ID_7715) {
1982 struct usb_serial_port *tmp = serial->port[0];
1983 serial->port[0] = serial->port[1];
1984 serial->port[1] = tmp;
1985 serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb;
1986 serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer;
1987 serial->port[0]->interrupt_in_endpointAddress =
1988 tmp->interrupt_in_endpointAddress;
1989 serial->port[1]->interrupt_in_urb = NULL;
1990 serial->port[1]->interrupt_in_buffer = NULL;
1991 }
1992
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001993 /* setting configuration feature to one */
1994 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Alan Cox4da1a172008-07-22 11:16:21 +01001995 (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5*HZ);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001996
Mike Dunnb69578d2010-04-15 17:01:33 -04001997 /* start the interrupt urb */
1998 ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
1999 if (ret_val)
2000 dev_err(&dev->dev,
2001 "%s - Error %d submitting control urb\n",
2002 __func__, ret_val);
2003
2004#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
2005 if (product == MOSCHIP_DEVICE_ID_7715) {
2006 ret_val = mos7715_parport_init(serial);
2007 if (ret_val < 0)
2008 return ret_val;
2009 }
2010#endif
Alan Cox4da1a172008-07-22 11:16:21 +01002011 /* LSR For Port 1 */
Mike Dunn63b91762010-04-15 17:02:09 -04002012 read_mos_reg(serial, 0, LSR, &data);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07002013 dev_dbg(&dev->dev, "LSR:%x\n", data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002014
2015 return 0;
2016}
2017
Alan Sternf9c99bb2009-06-02 11:53:55 -04002018static void mos7720_release(struct usb_serial *serial)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002019{
Mike Dunnb69578d2010-04-15 17:01:33 -04002020#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
2021 /* close the parallel port */
2022
2023 if (le16_to_cpu(serial->dev->descriptor.idProduct)
2024 == MOSCHIP_DEVICE_ID_7715) {
2025 struct urbtracker *urbtrack;
2026 unsigned long flags;
2027 struct mos7715_parport *mos_parport =
2028 usb_get_serial_data(serial);
2029
2030 /* prevent NULL ptr dereference in port callbacks */
2031 spin_lock(&release_lock);
2032 mos_parport->pp->private_data = NULL;
2033 spin_unlock(&release_lock);
2034
2035 /* wait for synchronous usb calls to return */
2036 if (mos_parport->msg_pending)
2037 wait_for_completion_timeout(&mos_parport->syncmsg_compl,
2038 MOS_WDR_TIMEOUT);
2039
2040 parport_remove_port(mos_parport->pp);
2041 usb_set_serial_data(serial, NULL);
2042 mos_parport->serial = NULL;
2043
2044 /* if tasklet currently scheduled, wait for it to complete */
2045 tasklet_kill(&mos_parport->urb_tasklet);
2046
2047 /* unlink any urbs sent by the tasklet */
2048 spin_lock_irqsave(&mos_parport->listlock, flags);
2049 list_for_each_entry(urbtrack,
2050 &mos_parport->active_urbs,
2051 urblist_entry)
2052 usb_unlink_urb(urbtrack->urb);
2053 spin_unlock_irqrestore(&mos_parport->listlock, flags);
2054
2055 kref_put(&mos_parport->ref_count, destroy_mos_parport);
2056 }
2057#endif
Johan Hovold4230af52012-10-25 10:29:05 +02002058}
2059
2060static int mos7720_port_probe(struct usb_serial_port *port)
2061{
2062 struct moschip_port *mos7720_port;
2063
2064 mos7720_port = kzalloc(sizeof(*mos7720_port), GFP_KERNEL);
2065 if (!mos7720_port)
2066 return -ENOMEM;
2067
2068 /* Initialize all port interrupt end point to port 0 int endpoint.
2069 * Our device has only one interrupt endpoint common to all ports.
2070 */
2071 port->interrupt_in_endpointAddress =
2072 port->serial->port[0]->interrupt_in_endpointAddress;
2073 mos7720_port->port = port;
2074
2075 usb_set_serial_port_data(port, mos7720_port);
2076
2077 return 0;
2078}
2079
2080static int mos7720_port_remove(struct usb_serial_port *port)
2081{
2082 struct moschip_port *mos7720_port;
2083
2084 mos7720_port = usb_get_serial_port_data(port);
2085 kfree(mos7720_port);
2086
2087 return 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002088}
2089
2090static struct usb_serial_driver moschip7720_2port_driver = {
2091 .driver = {
2092 .owner = THIS_MODULE,
2093 .name = "moschip7720",
2094 },
2095 .description = "Moschip 2 port adapter",
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002096 .id_table = id_table,
Mike Dunnfb088e32010-01-26 12:12:12 -05002097 .calc_num_ports = mos77xx_calc_num_ports,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002098 .open = mos7720_open,
2099 .close = mos7720_close,
2100 .throttle = mos7720_throttle,
2101 .unthrottle = mos7720_unthrottle,
Mike Dunnfb088e32010-01-26 12:12:12 -05002102 .probe = mos77xx_probe,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002103 .attach = mos7720_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002104 .release = mos7720_release,
Johan Hovold4230af52012-10-25 10:29:05 +02002105 .port_probe = mos7720_port_probe,
2106 .port_remove = mos7720_port_remove,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002107 .ioctl = mos7720_ioctl,
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07002108 .tiocmget = mos7720_tiocmget,
2109 .tiocmset = mos7720_tiocmset,
Alan Cox0bca1b92010-09-16 18:21:40 +01002110 .get_icount = mos7720_get_icount,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002111 .set_termios = mos7720_set_termios,
2112 .write = mos7720_write,
2113 .write_room = mos7720_write_room,
2114 .chars_in_buffer = mos7720_chars_in_buffer,
2115 .break_ctl = mos7720_break,
2116 .read_bulk_callback = mos7720_bulk_in_callback,
Mike Dunnfb088e32010-01-26 12:12:12 -05002117 .read_int_callback = NULL /* dynamically assigned in probe() */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002118};
2119
Alan Stern4d2a7af2012-02-23 14:57:09 -05002120static struct usb_serial_driver * const serial_drivers[] = {
2121 &moschip7720_2port_driver, NULL
2122};
2123
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002124module_usb_serial_driver(serial_drivers, id_table);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002125
Alan Cox4da1a172008-07-22 11:16:21 +01002126MODULE_AUTHOR(DRIVER_AUTHOR);
2127MODULE_DESCRIPTION(DRIVER_DESC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002128MODULE_LICENSE("GPL");