blob: 23544074eb1c3d1a1722ccc487601c7846beb492 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002/*
3 * mos7720.c
Rahul Bedarkarcd8c5052014-01-02 19:29:24 +05304 * Controls the Moschip 7720 usb to dual port serial converter
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07005 *
6 * Copyright 2006 Moschip Semiconductor Tech. Ltd.
7 *
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07008 * Developed by:
Greg Kroah-Hartman50d2dc72007-06-25 01:08:01 -07009 * Vijaya Kumar <vijaykumar.gn@gmail.com>
10 * Ajay Kumar <naanuajay@yahoo.com>
11 * Gurudeva <ngurudeva@yahoo.com>
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070012 *
13 * Cleaned up from the original by:
14 * Greg Kroah-Hartman <gregkh@suse.de>
15 *
16 * Originally based on drivers/usb/serial/io_edgeport.c which is:
17 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
18 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
19 */
20#include <linux/kernel.h>
21#include <linux/errno.h>
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070022#include <linux/slab.h>
23#include <linux/tty.h>
24#include <linux/tty_driver.h>
25#include <linux/tty_flip.h>
26#include <linux/module.h>
27#include <linux/spinlock.h>
28#include <linux/serial.h>
29#include <linux/serial_reg.h>
30#include <linux/usb.h>
31#include <linux/usb/serial.h>
Alan Cox4da1a172008-07-22 11:16:21 +010032#include <linux/uaccess.h>
Mike Dunnb69578d2010-04-15 17:01:33 -040033#include <linux/parport.h>
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070034
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070035#define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
36#define DRIVER_DESC "Moschip USB Serial Driver"
37
38/* default urb timeout */
Johan Hovold849513a2013-05-27 14:44:43 +020039#define MOS_WDR_TIMEOUT 5000
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070040
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070041#define MOS_MAX_PORT 0x02
42#define MOS_WRITE 0x0E
43#define MOS_READ 0x0D
44
Rahul Bedarkarcd8c5052014-01-02 19:29:24 +053045/* Interrupt Routines Defines */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070046#define SERIAL_IIR_RLS 0x06
47#define SERIAL_IIR_RDA 0x04
48#define SERIAL_IIR_CTI 0x0c
49#define SERIAL_IIR_THR 0x02
50#define SERIAL_IIR_MS 0x00
51
52#define NUM_URBS 16 /* URB Count */
53#define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
54
Mike Dunnb69578d2010-04-15 17:01:33 -040055/* This structure holds all of the local serial port information */
Alan Cox4da1a172008-07-22 11:16:21 +010056struct moschip_port {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070057 __u8 shadowLCR; /* last LCR value received */
58 __u8 shadowMCR; /* last MCR value received */
59 __u8 shadowMSR; /* last MSR value received */
60 char open;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070061 struct usb_serial_port *port; /* loop back to the owner */
62 struct urb *write_urb_pool[NUM_URBS];
63};
64
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070065#define USB_VENDOR_ID_MOSCHIP 0x9710
66#define MOSCHIP_DEVICE_ID_7720 0x7720
67#define MOSCHIP_DEVICE_ID_7715 0x7715
68
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -070069static const struct usb_device_id id_table[] = {
Alan Cox4da1a172008-07-22 11:16:21 +010070 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
Mike Dunnfb088e32010-01-26 12:12:12 -050071 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) },
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070072 { } /* terminating entry */
73};
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -070074MODULE_DEVICE_TABLE(usb, id_table);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070075
Mike Dunnb69578d2010-04-15 17:01:33 -040076#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
77
78/* initial values for parport regs */
79#define DCR_INIT_VAL 0x0c /* SLCTIN, nINIT */
80#define ECR_INIT_VAL 0x00 /* SPP mode */
81
Mike Dunnb69578d2010-04-15 17:01:33 -040082enum mos7715_pp_modes {
83 SPP = 0<<5,
84 PS2 = 1<<5, /* moschip calls this 'NIBBLE' mode */
85 PPF = 2<<5, /* moschip calls this 'CB-FIFO mode */
86};
87
88struct mos7715_parport {
89 struct parport *pp; /* back to containing struct */
90 struct kref ref_count; /* to instance of this struct */
Mike Dunnb69578d2010-04-15 17:01:33 -040091 bool msg_pending; /* usb sync call pending */
92 struct completion syncmsg_compl; /* usb sync call completed */
Davidlohr Bueso053af9e2020-11-19 20:53:00 -080093 struct work_struct work; /* restore deferred writes */
Mike Dunnb69578d2010-04-15 17:01:33 -040094 struct usb_serial *serial; /* back to containing struct */
95 __u8 shadowECR; /* parallel port regs... */
96 __u8 shadowDCR;
97 atomic_t shadowDSR; /* updated in int-in callback */
98};
99
100/* lock guards against dereferencing NULL ptr in parport ops callbacks */
101static DEFINE_SPINLOCK(release_lock);
102
Mike Dunn63b91762010-04-15 17:02:09 -0400103#endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
104
105static const unsigned int dummy; /* for clarity in register access fns */
106
Mike Dunnb69578d2010-04-15 17:01:33 -0400107enum mos_regs {
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +0530108 MOS7720_THR, /* serial port regs */
109 MOS7720_RHR,
110 MOS7720_IER,
111 MOS7720_FCR,
112 MOS7720_ISR,
113 MOS7720_LCR,
114 MOS7720_MCR,
115 MOS7720_LSR,
116 MOS7720_MSR,
117 MOS7720_SPR,
118 MOS7720_DLL,
119 MOS7720_DLM,
120 MOS7720_DPR, /* parallel port regs */
121 MOS7720_DSR,
122 MOS7720_DCR,
123 MOS7720_ECR,
124 MOS7720_SP1_REG, /* device control regs */
125 MOS7720_SP2_REG, /* serial port 2 (7720 only) */
126 MOS7720_PP_REG,
127 MOS7720_SP_CONTROL_REG,
Mike Dunnb69578d2010-04-15 17:01:33 -0400128};
129
130/*
131 * Return the correct value for the Windex field of the setup packet
132 * for a control endpoint message. See the 7715 datasheet.
133 */
134static inline __u16 get_reg_index(enum mos_regs reg)
135{
136 static const __u16 mos7715_index_lookup_table[] = {
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +0530137 0x00, /* MOS7720_THR */
138 0x00, /* MOS7720_RHR */
139 0x01, /* MOS7720_IER */
140 0x02, /* MOS7720_FCR */
141 0x02, /* MOS7720_ISR */
142 0x03, /* MOS7720_LCR */
143 0x04, /* MOS7720_MCR */
144 0x05, /* MOS7720_LSR */
145 0x06, /* MOS7720_MSR */
146 0x07, /* MOS7720_SPR */
147 0x00, /* MOS7720_DLL */
148 0x01, /* MOS7720_DLM */
149 0x00, /* MOS7720_DPR */
150 0x01, /* MOS7720_DSR */
151 0x02, /* MOS7720_DCR */
152 0x0a, /* MOS7720_ECR */
153 0x01, /* MOS7720_SP1_REG */
154 0x02, /* MOS7720_SP2_REG (7720 only) */
155 0x04, /* MOS7720_PP_REG (7715 only) */
156 0x08, /* MOS7720_SP_CONTROL_REG */
Mike Dunnb69578d2010-04-15 17:01:33 -0400157 };
158 return mos7715_index_lookup_table[reg];
159}
160
161/*
162 * Return the correct value for the upper byte of the Wvalue field of
163 * the setup packet for a control endpoint message.
164 */
Mike Dunn63b91762010-04-15 17:02:09 -0400165static inline __u16 get_reg_value(enum mos_regs reg,
166 unsigned int serial_portnum)
Mike Dunnb69578d2010-04-15 17:01:33 -0400167{
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +0530168 if (reg >= MOS7720_SP1_REG) /* control reg */
Mike Dunnb69578d2010-04-15 17:01:33 -0400169 return 0x0000;
Mike Dunn63b91762010-04-15 17:02:09 -0400170
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +0530171 else if (reg >= MOS7720_DPR) /* parallel port reg (7715 only) */
Mike Dunnb69578d2010-04-15 17:01:33 -0400172 return 0x0100;
Mike Dunn63b91762010-04-15 17:02:09 -0400173
174 else /* serial port reg */
175 return (serial_portnum + 2) << 8;
Mike Dunnb69578d2010-04-15 17:01:33 -0400176}
177
178/*
179 * Write data byte to the specified device register. The data is embedded in
Mike Dunn63b91762010-04-15 17:02:09 -0400180 * the value field of the setup packet. serial_portnum is ignored for registers
181 * not specific to a particular serial port.
Mike Dunnb69578d2010-04-15 17:01:33 -0400182 */
Mike Dunn63b91762010-04-15 17:02:09 -0400183static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
184 enum mos_regs reg, __u8 data)
Mike Dunnb69578d2010-04-15 17:01:33 -0400185{
Mike Dunnb69578d2010-04-15 17:01:33 -0400186 struct usb_device *usbdev = serial->dev;
187 unsigned int pipe = usb_sndctrlpipe(usbdev, 0);
188 __u8 request = (__u8)0x0e;
189 __u8 requesttype = (__u8)0x40;
Mike Dunnb69578d2010-04-15 17:01:33 -0400190 __u16 index = get_reg_index(reg);
Mike Dunn63b91762010-04-15 17:02:09 -0400191 __u16 value = get_reg_value(reg, serial_portnum) + data;
192 int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
193 index, NULL, 0, MOS_WDR_TIMEOUT);
Mike Dunnb69578d2010-04-15 17:01:33 -0400194 if (status < 0)
195 dev_err(&usbdev->dev,
Johan Hovoldd9a38a82014-03-12 19:09:42 +0100196 "mos7720: usb_control_msg() failed: %d\n", status);
Mike Dunnb69578d2010-04-15 17:01:33 -0400197 return status;
198}
199
200/*
201 * Read data byte from the specified device register. The data returned by the
Mike Dunn63b91762010-04-15 17:02:09 -0400202 * device is embedded in the value field of the setup packet. serial_portnum is
203 * ignored for registers that are not specific to a particular serial port.
Mike Dunnb69578d2010-04-15 17:01:33 -0400204 */
Mike Dunn63b91762010-04-15 17:02:09 -0400205static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
206 enum mos_regs reg, __u8 *data)
Mike Dunnb69578d2010-04-15 17:01:33 -0400207{
Mike Dunn63b91762010-04-15 17:02:09 -0400208 struct usb_device *usbdev = serial->dev;
Mike Dunnb69578d2010-04-15 17:01:33 -0400209 unsigned int pipe = usb_rcvctrlpipe(usbdev, 0);
210 __u8 request = (__u8)0x0d;
211 __u8 requesttype = (__u8)0xc0;
Mike Dunnb69578d2010-04-15 17:01:33 -0400212 __u16 index = get_reg_index(reg);
Mike Dunn63b91762010-04-15 17:02:09 -0400213 __u16 value = get_reg_value(reg, serial_portnum);
Johan Hovold72ea18a2013-05-27 14:44:39 +0200214 u8 *buf;
215 int status;
216
217 buf = kmalloc(1, GFP_KERNEL);
Tom Rix161a5822021-01-11 14:09:04 -0800218 if (!buf) {
219 *data = 0;
Johan Hovold72ea18a2013-05-27 14:44:39 +0200220 return -ENOMEM;
Tom Rix161a5822021-01-11 14:09:04 -0800221 }
Johan Hovold72ea18a2013-05-27 14:44:39 +0200222
223 status = usb_control_msg(usbdev, pipe, request, requesttype, value,
224 index, buf, 1, MOS_WDR_TIMEOUT);
Johan Hovold0d130362017-01-12 14:56:17 +0100225 if (status == 1) {
Johan Hovold72ea18a2013-05-27 14:44:39 +0200226 *data = *buf;
Johan Hovold0d130362017-01-12 14:56:17 +0100227 } else {
Mike Dunnb69578d2010-04-15 17:01:33 -0400228 dev_err(&usbdev->dev,
Johan Hovoldd9a38a82014-03-12 19:09:42 +0100229 "mos7720: usb_control_msg() failed: %d\n", status);
Johan Hovold0d130362017-01-12 14:56:17 +0100230 if (status >= 0)
231 status = -EIO;
232 *data = 0;
233 }
234
Johan Hovold72ea18a2013-05-27 14:44:39 +0200235 kfree(buf);
236
Mike Dunnb69578d2010-04-15 17:01:33 -0400237 return status;
238}
239
Mike Dunn63b91762010-04-15 17:02:09 -0400240#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
241
Mike Dunnb69578d2010-04-15 17:01:33 -0400242static inline int mos7715_change_mode(struct mos7715_parport *mos_parport,
243 enum mos7715_pp_modes mode)
244{
245 mos_parport->shadowECR = mode;
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +0530246 write_mos_reg(mos_parport->serial, dummy, MOS7720_ECR,
247 mos_parport->shadowECR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400248 return 0;
249}
250
251static void destroy_mos_parport(struct kref *kref)
252{
253 struct mos7715_parport *mos_parport =
254 container_of(kref, struct mos7715_parport, ref_count);
255
Mike Dunnb69578d2010-04-15 17:01:33 -0400256 kfree(mos_parport);
257}
258
Mike Dunnb69578d2010-04-15 17:01:33 -0400259/*
Davidlohr Bueso053af9e2020-11-19 20:53:00 -0800260 * This is the common top part of all parallel port callback operations that
Mike Dunnb69578d2010-04-15 17:01:33 -0400261 * send synchronous messages to the device. This implements convoluted locking
262 * that avoids two scenarios: (1) a port operation is called after usbserial
263 * has called our release function, at which point struct mos7715_parport has
264 * been destroyed, and (2) the device has been disconnected, but usbserial has
265 * not called the release function yet because someone has a serial port open.
266 * The shared release_lock prevents the first, and the mutex and disconnected
267 * flag maintained by usbserial covers the second. We also use the msg_pending
Rahul Bedarkarcd8c5052014-01-02 19:29:24 +0530268 * flag to ensure that all synchronous usb message calls have completed before
Mike Dunnb69578d2010-04-15 17:01:33 -0400269 * our release function can return.
270 */
271static int parport_prologue(struct parport *pp)
272{
273 struct mos7715_parport *mos_parport;
274
275 spin_lock(&release_lock);
276 mos_parport = pp->private_data;
277 if (unlikely(mos_parport == NULL)) {
278 /* release fn called, port struct destroyed */
279 spin_unlock(&release_lock);
280 return -1;
281 }
282 mos_parport->msg_pending = true; /* synch usb call pending */
Wolfram Sang16735d02013-11-14 14:32:02 -0800283 reinit_completion(&mos_parport->syncmsg_compl);
Mike Dunnb69578d2010-04-15 17:01:33 -0400284 spin_unlock(&release_lock);
285
Davidlohr Bueso053af9e2020-11-19 20:53:00 -0800286 /* ensure writes from restore are submitted before new requests */
287 if (work_pending(&mos_parport->work))
288 flush_work(&mos_parport->work);
289
Mike Dunnb69578d2010-04-15 17:01:33 -0400290 mutex_lock(&mos_parport->serial->disc_mutex);
291 if (mos_parport->serial->disconnected) {
292 /* device disconnected */
293 mutex_unlock(&mos_parport->serial->disc_mutex);
294 mos_parport->msg_pending = false;
295 complete(&mos_parport->syncmsg_compl);
296 return -1;
297 }
298
299 return 0;
300}
301
302/*
Rahul Bedarkarcd8c5052014-01-02 19:29:24 +0530303 * This is the common bottom part of all parallel port functions that send
Mike Dunnb69578d2010-04-15 17:01:33 -0400304 * synchronous messages to the device.
305 */
306static inline void parport_epilogue(struct parport *pp)
307{
308 struct mos7715_parport *mos_parport = pp->private_data;
309 mutex_unlock(&mos_parport->serial->disc_mutex);
310 mos_parport->msg_pending = false;
311 complete(&mos_parport->syncmsg_compl);
312}
313
Davidlohr Bueso053af9e2020-11-19 20:53:00 -0800314static void deferred_restore_writes(struct work_struct *work)
315{
316 struct mos7715_parport *mos_parport;
317
318 mos_parport = container_of(work, struct mos7715_parport, work);
319
320 mutex_lock(&mos_parport->serial->disc_mutex);
321
322 /* if device disconnected, game over */
323 if (mos_parport->serial->disconnected)
324 goto done;
325
326 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
327 mos_parport->shadowDCR);
328 write_mos_reg(mos_parport->serial, dummy, MOS7720_ECR,
329 mos_parport->shadowECR);
330done:
331 mutex_unlock(&mos_parport->serial->disc_mutex);
332}
333
Mike Dunnb69578d2010-04-15 17:01:33 -0400334static void parport_mos7715_write_data(struct parport *pp, unsigned char d)
335{
336 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700337
Mike Dunnb69578d2010-04-15 17:01:33 -0400338 if (parport_prologue(pp) < 0)
339 return;
340 mos7715_change_mode(mos_parport, SPP);
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +0530341 write_mos_reg(mos_parport->serial, dummy, MOS7720_DPR, (__u8)d);
Mike Dunnb69578d2010-04-15 17:01:33 -0400342 parport_epilogue(pp);
343}
344
345static unsigned char parport_mos7715_read_data(struct parport *pp)
346{
347 struct mos7715_parport *mos_parport = pp->private_data;
348 unsigned char d;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700349
Mike Dunnb69578d2010-04-15 17:01:33 -0400350 if (parport_prologue(pp) < 0)
351 return 0;
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +0530352 read_mos_reg(mos_parport->serial, dummy, MOS7720_DPR, &d);
Mike Dunnb69578d2010-04-15 17:01:33 -0400353 parport_epilogue(pp);
354 return d;
355}
356
357static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
358{
359 struct mos7715_parport *mos_parport = pp->private_data;
360 __u8 data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700361
Mike Dunnb69578d2010-04-15 17:01:33 -0400362 if (parport_prologue(pp) < 0)
363 return;
364 data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +0530365 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR, data);
Mike Dunnb69578d2010-04-15 17:01:33 -0400366 mos_parport->shadowDCR = data;
367 parport_epilogue(pp);
368}
369
370static unsigned char parport_mos7715_read_control(struct parport *pp)
371{
Colin Ian Kingf0b41982018-01-17 11:54:27 +0000372 struct mos7715_parport *mos_parport;
Mike Dunnb69578d2010-04-15 17:01:33 -0400373 __u8 dcr;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700374
Mike Dunnb69578d2010-04-15 17:01:33 -0400375 spin_lock(&release_lock);
376 mos_parport = pp->private_data;
377 if (unlikely(mos_parport == NULL)) {
378 spin_unlock(&release_lock);
379 return 0;
380 }
381 dcr = mos_parport->shadowDCR & 0x0f;
382 spin_unlock(&release_lock);
383 return dcr;
384}
385
386static unsigned char parport_mos7715_frob_control(struct parport *pp,
387 unsigned char mask,
388 unsigned char val)
389{
390 struct mos7715_parport *mos_parport = pp->private_data;
391 __u8 dcr;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700392
Mike Dunnb69578d2010-04-15 17:01:33 -0400393 mask &= 0x0f;
394 val &= 0x0f;
395 if (parport_prologue(pp) < 0)
396 return 0;
397 mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +0530398 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
399 mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400400 dcr = mos_parport->shadowDCR & 0x0f;
401 parport_epilogue(pp);
402 return dcr;
403}
404
405static unsigned char parport_mos7715_read_status(struct parport *pp)
406{
407 unsigned char status;
Colin Ian Kingf0b41982018-01-17 11:54:27 +0000408 struct mos7715_parport *mos_parport;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700409
Mike Dunnb69578d2010-04-15 17:01:33 -0400410 spin_lock(&release_lock);
411 mos_parport = pp->private_data;
412 if (unlikely(mos_parport == NULL)) { /* release called */
413 spin_unlock(&release_lock);
414 return 0;
415 }
416 status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
417 spin_unlock(&release_lock);
418 return status;
419}
420
421static void parport_mos7715_enable_irq(struct parport *pp)
422{
Mike Dunnb69578d2010-04-15 17:01:33 -0400423}
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700424
Mike Dunnb69578d2010-04-15 17:01:33 -0400425static void parport_mos7715_disable_irq(struct parport *pp)
426{
Mike Dunnb69578d2010-04-15 17:01:33 -0400427}
428
429static void parport_mos7715_data_forward(struct parport *pp)
430{
431 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700432
Mike Dunnb69578d2010-04-15 17:01:33 -0400433 if (parport_prologue(pp) < 0)
434 return;
435 mos7715_change_mode(mos_parport, PS2);
436 mos_parport->shadowDCR &= ~0x20;
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +0530437 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
438 mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400439 parport_epilogue(pp);
440}
441
442static void parport_mos7715_data_reverse(struct parport *pp)
443{
444 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700445
Mike Dunnb69578d2010-04-15 17:01:33 -0400446 if (parport_prologue(pp) < 0)
447 return;
448 mos7715_change_mode(mos_parport, PS2);
449 mos_parport->shadowDCR |= 0x20;
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +0530450 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
451 mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400452 parport_epilogue(pp);
453}
454
455static void parport_mos7715_init_state(struct pardevice *dev,
456 struct parport_state *s)
457{
Mike Dunnb69578d2010-04-15 17:01:33 -0400458 s->u.pc.ctr = DCR_INIT_VAL;
459 s->u.pc.ecr = ECR_INIT_VAL;
460}
461
462/* N.B. Parport core code requires that this function not block */
463static void parport_mos7715_save_state(struct parport *pp,
464 struct parport_state *s)
465{
466 struct mos7715_parport *mos_parport;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700467
Mike Dunnb69578d2010-04-15 17:01:33 -0400468 spin_lock(&release_lock);
469 mos_parport = pp->private_data;
470 if (unlikely(mos_parport == NULL)) { /* release called */
471 spin_unlock(&release_lock);
472 return;
473 }
474 s->u.pc.ctr = mos_parport->shadowDCR;
475 s->u.pc.ecr = mos_parport->shadowECR;
476 spin_unlock(&release_lock);
477}
478
479/* N.B. Parport core code requires that this function not block */
480static void parport_mos7715_restore_state(struct parport *pp,
481 struct parport_state *s)
482{
483 struct mos7715_parport *mos_parport;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700484
Mike Dunnb69578d2010-04-15 17:01:33 -0400485 spin_lock(&release_lock);
486 mos_parport = pp->private_data;
487 if (unlikely(mos_parport == NULL)) { /* release called */
488 spin_unlock(&release_lock);
489 return;
490 }
Johan Hovold975323a2020-11-04 17:47:27 +0100491 mos_parport->shadowDCR = s->u.pc.ctr;
492 mos_parport->shadowECR = s->u.pc.ecr;
Davidlohr Bueso053af9e2020-11-19 20:53:00 -0800493
494 schedule_work(&mos_parport->work);
Mike Dunnb69578d2010-04-15 17:01:33 -0400495 spin_unlock(&release_lock);
496}
497
498static size_t parport_mos7715_write_compat(struct parport *pp,
499 const void *buffer,
500 size_t len, int flags)
501{
502 int retval;
503 struct mos7715_parport *mos_parport = pp->private_data;
504 int actual_len;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700505
Mike Dunnb69578d2010-04-15 17:01:33 -0400506 if (parport_prologue(pp) < 0)
507 return 0;
508 mos7715_change_mode(mos_parport, PPF);
509 retval = usb_bulk_msg(mos_parport->serial->dev,
510 usb_sndbulkpipe(mos_parport->serial->dev, 2),
511 (void *)buffer, len, &actual_len,
512 MOS_WDR_TIMEOUT);
513 parport_epilogue(pp);
514 if (retval) {
515 dev_err(&mos_parport->serial->dev->dev,
Johan Hovoldd9a38a82014-03-12 19:09:42 +0100516 "mos7720: usb_bulk_msg() failed: %d\n", retval);
Mike Dunnb69578d2010-04-15 17:01:33 -0400517 return 0;
518 }
519 return actual_len;
520}
521
522static struct parport_operations parport_mos7715_ops = {
523 .owner = THIS_MODULE,
524 .write_data = parport_mos7715_write_data,
525 .read_data = parport_mos7715_read_data,
526
527 .write_control = parport_mos7715_write_control,
528 .read_control = parport_mos7715_read_control,
529 .frob_control = parport_mos7715_frob_control,
530
531 .read_status = parport_mos7715_read_status,
532
533 .enable_irq = parport_mos7715_enable_irq,
534 .disable_irq = parport_mos7715_disable_irq,
535
536 .data_forward = parport_mos7715_data_forward,
537 .data_reverse = parport_mos7715_data_reverse,
538
539 .init_state = parport_mos7715_init_state,
540 .save_state = parport_mos7715_save_state,
541 .restore_state = parport_mos7715_restore_state,
542
543 .compat_write_data = parport_mos7715_write_compat,
544
545 .nibble_read_data = parport_ieee1284_read_nibble,
546 .byte_read_data = parport_ieee1284_read_byte,
547};
548
549/*
550 * Allocate and initialize parallel port control struct, initialize
551 * the parallel port hardware device, and register with the parport subsystem.
552 */
553static int mos7715_parport_init(struct usb_serial *serial)
554{
555 struct mos7715_parport *mos_parport;
556
557 /* allocate and initialize parallel port control struct */
558 mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
Johan Hovold10c642d2013-12-29 19:22:56 +0100559 if (!mos_parport)
Mike Dunnb69578d2010-04-15 17:01:33 -0400560 return -ENOMEM;
Johan Hovold10c642d2013-12-29 19:22:56 +0100561
Mike Dunnb69578d2010-04-15 17:01:33 -0400562 mos_parport->msg_pending = false;
563 kref_init(&mos_parport->ref_count);
Mike Dunnb69578d2010-04-15 17:01:33 -0400564 usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
565 mos_parport->serial = serial;
Davidlohr Bueso053af9e2020-11-19 20:53:00 -0800566 INIT_WORK(&mos_parport->work, deferred_restore_writes);
Mike Dunnb69578d2010-04-15 17:01:33 -0400567 init_completion(&mos_parport->syncmsg_compl);
568
569 /* cycle parallel port reset bit */
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +0530570 write_mos_reg(mos_parport->serial, dummy, MOS7720_PP_REG, (__u8)0x80);
571 write_mos_reg(mos_parport->serial, dummy, MOS7720_PP_REG, (__u8)0x00);
Mike Dunnb69578d2010-04-15 17:01:33 -0400572
573 /* initialize device registers */
574 mos_parport->shadowDCR = DCR_INIT_VAL;
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +0530575 write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
576 mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400577 mos_parport->shadowECR = ECR_INIT_VAL;
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +0530578 write_mos_reg(mos_parport->serial, dummy, MOS7720_ECR,
579 mos_parport->shadowECR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400580
581 /* register with parport core */
582 mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE,
583 PARPORT_DMA_NONE,
584 &parport_mos7715_ops);
585 if (mos_parport->pp == NULL) {
586 dev_err(&serial->interface->dev,
587 "Could not register parport\n");
588 kref_put(&mos_parport->ref_count, destroy_mos_parport);
589 return -EIO;
590 }
591 mos_parport->pp->private_data = mos_parport;
592 mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP;
593 mos_parport->pp->dev = &serial->interface->dev;
594 parport_announce_port(mos_parport->pp);
595
596 return 0;
597}
598#endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700599
600/*
601 * mos7720_interrupt_callback
602 * this is the callback function for when we have received data on the
603 * interrupt endpoint.
604 */
605static void mos7720_interrupt_callback(struct urb *urb)
606{
607 int result;
608 int length;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700609 int status = urb->status;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700610 struct device *dev = &urb->dev->dev;
Oliver Neukum325b70c2007-03-19 13:58:29 +0100611 __u8 *data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700612 __u8 sp1;
613 __u8 sp2;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700614
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700615 switch (status) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700616 case 0:
617 /* success */
618 break;
619 case -ECONNRESET:
620 case -ENOENT:
621 case -ESHUTDOWN:
622 /* this urb is terminated, clean up */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700623 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700624 return;
625 default:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700626 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700627 goto exit;
628 }
629
630 length = urb->actual_length;
631 data = urb->transfer_buffer;
632
633 /* Moschip get 4 bytes
634 * Byte 1 IIR Port 1 (port.number is 0)
635 * Byte 2 IIR Port 2 (port.number is 1)
636 * Byte 3 --------------
637 * Byte 4 FIFO status for both */
Oliver Neukum325b70c2007-03-19 13:58:29 +0100638
639 /* the above description is inverted
640 * oneukum 2007-03-14 */
641
642 if (unlikely(length != 4)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700643 dev_dbg(dev, "Wrong data !!!\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700644 return;
645 }
646
Oliver Neukum325b70c2007-03-19 13:58:29 +0100647 sp1 = data[3];
648 sp2 = data[2];
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700649
Oliver Neukum325b70c2007-03-19 13:58:29 +0100650 if ((sp1 | sp2) & 0x01) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700651 /* No Interrupt Pending in both the ports */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700652 dev_dbg(dev, "No Interrupt !!!\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700653 } else {
654 switch (sp1 & 0x0f) {
655 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700656 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 -0700657 break;
658 case SERIAL_IIR_CTI:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700659 dev_dbg(dev, "Serial Port 1: Receiver time out\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700660 break;
661 case SERIAL_IIR_MS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700662 /* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700663 break;
664 }
665
666 switch (sp2 & 0x0f) {
667 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700668 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 -0700669 break;
670 case SERIAL_IIR_CTI:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700671 dev_dbg(dev, "Serial Port 2: Receiver time out\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700672 break;
673 case SERIAL_IIR_MS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700674 /* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700675 break;
676 }
677 }
678
679exit:
680 result = usb_submit_urb(urb, GFP_ATOMIC);
681 if (result)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700682 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700683}
684
685/*
Mike Dunnfb088e32010-01-26 12:12:12 -0500686 * mos7715_interrupt_callback
687 * this is the 7715's callback function for when we have received data on
688 * the interrupt endpoint.
689 */
690static void mos7715_interrupt_callback(struct urb *urb)
691{
692 int result;
693 int length;
694 int status = urb->status;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700695 struct device *dev = &urb->dev->dev;
Mike Dunnfb088e32010-01-26 12:12:12 -0500696 __u8 *data;
697 __u8 iir;
698
699 switch (status) {
700 case 0:
701 /* success */
702 break;
703 case -ECONNRESET:
704 case -ENOENT:
705 case -ESHUTDOWN:
Mike Dunnb69578d2010-04-15 17:01:33 -0400706 case -ENODEV:
Mike Dunnfb088e32010-01-26 12:12:12 -0500707 /* this urb is terminated, clean up */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700708 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
Mike Dunnfb088e32010-01-26 12:12:12 -0500709 return;
710 default:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700711 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
Mike Dunnfb088e32010-01-26 12:12:12 -0500712 goto exit;
713 }
714
715 length = urb->actual_length;
716 data = urb->transfer_buffer;
717
718 /* Structure of data from 7715 device:
719 * Byte 1: IIR serial Port
720 * Byte 2: unused
721 * Byte 2: DSR parallel port
722 * Byte 4: FIFO status for both */
723
724 if (unlikely(length != 4)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700725 dev_dbg(dev, "Wrong data !!!\n");
Mike Dunnfb088e32010-01-26 12:12:12 -0500726 return;
727 }
728
729 iir = data[0];
730 if (!(iir & 0x01)) { /* serial port interrupt pending */
731 switch (iir & 0x0f) {
732 case SERIAL_IIR_RLS:
Johan Hovoldd9a38a82014-03-12 19:09:42 +0100733 dev_dbg(dev, "Serial Port: Receiver status error or address bit detected in 9-bit mode\n");
Mike Dunnfb088e32010-01-26 12:12:12 -0500734 break;
735 case SERIAL_IIR_CTI:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700736 dev_dbg(dev, "Serial Port: Receiver time out\n");
Mike Dunnfb088e32010-01-26 12:12:12 -0500737 break;
738 case SERIAL_IIR_MS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700739 /* dev_dbg(dev, "Serial Port: Modem status change\n"); */
Mike Dunnfb088e32010-01-26 12:12:12 -0500740 break;
741 }
742 }
743
Mike Dunnb69578d2010-04-15 17:01:33 -0400744#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
745 { /* update local copy of DSR reg */
746 struct usb_serial_port *port = urb->context;
747 struct mos7715_parport *mos_parport = port->serial->private;
748 if (unlikely(mos_parport == NULL))
749 return;
750 atomic_set(&mos_parport->shadowDSR, data[2]);
751 }
752#endif
753
Mike Dunnfb088e32010-01-26 12:12:12 -0500754exit:
755 result = usb_submit_urb(urb, GFP_ATOMIC);
756 if (result)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700757 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
Mike Dunnfb088e32010-01-26 12:12:12 -0500758}
759
760/*
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700761 * mos7720_bulk_in_callback
762 * this is the callback function for when we have received data on the
763 * bulk in endpoint.
764 */
765static void mos7720_bulk_in_callback(struct urb *urb)
766{
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700767 int retval;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700768 unsigned char *data ;
769 struct usb_serial_port *port;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700770 int status = urb->status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700771
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700772 if (status) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700773 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700774 return;
775 }
776
Mike Dunnb69578d2010-04-15 17:01:33 -0400777 port = urb->context;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700778
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700779 dev_dbg(&port->dev, "Entering...%s\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700780
781 data = urb->transfer_buffer;
782
Jiri Slaby2e124b42013-01-03 15:53:06 +0100783 if (urb->actual_length) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100784 tty_insert_flip_string(&port->port, data, urb->actual_length);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100785 tty_flip_buffer_push(&port->port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700786 }
787
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700788 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700789 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
790 if (retval)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700791 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700792 }
793}
794
795/*
796 * mos7720_bulk_out_data_callback
797 * this is the callback function for when we have finished sending serial
798 * data on the bulk out endpoint.
799 */
800static void mos7720_bulk_out_data_callback(struct urb *urb)
801{
802 struct moschip_port *mos7720_port;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700803 int status = urb->status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700804
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700805 if (status) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700806 dev_dbg(&urb->dev->dev, "nonzero write bulk status received:%d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700807 return;
808 }
809
810 mos7720_port = urb->context;
811 if (!mos7720_port) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700812 dev_dbg(&urb->dev->dev, "NULL mos7720_port pointer\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700813 return ;
814 }
815
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100816 if (mos7720_port->open)
817 tty_port_tty_wakeup(&mos7720_port->port->port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700818}
819
Johan Hovold07814242017-03-16 17:13:30 +0100820static int mos77xx_calc_num_ports(struct usb_serial *serial,
821 struct usb_serial_endpoints *epds)
Mike Dunnfb088e32010-01-26 12:12:12 -0500822{
823 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
Johan Hovoldd7605572017-03-16 17:13:45 +0100824
825 if (product == MOSCHIP_DEVICE_ID_7715) {
826 /*
827 * The 7715 uses the first bulk in/out endpoint pair for the
828 * parallel port, and the second for the serial port. We swap
Jiang Jian9ec7e8d2022-06-22 18:42:17 +0800829 * the endpoint descriptors here so that the first and
Johan Hovoldd7605572017-03-16 17:13:45 +0100830 * only registered port structure uses the serial-port
831 * endpoints.
832 */
833 swap(epds->bulk_in[0], epds->bulk_in[1]);
834 swap(epds->bulk_out[0], epds->bulk_out[1]);
835
Mike Dunnfb088e32010-01-26 12:12:12 -0500836 return 1;
Johan Hovoldd7605572017-03-16 17:13:45 +0100837 }
Mike Dunnfb088e32010-01-26 12:12:12 -0500838
839 return 2;
840}
841
Alan Coxa509a7e2009-09-19 13:13:26 -0700842static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700843{
844 struct usb_serial *serial;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700845 struct urb *urb;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700846 struct moschip_port *mos7720_port;
847 int response;
848 int port_number;
Mike Dunn63b91762010-04-15 17:02:09 -0400849 __u8 data;
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100850 int allocated_urbs = 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700851 int j;
852
853 serial = port->serial;
854
855 mos7720_port = usb_get_serial_port_data(port);
856 if (mos7720_port == NULL)
857 return -ENODEV;
858
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700859 usb_clear_halt(serial->dev, port->write_urb->pipe);
860 usb_clear_halt(serial->dev, port->read_urb->pipe);
861
862 /* Initialising the write urb pool */
863 for (j = 0; j < NUM_URBS; ++j) {
Alan Cox4da1a172008-07-22 11:16:21 +0100864 urb = usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700865 mos7720_port->write_urb_pool[j] = urb;
Johan Hovold10c642d2013-12-29 19:22:56 +0100866 if (!urb)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700867 continue;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700868
869 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
870 GFP_KERNEL);
871 if (!urb->transfer_buffer) {
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100872 usb_free_urb(mos7720_port->write_urb_pool[j]);
873 mos7720_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700874 continue;
875 }
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100876 allocated_urbs++;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700877 }
878
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100879 if (!allocated_urbs)
880 return -ENOMEM;
881
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700882 /* Initialize MCS7720 -- Write Init values to corresponding Registers
883 *
884 * Register Index
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +0530885 * 0 : MOS7720_THR/MOS7720_RHR
886 * 1 : MOS7720_IER
887 * 2 : MOS7720_FCR
888 * 3 : MOS7720_LCR
889 * 4 : MOS7720_MCR
890 * 5 : MOS7720_LSR
891 * 6 : MOS7720_MSR
892 * 7 : MOS7720_SPR
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700893 *
894 * 0x08 : SP1/2 Control Reg
895 */
Greg Kroah-Hartman11438322013-06-06 10:32:00 -0700896 port_number = port->port_number;
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +0530897 read_mos_reg(serial, port_number, MOS7720_LSR, &data);
Mike Dunn63b91762010-04-15 17:02:09 -0400898
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700899 dev_dbg(&port->dev, "SS::%p LSR:%x\n", mos7720_port, data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700900
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +0530901 write_mos_reg(serial, dummy, MOS7720_SP1_REG, 0x02);
902 write_mos_reg(serial, dummy, MOS7720_SP2_REG, 0x02);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700903
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +0530904 write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
905 write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700906
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +0530907 write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
Mike Dunn63b91762010-04-15 17:02:09 -0400908 mos7720_port->shadowLCR = 0x03;
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +0530909 write_mos_reg(serial, port_number, MOS7720_LCR,
910 mos7720_port->shadowLCR);
Mike Dunn63b91762010-04-15 17:02:09 -0400911 mos7720_port->shadowMCR = 0x0b;
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +0530912 write_mos_reg(serial, port_number, MOS7720_MCR,
913 mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700914
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +0530915 write_mos_reg(serial, port_number, MOS7720_SP_CONTROL_REG, 0x00);
916 read_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, &data);
Greg Kroah-Hartman11438322013-06-06 10:32:00 -0700917 data = data | (port->port_number + 1);
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +0530918 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, data);
Mike Dunn63b91762010-04-15 17:02:09 -0400919 mos7720_port->shadowLCR = 0x83;
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +0530920 write_mos_reg(serial, port_number, MOS7720_LCR,
921 mos7720_port->shadowLCR);
922 write_mos_reg(serial, port_number, MOS7720_THR, 0x0c);
923 write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
Mike Dunn63b91762010-04-15 17:02:09 -0400924 mos7720_port->shadowLCR = 0x03;
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +0530925 write_mos_reg(serial, port_number, MOS7720_LCR,
926 mos7720_port->shadowLCR);
927 write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700928
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700929 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
930 if (response)
Alan Cox4da1a172008-07-22 11:16:21 +0100931 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
932 __func__, response);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700933
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700934 /* initialize our port settings */
935 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
936
937 /* send a open port command */
938 mos7720_port->open = 1;
939
940 return 0;
941}
942
943/*
944 * mos7720_chars_in_buffer
945 * this function is called by the tty driver when it wants to know how many
946 * bytes of data we currently have outstanding in the port (data that has
947 * been written, but hasn't made it out the port yet)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700948 */
Jiri Slaby155591d2021-05-05 11:19:20 +0200949static unsigned int mos7720_chars_in_buffer(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700950{
Alan Cox95da3102008-07-22 11:09:07 +0100951 struct usb_serial_port *port = tty->driver_data;
Johan Hovold683c5cf2021-05-19 11:20:04 +0200952 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700953 int i;
Jiri Slaby155591d2021-05-05 11:19:20 +0200954 unsigned int chars = 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700955
956 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +0100957 if (mos7720_port->write_urb_pool[i] &&
958 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700959 chars += URB_TRANSFER_BUFFER_SIZE;
960 }
Jiri Slaby155591d2021-05-05 11:19:20 +0200961 dev_dbg(&port->dev, "%s - returns %u\n", __func__, chars);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700962 return chars;
963}
964
Alan Cox335f8512009-06-11 12:26:29 +0100965static void mos7720_close(struct usb_serial_port *port)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700966{
967 struct usb_serial *serial;
968 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700969 int j;
970
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700971 serial = port->serial;
972
973 mos7720_port = usb_get_serial_port_data(port);
974 if (mos7720_port == NULL)
975 return;
976
977 for (j = 0; j < NUM_URBS; ++j)
978 usb_kill_urb(mos7720_port->write_urb_pool[j]);
979
980 /* Freeing Write URBs */
981 for (j = 0; j < NUM_URBS; ++j) {
982 if (mos7720_port->write_urb_pool[j]) {
983 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
984 usb_free_urb(mos7720_port->write_urb_pool[j]);
985 }
986 }
987
988 /* While closing port, shutdown all bulk read, write *
Oliver Neukuma1cd7e92008-01-16 17:18:52 +0100989 * and interrupt read if they exists, otherwise nop */
Oliver Neukuma1cd7e92008-01-16 17:18:52 +0100990 usb_kill_urb(port->write_urb);
Oliver Neukuma1cd7e92008-01-16 17:18:52 +0100991 usb_kill_urb(port->read_urb);
992
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +0530993 write_mos_reg(serial, port->port_number, MOS7720_MCR, 0x00);
994 write_mos_reg(serial, port->port_number, MOS7720_IER, 0x00);
Johan Hovoldcf41aa92013-03-21 12:37:41 +0100995
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700996 mos7720_port->open = 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700997}
998
Johan Hovold6ff58ae2023-06-04 14:35:03 +0200999static int mos7720_break(struct tty_struct *tty, int break_state)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001000{
Alan Cox95da3102008-07-22 11:09:07 +01001001 struct usb_serial_port *port = tty->driver_data;
Alan Cox4da1a172008-07-22 11:16:21 +01001002 unsigned char data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001003 struct usb_serial *serial;
1004 struct moschip_port *mos7720_port;
1005
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001006 serial = port->serial;
1007
1008 mos7720_port = usb_get_serial_port_data(port);
1009 if (mos7720_port == NULL)
Johan Hovold6ff58ae2023-06-04 14:35:03 +02001010 return -ENODEV;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001011
1012 if (break_state == -1)
1013 data = mos7720_port->shadowLCR | UART_LCR_SBC;
1014 else
1015 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
1016
1017 mos7720_port->shadowLCR = data;
Johan Hovold6ff58ae2023-06-04 14:35:03 +02001018
1019 return write_mos_reg(serial, port->port_number, MOS7720_LCR,
1020 mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001021}
1022
1023/*
1024 * mos7720_write_room
1025 * this function is called by the tty driver when it wants to know how many
1026 * bytes of data we can accept for a specific port.
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001027 */
Jiri Slaby94cc7aea2021-05-05 11:19:16 +02001028static unsigned int mos7720_write_room(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001029{
Alan Cox95da3102008-07-22 11:09:07 +01001030 struct usb_serial_port *port = tty->driver_data;
Johan Hovold683c5cf2021-05-19 11:20:04 +02001031 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
Jiri Slaby94cc7aea2021-05-05 11:19:16 +02001032 unsigned int room = 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001033 int i;
1034
Alan Coxa5b6f602008-04-08 17:16:06 +01001035 /* FIXME: Locking */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001036 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001037 if (mos7720_port->write_urb_pool[i] &&
1038 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001039 room += URB_TRANSFER_BUFFER_SIZE;
1040 }
1041
Jiri Slaby94cc7aea2021-05-05 11:19:16 +02001042 dev_dbg(&port->dev, "%s - returns %u\n", __func__, room);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001043 return room;
1044}
1045
Alan Cox95da3102008-07-22 11:09:07 +01001046static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
1047 const unsigned char *data, int count)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001048{
1049 int status;
1050 int i;
1051 int bytes_sent = 0;
1052 int transfer_size;
1053
1054 struct moschip_port *mos7720_port;
1055 struct usb_serial *serial;
1056 struct urb *urb;
1057 const unsigned char *current_position = data;
1058
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001059 serial = port->serial;
1060
1061 mos7720_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001062 if (mos7720_port == NULL)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001063 return -ENODEV;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001064
1065 /* try to find a free urb in the list */
1066 urb = NULL;
1067
1068 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001069 if (mos7720_port->write_urb_pool[i] &&
1070 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001071 urb = mos7720_port->write_urb_pool[i];
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001072 dev_dbg(&port->dev, "URB:%d\n", i);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001073 break;
1074 }
1075 }
1076
1077 if (urb == NULL) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001078 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001079 goto exit;
1080 }
1081
1082 if (urb->transfer_buffer == NULL) {
1083 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
Alexey Khoroshilov5a5a1d62016-08-12 01:05:08 +03001084 GFP_ATOMIC);
Dan Carpenterfea73722021-01-28 12:35:23 +03001085 if (!urb->transfer_buffer) {
1086 bytes_sent = -ENOMEM;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001087 goto exit;
Dan Carpenterfea73722021-01-28 12:35:23 +03001088 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001089 }
Alan Cox4da1a172008-07-22 11:16:21 +01001090 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001091
1092 memcpy(urb->transfer_buffer, current_position, transfer_size);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001093 usb_serial_debug_data(&port->dev, __func__, transfer_size,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001094 urb->transfer_buffer);
1095
1096 /* fill urb with data and submit */
1097 usb_fill_bulk_urb(urb, serial->dev,
1098 usb_sndbulkpipe(serial->dev,
Alan Cox4da1a172008-07-22 11:16:21 +01001099 port->bulk_out_endpointAddress),
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001100 urb->transfer_buffer, transfer_size,
1101 mos7720_bulk_out_data_callback, mos7720_port);
1102
1103 /* send it down the pipe */
Alan Cox4da1a172008-07-22 11:16:21 +01001104 status = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001105 if (status) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001106 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001107 "with status = %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001108 bytes_sent = status;
1109 goto exit;
1110 }
1111 bytes_sent = transfer_size;
1112
1113exit:
1114 return bytes_sent;
1115}
1116
Alan Cox95da3102008-07-22 11:09:07 +01001117static void mos7720_throttle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001118{
Alan Cox95da3102008-07-22 11:09:07 +01001119 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001120 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001121 int status;
1122
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001123 mos7720_port = usb_get_serial_port_data(port);
1124
1125 if (mos7720_port == NULL)
1126 return;
1127
1128 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001129 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001130 return;
1131 }
1132
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001133 /* if we are implementing XON/XOFF, send the stop character */
1134 if (I_IXOFF(tty)) {
1135 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001136 status = mos7720_write(tty, port, &stop_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001137 if (status <= 0)
1138 return;
1139 }
1140
1141 /* if we are implementing RTS/CTS, toggle that line */
Peter Hurley9db276f2016-01-10 20:36:15 -08001142 if (C_CRTSCTS(tty)) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001143 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +05301144 write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001145 mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001146 }
1147}
1148
Alan Cox95da3102008-07-22 11:09:07 +01001149static void mos7720_unthrottle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001150{
Alan Cox95da3102008-07-22 11:09:07 +01001151 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001152 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +01001153 int status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001154
1155 if (mos7720_port == NULL)
1156 return;
1157
1158 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001159 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001160 return;
1161 }
1162
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001163 /* if we are implementing XON/XOFF, send the start character */
1164 if (I_IXOFF(tty)) {
1165 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001166 status = mos7720_write(tty, port, &start_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001167 if (status <= 0)
1168 return;
1169 }
1170
1171 /* if we are implementing RTS/CTS, toggle that line */
Peter Hurley9db276f2016-01-10 20:36:15 -08001172 if (C_CRTSCTS(tty)) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001173 mos7720_port->shadowMCR |= UART_MCR_RTS;
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +05301174 write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001175 mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001176 }
1177}
1178
Mike Dunnb69578d2010-04-15 17:01:33 -04001179/* FIXME: this function does not work */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001180static int set_higher_rates(struct moschip_port *mos7720_port,
1181 unsigned int baud)
1182{
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001183 struct usb_serial_port *port;
1184 struct usb_serial *serial;
1185 int port_number;
Mike Dunn63b91762010-04-15 17:02:09 -04001186 enum mos_regs sp_reg;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001187 if (mos7720_port == NULL)
1188 return -EINVAL;
1189
1190 port = mos7720_port->port;
1191 serial = port->serial;
1192
Alan Cox4da1a172008-07-22 11:16:21 +01001193 /***********************************************
1194 * Init Sequence for higher rates
1195 ***********************************************/
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001196 dev_dbg(&port->dev, "Sending Setting Commands ..........\n");
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001197 port_number = port->port_number;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001198
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +05301199 write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
1200 write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
1201 write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
Mike Dunn63b91762010-04-15 17:02:09 -04001202 mos7720_port->shadowMCR = 0x0b;
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +05301203 write_mos_reg(serial, port_number, MOS7720_MCR,
1204 mos7720_port->shadowMCR);
1205 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, 0x00);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001206
Alan Cox4da1a172008-07-22 11:16:21 +01001207 /***********************************************
1208 * Set for higher rates *
1209 ***********************************************/
Mike Dunnb69578d2010-04-15 17:01:33 -04001210 /* writing baud rate verbatum into uart clock field clearly not right */
Mike Dunn63b91762010-04-15 17:02:09 -04001211 if (port_number == 0)
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +05301212 sp_reg = MOS7720_SP1_REG;
Mike Dunn63b91762010-04-15 17:02:09 -04001213 else
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +05301214 sp_reg = MOS7720_SP2_REG;
Mike Dunn63b91762010-04-15 17:02:09 -04001215 write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +05301216 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, 0x03);
Mike Dunn63b91762010-04-15 17:02:09 -04001217 mos7720_port->shadowMCR = 0x2b;
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +05301218 write_mos_reg(serial, port_number, MOS7720_MCR,
1219 mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001220
Alan Cox4da1a172008-07-22 11:16:21 +01001221 /***********************************************
1222 * Set DLL/DLM
1223 ***********************************************/
Mike Dunn63b91762010-04-15 17:02:09 -04001224 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +05301225 write_mos_reg(serial, port_number, MOS7720_LCR,
1226 mos7720_port->shadowLCR);
1227 write_mos_reg(serial, port_number, MOS7720_DLL, 0x01);
1228 write_mos_reg(serial, port_number, MOS7720_DLM, 0x00);
Mike Dunn63b91762010-04-15 17:02:09 -04001229 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +05301230 write_mos_reg(serial, port_number, MOS7720_LCR,
1231 mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001232
1233 return 0;
1234}
1235
1236/* baud rate information */
Alan Cox4da1a172008-07-22 11:16:21 +01001237struct divisor_table_entry {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001238 __u32 baudrate;
1239 __u16 divisor;
1240};
1241
1242/* Define table of divisors for moschip 7720 hardware *
1243 * These assume a 3.6864MHz crystal, the standard /16, and *
1244 * MCR.7 = 0. */
Johan Hovold4f37fa52017-04-18 14:42:28 +02001245static const struct divisor_table_entry divisor_table[] = {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001246 { 50, 2304},
1247 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
1248 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
1249 { 150, 768},
1250 { 300, 384},
1251 { 600, 192},
1252 { 1200, 96},
1253 { 1800, 64},
1254 { 2400, 48},
1255 { 4800, 24},
1256 { 7200, 16},
1257 { 9600, 12},
1258 { 19200, 6},
1259 { 38400, 3},
1260 { 57600, 2},
1261 { 115200, 1},
1262};
1263
1264/*****************************************************************************
1265 * calc_baud_rate_divisor
1266 * this function calculates the proper baud rate divisor for the specified
1267 * baud rate.
1268 *****************************************************************************/
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001269static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, int *divisor)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001270{
1271 int i;
1272 __u16 custom;
1273 __u16 round1;
1274 __u16 round;
1275
1276
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001277 dev_dbg(&port->dev, "%s - %d\n", __func__, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001278
1279 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
1280 if (divisor_table[i].baudrate == baudrate) {
1281 *divisor = divisor_table[i].divisor;
1282 return 0;
1283 }
1284 }
1285
Alan Cox4da1a172008-07-22 11:16:21 +01001286 /* After trying for all the standard baud rates *
1287 * Try calculating the divisor for this baud rate */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001288 if (baudrate > 75 && baudrate < 230400) {
1289 /* get the divisor */
1290 custom = (__u16)(230400L / baudrate);
1291
1292 /* Check for round off */
1293 round1 = (__u16)(2304000L / baudrate);
1294 round = (__u16)(round1 - (custom * 10));
1295 if (round > 4)
1296 custom++;
1297 *divisor = custom;
1298
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001299 dev_dbg(&port->dev, "Baud %d = %d\n", baudrate, custom);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001300 return 0;
1301 }
1302
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001303 dev_dbg(&port->dev, "Baud calculation Failed...\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001304 return -EINVAL;
1305}
1306
1307/*
1308 * send_cmd_write_baud_rate
1309 * this function sends the proper command to change the baud rate of the
1310 * specified port.
1311 */
1312static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1313 int baudrate)
1314{
1315 struct usb_serial_port *port;
1316 struct usb_serial *serial;
1317 int divisor;
1318 int status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001319 unsigned char number;
1320
1321 if (mos7720_port == NULL)
1322 return -1;
1323
1324 port = mos7720_port->port;
1325 serial = port->serial;
1326
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001327 number = port->port_number;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001328 dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001329
Alan Cox4da1a172008-07-22 11:16:21 +01001330 /* Calculate the Divisor */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001331 status = calc_baud_rate_divisor(port, baudrate, &divisor);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001332 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001333 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001334 return status;
1335 }
1336
Alan Cox4da1a172008-07-22 11:16:21 +01001337 /* Enable access to divisor latch */
Mike Dunn63b91762010-04-15 17:02:09 -04001338 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +05301339 write_mos_reg(serial, number, MOS7720_LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001340
1341 /* Write the divisor */
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +05301342 write_mos_reg(serial, number, MOS7720_DLL, (__u8)(divisor & 0xff));
1343 write_mos_reg(serial, number, MOS7720_DLM,
1344 (__u8)((divisor & 0xff00) >> 8));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001345
Alan Cox4da1a172008-07-22 11:16:21 +01001346 /* Disable access to divisor latch */
Mike Dunn63b91762010-04-15 17:02:09 -04001347 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +05301348 write_mos_reg(serial, number, MOS7720_LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001349
1350 return status;
1351}
1352
1353/*
1354 * change_port_settings
1355 * This routine is called to set the UART on the device to match
1356 * the specified new settings.
1357 */
Alan Cox95da3102008-07-22 11:09:07 +01001358static void change_port_settings(struct tty_struct *tty,
1359 struct moschip_port *mos7720_port,
Ilpo Järvinenf6d47fe2022-08-16 14:57:38 +03001360 const struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001361{
1362 struct usb_serial_port *port;
1363 struct usb_serial *serial;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001364 int baud;
1365 unsigned cflag;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001366 __u8 lData;
1367 __u8 lParity;
1368 __u8 lStop;
1369 int status;
1370 int port_number;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001371
1372 if (mos7720_port == NULL)
1373 return ;
1374
1375 port = mos7720_port->port;
1376 serial = port->serial;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001377 port_number = port->port_number;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001378
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001379 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001380 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001381 return;
1382 }
1383
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001384 lStop = 0x00; /* 1 stop bit */
1385 lParity = 0x00; /* No parity */
1386
Alan Coxadc8d742012-07-14 15:31:47 +01001387 cflag = tty->termios.c_cflag;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001388
Jiri Slaby5e1440b2022-02-24 10:55:56 +01001389 lData = UART_LCR_WLEN(tty_get_char_size(cflag));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001390
1391 /* Change the Parity bit */
1392 if (cflag & PARENB) {
1393 if (cflag & PARODD) {
1394 lParity = UART_LCR_PARITY;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001395 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001396 } else {
1397 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001398 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001399 }
1400
1401 } else {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001402 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001403 }
1404
1405 if (cflag & CMSPAR)
1406 lParity = lParity | 0x20;
1407
1408 /* Change the Stop bit */
1409 if (cflag & CSTOPB) {
1410 lStop = UART_LCR_STOP;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001411 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001412 } else {
1413 lStop = 0x00;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001414 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001415 }
1416
1417#define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1418#define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1419#define LCR_PAR_MASK 0x38 /* Mask for parity field */
1420
1421 /* Update the LCR with the correct value */
Alan Cox4da1a172008-07-22 11:16:21 +01001422 mos7720_port->shadowLCR &=
Mike Dunn63b91762010-04-15 17:02:09 -04001423 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001424 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1425
1426
1427 /* Disable Interrupts */
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +05301428 write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
1429 write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
1430 write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001431
1432 /* Send the updated LCR value to the mos7720 */
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +05301433 write_mos_reg(serial, port_number, MOS7720_LCR,
1434 mos7720_port->shadowLCR);
Mike Dunn63b91762010-04-15 17:02:09 -04001435 mos7720_port->shadowMCR = 0x0b;
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +05301436 write_mos_reg(serial, port_number, MOS7720_MCR,
1437 mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001438
1439 /* set up the MCR register and send it to the mos7720 */
1440 mos7720_port->shadowMCR = UART_MCR_OUT2;
1441 if (cflag & CBAUD)
1442 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1443
1444 if (cflag & CRTSCTS) {
1445 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
Alan Cox4da1a172008-07-22 11:16:21 +01001446 /* To set hardware flow control to the specified *
1447 * serial port, in SP1/2_CONTROL_REG */
Johan Hovolda26f0092013-06-04 18:50:31 +02001448 if (port_number)
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +05301449 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG,
1450 0x01);
Mike Dunn63b91762010-04-15 17:02:09 -04001451 else
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +05301452 write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG,
1453 0x02);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001454
Mike Dunn63b91762010-04-15 17:02:09 -04001455 } else
1456 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1457
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +05301458 write_mos_reg(serial, port_number, MOS7720_MCR,
1459 mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001460
1461 /* Determine divisor based on baud rate */
1462 baud = tty_get_baud_rate(tty);
1463 if (!baud) {
1464 /* pick a default, any default... */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001465 dev_dbg(&port->dev, "Picked default baud...\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001466 baud = 9600;
1467 }
1468
1469 if (baud >= 230400) {
1470 set_higher_rates(mos7720_port, baud);
1471 /* Enable Interrupts */
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +05301472 write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001473 return;
1474 }
1475
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001476 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001477 status = send_cmd_write_baud_rate(mos7720_port, baud);
Alan Cox65d063ab2008-01-03 17:01:18 +00001478 /* FIXME: needs to write actual resulting baud back not just
1479 blindly do so */
1480 if (cflag & CBAUD)
1481 tty_encode_baud_rate(tty, baud, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001482 /* Enable Interrupts */
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +05301483 write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001484
1485 if (port->read_urb->status != -EINPROGRESS) {
Johan Hovold1dbd11b2014-10-29 09:07:33 +01001486 status = usb_submit_urb(port->read_urb, GFP_KERNEL);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001487 if (status)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001488 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001489 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001490}
1491
1492/*
1493 * mos7720_set_termios
1494 * this function is called by the tty driver when it wants to change the
1495 * termios structure.
1496 */
Alan Cox95da3102008-07-22 11:09:07 +01001497static void mos7720_set_termios(struct tty_struct *tty,
Ilpo Järvinenf6d47fe2022-08-16 14:57:38 +03001498 struct usb_serial_port *port,
1499 const struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001500{
1501 int status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001502 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001503
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001504 mos7720_port = usb_get_serial_port_data(port);
1505
1506 if (mos7720_port == NULL)
1507 return;
1508
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001509 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001510 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001511 return;
1512 }
1513
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001514 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001515 change_port_settings(tty, mos7720_port, old_termios);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001516
Alan Cox4da1a172008-07-22 11:16:21 +01001517 if (port->read_urb->status != -EINPROGRESS) {
Johan Hovold1dbd11b2014-10-29 09:07:33 +01001518 status = usb_submit_urb(port->read_urb, GFP_KERNEL);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001519 if (status)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001520 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001521 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001522}
1523
1524/*
1525 * get_lsr_info - get line status register info
1526 *
1527 * Purpose: Let user call ioctl() to get info when the UART physically
1528 * is emptied. On bus types like RS485, the transmitter must
1529 * release the bus after transmitting. This must be done when
1530 * the transmit shift register is empty, not be done when the
1531 * transmit holding register is empty. This functionality
1532 * allows an RS485 driver to be written in user space.
1533 */
Alan Cox4da1a172008-07-22 11:16:21 +01001534static int get_lsr_info(struct tty_struct *tty,
1535 struct moschip_port *mos7720_port, unsigned int __user *value)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001536{
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001537 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001538 unsigned int result = 0;
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001539 unsigned char data = 0;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001540 int port_number = port->port_number;
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001541 int count;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001542
Alan Cox95da3102008-07-22 11:09:07 +01001543 count = mos7720_chars_in_buffer(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001544 if (count == 0) {
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +05301545 read_mos_reg(port->serial, port_number, MOS7720_LSR, &data);
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001546 if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1547 == (UART_LSR_TEMT | UART_LSR_THRE)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001548 dev_dbg(&port->dev, "%s -- Empty\n", __func__);
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001549 result = TIOCSER_TEMT;
1550 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001551 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001552 if (copy_to_user(value, &result, sizeof(int)))
1553 return -EFAULT;
1554 return 0;
1555}
1556
Alan Cox60b33c12011-02-14 16:26:14 +00001557static int mos7720_tiocmget(struct tty_struct *tty)
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001558{
1559 struct usb_serial_port *port = tty->driver_data;
1560 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1561 unsigned int result = 0;
1562 unsigned int mcr ;
1563 unsigned int msr ;
1564
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001565 mcr = mos7720_port->shadowMCR;
1566 msr = mos7720_port->shadowMSR;
1567
1568 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1569 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1570 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1571 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
1572 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1573 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
1574
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001575 return result;
1576}
1577
Alan Cox20b9d172011-02-14 16:26:50 +00001578static int mos7720_tiocmset(struct tty_struct *tty,
Mike Dunn63b91762010-04-15 17:02:09 -04001579 unsigned int set, unsigned int clear)
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001580{
1581 struct usb_serial_port *port = tty->driver_data;
1582 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1583 unsigned int mcr ;
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001584
1585 mcr = mos7720_port->shadowMCR;
1586
1587 if (set & TIOCM_RTS)
1588 mcr |= UART_MCR_RTS;
1589 if (set & TIOCM_DTR)
1590 mcr |= UART_MCR_DTR;
1591 if (set & TIOCM_LOOP)
1592 mcr |= UART_MCR_LOOP;
1593
1594 if (clear & TIOCM_RTS)
1595 mcr &= ~UART_MCR_RTS;
1596 if (clear & TIOCM_DTR)
1597 mcr &= ~UART_MCR_DTR;
1598 if (clear & TIOCM_LOOP)
1599 mcr &= ~UART_MCR_LOOP;
1600
1601 mos7720_port->shadowMCR = mcr;
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +05301602 write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
Greg Kroah-Hartman11438322013-06-06 10:32:00 -07001603 mos7720_port->shadowMCR);
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001604
1605 return 0;
1606}
1607
Alan Cox00a0d0d2011-02-14 16:27:06 +00001608static int mos7720_ioctl(struct tty_struct *tty,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001609 unsigned int cmd, unsigned long arg)
1610{
Alan Cox95da3102008-07-22 11:09:07 +01001611 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001612 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001613
1614 mos7720_port = usb_get_serial_port_data(port);
1615 if (mos7720_port == NULL)
1616 return -ENODEV;
1617
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001618 switch (cmd) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001619 case TIOCSERGETLSR:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001620 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
Alan Cox4da1a172008-07-22 11:16:21 +01001621 return get_lsr_info(tty, mos7720_port,
1622 (unsigned int __user *)arg);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001623 }
1624
1625 return -ENOIOCTLCMD;
1626}
1627
1628static int mos7720_startup(struct usb_serial *serial)
1629{
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001630 struct usb_device *dev;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001631 char data;
Huzaifa Sidhpurwala91f58ae2011-02-21 12:58:45 +05301632 u16 product;
Mike Dunnb69578d2010-04-15 17:01:33 -04001633 int ret_val;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001634
Huzaifa Sidhpurwala91f58ae2011-02-21 12:58:45 +05301635 product = le16_to_cpu(serial->dev->descriptor.idProduct);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001636 dev = serial->dev;
1637
Mike Dunnb69578d2010-04-15 17:01:33 -04001638 if (product == MOSCHIP_DEVICE_ID_7715) {
Johan Hovoldd7605572017-03-16 17:13:45 +01001639 struct urb *urb = serial->port[0]->interrupt_in_urb;
1640
Johan Hovold6a1eaf12017-03-16 17:13:46 +01001641 urb->complete = mos7715_interrupt_callback;
Johan Hovoldd7605572017-03-16 17:13:45 +01001642
1643#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
Mike Dunnb69578d2010-04-15 17:01:33 -04001644 ret_val = mos7715_parport_init(serial);
Johan Hovold75dd2112017-01-03 16:39:52 +01001645 if (ret_val < 0)
Mike Dunnb69578d2010-04-15 17:01:33 -04001646 return ret_val;
Mike Dunnb69578d2010-04-15 17:01:33 -04001647#endif
Johan Hovoldd7605572017-03-16 17:13:45 +01001648 }
Johan Hovold75dd2112017-01-03 16:39:52 +01001649 /* start the interrupt urb */
1650 ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
1651 if (ret_val) {
1652 dev_err(&dev->dev, "failed to submit interrupt urb: %d\n",
1653 ret_val);
1654 }
1655
Alan Cox4da1a172008-07-22 11:16:21 +01001656 /* LSR For Port 1 */
Sudip Mukherjeeee5729e2015-06-23 18:59:40 +05301657 read_mos_reg(serial, 0, MOS7720_LSR, &data);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001658 dev_dbg(&dev->dev, "LSR:%x\n", data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001659
1660 return 0;
1661}
1662
Alan Sternf9c99bb2009-06-02 11:53:55 -04001663static void mos7720_release(struct usb_serial *serial)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001664{
Johan Hovold91a1ff42017-01-03 16:39:51 +01001665 usb_kill_urb(serial->port[0]->interrupt_in_urb);
1666
Mike Dunnb69578d2010-04-15 17:01:33 -04001667#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1668 /* close the parallel port */
1669
1670 if (le16_to_cpu(serial->dev->descriptor.idProduct)
1671 == MOSCHIP_DEVICE_ID_7715) {
Mike Dunnb69578d2010-04-15 17:01:33 -04001672 struct mos7715_parport *mos_parport =
1673 usb_get_serial_data(serial);
1674
1675 /* prevent NULL ptr dereference in port callbacks */
1676 spin_lock(&release_lock);
1677 mos_parport->pp->private_data = NULL;
1678 spin_unlock(&release_lock);
1679
1680 /* wait for synchronous usb calls to return */
1681 if (mos_parport->msg_pending)
1682 wait_for_completion_timeout(&mos_parport->syncmsg_compl,
Johan Hovold849513a2013-05-27 14:44:43 +02001683 msecs_to_jiffies(MOS_WDR_TIMEOUT));
Davidlohr Bueso053af9e2020-11-19 20:53:00 -08001684 /*
1685 * If delayed work is currently scheduled, wait for it to
1686 * complete. This also implies barriers that ensure the
1687 * below serial clearing is not hoisted above the ->work.
1688 */
1689 cancel_work_sync(&mos_parport->work);
Mike Dunnb69578d2010-04-15 17:01:33 -04001690
1691 parport_remove_port(mos_parport->pp);
1692 usb_set_serial_data(serial, NULL);
1693 mos_parport->serial = NULL;
1694
Sudip Mukherjeedcb21ad2016-05-30 19:16:33 +05301695 parport_del_port(mos_parport->pp);
Mike Dunnb69578d2010-04-15 17:01:33 -04001696
1697 kref_put(&mos_parport->ref_count, destroy_mos_parport);
1698 }
1699#endif
Johan Hovold4230af52012-10-25 10:29:05 +02001700}
1701
1702static int mos7720_port_probe(struct usb_serial_port *port)
1703{
1704 struct moschip_port *mos7720_port;
1705
1706 mos7720_port = kzalloc(sizeof(*mos7720_port), GFP_KERNEL);
1707 if (!mos7720_port)
1708 return -ENOMEM;
1709
Johan Hovold4230af52012-10-25 10:29:05 +02001710 mos7720_port->port = port;
1711
1712 usb_set_serial_port_data(port, mos7720_port);
1713
1714 return 0;
1715}
1716
Uwe Kleine-Königc5d14482021-02-08 15:31:49 +01001717static void mos7720_port_remove(struct usb_serial_port *port)
Johan Hovold4230af52012-10-25 10:29:05 +02001718{
1719 struct moschip_port *mos7720_port;
1720
1721 mos7720_port = usb_get_serial_port_data(port);
1722 kfree(mos7720_port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001723}
1724
1725static struct usb_serial_driver moschip7720_2port_driver = {
1726 .driver = {
1727 .owner = THIS_MODULE,
1728 .name = "moschip7720",
1729 },
1730 .description = "Moschip 2 port adapter",
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07001731 .id_table = id_table,
Johan Hovold206ff832017-03-02 12:51:27 +01001732 .num_bulk_in = 2,
1733 .num_bulk_out = 2,
Johan Hovold6a1eaf12017-03-16 17:13:46 +01001734 .num_interrupt_in = 1,
Mike Dunnfb088e32010-01-26 12:12:12 -05001735 .calc_num_ports = mos77xx_calc_num_ports,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001736 .open = mos7720_open,
1737 .close = mos7720_close,
1738 .throttle = mos7720_throttle,
1739 .unthrottle = mos7720_unthrottle,
1740 .attach = mos7720_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04001741 .release = mos7720_release,
Johan Hovold4230af52012-10-25 10:29:05 +02001742 .port_probe = mos7720_port_probe,
1743 .port_remove = mos7720_port_remove,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001744 .ioctl = mos7720_ioctl,
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001745 .tiocmget = mos7720_tiocmget,
1746 .tiocmset = mos7720_tiocmset,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001747 .set_termios = mos7720_set_termios,
1748 .write = mos7720_write,
1749 .write_room = mos7720_write_room,
1750 .chars_in_buffer = mos7720_chars_in_buffer,
1751 .break_ctl = mos7720_break,
1752 .read_bulk_callback = mos7720_bulk_in_callback,
Johan Hovoldfde1faf2017-01-03 16:39:53 +01001753 .read_int_callback = mos7720_interrupt_callback,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001754};
1755
Alan Stern4d2a7af2012-02-23 14:57:09 -05001756static struct usb_serial_driver * const serial_drivers[] = {
1757 &moschip7720_2port_driver, NULL
1758};
1759
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07001760module_usb_serial_driver(serial_drivers, id_table);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001761
Alan Cox4da1a172008-07-22 11:16:21 +01001762MODULE_AUTHOR(DRIVER_AUTHOR);
1763MODULE_DESCRIPTION(DRIVER_DESC);
Johan Hovold627cfa82017-11-03 18:12:08 +01001764MODULE_LICENSE("GPL v2");