blob: ee68191e87c62ee0d4ba667ae15937b77f607e28 [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 */
Johan Hovold849513a2013-05-27 14:44:43 +020043#define MOS_WDR_TIMEOUT 5000
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070044
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;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070065 struct usb_serial_port *port; /* loop back to the owner */
66 struct urb *write_urb_pool[NUM_URBS];
67};
68
Mike Dunnfb088e32010-01-26 12:12:12 -050069static struct usb_serial_driver moschip7720_2port_driver;
70
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070071#define USB_VENDOR_ID_MOSCHIP 0x9710
72#define MOSCHIP_DEVICE_ID_7720 0x7720
73#define MOSCHIP_DEVICE_ID_7715 0x7715
74
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -070075static const struct usb_device_id id_table[] = {
Alan Cox4da1a172008-07-22 11:16:21 +010076 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
Mike Dunnfb088e32010-01-26 12:12:12 -050077 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) },
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070078 { } /* terminating entry */
79};
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -070080MODULE_DEVICE_TABLE(usb, id_table);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070081
Mike Dunnb69578d2010-04-15 17:01:33 -040082#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
83
84/* initial values for parport regs */
85#define DCR_INIT_VAL 0x0c /* SLCTIN, nINIT */
86#define ECR_INIT_VAL 0x00 /* SPP mode */
87
88struct urbtracker {
89 struct mos7715_parport *mos_parport;
90 struct list_head urblist_entry;
91 struct kref ref_count;
92 struct urb *urb;
Johan Hovoldef6c8c12013-08-13 13:27:34 +020093 struct usb_ctrlrequest *setup;
Mike Dunnb69578d2010-04-15 17:01:33 -040094};
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);
Johan Hovold72ea18a2013-05-27 14:44:39 +0200231 u8 *buf;
232 int status;
233
234 buf = kmalloc(1, GFP_KERNEL);
235 if (!buf)
236 return -ENOMEM;
237
238 status = usb_control_msg(usbdev, pipe, request, requesttype, value,
239 index, buf, 1, MOS_WDR_TIMEOUT);
240 if (status == 1)
241 *data = *buf;
242 else if (status < 0)
Mike Dunnb69578d2010-04-15 17:01:33 -0400243 dev_err(&usbdev->dev,
244 "mos7720: usb_control_msg() failed: %d", status);
Johan Hovold72ea18a2013-05-27 14:44:39 +0200245 kfree(buf);
246
Mike Dunnb69578d2010-04-15 17:01:33 -0400247 return status;
248}
249
Mike Dunn63b91762010-04-15 17:02:09 -0400250#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
251
Mike Dunnb69578d2010-04-15 17:01:33 -0400252static inline int mos7715_change_mode(struct mos7715_parport *mos_parport,
253 enum mos7715_pp_modes mode)
254{
255 mos_parport->shadowECR = mode;
Mike Dunn63b91762010-04-15 17:02:09 -0400256 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400257 return 0;
258}
259
260static void destroy_mos_parport(struct kref *kref)
261{
262 struct mos7715_parport *mos_parport =
263 container_of(kref, struct mos7715_parport, ref_count);
264
Mike Dunnb69578d2010-04-15 17:01:33 -0400265 kfree(mos_parport);
266}
267
268static void destroy_urbtracker(struct kref *kref)
269{
270 struct urbtracker *urbtrack =
271 container_of(kref, struct urbtracker, ref_count);
272 struct mos7715_parport *mos_parport = urbtrack->mos_parport;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700273
Mike Dunnb69578d2010-04-15 17:01:33 -0400274 usb_free_urb(urbtrack->urb);
Johan Hovoldef6c8c12013-08-13 13:27:34 +0200275 kfree(urbtrack->setup);
Mike Dunnb69578d2010-04-15 17:01:33 -0400276 kfree(urbtrack);
277 kref_put(&mos_parport->ref_count, destroy_mos_parport);
278}
279
280/*
281 * This runs as a tasklet when sending an urb in a non-blocking parallel
282 * port callback had to be deferred because the disconnect mutex could not be
283 * obtained at the time.
284 */
285static void send_deferred_urbs(unsigned long _mos_parport)
286{
287 int ret_val;
288 unsigned long flags;
289 struct mos7715_parport *mos_parport = (void *)_mos_parport;
Wei Yongjun67990472012-08-21 11:28:45 +0800290 struct urbtracker *urbtrack, *tmp;
Mike Dunnb69578d2010-04-15 17:01:33 -0400291 struct list_head *cursor, *next;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700292 struct device *dev;
Mike Dunnb69578d2010-04-15 17:01:33 -0400293
Mike Dunnb69578d2010-04-15 17:01:33 -0400294 /* if release function ran, game over */
295 if (unlikely(mos_parport->serial == NULL))
296 return;
297
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700298 dev = &mos_parport->serial->dev->dev;
299
Mike Dunnb69578d2010-04-15 17:01:33 -0400300 /* try again to get the mutex */
301 if (!mutex_trylock(&mos_parport->serial->disc_mutex)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700302 dev_dbg(dev, "%s: rescheduling tasklet\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400303 tasklet_schedule(&mos_parport->urb_tasklet);
304 return;
305 }
306
307 /* if device disconnected, game over */
308 if (unlikely(mos_parport->serial->disconnected)) {
309 mutex_unlock(&mos_parport->serial->disc_mutex);
310 return;
311 }
312
313 spin_lock_irqsave(&mos_parport->listlock, flags);
314 if (list_empty(&mos_parport->deferred_urbs)) {
315 spin_unlock_irqrestore(&mos_parport->listlock, flags);
316 mutex_unlock(&mos_parport->serial->disc_mutex);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700317 dev_dbg(dev, "%s: deferred_urbs list empty\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400318 return;
319 }
320
321 /* move contents of deferred_urbs list to active_urbs list and submit */
322 list_for_each_safe(cursor, next, &mos_parport->deferred_urbs)
323 list_move_tail(cursor, &mos_parport->active_urbs);
Wei Yongjun67990472012-08-21 11:28:45 +0800324 list_for_each_entry_safe(urbtrack, tmp, &mos_parport->active_urbs,
Mike Dunnb69578d2010-04-15 17:01:33 -0400325 urblist_entry) {
326 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700327 dev_dbg(dev, "%s: urb submitted\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400328 if (ret_val) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700329 dev_err(dev, "usb_submit_urb() failed: %d\n", ret_val);
Mike Dunnb69578d2010-04-15 17:01:33 -0400330 list_del(&urbtrack->urblist_entry);
331 kref_put(&urbtrack->ref_count, destroy_urbtracker);
332 }
333 }
334 spin_unlock_irqrestore(&mos_parport->listlock, flags);
335 mutex_unlock(&mos_parport->serial->disc_mutex);
336}
337
338/* callback for parallel port control urbs submitted asynchronously */
339static void async_complete(struct urb *urb)
340{
341 struct urbtracker *urbtrack = urb->context;
342 int status = urb->status;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700343
Mike Dunnb69578d2010-04-15 17:01:33 -0400344 if (unlikely(status))
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700345 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
Mike Dunnb69578d2010-04-15 17:01:33 -0400346
347 /* remove the urbtracker from the active_urbs list */
348 spin_lock(&urbtrack->mos_parport->listlock);
349 list_del(&urbtrack->urblist_entry);
350 spin_unlock(&urbtrack->mos_parport->listlock);
351 kref_put(&urbtrack->ref_count, destroy_urbtracker);
352}
353
354static int write_parport_reg_nonblock(struct mos7715_parport *mos_parport,
355 enum mos_regs reg, __u8 data)
356{
357 struct urbtracker *urbtrack;
358 int ret_val;
359 unsigned long flags;
Mike Dunnb69578d2010-04-15 17:01:33 -0400360 struct usb_serial *serial = mos_parport->serial;
361 struct usb_device *usbdev = serial->dev;
Mike Dunnb69578d2010-04-15 17:01:33 -0400362
363 /* create and initialize the control urb and containing urbtracker */
364 urbtrack = kmalloc(sizeof(struct urbtracker), GFP_ATOMIC);
Johan Hovold10c642d2013-12-29 19:22:56 +0100365 if (!urbtrack)
Mike Dunnb69578d2010-04-15 17:01:33 -0400366 return -ENOMEM;
Johan Hovold10c642d2013-12-29 19:22:56 +0100367
Mike Dunnb69578d2010-04-15 17:01:33 -0400368 kref_get(&mos_parport->ref_count);
369 urbtrack->mos_parport = mos_parport;
370 urbtrack->urb = usb_alloc_urb(0, GFP_ATOMIC);
Johan Hovold10c642d2013-12-29 19:22:56 +0100371 if (!urbtrack->urb) {
Mike Dunnb69578d2010-04-15 17:01:33 -0400372 kfree(urbtrack);
373 return -ENOMEM;
374 }
Dan Carpenterd0bd9a42013-08-16 10:16:59 +0300375 urbtrack->setup = kmalloc(sizeof(*urbtrack->setup), GFP_ATOMIC);
Johan Hovoldef6c8c12013-08-13 13:27:34 +0200376 if (!urbtrack->setup) {
377 usb_free_urb(urbtrack->urb);
378 kfree(urbtrack);
379 return -ENOMEM;
380 }
381 urbtrack->setup->bRequestType = (__u8)0x40;
382 urbtrack->setup->bRequest = (__u8)0x0e;
Johan Hovold3b716ca2013-08-19 13:05:45 +0200383 urbtrack->setup->wValue = cpu_to_le16(get_reg_value(reg, dummy));
384 urbtrack->setup->wIndex = cpu_to_le16(get_reg_index(reg));
Johan Hovoldef6c8c12013-08-13 13:27:34 +0200385 urbtrack->setup->wLength = 0;
Mike Dunnb69578d2010-04-15 17:01:33 -0400386 usb_fill_control_urb(urbtrack->urb, usbdev,
387 usb_sndctrlpipe(usbdev, 0),
Johan Hovoldef6c8c12013-08-13 13:27:34 +0200388 (unsigned char *)urbtrack->setup,
Mike Dunnb69578d2010-04-15 17:01:33 -0400389 NULL, 0, async_complete, urbtrack);
390 kref_init(&urbtrack->ref_count);
391 INIT_LIST_HEAD(&urbtrack->urblist_entry);
392
393 /*
394 * get the disconnect mutex, or add tracker to the deferred_urbs list
395 * and schedule a tasklet to try again later
396 */
397 if (!mutex_trylock(&serial->disc_mutex)) {
398 spin_lock_irqsave(&mos_parport->listlock, flags);
399 list_add_tail(&urbtrack->urblist_entry,
400 &mos_parport->deferred_urbs);
401 spin_unlock_irqrestore(&mos_parport->listlock, flags);
402 tasklet_schedule(&mos_parport->urb_tasklet);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700403 dev_dbg(&usbdev->dev, "tasklet scheduled");
Mike Dunnb69578d2010-04-15 17:01:33 -0400404 return 0;
405 }
406
407 /* bail if device disconnected */
408 if (serial->disconnected) {
409 kref_put(&urbtrack->ref_count, destroy_urbtracker);
410 mutex_unlock(&serial->disc_mutex);
411 return -ENODEV;
412 }
413
414 /* add the tracker to the active_urbs list and submit */
415 spin_lock_irqsave(&mos_parport->listlock, flags);
416 list_add_tail(&urbtrack->urblist_entry, &mos_parport->active_urbs);
417 spin_unlock_irqrestore(&mos_parport->listlock, flags);
418 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
419 mutex_unlock(&serial->disc_mutex);
420 if (ret_val) {
421 dev_err(&usbdev->dev,
422 "%s: submit_urb() failed: %d", __func__, ret_val);
423 spin_lock_irqsave(&mos_parport->listlock, flags);
424 list_del(&urbtrack->urblist_entry);
425 spin_unlock_irqrestore(&mos_parport->listlock, flags);
426 kref_put(&urbtrack->ref_count, destroy_urbtracker);
427 return ret_val;
428 }
429 return 0;
430}
431
432/*
433 * This is the the common top part of all parallel port callback operations that
434 * send synchronous messages to the device. This implements convoluted locking
435 * that avoids two scenarios: (1) a port operation is called after usbserial
436 * has called our release function, at which point struct mos7715_parport has
437 * been destroyed, and (2) the device has been disconnected, but usbserial has
438 * not called the release function yet because someone has a serial port open.
439 * The shared release_lock prevents the first, and the mutex and disconnected
440 * flag maintained by usbserial covers the second. We also use the msg_pending
441 * flag to ensure that all synchronous usb messgage calls have completed before
442 * our release function can return.
443 */
444static int parport_prologue(struct parport *pp)
445{
446 struct mos7715_parport *mos_parport;
447
448 spin_lock(&release_lock);
449 mos_parport = pp->private_data;
450 if (unlikely(mos_parport == NULL)) {
451 /* release fn called, port struct destroyed */
452 spin_unlock(&release_lock);
453 return -1;
454 }
455 mos_parport->msg_pending = true; /* synch usb call pending */
Wolfram Sang16735d02013-11-14 14:32:02 -0800456 reinit_completion(&mos_parport->syncmsg_compl);
Mike Dunnb69578d2010-04-15 17:01:33 -0400457 spin_unlock(&release_lock);
458
459 mutex_lock(&mos_parport->serial->disc_mutex);
460 if (mos_parport->serial->disconnected) {
461 /* device disconnected */
462 mutex_unlock(&mos_parport->serial->disc_mutex);
463 mos_parport->msg_pending = false;
464 complete(&mos_parport->syncmsg_compl);
465 return -1;
466 }
467
468 return 0;
469}
470
471/*
472 * This is the the common bottom part of all parallel port functions that send
473 * synchronous messages to the device.
474 */
475static inline void parport_epilogue(struct parport *pp)
476{
477 struct mos7715_parport *mos_parport = pp->private_data;
478 mutex_unlock(&mos_parport->serial->disc_mutex);
479 mos_parport->msg_pending = false;
480 complete(&mos_parport->syncmsg_compl);
481}
482
483static void parport_mos7715_write_data(struct parport *pp, unsigned char d)
484{
485 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700486
Mike Dunnb69578d2010-04-15 17:01:33 -0400487 if (parport_prologue(pp) < 0)
488 return;
489 mos7715_change_mode(mos_parport, SPP);
Mike Dunn63b91762010-04-15 17:02:09 -0400490 write_mos_reg(mos_parport->serial, dummy, DPR, (__u8)d);
Mike Dunnb69578d2010-04-15 17:01:33 -0400491 parport_epilogue(pp);
492}
493
494static unsigned char parport_mos7715_read_data(struct parport *pp)
495{
496 struct mos7715_parport *mos_parport = pp->private_data;
497 unsigned char d;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700498
Mike Dunnb69578d2010-04-15 17:01:33 -0400499 if (parport_prologue(pp) < 0)
500 return 0;
Mike Dunn63b91762010-04-15 17:02:09 -0400501 read_mos_reg(mos_parport->serial, dummy, DPR, &d);
Mike Dunnb69578d2010-04-15 17:01:33 -0400502 parport_epilogue(pp);
503 return d;
504}
505
506static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
507{
508 struct mos7715_parport *mos_parport = pp->private_data;
509 __u8 data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700510
Mike Dunnb69578d2010-04-15 17:01:33 -0400511 if (parport_prologue(pp) < 0)
512 return;
513 data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
Mike Dunn63b91762010-04-15 17:02:09 -0400514 write_mos_reg(mos_parport->serial, dummy, DCR, data);
Mike Dunnb69578d2010-04-15 17:01:33 -0400515 mos_parport->shadowDCR = data;
516 parport_epilogue(pp);
517}
518
519static unsigned char parport_mos7715_read_control(struct parport *pp)
520{
521 struct mos7715_parport *mos_parport = pp->private_data;
522 __u8 dcr;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700523
Mike Dunnb69578d2010-04-15 17:01:33 -0400524 spin_lock(&release_lock);
525 mos_parport = pp->private_data;
526 if (unlikely(mos_parport == NULL)) {
527 spin_unlock(&release_lock);
528 return 0;
529 }
530 dcr = mos_parport->shadowDCR & 0x0f;
531 spin_unlock(&release_lock);
532 return dcr;
533}
534
535static unsigned char parport_mos7715_frob_control(struct parport *pp,
536 unsigned char mask,
537 unsigned char val)
538{
539 struct mos7715_parport *mos_parport = pp->private_data;
540 __u8 dcr;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700541
Mike Dunnb69578d2010-04-15 17:01:33 -0400542 mask &= 0x0f;
543 val &= 0x0f;
544 if (parport_prologue(pp) < 0)
545 return 0;
546 mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
Mike Dunn63b91762010-04-15 17:02:09 -0400547 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400548 dcr = mos_parport->shadowDCR & 0x0f;
549 parport_epilogue(pp);
550 return dcr;
551}
552
553static unsigned char parport_mos7715_read_status(struct parport *pp)
554{
555 unsigned char status;
556 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700557
Mike Dunnb69578d2010-04-15 17:01:33 -0400558 spin_lock(&release_lock);
559 mos_parport = pp->private_data;
560 if (unlikely(mos_parport == NULL)) { /* release called */
561 spin_unlock(&release_lock);
562 return 0;
563 }
564 status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
565 spin_unlock(&release_lock);
566 return status;
567}
568
569static void parport_mos7715_enable_irq(struct parport *pp)
570{
Mike Dunnb69578d2010-04-15 17:01:33 -0400571}
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700572
Mike Dunnb69578d2010-04-15 17:01:33 -0400573static void parport_mos7715_disable_irq(struct parport *pp)
574{
Mike Dunnb69578d2010-04-15 17:01:33 -0400575}
576
577static void parport_mos7715_data_forward(struct parport *pp)
578{
579 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700580
Mike Dunnb69578d2010-04-15 17:01:33 -0400581 if (parport_prologue(pp) < 0)
582 return;
583 mos7715_change_mode(mos_parport, PS2);
584 mos_parport->shadowDCR &= ~0x20;
Mike Dunn63b91762010-04-15 17:02:09 -0400585 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400586 parport_epilogue(pp);
587}
588
589static void parport_mos7715_data_reverse(struct parport *pp)
590{
591 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700592
Mike Dunnb69578d2010-04-15 17:01:33 -0400593 if (parport_prologue(pp) < 0)
594 return;
595 mos7715_change_mode(mos_parport, PS2);
596 mos_parport->shadowDCR |= 0x20;
Mike Dunn63b91762010-04-15 17:02:09 -0400597 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400598 parport_epilogue(pp);
599}
600
601static void parport_mos7715_init_state(struct pardevice *dev,
602 struct parport_state *s)
603{
Mike Dunnb69578d2010-04-15 17:01:33 -0400604 s->u.pc.ctr = DCR_INIT_VAL;
605 s->u.pc.ecr = ECR_INIT_VAL;
606}
607
608/* N.B. Parport core code requires that this function not block */
609static void parport_mos7715_save_state(struct parport *pp,
610 struct parport_state *s)
611{
612 struct mos7715_parport *mos_parport;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700613
Mike Dunnb69578d2010-04-15 17:01:33 -0400614 spin_lock(&release_lock);
615 mos_parport = pp->private_data;
616 if (unlikely(mos_parport == NULL)) { /* release called */
617 spin_unlock(&release_lock);
618 return;
619 }
620 s->u.pc.ctr = mos_parport->shadowDCR;
621 s->u.pc.ecr = mos_parport->shadowECR;
622 spin_unlock(&release_lock);
623}
624
625/* N.B. Parport core code requires that this function not block */
626static void parport_mos7715_restore_state(struct parport *pp,
627 struct parport_state *s)
628{
629 struct mos7715_parport *mos_parport;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700630
Mike Dunnb69578d2010-04-15 17:01:33 -0400631 spin_lock(&release_lock);
632 mos_parport = pp->private_data;
633 if (unlikely(mos_parport == NULL)) { /* release called */
634 spin_unlock(&release_lock);
635 return;
636 }
637 write_parport_reg_nonblock(mos_parport, DCR, mos_parport->shadowDCR);
638 write_parport_reg_nonblock(mos_parport, ECR, mos_parport->shadowECR);
639 spin_unlock(&release_lock);
640}
641
642static size_t parport_mos7715_write_compat(struct parport *pp,
643 const void *buffer,
644 size_t len, int flags)
645{
646 int retval;
647 struct mos7715_parport *mos_parport = pp->private_data;
648 int actual_len;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700649
Mike Dunnb69578d2010-04-15 17:01:33 -0400650 if (parport_prologue(pp) < 0)
651 return 0;
652 mos7715_change_mode(mos_parport, PPF);
653 retval = usb_bulk_msg(mos_parport->serial->dev,
654 usb_sndbulkpipe(mos_parport->serial->dev, 2),
655 (void *)buffer, len, &actual_len,
656 MOS_WDR_TIMEOUT);
657 parport_epilogue(pp);
658 if (retval) {
659 dev_err(&mos_parport->serial->dev->dev,
660 "mos7720: usb_bulk_msg() failed: %d", retval);
661 return 0;
662 }
663 return actual_len;
664}
665
666static struct parport_operations parport_mos7715_ops = {
667 .owner = THIS_MODULE,
668 .write_data = parport_mos7715_write_data,
669 .read_data = parport_mos7715_read_data,
670
671 .write_control = parport_mos7715_write_control,
672 .read_control = parport_mos7715_read_control,
673 .frob_control = parport_mos7715_frob_control,
674
675 .read_status = parport_mos7715_read_status,
676
677 .enable_irq = parport_mos7715_enable_irq,
678 .disable_irq = parport_mos7715_disable_irq,
679
680 .data_forward = parport_mos7715_data_forward,
681 .data_reverse = parport_mos7715_data_reverse,
682
683 .init_state = parport_mos7715_init_state,
684 .save_state = parport_mos7715_save_state,
685 .restore_state = parport_mos7715_restore_state,
686
687 .compat_write_data = parport_mos7715_write_compat,
688
689 .nibble_read_data = parport_ieee1284_read_nibble,
690 .byte_read_data = parport_ieee1284_read_byte,
691};
692
693/*
694 * Allocate and initialize parallel port control struct, initialize
695 * the parallel port hardware device, and register with the parport subsystem.
696 */
697static int mos7715_parport_init(struct usb_serial *serial)
698{
699 struct mos7715_parport *mos_parport;
700
701 /* allocate and initialize parallel port control struct */
702 mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +0100703 if (!mos_parport)
Mike Dunnb69578d2010-04-15 17:01:33 -0400704 return -ENOMEM;
Johan Hovold10c642d2013-12-29 19:22:56 +0100705
Mike Dunnb69578d2010-04-15 17:01:33 -0400706 mos_parport->msg_pending = false;
707 kref_init(&mos_parport->ref_count);
708 spin_lock_init(&mos_parport->listlock);
709 INIT_LIST_HEAD(&mos_parport->active_urbs);
710 INIT_LIST_HEAD(&mos_parport->deferred_urbs);
711 usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
712 mos_parport->serial = serial;
713 tasklet_init(&mos_parport->urb_tasklet, send_deferred_urbs,
714 (unsigned long) mos_parport);
715 init_completion(&mos_parport->syncmsg_compl);
716
717 /* cycle parallel port reset bit */
Mike Dunn63b91762010-04-15 17:02:09 -0400718 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x80);
719 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x00);
Mike Dunnb69578d2010-04-15 17:01:33 -0400720
721 /* initialize device registers */
722 mos_parport->shadowDCR = DCR_INIT_VAL;
Mike Dunn63b91762010-04-15 17:02:09 -0400723 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400724 mos_parport->shadowECR = ECR_INIT_VAL;
Mike Dunn63b91762010-04-15 17:02:09 -0400725 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400726
727 /* register with parport core */
728 mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE,
729 PARPORT_DMA_NONE,
730 &parport_mos7715_ops);
731 if (mos_parport->pp == NULL) {
732 dev_err(&serial->interface->dev,
733 "Could not register parport\n");
734 kref_put(&mos_parport->ref_count, destroy_mos_parport);
735 return -EIO;
736 }
737 mos_parport->pp->private_data = mos_parport;
738 mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP;
739 mos_parport->pp->dev = &serial->interface->dev;
740 parport_announce_port(mos_parport->pp);
741
742 return 0;
743}
744#endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700745
746/*
747 * mos7720_interrupt_callback
748 * this is the callback function for when we have received data on the
749 * interrupt endpoint.
750 */
751static void mos7720_interrupt_callback(struct urb *urb)
752{
753 int result;
754 int length;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700755 int status = urb->status;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700756 struct device *dev = &urb->dev->dev;
Oliver Neukum325b70c2007-03-19 13:58:29 +0100757 __u8 *data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700758 __u8 sp1;
759 __u8 sp2;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700760
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700761 switch (status) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700762 case 0:
763 /* success */
764 break;
765 case -ECONNRESET:
766 case -ENOENT:
767 case -ESHUTDOWN:
768 /* this urb is terminated, clean up */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700769 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700770 return;
771 default:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700772 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700773 goto exit;
774 }
775
776 length = urb->actual_length;
777 data = urb->transfer_buffer;
778
779 /* Moschip get 4 bytes
780 * Byte 1 IIR Port 1 (port.number is 0)
781 * Byte 2 IIR Port 2 (port.number is 1)
782 * Byte 3 --------------
783 * Byte 4 FIFO status for both */
Oliver Neukum325b70c2007-03-19 13:58:29 +0100784
785 /* the above description is inverted
786 * oneukum 2007-03-14 */
787
788 if (unlikely(length != 4)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700789 dev_dbg(dev, "Wrong data !!!\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700790 return;
791 }
792
Oliver Neukum325b70c2007-03-19 13:58:29 +0100793 sp1 = data[3];
794 sp2 = data[2];
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700795
Oliver Neukum325b70c2007-03-19 13:58:29 +0100796 if ((sp1 | sp2) & 0x01) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700797 /* No Interrupt Pending in both the ports */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700798 dev_dbg(dev, "No Interrupt !!!\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700799 } else {
800 switch (sp1 & 0x0f) {
801 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700802 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 -0700803 break;
804 case SERIAL_IIR_CTI:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700805 dev_dbg(dev, "Serial Port 1: Receiver time out\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700806 break;
807 case SERIAL_IIR_MS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700808 /* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700809 break;
810 }
811
812 switch (sp2 & 0x0f) {
813 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700814 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 -0700815 break;
816 case SERIAL_IIR_CTI:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700817 dev_dbg(dev, "Serial Port 2: Receiver time out\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700818 break;
819 case SERIAL_IIR_MS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700820 /* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700821 break;
822 }
823 }
824
825exit:
826 result = usb_submit_urb(urb, GFP_ATOMIC);
827 if (result)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700828 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700829}
830
831/*
Mike Dunnfb088e32010-01-26 12:12:12 -0500832 * mos7715_interrupt_callback
833 * this is the 7715's callback function for when we have received data on
834 * the interrupt endpoint.
835 */
836static void mos7715_interrupt_callback(struct urb *urb)
837{
838 int result;
839 int length;
840 int status = urb->status;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700841 struct device *dev = &urb->dev->dev;
Mike Dunnfb088e32010-01-26 12:12:12 -0500842 __u8 *data;
843 __u8 iir;
844
845 switch (status) {
846 case 0:
847 /* success */
848 break;
849 case -ECONNRESET:
850 case -ENOENT:
851 case -ESHUTDOWN:
Mike Dunnb69578d2010-04-15 17:01:33 -0400852 case -ENODEV:
Mike Dunnfb088e32010-01-26 12:12:12 -0500853 /* this urb is terminated, clean up */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700854 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
Mike Dunnfb088e32010-01-26 12:12:12 -0500855 return;
856 default:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700857 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
Mike Dunnfb088e32010-01-26 12:12:12 -0500858 goto exit;
859 }
860
861 length = urb->actual_length;
862 data = urb->transfer_buffer;
863
864 /* Structure of data from 7715 device:
865 * Byte 1: IIR serial Port
866 * Byte 2: unused
867 * Byte 2: DSR parallel port
868 * Byte 4: FIFO status for both */
869
870 if (unlikely(length != 4)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700871 dev_dbg(dev, "Wrong data !!!\n");
Mike Dunnfb088e32010-01-26 12:12:12 -0500872 return;
873 }
874
875 iir = data[0];
876 if (!(iir & 0x01)) { /* serial port interrupt pending */
877 switch (iir & 0x0f) {
878 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700879 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 -0500880 break;
881 case SERIAL_IIR_CTI:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700882 dev_dbg(dev, "Serial Port: Receiver time out\n");
Mike Dunnfb088e32010-01-26 12:12:12 -0500883 break;
884 case SERIAL_IIR_MS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700885 /* dev_dbg(dev, "Serial Port: Modem status change\n"); */
Mike Dunnfb088e32010-01-26 12:12:12 -0500886 break;
887 }
888 }
889
Mike Dunnb69578d2010-04-15 17:01:33 -0400890#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
891 { /* update local copy of DSR reg */
892 struct usb_serial_port *port = urb->context;
893 struct mos7715_parport *mos_parport = port->serial->private;
894 if (unlikely(mos_parport == NULL))
895 return;
896 atomic_set(&mos_parport->shadowDSR, data[2]);
897 }
898#endif
899
Mike Dunnfb088e32010-01-26 12:12:12 -0500900exit:
901 result = usb_submit_urb(urb, GFP_ATOMIC);
902 if (result)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700903 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
Mike Dunnfb088e32010-01-26 12:12:12 -0500904}
905
906/*
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700907 * mos7720_bulk_in_callback
908 * this is the callback function for when we have received data on the
909 * bulk in endpoint.
910 */
911static void mos7720_bulk_in_callback(struct urb *urb)
912{
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700913 int retval;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700914 unsigned char *data ;
915 struct usb_serial_port *port;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700916 int status = urb->status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700917
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700918 if (status) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700919 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700920 return;
921 }
922
Mike Dunnb69578d2010-04-15 17:01:33 -0400923 port = urb->context;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700924
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700925 dev_dbg(&port->dev, "Entering...%s\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700926
927 data = urb->transfer_buffer;
928
Jiri Slaby2e124b42013-01-03 15:53:06 +0100929 if (urb->actual_length) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100930 tty_insert_flip_string(&port->port, data, urb->actual_length);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100931 tty_flip_buffer_push(&port->port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700932 }
933
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700934 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700935 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
936 if (retval)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700937 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700938 }
939}
940
941/*
942 * mos7720_bulk_out_data_callback
943 * this is the callback function for when we have finished sending serial
944 * data on the bulk out endpoint.
945 */
946static void mos7720_bulk_out_data_callback(struct urb *urb)
947{
948 struct moschip_port *mos7720_port;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700949 int status = urb->status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700950
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700951 if (status) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700952 dev_dbg(&urb->dev->dev, "nonzero write bulk status received:%d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700953 return;
954 }
955
956 mos7720_port = urb->context;
957 if (!mos7720_port) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700958 dev_dbg(&urb->dev->dev, "NULL mos7720_port pointer\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700959 return ;
960 }
961
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100962 if (mos7720_port->open)
963 tty_port_tty_wakeup(&mos7720_port->port->port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700964}
965
966/*
Mike Dunnfb088e32010-01-26 12:12:12 -0500967 * mos77xx_probe
968 * this function installs the appropriate read interrupt endpoint callback
969 * depending on whether the device is a 7720 or 7715, thus avoiding costly
970 * run-time checks in the high-frequency callback routine itself.
971 */
972static int mos77xx_probe(struct usb_serial *serial,
973 const struct usb_device_id *id)
974{
975 if (id->idProduct == MOSCHIP_DEVICE_ID_7715)
976 moschip7720_2port_driver.read_int_callback =
977 mos7715_interrupt_callback;
978 else
979 moschip7720_2port_driver.read_int_callback =
980 mos7720_interrupt_callback;
981
982 return 0;
983}
984
985static int mos77xx_calc_num_ports(struct usb_serial *serial)
986{
987 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
988 if (product == MOSCHIP_DEVICE_ID_7715)
989 return 1;
990
991 return 2;
992}
993
Alan Coxa509a7e2009-09-19 13:13:26 -0700994static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700995{
996 struct usb_serial *serial;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700997 struct urb *urb;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700998 struct moschip_port *mos7720_port;
999 int response;
1000 int port_number;
Mike Dunn63b91762010-04-15 17:02:09 -04001001 __u8 data;
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001002 int allocated_urbs = 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001003 int j;
1004
1005 serial = port->serial;
1006
1007 mos7720_port = usb_get_serial_port_data(port);
1008 if (mos7720_port == NULL)
1009 return -ENODEV;
1010
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001011 usb_clear_halt(serial->dev, port->write_urb->pipe);
1012 usb_clear_halt(serial->dev, port->read_urb->pipe);
1013
1014 /* Initialising the write urb pool */
1015 for (j = 0; j < NUM_URBS; ++j) {
Alan Cox4da1a172008-07-22 11:16:21 +01001016 urb = usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001017 mos7720_port->write_urb_pool[j] = urb;
Johan Hovold10c642d2013-12-29 19:22:56 +01001018 if (!urb)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001019 continue;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001020
1021 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1022 GFP_KERNEL);
1023 if (!urb->transfer_buffer) {
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001024 usb_free_urb(mos7720_port->write_urb_pool[j]);
1025 mos7720_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001026 continue;
1027 }
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001028 allocated_urbs++;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001029 }
1030
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001031 if (!allocated_urbs)
1032 return -ENOMEM;
1033
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001034 /* Initialize MCS7720 -- Write Init values to corresponding Registers
1035 *
1036 * Register Index
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001037 * 0 : THR/RHR
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001038 * 1 : IER
1039 * 2 : FCR
1040 * 3 : LCR
1041 * 4 : MCR
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001042 * 5 : LSR
1043 * 6 : MSR
1044 * 7 : SPR
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001045 *
1046 * 0x08 : SP1/2 Control Reg
1047 */
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001048 port_number = port->port_number;
Mike Dunn63b91762010-04-15 17:02:09 -04001049 read_mos_reg(serial, port_number, LSR, &data);
1050
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001051 dev_dbg(&port->dev, "SS::%p LSR:%x\n", mos7720_port, data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001052
Mike Dunn63b91762010-04-15 17:02:09 -04001053 write_mos_reg(serial, dummy, SP1_REG, 0x02);
1054 write_mos_reg(serial, dummy, SP2_REG, 0x02);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001055
Mike Dunn63b91762010-04-15 17:02:09 -04001056 write_mos_reg(serial, port_number, IER, 0x00);
1057 write_mos_reg(serial, port_number, FCR, 0x00);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001058
Mike Dunn63b91762010-04-15 17:02:09 -04001059 write_mos_reg(serial, port_number, FCR, 0xcf);
1060 mos7720_port->shadowLCR = 0x03;
1061 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1062 mos7720_port->shadowMCR = 0x0b;
1063 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001064
Mike Dunn63b91762010-04-15 17:02:09 -04001065 write_mos_reg(serial, port_number, SP_CONTROL_REG, 0x00);
1066 read_mos_reg(serial, dummy, SP_CONTROL_REG, &data);
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001067 data = data | (port->port_number + 1);
Mike Dunn63b91762010-04-15 17:02:09 -04001068 write_mos_reg(serial, dummy, SP_CONTROL_REG, data);
1069 mos7720_port->shadowLCR = 0x83;
1070 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1071 write_mos_reg(serial, port_number, THR, 0x0c);
1072 write_mos_reg(serial, port_number, IER, 0x00);
1073 mos7720_port->shadowLCR = 0x03;
1074 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1075 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001076
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001077 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
1078 if (response)
Alan Cox4da1a172008-07-22 11:16:21 +01001079 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
1080 __func__, response);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001081
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001082 /* initialize our port settings */
1083 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
1084
1085 /* send a open port command */
1086 mos7720_port->open = 1;
1087
1088 return 0;
1089}
1090
1091/*
1092 * mos7720_chars_in_buffer
1093 * this function is called by the tty driver when it wants to know how many
1094 * bytes of data we currently have outstanding in the port (data that has
1095 * been written, but hasn't made it out the port yet)
1096 * If successful, we return the number of bytes left to be written in the
1097 * system,
1098 * Otherwise we return a negative error number.
1099 */
Alan Cox95da3102008-07-22 11:09:07 +01001100static int mos7720_chars_in_buffer(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001101{
Alan Cox95da3102008-07-22 11:09:07 +01001102 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001103 int i;
1104 int chars = 0;
1105 struct moschip_port *mos7720_port;
1106
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001107 mos7720_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001108 if (mos7720_port == NULL)
Alan Cox23198fd2009-07-20 16:05:27 +01001109 return 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001110
1111 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001112 if (mos7720_port->write_urb_pool[i] &&
1113 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001114 chars += URB_TRANSFER_BUFFER_SIZE;
1115 }
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001116 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001117 return chars;
1118}
1119
Alan Cox335f8512009-06-11 12:26:29 +01001120static void mos7720_close(struct usb_serial_port *port)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001121{
1122 struct usb_serial *serial;
1123 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001124 int j;
1125
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001126 serial = port->serial;
1127
1128 mos7720_port = usb_get_serial_port_data(port);
1129 if (mos7720_port == NULL)
1130 return;
1131
1132 for (j = 0; j < NUM_URBS; ++j)
1133 usb_kill_urb(mos7720_port->write_urb_pool[j]);
1134
1135 /* Freeing Write URBs */
1136 for (j = 0; j < NUM_URBS; ++j) {
1137 if (mos7720_port->write_urb_pool[j]) {
1138 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
1139 usb_free_urb(mos7720_port->write_urb_pool[j]);
1140 }
1141 }
1142
1143 /* While closing port, shutdown all bulk read, write *
Oliver Neukuma1cd7e92008-01-16 17:18:52 +01001144 * and interrupt read if they exists, otherwise nop */
Oliver Neukuma1cd7e92008-01-16 17:18:52 +01001145 usb_kill_urb(port->write_urb);
Oliver Neukuma1cd7e92008-01-16 17:18:52 +01001146 usb_kill_urb(port->read_urb);
1147
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001148 write_mos_reg(serial, port->port_number, MCR, 0x00);
1149 write_mos_reg(serial, port->port_number, IER, 0x00);
Johan Hovoldcf41aa92013-03-21 12:37:41 +01001150
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001151 mos7720_port->open = 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001152}
1153
Alan Cox95da3102008-07-22 11:09:07 +01001154static void mos7720_break(struct tty_struct *tty, int break_state)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001155{
Alan Cox95da3102008-07-22 11:09:07 +01001156 struct usb_serial_port *port = tty->driver_data;
Alan Cox4da1a172008-07-22 11:16:21 +01001157 unsigned char data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001158 struct usb_serial *serial;
1159 struct moschip_port *mos7720_port;
1160
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001161 serial = port->serial;
1162
1163 mos7720_port = usb_get_serial_port_data(port);
1164 if (mos7720_port == NULL)
1165 return;
1166
1167 if (break_state == -1)
1168 data = mos7720_port->shadowLCR | UART_LCR_SBC;
1169 else
1170 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
1171
1172 mos7720_port->shadowLCR = data;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001173 write_mos_reg(serial, port->port_number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001174}
1175
1176/*
1177 * mos7720_write_room
1178 * this function is called by the tty driver when it wants to know how many
1179 * bytes of data we can accept for a specific port.
1180 * If successful, we return the amount of room that we have for this port
1181 * Otherwise we return a negative error number.
1182 */
Alan Cox95da3102008-07-22 11:09:07 +01001183static int mos7720_write_room(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001184{
Alan Cox95da3102008-07-22 11:09:07 +01001185 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001186 struct moschip_port *mos7720_port;
1187 int room = 0;
1188 int i;
1189
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001190 mos7720_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001191 if (mos7720_port == NULL)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001192 return -ENODEV;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001193
Alan Coxa5b6f602008-04-08 17:16:06 +01001194 /* FIXME: Locking */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001195 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001196 if (mos7720_port->write_urb_pool[i] &&
1197 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001198 room += URB_TRANSFER_BUFFER_SIZE;
1199 }
1200
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001201 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001202 return room;
1203}
1204
Alan Cox95da3102008-07-22 11:09:07 +01001205static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
1206 const unsigned char *data, int count)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001207{
1208 int status;
1209 int i;
1210 int bytes_sent = 0;
1211 int transfer_size;
1212
1213 struct moschip_port *mos7720_port;
1214 struct usb_serial *serial;
1215 struct urb *urb;
1216 const unsigned char *current_position = data;
1217
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001218 serial = port->serial;
1219
1220 mos7720_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001221 if (mos7720_port == NULL)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001222 return -ENODEV;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001223
1224 /* try to find a free urb in the list */
1225 urb = NULL;
1226
1227 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001228 if (mos7720_port->write_urb_pool[i] &&
1229 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001230 urb = mos7720_port->write_urb_pool[i];
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001231 dev_dbg(&port->dev, "URB:%d\n", i);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001232 break;
1233 }
1234 }
1235
1236 if (urb == NULL) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001237 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001238 goto exit;
1239 }
1240
1241 if (urb->transfer_buffer == NULL) {
1242 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1243 GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +01001244 if (!urb->transfer_buffer)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001245 goto exit;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001246 }
Alan Cox4da1a172008-07-22 11:16:21 +01001247 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001248
1249 memcpy(urb->transfer_buffer, current_position, transfer_size);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001250 usb_serial_debug_data(&port->dev, __func__, transfer_size,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001251 urb->transfer_buffer);
1252
1253 /* fill urb with data and submit */
1254 usb_fill_bulk_urb(urb, serial->dev,
1255 usb_sndbulkpipe(serial->dev,
Alan Cox4da1a172008-07-22 11:16:21 +01001256 port->bulk_out_endpointAddress),
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001257 urb->transfer_buffer, transfer_size,
1258 mos7720_bulk_out_data_callback, mos7720_port);
1259
1260 /* send it down the pipe */
Alan Cox4da1a172008-07-22 11:16:21 +01001261 status = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001262 if (status) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001263 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001264 "with status = %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001265 bytes_sent = status;
1266 goto exit;
1267 }
1268 bytes_sent = transfer_size;
1269
1270exit:
1271 return bytes_sent;
1272}
1273
Alan Cox95da3102008-07-22 11:09:07 +01001274static void mos7720_throttle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001275{
Alan Cox95da3102008-07-22 11:09:07 +01001276 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001277 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001278 int status;
1279
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001280 mos7720_port = usb_get_serial_port_data(port);
1281
1282 if (mos7720_port == NULL)
1283 return;
1284
1285 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001286 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001287 return;
1288 }
1289
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001290 /* if we are implementing XON/XOFF, send the stop character */
1291 if (I_IXOFF(tty)) {
1292 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001293 status = mos7720_write(tty, port, &stop_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001294 if (status <= 0)
1295 return;
1296 }
1297
1298 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001299 if (tty->termios.c_cflag & CRTSCTS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001300 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001301 write_mos_reg(port->serial, port->port_number, MCR,
1302 mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001303 if (status != 0)
1304 return;
1305 }
1306}
1307
Alan Cox95da3102008-07-22 11:09:07 +01001308static void mos7720_unthrottle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001309{
Alan Cox95da3102008-07-22 11:09:07 +01001310 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001311 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +01001312 int status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001313
1314 if (mos7720_port == NULL)
1315 return;
1316
1317 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001318 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001319 return;
1320 }
1321
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001322 /* if we are implementing XON/XOFF, send the start character */
1323 if (I_IXOFF(tty)) {
1324 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001325 status = mos7720_write(tty, port, &start_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001326 if (status <= 0)
1327 return;
1328 }
1329
1330 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001331 if (tty->termios.c_cflag & CRTSCTS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001332 mos7720_port->shadowMCR |= UART_MCR_RTS;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001333 write_mos_reg(port->serial, port->port_number, MCR,
1334 mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001335 if (status != 0)
1336 return;
1337 }
1338}
1339
Mike Dunnb69578d2010-04-15 17:01:33 -04001340/* FIXME: this function does not work */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001341static int set_higher_rates(struct moschip_port *mos7720_port,
1342 unsigned int baud)
1343{
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001344 struct usb_serial_port *port;
1345 struct usb_serial *serial;
1346 int port_number;
Mike Dunn63b91762010-04-15 17:02:09 -04001347 enum mos_regs sp_reg;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001348 if (mos7720_port == NULL)
1349 return -EINVAL;
1350
1351 port = mos7720_port->port;
1352 serial = port->serial;
1353
Alan Cox4da1a172008-07-22 11:16:21 +01001354 /***********************************************
1355 * Init Sequence for higher rates
1356 ***********************************************/
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001357 dev_dbg(&port->dev, "Sending Setting Commands ..........\n");
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001358 port_number = port->port_number;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001359
Mike Dunn63b91762010-04-15 17:02:09 -04001360 write_mos_reg(serial, port_number, IER, 0x00);
1361 write_mos_reg(serial, port_number, FCR, 0x00);
1362 write_mos_reg(serial, port_number, FCR, 0xcf);
1363 mos7720_port->shadowMCR = 0x0b;
1364 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1365 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x00);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001366
Alan Cox4da1a172008-07-22 11:16:21 +01001367 /***********************************************
1368 * Set for higher rates *
1369 ***********************************************/
Mike Dunnb69578d2010-04-15 17:01:33 -04001370 /* writing baud rate verbatum into uart clock field clearly not right */
Mike Dunn63b91762010-04-15 17:02:09 -04001371 if (port_number == 0)
1372 sp_reg = SP1_REG;
1373 else
1374 sp_reg = SP2_REG;
1375 write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
1376 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x03);
1377 mos7720_port->shadowMCR = 0x2b;
1378 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001379
Alan Cox4da1a172008-07-22 11:16:21 +01001380 /***********************************************
1381 * Set DLL/DLM
1382 ***********************************************/
Mike Dunn63b91762010-04-15 17:02:09 -04001383 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1384 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1385 write_mos_reg(serial, port_number, DLL, 0x01);
1386 write_mos_reg(serial, port_number, DLM, 0x00);
1387 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1388 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001389
1390 return 0;
1391}
1392
1393/* baud rate information */
Alan Cox4da1a172008-07-22 11:16:21 +01001394struct divisor_table_entry {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001395 __u32 baudrate;
1396 __u16 divisor;
1397};
1398
1399/* Define table of divisors for moschip 7720 hardware *
1400 * These assume a 3.6864MHz crystal, the standard /16, and *
1401 * MCR.7 = 0. */
1402static struct divisor_table_entry divisor_table[] = {
1403 { 50, 2304},
1404 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
1405 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
1406 { 150, 768},
1407 { 300, 384},
1408 { 600, 192},
1409 { 1200, 96},
1410 { 1800, 64},
1411 { 2400, 48},
1412 { 4800, 24},
1413 { 7200, 16},
1414 { 9600, 12},
1415 { 19200, 6},
1416 { 38400, 3},
1417 { 57600, 2},
1418 { 115200, 1},
1419};
1420
1421/*****************************************************************************
1422 * calc_baud_rate_divisor
1423 * this function calculates the proper baud rate divisor for the specified
1424 * baud rate.
1425 *****************************************************************************/
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001426static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, int *divisor)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001427{
1428 int i;
1429 __u16 custom;
1430 __u16 round1;
1431 __u16 round;
1432
1433
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001434 dev_dbg(&port->dev, "%s - %d\n", __func__, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001435
1436 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
1437 if (divisor_table[i].baudrate == baudrate) {
1438 *divisor = divisor_table[i].divisor;
1439 return 0;
1440 }
1441 }
1442
Alan Cox4da1a172008-07-22 11:16:21 +01001443 /* After trying for all the standard baud rates *
1444 * Try calculating the divisor for this baud rate */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001445 if (baudrate > 75 && baudrate < 230400) {
1446 /* get the divisor */
1447 custom = (__u16)(230400L / baudrate);
1448
1449 /* Check for round off */
1450 round1 = (__u16)(2304000L / baudrate);
1451 round = (__u16)(round1 - (custom * 10));
1452 if (round > 4)
1453 custom++;
1454 *divisor = custom;
1455
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001456 dev_dbg(&port->dev, "Baud %d = %d\n", baudrate, custom);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001457 return 0;
1458 }
1459
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001460 dev_dbg(&port->dev, "Baud calculation Failed...\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001461 return -EINVAL;
1462}
1463
1464/*
1465 * send_cmd_write_baud_rate
1466 * this function sends the proper command to change the baud rate of the
1467 * specified port.
1468 */
1469static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1470 int baudrate)
1471{
1472 struct usb_serial_port *port;
1473 struct usb_serial *serial;
1474 int divisor;
1475 int status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001476 unsigned char number;
1477
1478 if (mos7720_port == NULL)
1479 return -1;
1480
1481 port = mos7720_port->port;
1482 serial = port->serial;
1483
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001484 number = port->port_number;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001485 dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001486
Alan Cox4da1a172008-07-22 11:16:21 +01001487 /* Calculate the Divisor */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001488 status = calc_baud_rate_divisor(port, baudrate, &divisor);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001489 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001490 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001491 return status;
1492 }
1493
Alan Cox4da1a172008-07-22 11:16:21 +01001494 /* Enable access to divisor latch */
Mike Dunn63b91762010-04-15 17:02:09 -04001495 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1496 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001497
1498 /* Write the divisor */
Mike Dunn63b91762010-04-15 17:02:09 -04001499 write_mos_reg(serial, number, DLL, (__u8)(divisor & 0xff));
1500 write_mos_reg(serial, number, DLM, (__u8)((divisor & 0xff00) >> 8));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001501
Alan Cox4da1a172008-07-22 11:16:21 +01001502 /* Disable access to divisor latch */
Mike Dunn63b91762010-04-15 17:02:09 -04001503 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1504 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001505
1506 return status;
1507}
1508
1509/*
1510 * change_port_settings
1511 * This routine is called to set the UART on the device to match
1512 * the specified new settings.
1513 */
Alan Cox95da3102008-07-22 11:09:07 +01001514static void change_port_settings(struct tty_struct *tty,
1515 struct moschip_port *mos7720_port,
Alan Cox606d0992006-12-08 02:38:45 -08001516 struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001517{
1518 struct usb_serial_port *port;
1519 struct usb_serial *serial;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001520 int baud;
1521 unsigned cflag;
1522 unsigned iflag;
1523 __u8 mask = 0xff;
1524 __u8 lData;
1525 __u8 lParity;
1526 __u8 lStop;
1527 int status;
1528 int port_number;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001529
1530 if (mos7720_port == NULL)
1531 return ;
1532
1533 port = mos7720_port->port;
1534 serial = port->serial;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001535 port_number = port->port_number;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001536
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001537 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001538 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001539 return;
1540 }
1541
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001542 lData = UART_LCR_WLEN8;
1543 lStop = 0x00; /* 1 stop bit */
1544 lParity = 0x00; /* No parity */
1545
Alan Coxadc8d742012-07-14 15:31:47 +01001546 cflag = tty->termios.c_cflag;
1547 iflag = tty->termios.c_iflag;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001548
1549 /* Change the number of bits */
1550 switch (cflag & CSIZE) {
1551 case CS5:
1552 lData = UART_LCR_WLEN5;
1553 mask = 0x1f;
1554 break;
1555
1556 case CS6:
1557 lData = UART_LCR_WLEN6;
1558 mask = 0x3f;
1559 break;
1560
1561 case CS7:
1562 lData = UART_LCR_WLEN7;
1563 mask = 0x7f;
1564 break;
1565 default:
1566 case CS8:
1567 lData = UART_LCR_WLEN8;
1568 break;
1569 }
1570
1571 /* Change the Parity bit */
1572 if (cflag & PARENB) {
1573 if (cflag & PARODD) {
1574 lParity = UART_LCR_PARITY;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001575 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001576 } else {
1577 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001578 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001579 }
1580
1581 } else {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001582 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001583 }
1584
1585 if (cflag & CMSPAR)
1586 lParity = lParity | 0x20;
1587
1588 /* Change the Stop bit */
1589 if (cflag & CSTOPB) {
1590 lStop = UART_LCR_STOP;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001591 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001592 } else {
1593 lStop = 0x00;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001594 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001595 }
1596
1597#define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1598#define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1599#define LCR_PAR_MASK 0x38 /* Mask for parity field */
1600
1601 /* Update the LCR with the correct value */
Alan Cox4da1a172008-07-22 11:16:21 +01001602 mos7720_port->shadowLCR &=
Mike Dunn63b91762010-04-15 17:02:09 -04001603 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001604 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1605
1606
1607 /* Disable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001608 write_mos_reg(serial, port_number, IER, 0x00);
1609 write_mos_reg(serial, port_number, FCR, 0x00);
1610 write_mos_reg(serial, port_number, FCR, 0xcf);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001611
1612 /* Send the updated LCR value to the mos7720 */
Mike Dunn63b91762010-04-15 17:02:09 -04001613 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1614 mos7720_port->shadowMCR = 0x0b;
1615 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001616
1617 /* set up the MCR register and send it to the mos7720 */
1618 mos7720_port->shadowMCR = UART_MCR_OUT2;
1619 if (cflag & CBAUD)
1620 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1621
1622 if (cflag & CRTSCTS) {
1623 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
Alan Cox4da1a172008-07-22 11:16:21 +01001624 /* To set hardware flow control to the specified *
1625 * serial port, in SP1/2_CONTROL_REG */
Johan Hovolda26f0092013-06-04 18:50:31 +02001626 if (port_number)
Mike Dunn63b91762010-04-15 17:02:09 -04001627 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x01);
1628 else
1629 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x02);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001630
Mike Dunn63b91762010-04-15 17:02:09 -04001631 } else
1632 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1633
1634 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001635
1636 /* Determine divisor based on baud rate */
1637 baud = tty_get_baud_rate(tty);
1638 if (!baud) {
1639 /* pick a default, any default... */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001640 dev_dbg(&port->dev, "Picked default baud...\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001641 baud = 9600;
1642 }
1643
1644 if (baud >= 230400) {
1645 set_higher_rates(mos7720_port, baud);
1646 /* Enable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001647 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001648 return;
1649 }
1650
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001651 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001652 status = send_cmd_write_baud_rate(mos7720_port, baud);
Alan Cox65d063ab2008-01-03 17:01:18 +00001653 /* FIXME: needs to write actual resulting baud back not just
1654 blindly do so */
1655 if (cflag & CBAUD)
1656 tty_encode_baud_rate(tty, baud, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001657 /* Enable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001658 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001659
1660 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001661 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1662 if (status)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001663 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001664 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001665}
1666
1667/*
1668 * mos7720_set_termios
1669 * this function is called by the tty driver when it wants to change the
1670 * termios structure.
1671 */
Alan Cox95da3102008-07-22 11:09:07 +01001672static void mos7720_set_termios(struct tty_struct *tty,
1673 struct usb_serial_port *port, struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001674{
1675 int status;
1676 unsigned int cflag;
1677 struct usb_serial *serial;
1678 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001679
1680 serial = port->serial;
1681
1682 mos7720_port = usb_get_serial_port_data(port);
1683
1684 if (mos7720_port == NULL)
1685 return;
1686
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001687 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001688 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001689 return;
1690 }
1691
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001692 dev_dbg(&port->dev, "setting termios - ASPIRE\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001693
Alan Coxadc8d742012-07-14 15:31:47 +01001694 cflag = tty->termios.c_cflag;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001695
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001696 dev_dbg(&port->dev, "%s - cflag %08x iflag %08x\n", __func__,
Linus Torvaldsd9a80742012-10-01 13:23:01 -07001697 tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001698
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001699 dev_dbg(&port->dev, "%s - old cflag %08x old iflag %08x\n", __func__,
1700 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001701
1702 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001703 change_port_settings(tty, mos7720_port, old_termios);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001704
Alan Cox4da1a172008-07-22 11:16:21 +01001705 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001706 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1707 if (status)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001708 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001709 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001710}
1711
1712/*
1713 * get_lsr_info - get line status register info
1714 *
1715 * Purpose: Let user call ioctl() to get info when the UART physically
1716 * is emptied. On bus types like RS485, the transmitter must
1717 * release the bus after transmitting. This must be done when
1718 * the transmit shift register is empty, not be done when the
1719 * transmit holding register is empty. This functionality
1720 * allows an RS485 driver to be written in user space.
1721 */
Alan Cox4da1a172008-07-22 11:16:21 +01001722static int get_lsr_info(struct tty_struct *tty,
1723 struct moschip_port *mos7720_port, unsigned int __user *value)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001724{
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001725 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001726 unsigned int result = 0;
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001727 unsigned char data = 0;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001728 int port_number = port->port_number;
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001729 int count;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001730
Alan Cox95da3102008-07-22 11:09:07 +01001731 count = mos7720_chars_in_buffer(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001732 if (count == 0) {
Mike Dunn63b91762010-04-15 17:02:09 -04001733 read_mos_reg(port->serial, port_number, LSR, &data);
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001734 if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1735 == (UART_LSR_TEMT | UART_LSR_THRE)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001736 dev_dbg(&port->dev, "%s -- Empty\n", __func__);
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001737 result = TIOCSER_TEMT;
1738 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001739 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001740 if (copy_to_user(value, &result, sizeof(int)))
1741 return -EFAULT;
1742 return 0;
1743}
1744
Alan Cox60b33c12011-02-14 16:26:14 +00001745static int mos7720_tiocmget(struct tty_struct *tty)
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001746{
1747 struct usb_serial_port *port = tty->driver_data;
1748 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1749 unsigned int result = 0;
1750 unsigned int mcr ;
1751 unsigned int msr ;
1752
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001753 mcr = mos7720_port->shadowMCR;
1754 msr = mos7720_port->shadowMSR;
1755
1756 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1757 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1758 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1759 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
1760 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1761 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
1762
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001763 return result;
1764}
1765
Alan Cox20b9d172011-02-14 16:26:50 +00001766static int mos7720_tiocmset(struct tty_struct *tty,
Mike Dunn63b91762010-04-15 17:02:09 -04001767 unsigned int set, unsigned int clear)
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001768{
1769 struct usb_serial_port *port = tty->driver_data;
1770 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1771 unsigned int mcr ;
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001772
1773 mcr = mos7720_port->shadowMCR;
1774
1775 if (set & TIOCM_RTS)
1776 mcr |= UART_MCR_RTS;
1777 if (set & TIOCM_DTR)
1778 mcr |= UART_MCR_DTR;
1779 if (set & TIOCM_LOOP)
1780 mcr |= UART_MCR_LOOP;
1781
1782 if (clear & TIOCM_RTS)
1783 mcr &= ~UART_MCR_RTS;
1784 if (clear & TIOCM_DTR)
1785 mcr &= ~UART_MCR_DTR;
1786 if (clear & TIOCM_LOOP)
1787 mcr &= ~UART_MCR_LOOP;
1788
1789 mos7720_port->shadowMCR = mcr;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001790 write_mos_reg(port->serial, port->port_number, MCR,
1791 mos7720_port->shadowMCR);
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001792
1793 return 0;
1794}
1795
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001796static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1797 unsigned int __user *value)
1798{
Alan Cox0bca1b92010-09-16 18:21:40 +01001799 unsigned int mcr;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001800 unsigned int arg;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001801
1802 struct usb_serial_port *port;
1803
1804 if (mos7720_port == NULL)
1805 return -1;
1806
Alan Cox4da1a172008-07-22 11:16:21 +01001807 port = (struct usb_serial_port *)mos7720_port->port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001808 mcr = mos7720_port->shadowMCR;
1809
1810 if (copy_from_user(&arg, value, sizeof(int)))
1811 return -EFAULT;
1812
1813 switch (cmd) {
1814 case TIOCMBIS:
1815 if (arg & TIOCM_RTS)
1816 mcr |= UART_MCR_RTS;
1817 if (arg & TIOCM_DTR)
1818 mcr |= UART_MCR_RTS;
1819 if (arg & TIOCM_LOOP)
1820 mcr |= UART_MCR_LOOP;
1821 break;
1822
1823 case TIOCMBIC:
1824 if (arg & TIOCM_RTS)
1825 mcr &= ~UART_MCR_RTS;
1826 if (arg & TIOCM_DTR)
1827 mcr &= ~UART_MCR_RTS;
1828 if (arg & TIOCM_LOOP)
1829 mcr &= ~UART_MCR_LOOP;
1830 break;
1831
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001832 }
1833
1834 mos7720_port->shadowMCR = mcr;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001835 write_mos_reg(port->serial, port->port_number, MCR,
1836 mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001837
1838 return 0;
1839}
1840
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001841static int get_serial_info(struct moschip_port *mos7720_port,
1842 struct serial_struct __user *retinfo)
1843{
1844 struct serial_struct tmp;
1845
1846 if (!retinfo)
1847 return -EFAULT;
1848
1849 memset(&tmp, 0, sizeof(tmp));
1850
1851 tmp.type = PORT_16550A;
Greg Kroah-Hartmane5b1e202013-06-07 11:04:28 -07001852 tmp.line = mos7720_port->port->minor;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001853 tmp.port = mos7720_port->port->port_number;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001854 tmp.irq = 0;
1855 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
Alan Cox4da1a172008-07-22 11:16:21 +01001856 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001857 tmp.baud_base = 9600;
1858 tmp.close_delay = 5*HZ;
1859 tmp.closing_wait = 30*HZ;
1860
1861 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1862 return -EFAULT;
1863 return 0;
1864}
1865
Alan Cox00a0d0d2011-02-14 16:27:06 +00001866static int mos7720_ioctl(struct tty_struct *tty,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001867 unsigned int cmd, unsigned long arg)
1868{
Alan Cox95da3102008-07-22 11:09:07 +01001869 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001870 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001871
1872 mos7720_port = usb_get_serial_port_data(port);
1873 if (mos7720_port == NULL)
1874 return -ENODEV;
1875
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001876 switch (cmd) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001877 case TIOCSERGETLSR:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001878 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
Alan Cox4da1a172008-07-22 11:16:21 +01001879 return get_lsr_info(tty, mos7720_port,
1880 (unsigned int __user *)arg);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001881
Alan Cox95da3102008-07-22 11:09:07 +01001882 /* FIXME: These should be using the mode methods */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001883 case TIOCMBIS:
1884 case TIOCMBIC:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001885 dev_dbg(&port->dev, "%s TIOCMSET/TIOCMBIC/TIOCMSET\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001886 return set_modem_info(mos7720_port, cmd,
1887 (unsigned int __user *)arg);
1888
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001889 case TIOCGSERIAL:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001890 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001891 return get_serial_info(mos7720_port,
1892 (struct serial_struct __user *)arg);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001893 }
1894
1895 return -ENOIOCTLCMD;
1896}
1897
1898static int mos7720_startup(struct usb_serial *serial)
1899{
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001900 struct usb_device *dev;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001901 char data;
Huzaifa Sidhpurwala91f58ae2011-02-21 12:58:45 +05301902 u16 product;
Mike Dunnb69578d2010-04-15 17:01:33 -04001903 int ret_val;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001904
Huzaifa Sidhpurwala91f58ae2011-02-21 12:58:45 +05301905 product = le16_to_cpu(serial->dev->descriptor.idProduct);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001906 dev = serial->dev;
1907
Mike Dunnfb088e32010-01-26 12:12:12 -05001908 /*
1909 * The 7715 uses the first bulk in/out endpoint pair for the parallel
1910 * port, and the second for the serial port. Because the usbserial core
1911 * assumes both pairs are serial ports, we must engage in a bit of
1912 * subterfuge and swap the pointers for ports 0 and 1 in order to make
1913 * port 0 point to the serial port. However, both moschip devices use a
1914 * single interrupt-in endpoint for both ports (as mentioned a little
1915 * further down), and this endpoint was assigned to port 0. So after
1916 * the swap, we must copy the interrupt endpoint elements from port 1
1917 * (as newly assigned) to port 0, and null out port 1 pointers.
1918 */
1919 if (product == MOSCHIP_DEVICE_ID_7715) {
1920 struct usb_serial_port *tmp = serial->port[0];
1921 serial->port[0] = serial->port[1];
1922 serial->port[1] = tmp;
1923 serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb;
1924 serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer;
1925 serial->port[0]->interrupt_in_endpointAddress =
1926 tmp->interrupt_in_endpointAddress;
1927 serial->port[1]->interrupt_in_urb = NULL;
1928 serial->port[1]->interrupt_in_buffer = NULL;
1929 }
1930
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001931 /* setting configuration feature to one */
1932 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Johan Hovold849513a2013-05-27 14:44:43 +02001933 (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5000);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001934
Mike Dunnb69578d2010-04-15 17:01:33 -04001935 /* start the interrupt urb */
1936 ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
1937 if (ret_val)
1938 dev_err(&dev->dev,
1939 "%s - Error %d submitting control urb\n",
1940 __func__, ret_val);
1941
1942#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1943 if (product == MOSCHIP_DEVICE_ID_7715) {
1944 ret_val = mos7715_parport_init(serial);
1945 if (ret_val < 0)
1946 return ret_val;
1947 }
1948#endif
Alan Cox4da1a172008-07-22 11:16:21 +01001949 /* LSR For Port 1 */
Mike Dunn63b91762010-04-15 17:02:09 -04001950 read_mos_reg(serial, 0, LSR, &data);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001951 dev_dbg(&dev->dev, "LSR:%x\n", data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001952
1953 return 0;
1954}
1955
Alan Sternf9c99bb2009-06-02 11:53:55 -04001956static void mos7720_release(struct usb_serial *serial)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001957{
Mike Dunnb69578d2010-04-15 17:01:33 -04001958#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1959 /* close the parallel port */
1960
1961 if (le16_to_cpu(serial->dev->descriptor.idProduct)
1962 == MOSCHIP_DEVICE_ID_7715) {
1963 struct urbtracker *urbtrack;
1964 unsigned long flags;
1965 struct mos7715_parport *mos_parport =
1966 usb_get_serial_data(serial);
1967
1968 /* prevent NULL ptr dereference in port callbacks */
1969 spin_lock(&release_lock);
1970 mos_parport->pp->private_data = NULL;
1971 spin_unlock(&release_lock);
1972
1973 /* wait for synchronous usb calls to return */
1974 if (mos_parport->msg_pending)
1975 wait_for_completion_timeout(&mos_parport->syncmsg_compl,
Johan Hovold849513a2013-05-27 14:44:43 +02001976 msecs_to_jiffies(MOS_WDR_TIMEOUT));
Mike Dunnb69578d2010-04-15 17:01:33 -04001977
1978 parport_remove_port(mos_parport->pp);
1979 usb_set_serial_data(serial, NULL);
1980 mos_parport->serial = NULL;
1981
1982 /* if tasklet currently scheduled, wait for it to complete */
1983 tasklet_kill(&mos_parport->urb_tasklet);
1984
1985 /* unlink any urbs sent by the tasklet */
1986 spin_lock_irqsave(&mos_parport->listlock, flags);
1987 list_for_each_entry(urbtrack,
1988 &mos_parport->active_urbs,
1989 urblist_entry)
1990 usb_unlink_urb(urbtrack->urb);
1991 spin_unlock_irqrestore(&mos_parport->listlock, flags);
1992
1993 kref_put(&mos_parport->ref_count, destroy_mos_parport);
1994 }
1995#endif
Johan Hovold4230af52012-10-25 10:29:05 +02001996}
1997
1998static int mos7720_port_probe(struct usb_serial_port *port)
1999{
2000 struct moschip_port *mos7720_port;
2001
2002 mos7720_port = kzalloc(sizeof(*mos7720_port), GFP_KERNEL);
2003 if (!mos7720_port)
2004 return -ENOMEM;
2005
2006 /* Initialize all port interrupt end point to port 0 int endpoint.
2007 * Our device has only one interrupt endpoint common to all ports.
2008 */
2009 port->interrupt_in_endpointAddress =
2010 port->serial->port[0]->interrupt_in_endpointAddress;
2011 mos7720_port->port = port;
2012
2013 usb_set_serial_port_data(port, mos7720_port);
2014
2015 return 0;
2016}
2017
2018static int mos7720_port_remove(struct usb_serial_port *port)
2019{
2020 struct moschip_port *mos7720_port;
2021
2022 mos7720_port = usb_get_serial_port_data(port);
2023 kfree(mos7720_port);
2024
2025 return 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002026}
2027
2028static struct usb_serial_driver moschip7720_2port_driver = {
2029 .driver = {
2030 .owner = THIS_MODULE,
2031 .name = "moschip7720",
2032 },
2033 .description = "Moschip 2 port adapter",
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002034 .id_table = id_table,
Mike Dunnfb088e32010-01-26 12:12:12 -05002035 .calc_num_ports = mos77xx_calc_num_ports,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002036 .open = mos7720_open,
2037 .close = mos7720_close,
2038 .throttle = mos7720_throttle,
2039 .unthrottle = mos7720_unthrottle,
Mike Dunnfb088e32010-01-26 12:12:12 -05002040 .probe = mos77xx_probe,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002041 .attach = mos7720_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002042 .release = mos7720_release,
Johan Hovold4230af52012-10-25 10:29:05 +02002043 .port_probe = mos7720_port_probe,
2044 .port_remove = mos7720_port_remove,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002045 .ioctl = mos7720_ioctl,
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07002046 .tiocmget = mos7720_tiocmget,
2047 .tiocmset = mos7720_tiocmset,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002048 .set_termios = mos7720_set_termios,
2049 .write = mos7720_write,
2050 .write_room = mos7720_write_room,
2051 .chars_in_buffer = mos7720_chars_in_buffer,
2052 .break_ctl = mos7720_break,
2053 .read_bulk_callback = mos7720_bulk_in_callback,
Mike Dunnfb088e32010-01-26 12:12:12 -05002054 .read_int_callback = NULL /* dynamically assigned in probe() */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002055};
2056
Alan Stern4d2a7af2012-02-23 14:57:09 -05002057static struct usb_serial_driver * const serial_drivers[] = {
2058 &moschip7720_2port_driver, NULL
2059};
2060
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002061module_usb_serial_driver(serial_drivers, id_table);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002062
Alan Cox4da1a172008-07-22 11:16:21 +01002063MODULE_AUTHOR(DRIVER_AUTHOR);
2064MODULE_DESCRIPTION(DRIVER_DESC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002065MODULE_LICENSE("GPL");