blob: 4f70df33975a4a9bf02d3945e8b2bb956b1e7cd6 [file] [log] [blame]
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001/*
2 * mos7720.c
Rahul Bedarkarcd8c5052014-01-02 19:29:24 +05303 * Controls the Moschip 7720 usb to dual port serial converter
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07004 *
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>
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070025#include <linux/slab.h>
26#include <linux/tty.h>
27#include <linux/tty_driver.h>
28#include <linux/tty_flip.h>
29#include <linux/module.h>
30#include <linux/spinlock.h>
31#include <linux/serial.h>
32#include <linux/serial_reg.h>
33#include <linux/usb.h>
34#include <linux/usb/serial.h>
Alan Cox4da1a172008-07-22 11:16:21 +010035#include <linux/uaccess.h>
Mike Dunnb69578d2010-04-15 17:01:33 -040036#include <linux/parport.h>
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070037
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070038#define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
39#define DRIVER_DESC "Moschip USB Serial Driver"
40
41/* default urb timeout */
Johan Hovold849513a2013-05-27 14:44:43 +020042#define MOS_WDR_TIMEOUT 5000
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070043
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070044#define MOS_MAX_PORT 0x02
45#define MOS_WRITE 0x0E
46#define MOS_READ 0x0D
47
Rahul Bedarkarcd8c5052014-01-02 19:29:24 +053048/* Interrupt Routines Defines */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070049#define SERIAL_IIR_RLS 0x06
50#define SERIAL_IIR_RDA 0x04
51#define SERIAL_IIR_CTI 0x0c
52#define SERIAL_IIR_THR 0x02
53#define SERIAL_IIR_MS 0x00
54
55#define NUM_URBS 16 /* URB Count */
56#define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
57
Mike Dunnb69578d2010-04-15 17:01:33 -040058/* This structure holds all of the local serial port information */
Alan Cox4da1a172008-07-22 11:16:21 +010059struct moschip_port {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070060 __u8 shadowLCR; /* last LCR value received */
61 __u8 shadowMCR; /* last MCR value received */
62 __u8 shadowMSR; /* last MSR value received */
63 char open;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070064 struct usb_serial_port *port; /* loop back to the owner */
65 struct urb *write_urb_pool[NUM_URBS];
66};
67
Mike Dunnfb088e32010-01-26 12:12:12 -050068static struct usb_serial_driver moschip7720_2port_driver;
69
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070070#define USB_VENDOR_ID_MOSCHIP 0x9710
71#define MOSCHIP_DEVICE_ID_7720 0x7720
72#define MOSCHIP_DEVICE_ID_7715 0x7715
73
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -070074static const struct usb_device_id id_table[] = {
Alan Cox4da1a172008-07-22 11:16:21 +010075 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
Mike Dunnfb088e32010-01-26 12:12:12 -050076 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) },
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070077 { } /* terminating entry */
78};
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -070079MODULE_DEVICE_TABLE(usb, id_table);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070080
Mike Dunnb69578d2010-04-15 17:01:33 -040081#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
82
83/* initial values for parport regs */
84#define DCR_INIT_VAL 0x0c /* SLCTIN, nINIT */
85#define ECR_INIT_VAL 0x00 /* SPP mode */
86
87struct urbtracker {
88 struct mos7715_parport *mos_parport;
89 struct list_head urblist_entry;
90 struct kref ref_count;
91 struct urb *urb;
Johan Hovoldef6c8c12013-08-13 13:27:34 +020092 struct usb_ctrlrequest *setup;
Mike Dunnb69578d2010-04-15 17:01:33 -040093};
94
95enum mos7715_pp_modes {
96 SPP = 0<<5,
97 PS2 = 1<<5, /* moschip calls this 'NIBBLE' mode */
98 PPF = 2<<5, /* moschip calls this 'CB-FIFO mode */
99};
100
101struct mos7715_parport {
102 struct parport *pp; /* back to containing struct */
103 struct kref ref_count; /* to instance of this struct */
104 struct list_head deferred_urbs; /* list deferred async urbs */
105 struct list_head active_urbs; /* list async urbs in flight */
106 spinlock_t listlock; /* protects list access */
107 bool msg_pending; /* usb sync call pending */
108 struct completion syncmsg_compl; /* usb sync call completed */
109 struct tasklet_struct urb_tasklet; /* for sending deferred urbs */
110 struct usb_serial *serial; /* back to containing struct */
111 __u8 shadowECR; /* parallel port regs... */
112 __u8 shadowDCR;
113 atomic_t shadowDSR; /* updated in int-in callback */
114};
115
116/* lock guards against dereferencing NULL ptr in parport ops callbacks */
117static DEFINE_SPINLOCK(release_lock);
118
Mike Dunn63b91762010-04-15 17:02:09 -0400119#endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
120
121static const unsigned int dummy; /* for clarity in register access fns */
122
Mike Dunnb69578d2010-04-15 17:01:33 -0400123enum mos_regs {
124 THR, /* serial port regs */
125 RHR,
126 IER,
127 FCR,
128 ISR,
129 LCR,
130 MCR,
131 LSR,
132 MSR,
133 SPR,
134 DLL,
135 DLM,
136 DPR, /* parallel port regs */
137 DSR,
138 DCR,
139 ECR,
140 SP1_REG, /* device control regs */
141 SP2_REG, /* serial port 2 (7720 only) */
142 PP_REG,
143 SP_CONTROL_REG,
144};
145
146/*
147 * Return the correct value for the Windex field of the setup packet
148 * for a control endpoint message. See the 7715 datasheet.
149 */
150static inline __u16 get_reg_index(enum mos_regs reg)
151{
152 static const __u16 mos7715_index_lookup_table[] = {
153 0x00, /* THR */
154 0x00, /* RHR */
155 0x01, /* IER */
156 0x02, /* FCR */
157 0x02, /* ISR */
158 0x03, /* LCR */
159 0x04, /* MCR */
160 0x05, /* LSR */
161 0x06, /* MSR */
162 0x07, /* SPR */
163 0x00, /* DLL */
164 0x01, /* DLM */
165 0x00, /* DPR */
166 0x01, /* DSR */
167 0x02, /* DCR */
168 0x0a, /* ECR */
169 0x01, /* SP1_REG */
170 0x02, /* SP2_REG (7720 only) */
171 0x04, /* PP_REG (7715 only) */
172 0x08, /* SP_CONTROL_REG */
173 };
174 return mos7715_index_lookup_table[reg];
175}
176
177/*
178 * Return the correct value for the upper byte of the Wvalue field of
179 * the setup packet for a control endpoint message.
180 */
Mike Dunn63b91762010-04-15 17:02:09 -0400181static inline __u16 get_reg_value(enum mos_regs reg,
182 unsigned int serial_portnum)
Mike Dunnb69578d2010-04-15 17:01:33 -0400183{
184 if (reg >= SP1_REG) /* control reg */
185 return 0x0000;
Mike Dunn63b91762010-04-15 17:02:09 -0400186
187 else if (reg >= DPR) /* parallel port reg (7715 only) */
Mike Dunnb69578d2010-04-15 17:01:33 -0400188 return 0x0100;
Mike Dunn63b91762010-04-15 17:02:09 -0400189
190 else /* serial port reg */
191 return (serial_portnum + 2) << 8;
Mike Dunnb69578d2010-04-15 17:01:33 -0400192}
193
194/*
195 * Write data byte to the specified device register. The data is embedded in
Mike Dunn63b91762010-04-15 17:02:09 -0400196 * the value field of the setup packet. serial_portnum is ignored for registers
197 * not specific to a particular serial port.
Mike Dunnb69578d2010-04-15 17:01:33 -0400198 */
Mike Dunn63b91762010-04-15 17:02:09 -0400199static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
200 enum mos_regs reg, __u8 data)
Mike Dunnb69578d2010-04-15 17:01:33 -0400201{
Mike Dunnb69578d2010-04-15 17:01:33 -0400202 struct usb_device *usbdev = serial->dev;
203 unsigned int pipe = usb_sndctrlpipe(usbdev, 0);
204 __u8 request = (__u8)0x0e;
205 __u8 requesttype = (__u8)0x40;
Mike Dunnb69578d2010-04-15 17:01:33 -0400206 __u16 index = get_reg_index(reg);
Mike Dunn63b91762010-04-15 17:02:09 -0400207 __u16 value = get_reg_value(reg, serial_portnum) + data;
208 int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
209 index, NULL, 0, MOS_WDR_TIMEOUT);
Mike Dunnb69578d2010-04-15 17:01:33 -0400210 if (status < 0)
211 dev_err(&usbdev->dev,
Johan Hovoldd9a38a82014-03-12 19:09:42 +0100212 "mos7720: usb_control_msg() failed: %d\n", status);
Mike Dunnb69578d2010-04-15 17:01:33 -0400213 return status;
214}
215
216/*
217 * Read data byte from the specified device register. The data returned by the
Mike Dunn63b91762010-04-15 17:02:09 -0400218 * device is embedded in the value field of the setup packet. serial_portnum is
219 * ignored for registers that are not specific to a particular serial port.
Mike Dunnb69578d2010-04-15 17:01:33 -0400220 */
Mike Dunn63b91762010-04-15 17:02:09 -0400221static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
222 enum mos_regs reg, __u8 *data)
Mike Dunnb69578d2010-04-15 17:01:33 -0400223{
Mike Dunn63b91762010-04-15 17:02:09 -0400224 struct usb_device *usbdev = serial->dev;
Mike Dunnb69578d2010-04-15 17:01:33 -0400225 unsigned int pipe = usb_rcvctrlpipe(usbdev, 0);
226 __u8 request = (__u8)0x0d;
227 __u8 requesttype = (__u8)0xc0;
Mike Dunnb69578d2010-04-15 17:01:33 -0400228 __u16 index = get_reg_index(reg);
Mike Dunn63b91762010-04-15 17:02:09 -0400229 __u16 value = get_reg_value(reg, serial_portnum);
Johan Hovold72ea18a2013-05-27 14:44:39 +0200230 u8 *buf;
231 int status;
232
233 buf = kmalloc(1, GFP_KERNEL);
234 if (!buf)
235 return -ENOMEM;
236
237 status = usb_control_msg(usbdev, pipe, request, requesttype, value,
238 index, buf, 1, MOS_WDR_TIMEOUT);
239 if (status == 1)
240 *data = *buf;
241 else if (status < 0)
Mike Dunnb69578d2010-04-15 17:01:33 -0400242 dev_err(&usbdev->dev,
Johan Hovoldd9a38a82014-03-12 19:09:42 +0100243 "mos7720: usb_control_msg() failed: %d\n", status);
Johan Hovold72ea18a2013-05-27 14:44:39 +0200244 kfree(buf);
245
Mike Dunnb69578d2010-04-15 17:01:33 -0400246 return status;
247}
248
Mike Dunn63b91762010-04-15 17:02:09 -0400249#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
250
Mike Dunnb69578d2010-04-15 17:01:33 -0400251static inline int mos7715_change_mode(struct mos7715_parport *mos_parport,
252 enum mos7715_pp_modes mode)
253{
254 mos_parport->shadowECR = mode;
Mike Dunn63b91762010-04-15 17:02:09 -0400255 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400256 return 0;
257}
258
259static void destroy_mos_parport(struct kref *kref)
260{
261 struct mos7715_parport *mos_parport =
262 container_of(kref, struct mos7715_parport, ref_count);
263
Mike Dunnb69578d2010-04-15 17:01:33 -0400264 kfree(mos_parport);
265}
266
267static void destroy_urbtracker(struct kref *kref)
268{
269 struct urbtracker *urbtrack =
270 container_of(kref, struct urbtracker, ref_count);
271 struct mos7715_parport *mos_parport = urbtrack->mos_parport;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700272
Mike Dunnb69578d2010-04-15 17:01:33 -0400273 usb_free_urb(urbtrack->urb);
Johan Hovoldef6c8c12013-08-13 13:27:34 +0200274 kfree(urbtrack->setup);
Mike Dunnb69578d2010-04-15 17:01:33 -0400275 kfree(urbtrack);
276 kref_put(&mos_parport->ref_count, destroy_mos_parport);
277}
278
279/*
280 * This runs as a tasklet when sending an urb in a non-blocking parallel
281 * port callback had to be deferred because the disconnect mutex could not be
282 * obtained at the time.
283 */
284static void send_deferred_urbs(unsigned long _mos_parport)
285{
286 int ret_val;
287 unsigned long flags;
288 struct mos7715_parport *mos_parport = (void *)_mos_parport;
Wei Yongjun67990472012-08-21 11:28:45 +0800289 struct urbtracker *urbtrack, *tmp;
Mike Dunnb69578d2010-04-15 17:01:33 -0400290 struct list_head *cursor, *next;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700291 struct device *dev;
Mike Dunnb69578d2010-04-15 17:01:33 -0400292
Mike Dunnb69578d2010-04-15 17:01:33 -0400293 /* if release function ran, game over */
294 if (unlikely(mos_parport->serial == NULL))
295 return;
296
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700297 dev = &mos_parport->serial->dev->dev;
298
Mike Dunnb69578d2010-04-15 17:01:33 -0400299 /* try again to get the mutex */
300 if (!mutex_trylock(&mos_parport->serial->disc_mutex)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700301 dev_dbg(dev, "%s: rescheduling tasklet\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400302 tasklet_schedule(&mos_parport->urb_tasklet);
303 return;
304 }
305
306 /* if device disconnected, game over */
307 if (unlikely(mos_parport->serial->disconnected)) {
308 mutex_unlock(&mos_parport->serial->disc_mutex);
309 return;
310 }
311
312 spin_lock_irqsave(&mos_parport->listlock, flags);
313 if (list_empty(&mos_parport->deferred_urbs)) {
314 spin_unlock_irqrestore(&mos_parport->listlock, flags);
315 mutex_unlock(&mos_parport->serial->disc_mutex);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700316 dev_dbg(dev, "%s: deferred_urbs list empty\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400317 return;
318 }
319
320 /* move contents of deferred_urbs list to active_urbs list and submit */
321 list_for_each_safe(cursor, next, &mos_parport->deferred_urbs)
322 list_move_tail(cursor, &mos_parport->active_urbs);
Wei Yongjun67990472012-08-21 11:28:45 +0800323 list_for_each_entry_safe(urbtrack, tmp, &mos_parport->active_urbs,
Mike Dunnb69578d2010-04-15 17:01:33 -0400324 urblist_entry) {
325 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700326 dev_dbg(dev, "%s: urb submitted\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400327 if (ret_val) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700328 dev_err(dev, "usb_submit_urb() failed: %d\n", ret_val);
Mike Dunnb69578d2010-04-15 17:01:33 -0400329 list_del(&urbtrack->urblist_entry);
330 kref_put(&urbtrack->ref_count, destroy_urbtracker);
331 }
332 }
333 spin_unlock_irqrestore(&mos_parport->listlock, flags);
334 mutex_unlock(&mos_parport->serial->disc_mutex);
335}
336
337/* callback for parallel port control urbs submitted asynchronously */
338static void async_complete(struct urb *urb)
339{
340 struct urbtracker *urbtrack = urb->context;
341 int status = urb->status;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700342
Mike Dunnb69578d2010-04-15 17:01:33 -0400343 if (unlikely(status))
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700344 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
Mike Dunnb69578d2010-04-15 17:01:33 -0400345
346 /* remove the urbtracker from the active_urbs list */
347 spin_lock(&urbtrack->mos_parport->listlock);
348 list_del(&urbtrack->urblist_entry);
349 spin_unlock(&urbtrack->mos_parport->listlock);
350 kref_put(&urbtrack->ref_count, destroy_urbtracker);
351}
352
353static int write_parport_reg_nonblock(struct mos7715_parport *mos_parport,
354 enum mos_regs reg, __u8 data)
355{
356 struct urbtracker *urbtrack;
357 int ret_val;
358 unsigned long flags;
Mike Dunnb69578d2010-04-15 17:01:33 -0400359 struct usb_serial *serial = mos_parport->serial;
360 struct usb_device *usbdev = serial->dev;
Mike Dunnb69578d2010-04-15 17:01:33 -0400361
362 /* create and initialize the control urb and containing urbtracker */
363 urbtrack = kmalloc(sizeof(struct urbtracker), GFP_ATOMIC);
Johan Hovold10c642d2013-12-29 19:22:56 +0100364 if (!urbtrack)
Mike Dunnb69578d2010-04-15 17:01:33 -0400365 return -ENOMEM;
Johan Hovold10c642d2013-12-29 19:22:56 +0100366
Mike Dunnb69578d2010-04-15 17:01:33 -0400367 kref_get(&mos_parport->ref_count);
368 urbtrack->mos_parport = mos_parport;
369 urbtrack->urb = usb_alloc_urb(0, GFP_ATOMIC);
Johan Hovold10c642d2013-12-29 19:22:56 +0100370 if (!urbtrack->urb) {
Mike Dunnb69578d2010-04-15 17:01:33 -0400371 kfree(urbtrack);
372 return -ENOMEM;
373 }
Dan Carpenterd0bd9a42013-08-16 10:16:59 +0300374 urbtrack->setup = kmalloc(sizeof(*urbtrack->setup), GFP_ATOMIC);
Johan Hovoldef6c8c12013-08-13 13:27:34 +0200375 if (!urbtrack->setup) {
376 usb_free_urb(urbtrack->urb);
377 kfree(urbtrack);
378 return -ENOMEM;
379 }
380 urbtrack->setup->bRequestType = (__u8)0x40;
381 urbtrack->setup->bRequest = (__u8)0x0e;
Johan Hovold3b716ca2013-08-19 13:05:45 +0200382 urbtrack->setup->wValue = cpu_to_le16(get_reg_value(reg, dummy));
383 urbtrack->setup->wIndex = cpu_to_le16(get_reg_index(reg));
Johan Hovoldef6c8c12013-08-13 13:27:34 +0200384 urbtrack->setup->wLength = 0;
Mike Dunnb69578d2010-04-15 17:01:33 -0400385 usb_fill_control_urb(urbtrack->urb, usbdev,
386 usb_sndctrlpipe(usbdev, 0),
Johan Hovoldef6c8c12013-08-13 13:27:34 +0200387 (unsigned char *)urbtrack->setup,
Mike Dunnb69578d2010-04-15 17:01:33 -0400388 NULL, 0, async_complete, urbtrack);
389 kref_init(&urbtrack->ref_count);
390 INIT_LIST_HEAD(&urbtrack->urblist_entry);
391
392 /*
393 * get the disconnect mutex, or add tracker to the deferred_urbs list
394 * and schedule a tasklet to try again later
395 */
396 if (!mutex_trylock(&serial->disc_mutex)) {
397 spin_lock_irqsave(&mos_parport->listlock, flags);
398 list_add_tail(&urbtrack->urblist_entry,
399 &mos_parport->deferred_urbs);
400 spin_unlock_irqrestore(&mos_parport->listlock, flags);
401 tasklet_schedule(&mos_parport->urb_tasklet);
Johan Hovoldd9a38a82014-03-12 19:09:42 +0100402 dev_dbg(&usbdev->dev, "tasklet scheduled\n");
Mike Dunnb69578d2010-04-15 17:01:33 -0400403 return 0;
404 }
405
406 /* bail if device disconnected */
407 if (serial->disconnected) {
408 kref_put(&urbtrack->ref_count, destroy_urbtracker);
409 mutex_unlock(&serial->disc_mutex);
410 return -ENODEV;
411 }
412
413 /* add the tracker to the active_urbs list and submit */
414 spin_lock_irqsave(&mos_parport->listlock, flags);
415 list_add_tail(&urbtrack->urblist_entry, &mos_parport->active_urbs);
416 spin_unlock_irqrestore(&mos_parport->listlock, flags);
417 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
418 mutex_unlock(&serial->disc_mutex);
419 if (ret_val) {
420 dev_err(&usbdev->dev,
Johan Hovoldd9a38a82014-03-12 19:09:42 +0100421 "%s: submit_urb() failed: %d\n", __func__, ret_val);
Mike Dunnb69578d2010-04-15 17:01:33 -0400422 spin_lock_irqsave(&mos_parport->listlock, flags);
423 list_del(&urbtrack->urblist_entry);
424 spin_unlock_irqrestore(&mos_parport->listlock, flags);
425 kref_put(&urbtrack->ref_count, destroy_urbtracker);
426 return ret_val;
427 }
428 return 0;
429}
430
431/*
432 * This is the the common top part of all parallel port callback operations that
433 * send synchronous messages to the device. This implements convoluted locking
434 * that avoids two scenarios: (1) a port operation is called after usbserial
435 * has called our release function, at which point struct mos7715_parport has
436 * been destroyed, and (2) the device has been disconnected, but usbserial has
437 * not called the release function yet because someone has a serial port open.
438 * The shared release_lock prevents the first, and the mutex and disconnected
439 * flag maintained by usbserial covers the second. We also use the msg_pending
Rahul Bedarkarcd8c5052014-01-02 19:29:24 +0530440 * flag to ensure that all synchronous usb message calls have completed before
Mike Dunnb69578d2010-04-15 17:01:33 -0400441 * our release function can return.
442 */
443static int parport_prologue(struct parport *pp)
444{
445 struct mos7715_parport *mos_parport;
446
447 spin_lock(&release_lock);
448 mos_parport = pp->private_data;
449 if (unlikely(mos_parport == NULL)) {
450 /* release fn called, port struct destroyed */
451 spin_unlock(&release_lock);
452 return -1;
453 }
454 mos_parport->msg_pending = true; /* synch usb call pending */
Wolfram Sang16735d02013-11-14 14:32:02 -0800455 reinit_completion(&mos_parport->syncmsg_compl);
Mike Dunnb69578d2010-04-15 17:01:33 -0400456 spin_unlock(&release_lock);
457
458 mutex_lock(&mos_parport->serial->disc_mutex);
459 if (mos_parport->serial->disconnected) {
460 /* device disconnected */
461 mutex_unlock(&mos_parport->serial->disc_mutex);
462 mos_parport->msg_pending = false;
463 complete(&mos_parport->syncmsg_compl);
464 return -1;
465 }
466
467 return 0;
468}
469
470/*
Rahul Bedarkarcd8c5052014-01-02 19:29:24 +0530471 * This is the common bottom part of all parallel port functions that send
Mike Dunnb69578d2010-04-15 17:01:33 -0400472 * synchronous messages to the device.
473 */
474static inline void parport_epilogue(struct parport *pp)
475{
476 struct mos7715_parport *mos_parport = pp->private_data;
477 mutex_unlock(&mos_parport->serial->disc_mutex);
478 mos_parport->msg_pending = false;
479 complete(&mos_parport->syncmsg_compl);
480}
481
482static void parport_mos7715_write_data(struct parport *pp, unsigned char d)
483{
484 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700485
Mike Dunnb69578d2010-04-15 17:01:33 -0400486 if (parport_prologue(pp) < 0)
487 return;
488 mos7715_change_mode(mos_parport, SPP);
Mike Dunn63b91762010-04-15 17:02:09 -0400489 write_mos_reg(mos_parport->serial, dummy, DPR, (__u8)d);
Mike Dunnb69578d2010-04-15 17:01:33 -0400490 parport_epilogue(pp);
491}
492
493static unsigned char parport_mos7715_read_data(struct parport *pp)
494{
495 struct mos7715_parport *mos_parport = pp->private_data;
496 unsigned char d;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700497
Mike Dunnb69578d2010-04-15 17:01:33 -0400498 if (parport_prologue(pp) < 0)
499 return 0;
Mike Dunn63b91762010-04-15 17:02:09 -0400500 read_mos_reg(mos_parport->serial, dummy, DPR, &d);
Mike Dunnb69578d2010-04-15 17:01:33 -0400501 parport_epilogue(pp);
502 return d;
503}
504
505static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
506{
507 struct mos7715_parport *mos_parport = pp->private_data;
508 __u8 data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700509
Mike Dunnb69578d2010-04-15 17:01:33 -0400510 if (parport_prologue(pp) < 0)
511 return;
512 data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
Mike Dunn63b91762010-04-15 17:02:09 -0400513 write_mos_reg(mos_parport->serial, dummy, DCR, data);
Mike Dunnb69578d2010-04-15 17:01:33 -0400514 mos_parport->shadowDCR = data;
515 parport_epilogue(pp);
516}
517
518static unsigned char parport_mos7715_read_control(struct parport *pp)
519{
520 struct mos7715_parport *mos_parport = pp->private_data;
521 __u8 dcr;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700522
Mike Dunnb69578d2010-04-15 17:01:33 -0400523 spin_lock(&release_lock);
524 mos_parport = pp->private_data;
525 if (unlikely(mos_parport == NULL)) {
526 spin_unlock(&release_lock);
527 return 0;
528 }
529 dcr = mos_parport->shadowDCR & 0x0f;
530 spin_unlock(&release_lock);
531 return dcr;
532}
533
534static unsigned char parport_mos7715_frob_control(struct parport *pp,
535 unsigned char mask,
536 unsigned char val)
537{
538 struct mos7715_parport *mos_parport = pp->private_data;
539 __u8 dcr;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700540
Mike Dunnb69578d2010-04-15 17:01:33 -0400541 mask &= 0x0f;
542 val &= 0x0f;
543 if (parport_prologue(pp) < 0)
544 return 0;
545 mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
Mike Dunn63b91762010-04-15 17:02:09 -0400546 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400547 dcr = mos_parport->shadowDCR & 0x0f;
548 parport_epilogue(pp);
549 return dcr;
550}
551
552static unsigned char parport_mos7715_read_status(struct parport *pp)
553{
554 unsigned char status;
555 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700556
Mike Dunnb69578d2010-04-15 17:01:33 -0400557 spin_lock(&release_lock);
558 mos_parport = pp->private_data;
559 if (unlikely(mos_parport == NULL)) { /* release called */
560 spin_unlock(&release_lock);
561 return 0;
562 }
563 status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
564 spin_unlock(&release_lock);
565 return status;
566}
567
568static void parport_mos7715_enable_irq(struct parport *pp)
569{
Mike Dunnb69578d2010-04-15 17:01:33 -0400570}
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700571
Mike Dunnb69578d2010-04-15 17:01:33 -0400572static void parport_mos7715_disable_irq(struct parport *pp)
573{
Mike Dunnb69578d2010-04-15 17:01:33 -0400574}
575
576static void parport_mos7715_data_forward(struct parport *pp)
577{
578 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700579
Mike Dunnb69578d2010-04-15 17:01:33 -0400580 if (parport_prologue(pp) < 0)
581 return;
582 mos7715_change_mode(mos_parport, PS2);
583 mos_parport->shadowDCR &= ~0x20;
Mike Dunn63b91762010-04-15 17:02:09 -0400584 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400585 parport_epilogue(pp);
586}
587
588static void parport_mos7715_data_reverse(struct parport *pp)
589{
590 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700591
Mike Dunnb69578d2010-04-15 17:01:33 -0400592 if (parport_prologue(pp) < 0)
593 return;
594 mos7715_change_mode(mos_parport, PS2);
595 mos_parport->shadowDCR |= 0x20;
Mike Dunn63b91762010-04-15 17:02:09 -0400596 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400597 parport_epilogue(pp);
598}
599
600static void parport_mos7715_init_state(struct pardevice *dev,
601 struct parport_state *s)
602{
Mike Dunnb69578d2010-04-15 17:01:33 -0400603 s->u.pc.ctr = DCR_INIT_VAL;
604 s->u.pc.ecr = ECR_INIT_VAL;
605}
606
607/* N.B. Parport core code requires that this function not block */
608static void parport_mos7715_save_state(struct parport *pp,
609 struct parport_state *s)
610{
611 struct mos7715_parport *mos_parport;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700612
Mike Dunnb69578d2010-04-15 17:01:33 -0400613 spin_lock(&release_lock);
614 mos_parport = pp->private_data;
615 if (unlikely(mos_parport == NULL)) { /* release called */
616 spin_unlock(&release_lock);
617 return;
618 }
619 s->u.pc.ctr = mos_parport->shadowDCR;
620 s->u.pc.ecr = mos_parport->shadowECR;
621 spin_unlock(&release_lock);
622}
623
624/* N.B. Parport core code requires that this function not block */
625static void parport_mos7715_restore_state(struct parport *pp,
626 struct parport_state *s)
627{
628 struct mos7715_parport *mos_parport;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700629
Mike Dunnb69578d2010-04-15 17:01:33 -0400630 spin_lock(&release_lock);
631 mos_parport = pp->private_data;
632 if (unlikely(mos_parport == NULL)) { /* release called */
633 spin_unlock(&release_lock);
634 return;
635 }
636 write_parport_reg_nonblock(mos_parport, DCR, mos_parport->shadowDCR);
637 write_parport_reg_nonblock(mos_parport, ECR, mos_parport->shadowECR);
638 spin_unlock(&release_lock);
639}
640
641static size_t parport_mos7715_write_compat(struct parport *pp,
642 const void *buffer,
643 size_t len, int flags)
644{
645 int retval;
646 struct mos7715_parport *mos_parport = pp->private_data;
647 int actual_len;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700648
Mike Dunnb69578d2010-04-15 17:01:33 -0400649 if (parport_prologue(pp) < 0)
650 return 0;
651 mos7715_change_mode(mos_parport, PPF);
652 retval = usb_bulk_msg(mos_parport->serial->dev,
653 usb_sndbulkpipe(mos_parport->serial->dev, 2),
654 (void *)buffer, len, &actual_len,
655 MOS_WDR_TIMEOUT);
656 parport_epilogue(pp);
657 if (retval) {
658 dev_err(&mos_parport->serial->dev->dev,
Johan Hovoldd9a38a82014-03-12 19:09:42 +0100659 "mos7720: usb_bulk_msg() failed: %d\n", retval);
Mike Dunnb69578d2010-04-15 17:01:33 -0400660 return 0;
661 }
662 return actual_len;
663}
664
665static struct parport_operations parport_mos7715_ops = {
666 .owner = THIS_MODULE,
667 .write_data = parport_mos7715_write_data,
668 .read_data = parport_mos7715_read_data,
669
670 .write_control = parport_mos7715_write_control,
671 .read_control = parport_mos7715_read_control,
672 .frob_control = parport_mos7715_frob_control,
673
674 .read_status = parport_mos7715_read_status,
675
676 .enable_irq = parport_mos7715_enable_irq,
677 .disable_irq = parport_mos7715_disable_irq,
678
679 .data_forward = parport_mos7715_data_forward,
680 .data_reverse = parport_mos7715_data_reverse,
681
682 .init_state = parport_mos7715_init_state,
683 .save_state = parport_mos7715_save_state,
684 .restore_state = parport_mos7715_restore_state,
685
686 .compat_write_data = parport_mos7715_write_compat,
687
688 .nibble_read_data = parport_ieee1284_read_nibble,
689 .byte_read_data = parport_ieee1284_read_byte,
690};
691
692/*
693 * Allocate and initialize parallel port control struct, initialize
694 * the parallel port hardware device, and register with the parport subsystem.
695 */
696static int mos7715_parport_init(struct usb_serial *serial)
697{
698 struct mos7715_parport *mos_parport;
699
700 /* allocate and initialize parallel port control struct */
701 mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +0100702 if (!mos_parport)
Mike Dunnb69578d2010-04-15 17:01:33 -0400703 return -ENOMEM;
Johan Hovold10c642d2013-12-29 19:22:56 +0100704
Mike Dunnb69578d2010-04-15 17:01:33 -0400705 mos_parport->msg_pending = false;
706 kref_init(&mos_parport->ref_count);
707 spin_lock_init(&mos_parport->listlock);
708 INIT_LIST_HEAD(&mos_parport->active_urbs);
709 INIT_LIST_HEAD(&mos_parport->deferred_urbs);
710 usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
711 mos_parport->serial = serial;
712 tasklet_init(&mos_parport->urb_tasklet, send_deferred_urbs,
713 (unsigned long) mos_parport);
714 init_completion(&mos_parport->syncmsg_compl);
715
716 /* cycle parallel port reset bit */
Mike Dunn63b91762010-04-15 17:02:09 -0400717 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x80);
718 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x00);
Mike Dunnb69578d2010-04-15 17:01:33 -0400719
720 /* initialize device registers */
721 mos_parport->shadowDCR = DCR_INIT_VAL;
Mike Dunn63b91762010-04-15 17:02:09 -0400722 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400723 mos_parport->shadowECR = ECR_INIT_VAL;
Mike Dunn63b91762010-04-15 17:02:09 -0400724 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400725
726 /* register with parport core */
727 mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE,
728 PARPORT_DMA_NONE,
729 &parport_mos7715_ops);
730 if (mos_parport->pp == NULL) {
731 dev_err(&serial->interface->dev,
732 "Could not register parport\n");
733 kref_put(&mos_parport->ref_count, destroy_mos_parport);
734 return -EIO;
735 }
736 mos_parport->pp->private_data = mos_parport;
737 mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP;
738 mos_parport->pp->dev = &serial->interface->dev;
739 parport_announce_port(mos_parport->pp);
740
741 return 0;
742}
743#endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700744
745/*
746 * mos7720_interrupt_callback
747 * this is the callback function for when we have received data on the
748 * interrupt endpoint.
749 */
750static void mos7720_interrupt_callback(struct urb *urb)
751{
752 int result;
753 int length;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700754 int status = urb->status;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700755 struct device *dev = &urb->dev->dev;
Oliver Neukum325b70c2007-03-19 13:58:29 +0100756 __u8 *data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700757 __u8 sp1;
758 __u8 sp2;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700759
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700760 switch (status) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700761 case 0:
762 /* success */
763 break;
764 case -ECONNRESET:
765 case -ENOENT:
766 case -ESHUTDOWN:
767 /* this urb is terminated, clean up */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700768 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700769 return;
770 default:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700771 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700772 goto exit;
773 }
774
775 length = urb->actual_length;
776 data = urb->transfer_buffer;
777
778 /* Moschip get 4 bytes
779 * Byte 1 IIR Port 1 (port.number is 0)
780 * Byte 2 IIR Port 2 (port.number is 1)
781 * Byte 3 --------------
782 * Byte 4 FIFO status for both */
Oliver Neukum325b70c2007-03-19 13:58:29 +0100783
784 /* the above description is inverted
785 * oneukum 2007-03-14 */
786
787 if (unlikely(length != 4)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700788 dev_dbg(dev, "Wrong data !!!\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700789 return;
790 }
791
Oliver Neukum325b70c2007-03-19 13:58:29 +0100792 sp1 = data[3];
793 sp2 = data[2];
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700794
Oliver Neukum325b70c2007-03-19 13:58:29 +0100795 if ((sp1 | sp2) & 0x01) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700796 /* No Interrupt Pending in both the ports */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700797 dev_dbg(dev, "No Interrupt !!!\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700798 } else {
799 switch (sp1 & 0x0f) {
800 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700801 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 -0700802 break;
803 case SERIAL_IIR_CTI:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700804 dev_dbg(dev, "Serial Port 1: Receiver time out\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700805 break;
806 case SERIAL_IIR_MS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700807 /* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700808 break;
809 }
810
811 switch (sp2 & 0x0f) {
812 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700813 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 -0700814 break;
815 case SERIAL_IIR_CTI:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700816 dev_dbg(dev, "Serial Port 2: Receiver time out\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700817 break;
818 case SERIAL_IIR_MS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700819 /* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700820 break;
821 }
822 }
823
824exit:
825 result = usb_submit_urb(urb, GFP_ATOMIC);
826 if (result)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700827 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700828}
829
830/*
Mike Dunnfb088e32010-01-26 12:12:12 -0500831 * mos7715_interrupt_callback
832 * this is the 7715's callback function for when we have received data on
833 * the interrupt endpoint.
834 */
835static void mos7715_interrupt_callback(struct urb *urb)
836{
837 int result;
838 int length;
839 int status = urb->status;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700840 struct device *dev = &urb->dev->dev;
Mike Dunnfb088e32010-01-26 12:12:12 -0500841 __u8 *data;
842 __u8 iir;
843
844 switch (status) {
845 case 0:
846 /* success */
847 break;
848 case -ECONNRESET:
849 case -ENOENT:
850 case -ESHUTDOWN:
Mike Dunnb69578d2010-04-15 17:01:33 -0400851 case -ENODEV:
Mike Dunnfb088e32010-01-26 12:12:12 -0500852 /* this urb is terminated, clean up */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700853 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
Mike Dunnfb088e32010-01-26 12:12:12 -0500854 return;
855 default:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700856 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
Mike Dunnfb088e32010-01-26 12:12:12 -0500857 goto exit;
858 }
859
860 length = urb->actual_length;
861 data = urb->transfer_buffer;
862
863 /* Structure of data from 7715 device:
864 * Byte 1: IIR serial Port
865 * Byte 2: unused
866 * Byte 2: DSR parallel port
867 * Byte 4: FIFO status for both */
868
869 if (unlikely(length != 4)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700870 dev_dbg(dev, "Wrong data !!!\n");
Mike Dunnfb088e32010-01-26 12:12:12 -0500871 return;
872 }
873
874 iir = data[0];
875 if (!(iir & 0x01)) { /* serial port interrupt pending */
876 switch (iir & 0x0f) {
877 case SERIAL_IIR_RLS:
Johan Hovoldd9a38a82014-03-12 19:09:42 +0100878 dev_dbg(dev, "Serial Port: Receiver status error or address bit detected in 9-bit mode\n");
Mike Dunnfb088e32010-01-26 12:12:12 -0500879 break;
880 case SERIAL_IIR_CTI:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700881 dev_dbg(dev, "Serial Port: Receiver time out\n");
Mike Dunnfb088e32010-01-26 12:12:12 -0500882 break;
883 case SERIAL_IIR_MS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700884 /* dev_dbg(dev, "Serial Port: Modem status change\n"); */
Mike Dunnfb088e32010-01-26 12:12:12 -0500885 break;
886 }
887 }
888
Mike Dunnb69578d2010-04-15 17:01:33 -0400889#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
890 { /* update local copy of DSR reg */
891 struct usb_serial_port *port = urb->context;
892 struct mos7715_parport *mos_parport = port->serial->private;
893 if (unlikely(mos_parport == NULL))
894 return;
895 atomic_set(&mos_parport->shadowDSR, data[2]);
896 }
897#endif
898
Mike Dunnfb088e32010-01-26 12:12:12 -0500899exit:
900 result = usb_submit_urb(urb, GFP_ATOMIC);
901 if (result)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700902 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
Mike Dunnfb088e32010-01-26 12:12:12 -0500903}
904
905/*
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700906 * mos7720_bulk_in_callback
907 * this is the callback function for when we have received data on the
908 * bulk in endpoint.
909 */
910static void mos7720_bulk_in_callback(struct urb *urb)
911{
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700912 int retval;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700913 unsigned char *data ;
914 struct usb_serial_port *port;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700915 int status = urb->status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700916
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700917 if (status) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700918 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700919 return;
920 }
921
Mike Dunnb69578d2010-04-15 17:01:33 -0400922 port = urb->context;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700923
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700924 dev_dbg(&port->dev, "Entering...%s\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700925
926 data = urb->transfer_buffer;
927
Jiri Slaby2e124b42013-01-03 15:53:06 +0100928 if (urb->actual_length) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100929 tty_insert_flip_string(&port->port, data, urb->actual_length);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100930 tty_flip_buffer_push(&port->port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700931 }
932
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700933 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700934 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
935 if (retval)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700936 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700937 }
938}
939
940/*
941 * mos7720_bulk_out_data_callback
942 * this is the callback function for when we have finished sending serial
943 * data on the bulk out endpoint.
944 */
945static void mos7720_bulk_out_data_callback(struct urb *urb)
946{
947 struct moschip_port *mos7720_port;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700948 int status = urb->status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700949
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700950 if (status) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700951 dev_dbg(&urb->dev->dev, "nonzero write bulk status received:%d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700952 return;
953 }
954
955 mos7720_port = urb->context;
956 if (!mos7720_port) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700957 dev_dbg(&urb->dev->dev, "NULL mos7720_port pointer\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700958 return ;
959 }
960
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100961 if (mos7720_port->open)
962 tty_port_tty_wakeup(&mos7720_port->port->port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700963}
964
965/*
Mike Dunnfb088e32010-01-26 12:12:12 -0500966 * mos77xx_probe
967 * this function installs the appropriate read interrupt endpoint callback
968 * depending on whether the device is a 7720 or 7715, thus avoiding costly
969 * run-time checks in the high-frequency callback routine itself.
970 */
971static int mos77xx_probe(struct usb_serial *serial,
972 const struct usb_device_id *id)
973{
974 if (id->idProduct == MOSCHIP_DEVICE_ID_7715)
975 moschip7720_2port_driver.read_int_callback =
976 mos7715_interrupt_callback;
977 else
978 moschip7720_2port_driver.read_int_callback =
979 mos7720_interrupt_callback;
980
981 return 0;
982}
983
984static int mos77xx_calc_num_ports(struct usb_serial *serial)
985{
986 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
987 if (product == MOSCHIP_DEVICE_ID_7715)
988 return 1;
989
990 return 2;
991}
992
Alan Coxa509a7e2009-09-19 13:13:26 -0700993static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700994{
995 struct usb_serial *serial;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700996 struct urb *urb;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700997 struct moschip_port *mos7720_port;
998 int response;
999 int port_number;
Mike Dunn63b91762010-04-15 17:02:09 -04001000 __u8 data;
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001001 int allocated_urbs = 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001002 int j;
1003
1004 serial = port->serial;
1005
1006 mos7720_port = usb_get_serial_port_data(port);
1007 if (mos7720_port == NULL)
1008 return -ENODEV;
1009
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001010 usb_clear_halt(serial->dev, port->write_urb->pipe);
1011 usb_clear_halt(serial->dev, port->read_urb->pipe);
1012
1013 /* Initialising the write urb pool */
1014 for (j = 0; j < NUM_URBS; ++j) {
Alan Cox4da1a172008-07-22 11:16:21 +01001015 urb = usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001016 mos7720_port->write_urb_pool[j] = urb;
Johan Hovold10c642d2013-12-29 19:22:56 +01001017 if (!urb)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001018 continue;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001019
1020 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1021 GFP_KERNEL);
1022 if (!urb->transfer_buffer) {
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001023 usb_free_urb(mos7720_port->write_urb_pool[j]);
1024 mos7720_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001025 continue;
1026 }
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001027 allocated_urbs++;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001028 }
1029
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001030 if (!allocated_urbs)
1031 return -ENOMEM;
1032
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001033 /* Initialize MCS7720 -- Write Init values to corresponding Registers
1034 *
1035 * Register Index
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001036 * 0 : THR/RHR
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001037 * 1 : IER
1038 * 2 : FCR
1039 * 3 : LCR
1040 * 4 : MCR
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001041 * 5 : LSR
1042 * 6 : MSR
1043 * 7 : SPR
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001044 *
1045 * 0x08 : SP1/2 Control Reg
1046 */
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001047 port_number = port->port_number;
Mike Dunn63b91762010-04-15 17:02:09 -04001048 read_mos_reg(serial, port_number, LSR, &data);
1049
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001050 dev_dbg(&port->dev, "SS::%p LSR:%x\n", mos7720_port, data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001051
Mike Dunn63b91762010-04-15 17:02:09 -04001052 write_mos_reg(serial, dummy, SP1_REG, 0x02);
1053 write_mos_reg(serial, dummy, SP2_REG, 0x02);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001054
Mike Dunn63b91762010-04-15 17:02:09 -04001055 write_mos_reg(serial, port_number, IER, 0x00);
1056 write_mos_reg(serial, port_number, FCR, 0x00);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001057
Mike Dunn63b91762010-04-15 17:02:09 -04001058 write_mos_reg(serial, port_number, FCR, 0xcf);
1059 mos7720_port->shadowLCR = 0x03;
1060 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1061 mos7720_port->shadowMCR = 0x0b;
1062 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001063
Mike Dunn63b91762010-04-15 17:02:09 -04001064 write_mos_reg(serial, port_number, SP_CONTROL_REG, 0x00);
1065 read_mos_reg(serial, dummy, SP_CONTROL_REG, &data);
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001066 data = data | (port->port_number + 1);
Mike Dunn63b91762010-04-15 17:02:09 -04001067 write_mos_reg(serial, dummy, SP_CONTROL_REG, data);
1068 mos7720_port->shadowLCR = 0x83;
1069 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1070 write_mos_reg(serial, port_number, THR, 0x0c);
1071 write_mos_reg(serial, port_number, IER, 0x00);
1072 mos7720_port->shadowLCR = 0x03;
1073 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1074 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001075
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001076 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
1077 if (response)
Alan Cox4da1a172008-07-22 11:16:21 +01001078 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
1079 __func__, response);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001080
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001081 /* 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
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001147 write_mos_reg(serial, port->port_number, MCR, 0x00);
1148 write_mos_reg(serial, port->port_number, IER, 0x00);
Johan Hovoldcf41aa92013-03-21 12:37:41 +01001149
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001150 mos7720_port->open = 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001151}
1152
Alan Cox95da3102008-07-22 11:09:07 +01001153static void mos7720_break(struct tty_struct *tty, int break_state)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001154{
Alan Cox95da3102008-07-22 11:09:07 +01001155 struct usb_serial_port *port = tty->driver_data;
Alan Cox4da1a172008-07-22 11:16:21 +01001156 unsigned char data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001157 struct usb_serial *serial;
1158 struct moschip_port *mos7720_port;
1159
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001160 serial = port->serial;
1161
1162 mos7720_port = usb_get_serial_port_data(port);
1163 if (mos7720_port == NULL)
1164 return;
1165
1166 if (break_state == -1)
1167 data = mos7720_port->shadowLCR | UART_LCR_SBC;
1168 else
1169 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
1170
1171 mos7720_port->shadowLCR = data;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001172 write_mos_reg(serial, port->port_number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001173}
1174
1175/*
1176 * mos7720_write_room
1177 * this function is called by the tty driver when it wants to know how many
1178 * bytes of data we can accept for a specific port.
1179 * If successful, we return the amount of room that we have for this port
1180 * Otherwise we return a negative error number.
1181 */
Alan Cox95da3102008-07-22 11:09:07 +01001182static int mos7720_write_room(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001183{
Alan Cox95da3102008-07-22 11:09:07 +01001184 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001185 struct moschip_port *mos7720_port;
1186 int room = 0;
1187 int i;
1188
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001189 mos7720_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001190 if (mos7720_port == NULL)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001191 return -ENODEV;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001192
Alan Coxa5b6f602008-04-08 17:16:06 +01001193 /* FIXME: Locking */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001194 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001195 if (mos7720_port->write_urb_pool[i] &&
1196 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001197 room += URB_TRANSFER_BUFFER_SIZE;
1198 }
1199
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001200 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001201 return room;
1202}
1203
Alan Cox95da3102008-07-22 11:09:07 +01001204static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
1205 const unsigned char *data, int count)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001206{
1207 int status;
1208 int i;
1209 int bytes_sent = 0;
1210 int transfer_size;
1211
1212 struct moschip_port *mos7720_port;
1213 struct usb_serial *serial;
1214 struct urb *urb;
1215 const unsigned char *current_position = data;
1216
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001217 serial = port->serial;
1218
1219 mos7720_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001220 if (mos7720_port == NULL)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001221 return -ENODEV;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001222
1223 /* try to find a free urb in the list */
1224 urb = NULL;
1225
1226 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001227 if (mos7720_port->write_urb_pool[i] &&
1228 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001229 urb = mos7720_port->write_urb_pool[i];
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001230 dev_dbg(&port->dev, "URB:%d\n", i);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001231 break;
1232 }
1233 }
1234
1235 if (urb == NULL) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001236 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001237 goto exit;
1238 }
1239
1240 if (urb->transfer_buffer == NULL) {
1241 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1242 GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +01001243 if (!urb->transfer_buffer)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001244 goto exit;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001245 }
Alan Cox4da1a172008-07-22 11:16:21 +01001246 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001247
1248 memcpy(urb->transfer_buffer, current_position, transfer_size);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001249 usb_serial_debug_data(&port->dev, __func__, transfer_size,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001250 urb->transfer_buffer);
1251
1252 /* fill urb with data and submit */
1253 usb_fill_bulk_urb(urb, serial->dev,
1254 usb_sndbulkpipe(serial->dev,
Alan Cox4da1a172008-07-22 11:16:21 +01001255 port->bulk_out_endpointAddress),
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001256 urb->transfer_buffer, transfer_size,
1257 mos7720_bulk_out_data_callback, mos7720_port);
1258
1259 /* send it down the pipe */
Alan Cox4da1a172008-07-22 11:16:21 +01001260 status = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001261 if (status) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001262 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001263 "with status = %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001264 bytes_sent = status;
1265 goto exit;
1266 }
1267 bytes_sent = transfer_size;
1268
1269exit:
1270 return bytes_sent;
1271}
1272
Alan Cox95da3102008-07-22 11:09:07 +01001273static void mos7720_throttle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001274{
Alan Cox95da3102008-07-22 11:09:07 +01001275 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001276 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001277 int status;
1278
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001279 mos7720_port = usb_get_serial_port_data(port);
1280
1281 if (mos7720_port == NULL)
1282 return;
1283
1284 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001285 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001286 return;
1287 }
1288
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001289 /* if we are implementing XON/XOFF, send the stop character */
1290 if (I_IXOFF(tty)) {
1291 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001292 status = mos7720_write(tty, port, &stop_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001293 if (status <= 0)
1294 return;
1295 }
1296
1297 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001298 if (tty->termios.c_cflag & CRTSCTS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001299 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001300 write_mos_reg(port->serial, port->port_number, MCR,
1301 mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001302 }
1303}
1304
Alan Cox95da3102008-07-22 11:09:07 +01001305static void mos7720_unthrottle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001306{
Alan Cox95da3102008-07-22 11:09:07 +01001307 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001308 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +01001309 int status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001310
1311 if (mos7720_port == NULL)
1312 return;
1313
1314 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001315 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001316 return;
1317 }
1318
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001319 /* if we are implementing XON/XOFF, send the start character */
1320 if (I_IXOFF(tty)) {
1321 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001322 status = mos7720_write(tty, port, &start_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001323 if (status <= 0)
1324 return;
1325 }
1326
1327 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001328 if (tty->termios.c_cflag & CRTSCTS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001329 mos7720_port->shadowMCR |= UART_MCR_RTS;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001330 write_mos_reg(port->serial, port->port_number, MCR,
1331 mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001332 }
1333}
1334
Mike Dunnb69578d2010-04-15 17:01:33 -04001335/* FIXME: this function does not work */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001336static int set_higher_rates(struct moschip_port *mos7720_port,
1337 unsigned int baud)
1338{
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001339 struct usb_serial_port *port;
1340 struct usb_serial *serial;
1341 int port_number;
Mike Dunn63b91762010-04-15 17:02:09 -04001342 enum mos_regs sp_reg;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001343 if (mos7720_port == NULL)
1344 return -EINVAL;
1345
1346 port = mos7720_port->port;
1347 serial = port->serial;
1348
Alan Cox4da1a172008-07-22 11:16:21 +01001349 /***********************************************
1350 * Init Sequence for higher rates
1351 ***********************************************/
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001352 dev_dbg(&port->dev, "Sending Setting Commands ..........\n");
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001353 port_number = port->port_number;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001354
Mike Dunn63b91762010-04-15 17:02:09 -04001355 write_mos_reg(serial, port_number, IER, 0x00);
1356 write_mos_reg(serial, port_number, FCR, 0x00);
1357 write_mos_reg(serial, port_number, FCR, 0xcf);
1358 mos7720_port->shadowMCR = 0x0b;
1359 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1360 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x00);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001361
Alan Cox4da1a172008-07-22 11:16:21 +01001362 /***********************************************
1363 * Set for higher rates *
1364 ***********************************************/
Mike Dunnb69578d2010-04-15 17:01:33 -04001365 /* writing baud rate verbatum into uart clock field clearly not right */
Mike Dunn63b91762010-04-15 17:02:09 -04001366 if (port_number == 0)
1367 sp_reg = SP1_REG;
1368 else
1369 sp_reg = SP2_REG;
1370 write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
1371 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x03);
1372 mos7720_port->shadowMCR = 0x2b;
1373 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001374
Alan Cox4da1a172008-07-22 11:16:21 +01001375 /***********************************************
1376 * Set DLL/DLM
1377 ***********************************************/
Mike Dunn63b91762010-04-15 17:02:09 -04001378 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1379 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1380 write_mos_reg(serial, port_number, DLL, 0x01);
1381 write_mos_reg(serial, port_number, DLM, 0x00);
1382 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1383 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001384
1385 return 0;
1386}
1387
1388/* baud rate information */
Alan Cox4da1a172008-07-22 11:16:21 +01001389struct divisor_table_entry {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001390 __u32 baudrate;
1391 __u16 divisor;
1392};
1393
1394/* Define table of divisors for moschip 7720 hardware *
1395 * These assume a 3.6864MHz crystal, the standard /16, and *
1396 * MCR.7 = 0. */
1397static struct divisor_table_entry divisor_table[] = {
1398 { 50, 2304},
1399 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
1400 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
1401 { 150, 768},
1402 { 300, 384},
1403 { 600, 192},
1404 { 1200, 96},
1405 { 1800, 64},
1406 { 2400, 48},
1407 { 4800, 24},
1408 { 7200, 16},
1409 { 9600, 12},
1410 { 19200, 6},
1411 { 38400, 3},
1412 { 57600, 2},
1413 { 115200, 1},
1414};
1415
1416/*****************************************************************************
1417 * calc_baud_rate_divisor
1418 * this function calculates the proper baud rate divisor for the specified
1419 * baud rate.
1420 *****************************************************************************/
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001421static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, int *divisor)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001422{
1423 int i;
1424 __u16 custom;
1425 __u16 round1;
1426 __u16 round;
1427
1428
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001429 dev_dbg(&port->dev, "%s - %d\n", __func__, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001430
1431 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
1432 if (divisor_table[i].baudrate == baudrate) {
1433 *divisor = divisor_table[i].divisor;
1434 return 0;
1435 }
1436 }
1437
Alan Cox4da1a172008-07-22 11:16:21 +01001438 /* After trying for all the standard baud rates *
1439 * Try calculating the divisor for this baud rate */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001440 if (baudrate > 75 && baudrate < 230400) {
1441 /* get the divisor */
1442 custom = (__u16)(230400L / baudrate);
1443
1444 /* Check for round off */
1445 round1 = (__u16)(2304000L / baudrate);
1446 round = (__u16)(round1 - (custom * 10));
1447 if (round > 4)
1448 custom++;
1449 *divisor = custom;
1450
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001451 dev_dbg(&port->dev, "Baud %d = %d\n", baudrate, custom);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001452 return 0;
1453 }
1454
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001455 dev_dbg(&port->dev, "Baud calculation Failed...\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001456 return -EINVAL;
1457}
1458
1459/*
1460 * send_cmd_write_baud_rate
1461 * this function sends the proper command to change the baud rate of the
1462 * specified port.
1463 */
1464static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1465 int baudrate)
1466{
1467 struct usb_serial_port *port;
1468 struct usb_serial *serial;
1469 int divisor;
1470 int status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001471 unsigned char number;
1472
1473 if (mos7720_port == NULL)
1474 return -1;
1475
1476 port = mos7720_port->port;
1477 serial = port->serial;
1478
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001479 number = port->port_number;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001480 dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001481
Alan Cox4da1a172008-07-22 11:16:21 +01001482 /* Calculate the Divisor */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001483 status = calc_baud_rate_divisor(port, baudrate, &divisor);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001484 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001485 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001486 return status;
1487 }
1488
Alan Cox4da1a172008-07-22 11:16:21 +01001489 /* Enable access to divisor latch */
Mike Dunn63b91762010-04-15 17:02:09 -04001490 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1491 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001492
1493 /* Write the divisor */
Mike Dunn63b91762010-04-15 17:02:09 -04001494 write_mos_reg(serial, number, DLL, (__u8)(divisor & 0xff));
1495 write_mos_reg(serial, number, DLM, (__u8)((divisor & 0xff00) >> 8));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001496
Alan Cox4da1a172008-07-22 11:16:21 +01001497 /* Disable access to divisor latch */
Mike Dunn63b91762010-04-15 17:02:09 -04001498 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1499 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001500
1501 return status;
1502}
1503
1504/*
1505 * change_port_settings
1506 * This routine is called to set the UART on the device to match
1507 * the specified new settings.
1508 */
Alan Cox95da3102008-07-22 11:09:07 +01001509static void change_port_settings(struct tty_struct *tty,
1510 struct moschip_port *mos7720_port,
Alan Cox606d0992006-12-08 02:38:45 -08001511 struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001512{
1513 struct usb_serial_port *port;
1514 struct usb_serial *serial;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001515 int baud;
1516 unsigned cflag;
1517 unsigned iflag;
1518 __u8 mask = 0xff;
1519 __u8 lData;
1520 __u8 lParity;
1521 __u8 lStop;
1522 int status;
1523 int port_number;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001524
1525 if (mos7720_port == NULL)
1526 return ;
1527
1528 port = mos7720_port->port;
1529 serial = port->serial;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001530 port_number = port->port_number;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001531
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001532 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001533 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001534 return;
1535 }
1536
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001537 lData = UART_LCR_WLEN8;
1538 lStop = 0x00; /* 1 stop bit */
1539 lParity = 0x00; /* No parity */
1540
Alan Coxadc8d742012-07-14 15:31:47 +01001541 cflag = tty->termios.c_cflag;
1542 iflag = tty->termios.c_iflag;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001543
1544 /* Change the number of bits */
1545 switch (cflag & CSIZE) {
1546 case CS5:
1547 lData = UART_LCR_WLEN5;
1548 mask = 0x1f;
1549 break;
1550
1551 case CS6:
1552 lData = UART_LCR_WLEN6;
1553 mask = 0x3f;
1554 break;
1555
1556 case CS7:
1557 lData = UART_LCR_WLEN7;
1558 mask = 0x7f;
1559 break;
1560 default:
1561 case CS8:
1562 lData = UART_LCR_WLEN8;
1563 break;
1564 }
1565
1566 /* Change the Parity bit */
1567 if (cflag & PARENB) {
1568 if (cflag & PARODD) {
1569 lParity = UART_LCR_PARITY;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001570 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001571 } else {
1572 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001573 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001574 }
1575
1576 } else {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001577 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001578 }
1579
1580 if (cflag & CMSPAR)
1581 lParity = lParity | 0x20;
1582
1583 /* Change the Stop bit */
1584 if (cflag & CSTOPB) {
1585 lStop = UART_LCR_STOP;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001586 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001587 } else {
1588 lStop = 0x00;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001589 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001590 }
1591
1592#define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1593#define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1594#define LCR_PAR_MASK 0x38 /* Mask for parity field */
1595
1596 /* Update the LCR with the correct value */
Alan Cox4da1a172008-07-22 11:16:21 +01001597 mos7720_port->shadowLCR &=
Mike Dunn63b91762010-04-15 17:02:09 -04001598 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001599 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1600
1601
1602 /* Disable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001603 write_mos_reg(serial, port_number, IER, 0x00);
1604 write_mos_reg(serial, port_number, FCR, 0x00);
1605 write_mos_reg(serial, port_number, FCR, 0xcf);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001606
1607 /* Send the updated LCR value to the mos7720 */
Mike Dunn63b91762010-04-15 17:02:09 -04001608 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1609 mos7720_port->shadowMCR = 0x0b;
1610 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001611
1612 /* set up the MCR register and send it to the mos7720 */
1613 mos7720_port->shadowMCR = UART_MCR_OUT2;
1614 if (cflag & CBAUD)
1615 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1616
1617 if (cflag & CRTSCTS) {
1618 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
Alan Cox4da1a172008-07-22 11:16:21 +01001619 /* To set hardware flow control to the specified *
1620 * serial port, in SP1/2_CONTROL_REG */
Johan Hovolda26f0092013-06-04 18:50:31 +02001621 if (port_number)
Mike Dunn63b91762010-04-15 17:02:09 -04001622 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x01);
1623 else
1624 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x02);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001625
Mike Dunn63b91762010-04-15 17:02:09 -04001626 } else
1627 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1628
1629 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001630
1631 /* Determine divisor based on baud rate */
1632 baud = tty_get_baud_rate(tty);
1633 if (!baud) {
1634 /* pick a default, any default... */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001635 dev_dbg(&port->dev, "Picked default baud...\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001636 baud = 9600;
1637 }
1638
1639 if (baud >= 230400) {
1640 set_higher_rates(mos7720_port, baud);
1641 /* Enable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001642 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001643 return;
1644 }
1645
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001646 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001647 status = send_cmd_write_baud_rate(mos7720_port, baud);
Alan Cox65d063ab2008-01-03 17:01:18 +00001648 /* FIXME: needs to write actual resulting baud back not just
1649 blindly do so */
1650 if (cflag & CBAUD)
1651 tty_encode_baud_rate(tty, baud, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001652 /* Enable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001653 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001654
1655 if (port->read_urb->status != -EINPROGRESS) {
Johan Hovold1dbd11b2014-10-29 09:07:33 +01001656 status = usb_submit_urb(port->read_urb, GFP_KERNEL);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001657 if (status)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001658 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001659 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001660}
1661
1662/*
1663 * mos7720_set_termios
1664 * this function is called by the tty driver when it wants to change the
1665 * termios structure.
1666 */
Alan Cox95da3102008-07-22 11:09:07 +01001667static void mos7720_set_termios(struct tty_struct *tty,
1668 struct usb_serial_port *port, struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001669{
1670 int status;
1671 unsigned int cflag;
1672 struct usb_serial *serial;
1673 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001674
1675 serial = port->serial;
1676
1677 mos7720_port = usb_get_serial_port_data(port);
1678
1679 if (mos7720_port == NULL)
1680 return;
1681
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001682 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001683 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001684 return;
1685 }
1686
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001687 dev_dbg(&port->dev, "setting termios - ASPIRE\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001688
Alan Coxadc8d742012-07-14 15:31:47 +01001689 cflag = tty->termios.c_cflag;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001690
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001691 dev_dbg(&port->dev, "%s - cflag %08x iflag %08x\n", __func__,
Linus Torvaldsd9a80742012-10-01 13:23:01 -07001692 tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001693
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001694 dev_dbg(&port->dev, "%s - old cflag %08x old iflag %08x\n", __func__,
1695 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001696
1697 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001698 change_port_settings(tty, mos7720_port, old_termios);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001699
Alan Cox4da1a172008-07-22 11:16:21 +01001700 if (port->read_urb->status != -EINPROGRESS) {
Johan Hovold1dbd11b2014-10-29 09:07:33 +01001701 status = usb_submit_urb(port->read_urb, GFP_KERNEL);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001702 if (status)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001703 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001704 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001705}
1706
1707/*
1708 * get_lsr_info - get line status register info
1709 *
1710 * Purpose: Let user call ioctl() to get info when the UART physically
1711 * is emptied. On bus types like RS485, the transmitter must
1712 * release the bus after transmitting. This must be done when
1713 * the transmit shift register is empty, not be done when the
1714 * transmit holding register is empty. This functionality
1715 * allows an RS485 driver to be written in user space.
1716 */
Alan Cox4da1a172008-07-22 11:16:21 +01001717static int get_lsr_info(struct tty_struct *tty,
1718 struct moschip_port *mos7720_port, unsigned int __user *value)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001719{
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001720 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001721 unsigned int result = 0;
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001722 unsigned char data = 0;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001723 int port_number = port->port_number;
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001724 int count;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001725
Alan Cox95da3102008-07-22 11:09:07 +01001726 count = mos7720_chars_in_buffer(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001727 if (count == 0) {
Mike Dunn63b91762010-04-15 17:02:09 -04001728 read_mos_reg(port->serial, port_number, LSR, &data);
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001729 if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1730 == (UART_LSR_TEMT | UART_LSR_THRE)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001731 dev_dbg(&port->dev, "%s -- Empty\n", __func__);
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001732 result = TIOCSER_TEMT;
1733 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001734 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001735 if (copy_to_user(value, &result, sizeof(int)))
1736 return -EFAULT;
1737 return 0;
1738}
1739
Alan Cox60b33c12011-02-14 16:26:14 +00001740static int mos7720_tiocmget(struct tty_struct *tty)
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001741{
1742 struct usb_serial_port *port = tty->driver_data;
1743 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1744 unsigned int result = 0;
1745 unsigned int mcr ;
1746 unsigned int msr ;
1747
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001748 mcr = mos7720_port->shadowMCR;
1749 msr = mos7720_port->shadowMSR;
1750
1751 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1752 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1753 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1754 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
1755 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1756 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
1757
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001758 return result;
1759}
1760
Alan Cox20b9d172011-02-14 16:26:50 +00001761static int mos7720_tiocmset(struct tty_struct *tty,
Mike Dunn63b91762010-04-15 17:02:09 -04001762 unsigned int set, unsigned int clear)
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001763{
1764 struct usb_serial_port *port = tty->driver_data;
1765 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1766 unsigned int mcr ;
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001767
1768 mcr = mos7720_port->shadowMCR;
1769
1770 if (set & TIOCM_RTS)
1771 mcr |= UART_MCR_RTS;
1772 if (set & TIOCM_DTR)
1773 mcr |= UART_MCR_DTR;
1774 if (set & TIOCM_LOOP)
1775 mcr |= UART_MCR_LOOP;
1776
1777 if (clear & TIOCM_RTS)
1778 mcr &= ~UART_MCR_RTS;
1779 if (clear & TIOCM_DTR)
1780 mcr &= ~UART_MCR_DTR;
1781 if (clear & TIOCM_LOOP)
1782 mcr &= ~UART_MCR_LOOP;
1783
1784 mos7720_port->shadowMCR = mcr;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001785 write_mos_reg(port->serial, port->port_number, MCR,
1786 mos7720_port->shadowMCR);
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001787
1788 return 0;
1789}
1790
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001791static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1792 unsigned int __user *value)
1793{
Alan Cox0bca1b92010-09-16 18:21:40 +01001794 unsigned int mcr;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001795 unsigned int arg;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001796
1797 struct usb_serial_port *port;
1798
1799 if (mos7720_port == NULL)
1800 return -1;
1801
Alan Cox4da1a172008-07-22 11:16:21 +01001802 port = (struct usb_serial_port *)mos7720_port->port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001803 mcr = mos7720_port->shadowMCR;
1804
1805 if (copy_from_user(&arg, value, sizeof(int)))
1806 return -EFAULT;
1807
1808 switch (cmd) {
1809 case TIOCMBIS:
1810 if (arg & TIOCM_RTS)
1811 mcr |= UART_MCR_RTS;
1812 if (arg & TIOCM_DTR)
1813 mcr |= UART_MCR_RTS;
1814 if (arg & TIOCM_LOOP)
1815 mcr |= UART_MCR_LOOP;
1816 break;
1817
1818 case TIOCMBIC:
1819 if (arg & TIOCM_RTS)
1820 mcr &= ~UART_MCR_RTS;
1821 if (arg & TIOCM_DTR)
1822 mcr &= ~UART_MCR_RTS;
1823 if (arg & TIOCM_LOOP)
1824 mcr &= ~UART_MCR_LOOP;
1825 break;
1826
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001827 }
1828
1829 mos7720_port->shadowMCR = mcr;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001830 write_mos_reg(port->serial, port->port_number, MCR,
1831 mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001832
1833 return 0;
1834}
1835
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001836static int get_serial_info(struct moschip_port *mos7720_port,
1837 struct serial_struct __user *retinfo)
1838{
1839 struct serial_struct tmp;
1840
1841 if (!retinfo)
1842 return -EFAULT;
1843
1844 memset(&tmp, 0, sizeof(tmp));
1845
1846 tmp.type = PORT_16550A;
Greg Kroah-Hartmane5b1e202013-06-07 11:04:28 -07001847 tmp.line = mos7720_port->port->minor;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001848 tmp.port = mos7720_port->port->port_number;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001849 tmp.irq = 0;
1850 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
Alan Cox4da1a172008-07-22 11:16:21 +01001851 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001852 tmp.baud_base = 9600;
1853 tmp.close_delay = 5*HZ;
1854 tmp.closing_wait = 30*HZ;
1855
1856 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1857 return -EFAULT;
1858 return 0;
1859}
1860
Alan Cox00a0d0d2011-02-14 16:27:06 +00001861static int mos7720_ioctl(struct tty_struct *tty,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001862 unsigned int cmd, unsigned long arg)
1863{
Alan Cox95da3102008-07-22 11:09:07 +01001864 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001865 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001866
1867 mos7720_port = usb_get_serial_port_data(port);
1868 if (mos7720_port == NULL)
1869 return -ENODEV;
1870
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001871 switch (cmd) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001872 case TIOCSERGETLSR:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001873 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
Alan Cox4da1a172008-07-22 11:16:21 +01001874 return get_lsr_info(tty, mos7720_port,
1875 (unsigned int __user *)arg);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001876
Alan Cox95da3102008-07-22 11:09:07 +01001877 /* FIXME: These should be using the mode methods */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001878 case TIOCMBIS:
1879 case TIOCMBIC:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001880 dev_dbg(&port->dev, "%s TIOCMSET/TIOCMBIC/TIOCMSET\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001881 return set_modem_info(mos7720_port, cmd,
1882 (unsigned int __user *)arg);
1883
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001884 case TIOCGSERIAL:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001885 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001886 return get_serial_info(mos7720_port,
1887 (struct serial_struct __user *)arg);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001888 }
1889
1890 return -ENOIOCTLCMD;
1891}
1892
1893static int mos7720_startup(struct usb_serial *serial)
1894{
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001895 struct usb_device *dev;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001896 char data;
Huzaifa Sidhpurwala91f58ae2011-02-21 12:58:45 +05301897 u16 product;
Mike Dunnb69578d2010-04-15 17:01:33 -04001898 int ret_val;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001899
Huzaifa Sidhpurwala91f58ae2011-02-21 12:58:45 +05301900 product = le16_to_cpu(serial->dev->descriptor.idProduct);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001901 dev = serial->dev;
1902
Mike Dunnfb088e32010-01-26 12:12:12 -05001903 /*
1904 * The 7715 uses the first bulk in/out endpoint pair for the parallel
1905 * port, and the second for the serial port. Because the usbserial core
1906 * assumes both pairs are serial ports, we must engage in a bit of
1907 * subterfuge and swap the pointers for ports 0 and 1 in order to make
1908 * port 0 point to the serial port. However, both moschip devices use a
1909 * single interrupt-in endpoint for both ports (as mentioned a little
1910 * further down), and this endpoint was assigned to port 0. So after
1911 * the swap, we must copy the interrupt endpoint elements from port 1
1912 * (as newly assigned) to port 0, and null out port 1 pointers.
1913 */
1914 if (product == MOSCHIP_DEVICE_ID_7715) {
1915 struct usb_serial_port *tmp = serial->port[0];
1916 serial->port[0] = serial->port[1];
1917 serial->port[1] = tmp;
1918 serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb;
1919 serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer;
1920 serial->port[0]->interrupt_in_endpointAddress =
1921 tmp->interrupt_in_endpointAddress;
1922 serial->port[1]->interrupt_in_urb = NULL;
1923 serial->port[1]->interrupt_in_buffer = NULL;
1924 }
1925
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001926 /* setting configuration feature to one */
1927 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Johan Hovold849513a2013-05-27 14:44:43 +02001928 (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5000);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001929
Mike Dunnb69578d2010-04-15 17:01:33 -04001930 /* start the interrupt urb */
1931 ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
1932 if (ret_val)
1933 dev_err(&dev->dev,
1934 "%s - Error %d submitting control urb\n",
1935 __func__, ret_val);
1936
1937#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1938 if (product == MOSCHIP_DEVICE_ID_7715) {
1939 ret_val = mos7715_parport_init(serial);
1940 if (ret_val < 0)
1941 return ret_val;
1942 }
1943#endif
Alan Cox4da1a172008-07-22 11:16:21 +01001944 /* LSR For Port 1 */
Mike Dunn63b91762010-04-15 17:02:09 -04001945 read_mos_reg(serial, 0, LSR, &data);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001946 dev_dbg(&dev->dev, "LSR:%x\n", data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001947
1948 return 0;
1949}
1950
Alan Sternf9c99bb2009-06-02 11:53:55 -04001951static void mos7720_release(struct usb_serial *serial)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001952{
Mike Dunnb69578d2010-04-15 17:01:33 -04001953#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1954 /* close the parallel port */
1955
1956 if (le16_to_cpu(serial->dev->descriptor.idProduct)
1957 == MOSCHIP_DEVICE_ID_7715) {
1958 struct urbtracker *urbtrack;
1959 unsigned long flags;
1960 struct mos7715_parport *mos_parport =
1961 usb_get_serial_data(serial);
1962
1963 /* prevent NULL ptr dereference in port callbacks */
1964 spin_lock(&release_lock);
1965 mos_parport->pp->private_data = NULL;
1966 spin_unlock(&release_lock);
1967
1968 /* wait for synchronous usb calls to return */
1969 if (mos_parport->msg_pending)
1970 wait_for_completion_timeout(&mos_parport->syncmsg_compl,
Johan Hovold849513a2013-05-27 14:44:43 +02001971 msecs_to_jiffies(MOS_WDR_TIMEOUT));
Mike Dunnb69578d2010-04-15 17:01:33 -04001972
1973 parport_remove_port(mos_parport->pp);
1974 usb_set_serial_data(serial, NULL);
1975 mos_parport->serial = NULL;
1976
1977 /* if tasklet currently scheduled, wait for it to complete */
1978 tasklet_kill(&mos_parport->urb_tasklet);
1979
1980 /* unlink any urbs sent by the tasklet */
1981 spin_lock_irqsave(&mos_parport->listlock, flags);
1982 list_for_each_entry(urbtrack,
1983 &mos_parport->active_urbs,
1984 urblist_entry)
1985 usb_unlink_urb(urbtrack->urb);
1986 spin_unlock_irqrestore(&mos_parport->listlock, flags);
1987
1988 kref_put(&mos_parport->ref_count, destroy_mos_parport);
1989 }
1990#endif
Johan Hovold4230af52012-10-25 10:29:05 +02001991}
1992
1993static int mos7720_port_probe(struct usb_serial_port *port)
1994{
1995 struct moschip_port *mos7720_port;
1996
1997 mos7720_port = kzalloc(sizeof(*mos7720_port), GFP_KERNEL);
1998 if (!mos7720_port)
1999 return -ENOMEM;
2000
2001 /* Initialize all port interrupt end point to port 0 int endpoint.
2002 * Our device has only one interrupt endpoint common to all ports.
2003 */
2004 port->interrupt_in_endpointAddress =
2005 port->serial->port[0]->interrupt_in_endpointAddress;
2006 mos7720_port->port = port;
2007
2008 usb_set_serial_port_data(port, mos7720_port);
2009
2010 return 0;
2011}
2012
2013static int mos7720_port_remove(struct usb_serial_port *port)
2014{
2015 struct moschip_port *mos7720_port;
2016
2017 mos7720_port = usb_get_serial_port_data(port);
2018 kfree(mos7720_port);
2019
2020 return 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002021}
2022
2023static struct usb_serial_driver moschip7720_2port_driver = {
2024 .driver = {
2025 .owner = THIS_MODULE,
2026 .name = "moschip7720",
2027 },
2028 .description = "Moschip 2 port adapter",
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002029 .id_table = id_table,
Mike Dunnfb088e32010-01-26 12:12:12 -05002030 .calc_num_ports = mos77xx_calc_num_ports,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002031 .open = mos7720_open,
2032 .close = mos7720_close,
2033 .throttle = mos7720_throttle,
2034 .unthrottle = mos7720_unthrottle,
Mike Dunnfb088e32010-01-26 12:12:12 -05002035 .probe = mos77xx_probe,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002036 .attach = mos7720_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002037 .release = mos7720_release,
Johan Hovold4230af52012-10-25 10:29:05 +02002038 .port_probe = mos7720_port_probe,
2039 .port_remove = mos7720_port_remove,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002040 .ioctl = mos7720_ioctl,
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07002041 .tiocmget = mos7720_tiocmget,
2042 .tiocmset = mos7720_tiocmset,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002043 .set_termios = mos7720_set_termios,
2044 .write = mos7720_write,
2045 .write_room = mos7720_write_room,
2046 .chars_in_buffer = mos7720_chars_in_buffer,
2047 .break_ctl = mos7720_break,
2048 .read_bulk_callback = mos7720_bulk_in_callback,
Mike Dunnfb088e32010-01-26 12:12:12 -05002049 .read_int_callback = NULL /* dynamically assigned in probe() */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002050};
2051
Alan Stern4d2a7af2012-02-23 14:57:09 -05002052static struct usb_serial_driver * const serial_drivers[] = {
2053 &moschip7720_2port_driver, NULL
2054};
2055
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002056module_usb_serial_driver(serial_drivers, id_table);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002057
Alan Cox4da1a172008-07-22 11:16:21 +01002058MODULE_AUTHOR(DRIVER_AUTHOR);
2059MODULE_DESCRIPTION(DRIVER_DESC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002060MODULE_LICENSE("GPL");