blob: 0f16bf6ea71c1707fa6ec5d64bd9242eeac9cdfa [file] [log] [blame]
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001/*
2 * mos7720.c
3 * Controls the Moschip 7720 usb to dual port serial convertor
4 *
5 * Copyright 2006 Moschip Semiconductor Tech. Ltd.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, version 2 of the License.
10 *
11 * Developed by:
Greg Kroah-Hartman50d2dc72007-06-25 01:08:01 -070012 * Vijaya Kumar <vijaykumar.gn@gmail.com>
13 * Ajay Kumar <naanuajay@yahoo.com>
14 * Gurudeva <ngurudeva@yahoo.com>
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070015 *
16 * Cleaned up from the original by:
17 * Greg Kroah-Hartman <gregkh@suse.de>
18 *
19 * Originally based on drivers/usb/serial/io_edgeport.c which is:
20 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
21 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
22 */
23#include <linux/kernel.h>
24#include <linux/errno.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/tty.h>
28#include <linux/tty_driver.h>
29#include <linux/tty_flip.h>
30#include <linux/module.h>
31#include <linux/spinlock.h>
32#include <linux/serial.h>
33#include <linux/serial_reg.h>
34#include <linux/usb.h>
35#include <linux/usb/serial.h>
Alan Cox4da1a172008-07-22 11:16:21 +010036#include <linux/uaccess.h>
Mike Dunnb69578d2010-04-15 17:01:33 -040037#include <linux/parport.h>
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070038
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070039#define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
40#define DRIVER_DESC "Moschip USB Serial Driver"
41
42/* default urb timeout */
Johan Hovold849513a2013-05-27 14:44:43 +020043#define MOS_WDR_TIMEOUT 5000
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070044
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070045#define MOS_MAX_PORT 0x02
46#define MOS_WRITE 0x0E
47#define MOS_READ 0x0D
48
49/* Interrupt Rotinue Defines */
50#define SERIAL_IIR_RLS 0x06
51#define SERIAL_IIR_RDA 0x04
52#define SERIAL_IIR_CTI 0x0c
53#define SERIAL_IIR_THR 0x02
54#define SERIAL_IIR_MS 0x00
55
56#define NUM_URBS 16 /* URB Count */
57#define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
58
Mike Dunnb69578d2010-04-15 17:01:33 -040059/* This structure holds all of the local serial port information */
Alan Cox4da1a172008-07-22 11:16:21 +010060struct moschip_port {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070061 __u8 shadowLCR; /* last LCR value received */
62 __u8 shadowMCR; /* last MCR value received */
63 __u8 shadowMSR; /* last MSR value received */
64 char open;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070065 struct usb_serial_port *port; /* loop back to the owner */
66 struct urb *write_urb_pool[NUM_URBS];
67};
68
Mike Dunnfb088e32010-01-26 12:12:12 -050069static struct usb_serial_driver moschip7720_2port_driver;
70
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070071#define USB_VENDOR_ID_MOSCHIP 0x9710
72#define MOSCHIP_DEVICE_ID_7720 0x7720
73#define MOSCHIP_DEVICE_ID_7715 0x7715
74
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -070075static const struct usb_device_id id_table[] = {
Alan Cox4da1a172008-07-22 11:16:21 +010076 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
Mike Dunnfb088e32010-01-26 12:12:12 -050077 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) },
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070078 { } /* terminating entry */
79};
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -070080MODULE_DEVICE_TABLE(usb, id_table);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070081
Mike Dunnb69578d2010-04-15 17:01:33 -040082#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
83
84/* initial values for parport regs */
85#define DCR_INIT_VAL 0x0c /* SLCTIN, nINIT */
86#define ECR_INIT_VAL 0x00 /* SPP mode */
87
88struct urbtracker {
89 struct mos7715_parport *mos_parport;
90 struct list_head urblist_entry;
91 struct kref ref_count;
92 struct urb *urb;
Johan Hovoldf56852a2013-08-13 13:27:34 +020093 struct usb_ctrlrequest *setup;
Mike Dunnb69578d2010-04-15 17:01:33 -040094};
95
96enum mos7715_pp_modes {
97 SPP = 0<<5,
98 PS2 = 1<<5, /* moschip calls this 'NIBBLE' mode */
99 PPF = 2<<5, /* moschip calls this 'CB-FIFO mode */
100};
101
102struct mos7715_parport {
103 struct parport *pp; /* back to containing struct */
104 struct kref ref_count; /* to instance of this struct */
105 struct list_head deferred_urbs; /* list deferred async urbs */
106 struct list_head active_urbs; /* list async urbs in flight */
107 spinlock_t listlock; /* protects list access */
108 bool msg_pending; /* usb sync call pending */
109 struct completion syncmsg_compl; /* usb sync call completed */
110 struct tasklet_struct urb_tasklet; /* for sending deferred urbs */
111 struct usb_serial *serial; /* back to containing struct */
112 __u8 shadowECR; /* parallel port regs... */
113 __u8 shadowDCR;
114 atomic_t shadowDSR; /* updated in int-in callback */
115};
116
117/* lock guards against dereferencing NULL ptr in parport ops callbacks */
118static DEFINE_SPINLOCK(release_lock);
119
Mike Dunn63b91762010-04-15 17:02:09 -0400120#endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
121
122static const unsigned int dummy; /* for clarity in register access fns */
123
Mike Dunnb69578d2010-04-15 17:01:33 -0400124enum mos_regs {
125 THR, /* serial port regs */
126 RHR,
127 IER,
128 FCR,
129 ISR,
130 LCR,
131 MCR,
132 LSR,
133 MSR,
134 SPR,
135 DLL,
136 DLM,
137 DPR, /* parallel port regs */
138 DSR,
139 DCR,
140 ECR,
141 SP1_REG, /* device control regs */
142 SP2_REG, /* serial port 2 (7720 only) */
143 PP_REG,
144 SP_CONTROL_REG,
145};
146
147/*
148 * Return the correct value for the Windex field of the setup packet
149 * for a control endpoint message. See the 7715 datasheet.
150 */
151static inline __u16 get_reg_index(enum mos_regs reg)
152{
153 static const __u16 mos7715_index_lookup_table[] = {
154 0x00, /* THR */
155 0x00, /* RHR */
156 0x01, /* IER */
157 0x02, /* FCR */
158 0x02, /* ISR */
159 0x03, /* LCR */
160 0x04, /* MCR */
161 0x05, /* LSR */
162 0x06, /* MSR */
163 0x07, /* SPR */
164 0x00, /* DLL */
165 0x01, /* DLM */
166 0x00, /* DPR */
167 0x01, /* DSR */
168 0x02, /* DCR */
169 0x0a, /* ECR */
170 0x01, /* SP1_REG */
171 0x02, /* SP2_REG (7720 only) */
172 0x04, /* PP_REG (7715 only) */
173 0x08, /* SP_CONTROL_REG */
174 };
175 return mos7715_index_lookup_table[reg];
176}
177
178/*
179 * Return the correct value for the upper byte of the Wvalue field of
180 * the setup packet for a control endpoint message.
181 */
Mike Dunn63b91762010-04-15 17:02:09 -0400182static inline __u16 get_reg_value(enum mos_regs reg,
183 unsigned int serial_portnum)
Mike Dunnb69578d2010-04-15 17:01:33 -0400184{
185 if (reg >= SP1_REG) /* control reg */
186 return 0x0000;
Mike Dunn63b91762010-04-15 17:02:09 -0400187
188 else if (reg >= DPR) /* parallel port reg (7715 only) */
Mike Dunnb69578d2010-04-15 17:01:33 -0400189 return 0x0100;
Mike Dunn63b91762010-04-15 17:02:09 -0400190
191 else /* serial port reg */
192 return (serial_portnum + 2) << 8;
Mike Dunnb69578d2010-04-15 17:01:33 -0400193}
194
195/*
196 * Write data byte to the specified device register. The data is embedded in
Mike Dunn63b91762010-04-15 17:02:09 -0400197 * the value field of the setup packet. serial_portnum is ignored for registers
198 * not specific to a particular serial port.
Mike Dunnb69578d2010-04-15 17:01:33 -0400199 */
Mike Dunn63b91762010-04-15 17:02:09 -0400200static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
201 enum mos_regs reg, __u8 data)
Mike Dunnb69578d2010-04-15 17:01:33 -0400202{
Mike Dunnb69578d2010-04-15 17:01:33 -0400203 struct usb_device *usbdev = serial->dev;
204 unsigned int pipe = usb_sndctrlpipe(usbdev, 0);
205 __u8 request = (__u8)0x0e;
206 __u8 requesttype = (__u8)0x40;
Mike Dunnb69578d2010-04-15 17:01:33 -0400207 __u16 index = get_reg_index(reg);
Mike Dunn63b91762010-04-15 17:02:09 -0400208 __u16 value = get_reg_value(reg, serial_portnum) + data;
209 int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
210 index, NULL, 0, MOS_WDR_TIMEOUT);
Mike Dunnb69578d2010-04-15 17:01:33 -0400211 if (status < 0)
212 dev_err(&usbdev->dev,
213 "mos7720: usb_control_msg() failed: %d", status);
214 return status;
215}
216
217/*
218 * Read data byte from the specified device register. The data returned by the
Mike Dunn63b91762010-04-15 17:02:09 -0400219 * device is embedded in the value field of the setup packet. serial_portnum is
220 * ignored for registers that are not specific to a particular serial port.
Mike Dunnb69578d2010-04-15 17:01:33 -0400221 */
Mike Dunn63b91762010-04-15 17:02:09 -0400222static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
223 enum mos_regs reg, __u8 *data)
Mike Dunnb69578d2010-04-15 17:01:33 -0400224{
Mike Dunn63b91762010-04-15 17:02:09 -0400225 struct usb_device *usbdev = serial->dev;
Mike Dunnb69578d2010-04-15 17:01:33 -0400226 unsigned int pipe = usb_rcvctrlpipe(usbdev, 0);
227 __u8 request = (__u8)0x0d;
228 __u8 requesttype = (__u8)0xc0;
Mike Dunnb69578d2010-04-15 17:01:33 -0400229 __u16 index = get_reg_index(reg);
Mike Dunn63b91762010-04-15 17:02:09 -0400230 __u16 value = get_reg_value(reg, serial_portnum);
Johan Hovold72ea18a2013-05-27 14:44:39 +0200231 u8 *buf;
232 int status;
233
234 buf = kmalloc(1, GFP_KERNEL);
235 if (!buf)
236 return -ENOMEM;
237
238 status = usb_control_msg(usbdev, pipe, request, requesttype, value,
239 index, buf, 1, MOS_WDR_TIMEOUT);
240 if (status == 1)
241 *data = *buf;
242 else if (status < 0)
Mike Dunnb69578d2010-04-15 17:01:33 -0400243 dev_err(&usbdev->dev,
244 "mos7720: usb_control_msg() failed: %d", status);
Johan Hovold72ea18a2013-05-27 14:44:39 +0200245 kfree(buf);
246
Mike Dunnb69578d2010-04-15 17:01:33 -0400247 return status;
248}
249
Mike Dunn63b91762010-04-15 17:02:09 -0400250#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
251
Mike Dunnb69578d2010-04-15 17:01:33 -0400252static inline int mos7715_change_mode(struct mos7715_parport *mos_parport,
253 enum mos7715_pp_modes mode)
254{
255 mos_parport->shadowECR = mode;
Mike Dunn63b91762010-04-15 17:02:09 -0400256 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400257 return 0;
258}
259
260static void destroy_mos_parport(struct kref *kref)
261{
262 struct mos7715_parport *mos_parport =
263 container_of(kref, struct mos7715_parport, ref_count);
264
Mike Dunnb69578d2010-04-15 17:01:33 -0400265 kfree(mos_parport);
266}
267
268static void destroy_urbtracker(struct kref *kref)
269{
270 struct urbtracker *urbtrack =
271 container_of(kref, struct urbtracker, ref_count);
272 struct mos7715_parport *mos_parport = urbtrack->mos_parport;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700273
Mike Dunnb69578d2010-04-15 17:01:33 -0400274 usb_free_urb(urbtrack->urb);
Johan Hovoldf56852a2013-08-13 13:27:34 +0200275 kfree(urbtrack->setup);
Mike Dunnb69578d2010-04-15 17:01:33 -0400276 kfree(urbtrack);
277 kref_put(&mos_parport->ref_count, destroy_mos_parport);
278}
279
280/*
281 * This runs as a tasklet when sending an urb in a non-blocking parallel
282 * port callback had to be deferred because the disconnect mutex could not be
283 * obtained at the time.
284 */
285static void send_deferred_urbs(unsigned long _mos_parport)
286{
287 int ret_val;
288 unsigned long flags;
289 struct mos7715_parport *mos_parport = (void *)_mos_parport;
Wei Yongjun67990472012-08-21 11:28:45 +0800290 struct urbtracker *urbtrack, *tmp;
Mike Dunnb69578d2010-04-15 17:01:33 -0400291 struct list_head *cursor, *next;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700292 struct device *dev;
Mike Dunnb69578d2010-04-15 17:01:33 -0400293
Mike Dunnb69578d2010-04-15 17:01:33 -0400294 /* if release function ran, game over */
295 if (unlikely(mos_parport->serial == NULL))
296 return;
297
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700298 dev = &mos_parport->serial->dev->dev;
299
Mike Dunnb69578d2010-04-15 17:01:33 -0400300 /* try again to get the mutex */
301 if (!mutex_trylock(&mos_parport->serial->disc_mutex)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700302 dev_dbg(dev, "%s: rescheduling tasklet\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400303 tasklet_schedule(&mos_parport->urb_tasklet);
304 return;
305 }
306
307 /* if device disconnected, game over */
308 if (unlikely(mos_parport->serial->disconnected)) {
309 mutex_unlock(&mos_parport->serial->disc_mutex);
310 return;
311 }
312
313 spin_lock_irqsave(&mos_parport->listlock, flags);
314 if (list_empty(&mos_parport->deferred_urbs)) {
315 spin_unlock_irqrestore(&mos_parport->listlock, flags);
316 mutex_unlock(&mos_parport->serial->disc_mutex);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700317 dev_dbg(dev, "%s: deferred_urbs list empty\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400318 return;
319 }
320
321 /* move contents of deferred_urbs list to active_urbs list and submit */
322 list_for_each_safe(cursor, next, &mos_parport->deferred_urbs)
323 list_move_tail(cursor, &mos_parport->active_urbs);
Wei Yongjun67990472012-08-21 11:28:45 +0800324 list_for_each_entry_safe(urbtrack, tmp, &mos_parport->active_urbs,
Mike Dunnb69578d2010-04-15 17:01:33 -0400325 urblist_entry) {
326 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700327 dev_dbg(dev, "%s: urb submitted\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400328 if (ret_val) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700329 dev_err(dev, "usb_submit_urb() failed: %d\n", ret_val);
Mike Dunnb69578d2010-04-15 17:01:33 -0400330 list_del(&urbtrack->urblist_entry);
331 kref_put(&urbtrack->ref_count, destroy_urbtracker);
332 }
333 }
334 spin_unlock_irqrestore(&mos_parport->listlock, flags);
335 mutex_unlock(&mos_parport->serial->disc_mutex);
336}
337
338/* callback for parallel port control urbs submitted asynchronously */
339static void async_complete(struct urb *urb)
340{
341 struct urbtracker *urbtrack = urb->context;
342 int status = urb->status;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700343
Mike Dunnb69578d2010-04-15 17:01:33 -0400344 if (unlikely(status))
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700345 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
Mike Dunnb69578d2010-04-15 17:01:33 -0400346
347 /* remove the urbtracker from the active_urbs list */
348 spin_lock(&urbtrack->mos_parport->listlock);
349 list_del(&urbtrack->urblist_entry);
350 spin_unlock(&urbtrack->mos_parport->listlock);
351 kref_put(&urbtrack->ref_count, destroy_urbtracker);
352}
353
354static int write_parport_reg_nonblock(struct mos7715_parport *mos_parport,
355 enum mos_regs reg, __u8 data)
356{
357 struct urbtracker *urbtrack;
358 int ret_val;
359 unsigned long flags;
Mike Dunnb69578d2010-04-15 17:01:33 -0400360 struct usb_serial *serial = mos_parport->serial;
361 struct usb_device *usbdev = serial->dev;
Mike Dunnb69578d2010-04-15 17:01:33 -0400362
363 /* create and initialize the control urb and containing urbtracker */
364 urbtrack = kmalloc(sizeof(struct urbtracker), GFP_ATOMIC);
365 if (urbtrack == NULL) {
366 dev_err(&usbdev->dev, "out of memory");
367 return -ENOMEM;
368 }
369 kref_get(&mos_parport->ref_count);
370 urbtrack->mos_parport = mos_parport;
371 urbtrack->urb = usb_alloc_urb(0, GFP_ATOMIC);
372 if (urbtrack->urb == NULL) {
373 dev_err(&usbdev->dev, "out of urbs");
374 kfree(urbtrack);
375 return -ENOMEM;
376 }
Dan Carpenter37657622013-08-16 10:16:59 +0300377 urbtrack->setup = kmalloc(sizeof(*urbtrack->setup), GFP_ATOMIC);
Johan Hovoldf56852a2013-08-13 13:27:34 +0200378 if (!urbtrack->setup) {
379 usb_free_urb(urbtrack->urb);
380 kfree(urbtrack);
381 return -ENOMEM;
382 }
383 urbtrack->setup->bRequestType = (__u8)0x40;
384 urbtrack->setup->bRequest = (__u8)0x0e;
Johan Hovold7965eef2013-08-19 13:05:45 +0200385 urbtrack->setup->wValue = cpu_to_le16(get_reg_value(reg, dummy));
386 urbtrack->setup->wIndex = cpu_to_le16(get_reg_index(reg));
Johan Hovoldf56852a2013-08-13 13:27:34 +0200387 urbtrack->setup->wLength = 0;
Mike Dunnb69578d2010-04-15 17:01:33 -0400388 usb_fill_control_urb(urbtrack->urb, usbdev,
389 usb_sndctrlpipe(usbdev, 0),
Johan Hovoldf56852a2013-08-13 13:27:34 +0200390 (unsigned char *)urbtrack->setup,
Mike Dunnb69578d2010-04-15 17:01:33 -0400391 NULL, 0, async_complete, urbtrack);
392 kref_init(&urbtrack->ref_count);
393 INIT_LIST_HEAD(&urbtrack->urblist_entry);
394
395 /*
396 * get the disconnect mutex, or add tracker to the deferred_urbs list
397 * and schedule a tasklet to try again later
398 */
399 if (!mutex_trylock(&serial->disc_mutex)) {
400 spin_lock_irqsave(&mos_parport->listlock, flags);
401 list_add_tail(&urbtrack->urblist_entry,
402 &mos_parport->deferred_urbs);
403 spin_unlock_irqrestore(&mos_parport->listlock, flags);
404 tasklet_schedule(&mos_parport->urb_tasklet);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700405 dev_dbg(&usbdev->dev, "tasklet scheduled");
Mike Dunnb69578d2010-04-15 17:01:33 -0400406 return 0;
407 }
408
409 /* bail if device disconnected */
410 if (serial->disconnected) {
411 kref_put(&urbtrack->ref_count, destroy_urbtracker);
412 mutex_unlock(&serial->disc_mutex);
413 return -ENODEV;
414 }
415
416 /* add the tracker to the active_urbs list and submit */
417 spin_lock_irqsave(&mos_parport->listlock, flags);
418 list_add_tail(&urbtrack->urblist_entry, &mos_parport->active_urbs);
419 spin_unlock_irqrestore(&mos_parport->listlock, flags);
420 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
421 mutex_unlock(&serial->disc_mutex);
422 if (ret_val) {
423 dev_err(&usbdev->dev,
424 "%s: submit_urb() failed: %d", __func__, ret_val);
425 spin_lock_irqsave(&mos_parport->listlock, flags);
426 list_del(&urbtrack->urblist_entry);
427 spin_unlock_irqrestore(&mos_parport->listlock, flags);
428 kref_put(&urbtrack->ref_count, destroy_urbtracker);
429 return ret_val;
430 }
431 return 0;
432}
433
434/*
435 * This is the the common top part of all parallel port callback operations that
436 * send synchronous messages to the device. This implements convoluted locking
437 * that avoids two scenarios: (1) a port operation is called after usbserial
438 * has called our release function, at which point struct mos7715_parport has
439 * been destroyed, and (2) the device has been disconnected, but usbserial has
440 * not called the release function yet because someone has a serial port open.
441 * The shared release_lock prevents the first, and the mutex and disconnected
442 * flag maintained by usbserial covers the second. We also use the msg_pending
443 * flag to ensure that all synchronous usb messgage calls have completed before
444 * our release function can return.
445 */
446static int parport_prologue(struct parport *pp)
447{
448 struct mos7715_parport *mos_parport;
449
450 spin_lock(&release_lock);
451 mos_parport = pp->private_data;
452 if (unlikely(mos_parport == NULL)) {
453 /* release fn called, port struct destroyed */
454 spin_unlock(&release_lock);
455 return -1;
456 }
457 mos_parport->msg_pending = true; /* synch usb call pending */
458 INIT_COMPLETION(mos_parport->syncmsg_compl);
459 spin_unlock(&release_lock);
460
461 mutex_lock(&mos_parport->serial->disc_mutex);
462 if (mos_parport->serial->disconnected) {
463 /* device disconnected */
464 mutex_unlock(&mos_parport->serial->disc_mutex);
465 mos_parport->msg_pending = false;
466 complete(&mos_parport->syncmsg_compl);
467 return -1;
468 }
469
470 return 0;
471}
472
473/*
474 * This is the the common bottom part of all parallel port functions that send
475 * synchronous messages to the device.
476 */
477static inline void parport_epilogue(struct parport *pp)
478{
479 struct mos7715_parport *mos_parport = pp->private_data;
480 mutex_unlock(&mos_parport->serial->disc_mutex);
481 mos_parport->msg_pending = false;
482 complete(&mos_parport->syncmsg_compl);
483}
484
485static void parport_mos7715_write_data(struct parport *pp, unsigned char d)
486{
487 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700488
Mike Dunnb69578d2010-04-15 17:01:33 -0400489 if (parport_prologue(pp) < 0)
490 return;
491 mos7715_change_mode(mos_parport, SPP);
Mike Dunn63b91762010-04-15 17:02:09 -0400492 write_mos_reg(mos_parport->serial, dummy, DPR, (__u8)d);
Mike Dunnb69578d2010-04-15 17:01:33 -0400493 parport_epilogue(pp);
494}
495
496static unsigned char parport_mos7715_read_data(struct parport *pp)
497{
498 struct mos7715_parport *mos_parport = pp->private_data;
499 unsigned char d;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700500
Mike Dunnb69578d2010-04-15 17:01:33 -0400501 if (parport_prologue(pp) < 0)
502 return 0;
Mike Dunn63b91762010-04-15 17:02:09 -0400503 read_mos_reg(mos_parport->serial, dummy, DPR, &d);
Mike Dunnb69578d2010-04-15 17:01:33 -0400504 parport_epilogue(pp);
505 return d;
506}
507
508static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
509{
510 struct mos7715_parport *mos_parport = pp->private_data;
511 __u8 data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700512
Mike Dunnb69578d2010-04-15 17:01:33 -0400513 if (parport_prologue(pp) < 0)
514 return;
515 data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
Mike Dunn63b91762010-04-15 17:02:09 -0400516 write_mos_reg(mos_parport->serial, dummy, DCR, data);
Mike Dunnb69578d2010-04-15 17:01:33 -0400517 mos_parport->shadowDCR = data;
518 parport_epilogue(pp);
519}
520
521static unsigned char parport_mos7715_read_control(struct parport *pp)
522{
523 struct mos7715_parport *mos_parport = pp->private_data;
524 __u8 dcr;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700525
Mike Dunnb69578d2010-04-15 17:01:33 -0400526 spin_lock(&release_lock);
527 mos_parport = pp->private_data;
528 if (unlikely(mos_parport == NULL)) {
529 spin_unlock(&release_lock);
530 return 0;
531 }
532 dcr = mos_parport->shadowDCR & 0x0f;
533 spin_unlock(&release_lock);
534 return dcr;
535}
536
537static unsigned char parport_mos7715_frob_control(struct parport *pp,
538 unsigned char mask,
539 unsigned char val)
540{
541 struct mos7715_parport *mos_parport = pp->private_data;
542 __u8 dcr;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700543
Mike Dunnb69578d2010-04-15 17:01:33 -0400544 mask &= 0x0f;
545 val &= 0x0f;
546 if (parport_prologue(pp) < 0)
547 return 0;
548 mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
Mike Dunn63b91762010-04-15 17:02:09 -0400549 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400550 dcr = mos_parport->shadowDCR & 0x0f;
551 parport_epilogue(pp);
552 return dcr;
553}
554
555static unsigned char parport_mos7715_read_status(struct parport *pp)
556{
557 unsigned char status;
558 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700559
Mike Dunnb69578d2010-04-15 17:01:33 -0400560 spin_lock(&release_lock);
561 mos_parport = pp->private_data;
562 if (unlikely(mos_parport == NULL)) { /* release called */
563 spin_unlock(&release_lock);
564 return 0;
565 }
566 status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
567 spin_unlock(&release_lock);
568 return status;
569}
570
571static void parport_mos7715_enable_irq(struct parport *pp)
572{
Mike Dunnb69578d2010-04-15 17:01:33 -0400573}
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700574
Mike Dunnb69578d2010-04-15 17:01:33 -0400575static void parport_mos7715_disable_irq(struct parport *pp)
576{
Mike Dunnb69578d2010-04-15 17:01:33 -0400577}
578
579static void parport_mos7715_data_forward(struct parport *pp)
580{
581 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700582
Mike Dunnb69578d2010-04-15 17:01:33 -0400583 if (parport_prologue(pp) < 0)
584 return;
585 mos7715_change_mode(mos_parport, PS2);
586 mos_parport->shadowDCR &= ~0x20;
Mike Dunn63b91762010-04-15 17:02:09 -0400587 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400588 parport_epilogue(pp);
589}
590
591static void parport_mos7715_data_reverse(struct parport *pp)
592{
593 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700594
Mike Dunnb69578d2010-04-15 17:01:33 -0400595 if (parport_prologue(pp) < 0)
596 return;
597 mos7715_change_mode(mos_parport, PS2);
598 mos_parport->shadowDCR |= 0x20;
Mike Dunn63b91762010-04-15 17:02:09 -0400599 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400600 parport_epilogue(pp);
601}
602
603static void parport_mos7715_init_state(struct pardevice *dev,
604 struct parport_state *s)
605{
Mike Dunnb69578d2010-04-15 17:01:33 -0400606 s->u.pc.ctr = DCR_INIT_VAL;
607 s->u.pc.ecr = ECR_INIT_VAL;
608}
609
610/* N.B. Parport core code requires that this function not block */
611static void parport_mos7715_save_state(struct parport *pp,
612 struct parport_state *s)
613{
614 struct mos7715_parport *mos_parport;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700615
Mike Dunnb69578d2010-04-15 17:01:33 -0400616 spin_lock(&release_lock);
617 mos_parport = pp->private_data;
618 if (unlikely(mos_parport == NULL)) { /* release called */
619 spin_unlock(&release_lock);
620 return;
621 }
622 s->u.pc.ctr = mos_parport->shadowDCR;
623 s->u.pc.ecr = mos_parport->shadowECR;
624 spin_unlock(&release_lock);
625}
626
627/* N.B. Parport core code requires that this function not block */
628static void parport_mos7715_restore_state(struct parport *pp,
629 struct parport_state *s)
630{
631 struct mos7715_parport *mos_parport;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700632
Mike Dunnb69578d2010-04-15 17:01:33 -0400633 spin_lock(&release_lock);
634 mos_parport = pp->private_data;
635 if (unlikely(mos_parport == NULL)) { /* release called */
636 spin_unlock(&release_lock);
637 return;
638 }
639 write_parport_reg_nonblock(mos_parport, DCR, mos_parport->shadowDCR);
640 write_parport_reg_nonblock(mos_parport, ECR, mos_parport->shadowECR);
641 spin_unlock(&release_lock);
642}
643
644static size_t parport_mos7715_write_compat(struct parport *pp,
645 const void *buffer,
646 size_t len, int flags)
647{
648 int retval;
649 struct mos7715_parport *mos_parport = pp->private_data;
650 int actual_len;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700651
Mike Dunnb69578d2010-04-15 17:01:33 -0400652 if (parport_prologue(pp) < 0)
653 return 0;
654 mos7715_change_mode(mos_parport, PPF);
655 retval = usb_bulk_msg(mos_parport->serial->dev,
656 usb_sndbulkpipe(mos_parport->serial->dev, 2),
657 (void *)buffer, len, &actual_len,
658 MOS_WDR_TIMEOUT);
659 parport_epilogue(pp);
660 if (retval) {
661 dev_err(&mos_parport->serial->dev->dev,
662 "mos7720: usb_bulk_msg() failed: %d", retval);
663 return 0;
664 }
665 return actual_len;
666}
667
668static struct parport_operations parport_mos7715_ops = {
669 .owner = THIS_MODULE,
670 .write_data = parport_mos7715_write_data,
671 .read_data = parport_mos7715_read_data,
672
673 .write_control = parport_mos7715_write_control,
674 .read_control = parport_mos7715_read_control,
675 .frob_control = parport_mos7715_frob_control,
676
677 .read_status = parport_mos7715_read_status,
678
679 .enable_irq = parport_mos7715_enable_irq,
680 .disable_irq = parport_mos7715_disable_irq,
681
682 .data_forward = parport_mos7715_data_forward,
683 .data_reverse = parport_mos7715_data_reverse,
684
685 .init_state = parport_mos7715_init_state,
686 .save_state = parport_mos7715_save_state,
687 .restore_state = parport_mos7715_restore_state,
688
689 .compat_write_data = parport_mos7715_write_compat,
690
691 .nibble_read_data = parport_ieee1284_read_nibble,
692 .byte_read_data = parport_ieee1284_read_byte,
693};
694
695/*
696 * Allocate and initialize parallel port control struct, initialize
697 * the parallel port hardware device, and register with the parport subsystem.
698 */
699static int mos7715_parport_init(struct usb_serial *serial)
700{
701 struct mos7715_parport *mos_parport;
702
703 /* allocate and initialize parallel port control struct */
704 mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
705 if (mos_parport == NULL) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700706 dev_dbg(&serial->dev->dev, "%s: kzalloc failed\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400707 return -ENOMEM;
708 }
709 mos_parport->msg_pending = false;
710 kref_init(&mos_parport->ref_count);
711 spin_lock_init(&mos_parport->listlock);
712 INIT_LIST_HEAD(&mos_parport->active_urbs);
713 INIT_LIST_HEAD(&mos_parport->deferred_urbs);
714 usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
715 mos_parport->serial = serial;
716 tasklet_init(&mos_parport->urb_tasklet, send_deferred_urbs,
717 (unsigned long) mos_parport);
718 init_completion(&mos_parport->syncmsg_compl);
719
720 /* cycle parallel port reset bit */
Mike Dunn63b91762010-04-15 17:02:09 -0400721 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x80);
722 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x00);
Mike Dunnb69578d2010-04-15 17:01:33 -0400723
724 /* initialize device registers */
725 mos_parport->shadowDCR = DCR_INIT_VAL;
Mike Dunn63b91762010-04-15 17:02:09 -0400726 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400727 mos_parport->shadowECR = ECR_INIT_VAL;
Mike Dunn63b91762010-04-15 17:02:09 -0400728 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400729
730 /* register with parport core */
731 mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE,
732 PARPORT_DMA_NONE,
733 &parport_mos7715_ops);
734 if (mos_parport->pp == NULL) {
735 dev_err(&serial->interface->dev,
736 "Could not register parport\n");
737 kref_put(&mos_parport->ref_count, destroy_mos_parport);
738 return -EIO;
739 }
740 mos_parport->pp->private_data = mos_parport;
741 mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP;
742 mos_parport->pp->dev = &serial->interface->dev;
743 parport_announce_port(mos_parport->pp);
744
745 return 0;
746}
747#endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700748
749/*
750 * mos7720_interrupt_callback
751 * this is the callback function for when we have received data on the
752 * interrupt endpoint.
753 */
754static void mos7720_interrupt_callback(struct urb *urb)
755{
756 int result;
757 int length;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700758 int status = urb->status;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700759 struct device *dev = &urb->dev->dev;
Oliver Neukum325b70c2007-03-19 13:58:29 +0100760 __u8 *data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700761 __u8 sp1;
762 __u8 sp2;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700763
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700764 switch (status) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700765 case 0:
766 /* success */
767 break;
768 case -ECONNRESET:
769 case -ENOENT:
770 case -ESHUTDOWN:
771 /* this urb is terminated, clean up */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700772 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700773 return;
774 default:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700775 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700776 goto exit;
777 }
778
779 length = urb->actual_length;
780 data = urb->transfer_buffer;
781
782 /* Moschip get 4 bytes
783 * Byte 1 IIR Port 1 (port.number is 0)
784 * Byte 2 IIR Port 2 (port.number is 1)
785 * Byte 3 --------------
786 * Byte 4 FIFO status for both */
Oliver Neukum325b70c2007-03-19 13:58:29 +0100787
788 /* the above description is inverted
789 * oneukum 2007-03-14 */
790
791 if (unlikely(length != 4)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700792 dev_dbg(dev, "Wrong data !!!\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700793 return;
794 }
795
Oliver Neukum325b70c2007-03-19 13:58:29 +0100796 sp1 = data[3];
797 sp2 = data[2];
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700798
Oliver Neukum325b70c2007-03-19 13:58:29 +0100799 if ((sp1 | sp2) & 0x01) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700800 /* No Interrupt Pending in both the ports */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700801 dev_dbg(dev, "No Interrupt !!!\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700802 } else {
803 switch (sp1 & 0x0f) {
804 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700805 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 -0700806 break;
807 case SERIAL_IIR_CTI:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700808 dev_dbg(dev, "Serial Port 1: Receiver time out\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700809 break;
810 case SERIAL_IIR_MS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700811 /* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700812 break;
813 }
814
815 switch (sp2 & 0x0f) {
816 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700817 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 -0700818 break;
819 case SERIAL_IIR_CTI:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700820 dev_dbg(dev, "Serial Port 2: Receiver time out\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700821 break;
822 case SERIAL_IIR_MS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700823 /* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700824 break;
825 }
826 }
827
828exit:
829 result = usb_submit_urb(urb, GFP_ATOMIC);
830 if (result)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700831 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700832}
833
834/*
Mike Dunnfb088e32010-01-26 12:12:12 -0500835 * mos7715_interrupt_callback
836 * this is the 7715's callback function for when we have received data on
837 * the interrupt endpoint.
838 */
839static void mos7715_interrupt_callback(struct urb *urb)
840{
841 int result;
842 int length;
843 int status = urb->status;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700844 struct device *dev = &urb->dev->dev;
Mike Dunnfb088e32010-01-26 12:12:12 -0500845 __u8 *data;
846 __u8 iir;
847
848 switch (status) {
849 case 0:
850 /* success */
851 break;
852 case -ECONNRESET:
853 case -ENOENT:
854 case -ESHUTDOWN:
Mike Dunnb69578d2010-04-15 17:01:33 -0400855 case -ENODEV:
Mike Dunnfb088e32010-01-26 12:12:12 -0500856 /* this urb is terminated, clean up */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700857 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
Mike Dunnfb088e32010-01-26 12:12:12 -0500858 return;
859 default:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700860 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
Mike Dunnfb088e32010-01-26 12:12:12 -0500861 goto exit;
862 }
863
864 length = urb->actual_length;
865 data = urb->transfer_buffer;
866
867 /* Structure of data from 7715 device:
868 * Byte 1: IIR serial Port
869 * Byte 2: unused
870 * Byte 2: DSR parallel port
871 * Byte 4: FIFO status for both */
872
873 if (unlikely(length != 4)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700874 dev_dbg(dev, "Wrong data !!!\n");
Mike Dunnfb088e32010-01-26 12:12:12 -0500875 return;
876 }
877
878 iir = data[0];
879 if (!(iir & 0x01)) { /* serial port interrupt pending */
880 switch (iir & 0x0f) {
881 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700882 dev_dbg(dev, "Serial Port: Receiver status error or address bit detected in 9-bit mode\n\n");
Mike Dunnfb088e32010-01-26 12:12:12 -0500883 break;
884 case SERIAL_IIR_CTI:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700885 dev_dbg(dev, "Serial Port: Receiver time out\n");
Mike Dunnfb088e32010-01-26 12:12:12 -0500886 break;
887 case SERIAL_IIR_MS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700888 /* dev_dbg(dev, "Serial Port: Modem status change\n"); */
Mike Dunnfb088e32010-01-26 12:12:12 -0500889 break;
890 }
891 }
892
Mike Dunnb69578d2010-04-15 17:01:33 -0400893#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
894 { /* update local copy of DSR reg */
895 struct usb_serial_port *port = urb->context;
896 struct mos7715_parport *mos_parport = port->serial->private;
897 if (unlikely(mos_parport == NULL))
898 return;
899 atomic_set(&mos_parport->shadowDSR, data[2]);
900 }
901#endif
902
Mike Dunnfb088e32010-01-26 12:12:12 -0500903exit:
904 result = usb_submit_urb(urb, GFP_ATOMIC);
905 if (result)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700906 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
Mike Dunnfb088e32010-01-26 12:12:12 -0500907}
908
909/*
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700910 * mos7720_bulk_in_callback
911 * this is the callback function for when we have received data on the
912 * bulk in endpoint.
913 */
914static void mos7720_bulk_in_callback(struct urb *urb)
915{
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700916 int retval;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700917 unsigned char *data ;
918 struct usb_serial_port *port;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700919 int status = urb->status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700920
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700921 if (status) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700922 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700923 return;
924 }
925
Mike Dunnb69578d2010-04-15 17:01:33 -0400926 port = urb->context;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700927
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700928 dev_dbg(&port->dev, "Entering...%s\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700929
930 data = urb->transfer_buffer;
931
Jiri Slaby2e124b42013-01-03 15:53:06 +0100932 if (urb->actual_length) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100933 tty_insert_flip_string(&port->port, data, urb->actual_length);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100934 tty_flip_buffer_push(&port->port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700935 }
936
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700937 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700938 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
939 if (retval)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700940 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700941 }
942}
943
944/*
945 * mos7720_bulk_out_data_callback
946 * this is the callback function for when we have finished sending serial
947 * data on the bulk out endpoint.
948 */
949static void mos7720_bulk_out_data_callback(struct urb *urb)
950{
951 struct moschip_port *mos7720_port;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700952 int status = urb->status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700953
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700954 if (status) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700955 dev_dbg(&urb->dev->dev, "nonzero write bulk status received:%d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700956 return;
957 }
958
959 mos7720_port = urb->context;
960 if (!mos7720_port) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700961 dev_dbg(&urb->dev->dev, "NULL mos7720_port pointer\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700962 return ;
963 }
964
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100965 if (mos7720_port->open)
966 tty_port_tty_wakeup(&mos7720_port->port->port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700967}
968
969/*
Mike Dunnfb088e32010-01-26 12:12:12 -0500970 * mos77xx_probe
971 * this function installs the appropriate read interrupt endpoint callback
972 * depending on whether the device is a 7720 or 7715, thus avoiding costly
973 * run-time checks in the high-frequency callback routine itself.
974 */
975static int mos77xx_probe(struct usb_serial *serial,
976 const struct usb_device_id *id)
977{
978 if (id->idProduct == MOSCHIP_DEVICE_ID_7715)
979 moschip7720_2port_driver.read_int_callback =
980 mos7715_interrupt_callback;
981 else
982 moschip7720_2port_driver.read_int_callback =
983 mos7720_interrupt_callback;
984
985 return 0;
986}
987
988static int mos77xx_calc_num_ports(struct usb_serial *serial)
989{
990 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
991 if (product == MOSCHIP_DEVICE_ID_7715)
992 return 1;
993
994 return 2;
995}
996
Alan Coxa509a7e2009-09-19 13:13:26 -0700997static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700998{
999 struct usb_serial *serial;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001000 struct urb *urb;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001001 struct moschip_port *mos7720_port;
1002 int response;
1003 int port_number;
Mike Dunn63b91762010-04-15 17:02:09 -04001004 __u8 data;
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001005 int allocated_urbs = 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001006 int j;
1007
1008 serial = port->serial;
1009
1010 mos7720_port = usb_get_serial_port_data(port);
1011 if (mos7720_port == NULL)
1012 return -ENODEV;
1013
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001014 usb_clear_halt(serial->dev, port->write_urb->pipe);
1015 usb_clear_halt(serial->dev, port->read_urb->pipe);
1016
1017 /* Initialising the write urb pool */
1018 for (j = 0; j < NUM_URBS; ++j) {
Alan Cox4da1a172008-07-22 11:16:21 +01001019 urb = usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001020 mos7720_port->write_urb_pool[j] = urb;
1021
1022 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001023 dev_err(&port->dev, "No more urbs???\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001024 continue;
1025 }
1026
1027 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1028 GFP_KERNEL);
1029 if (!urb->transfer_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001030 dev_err(&port->dev,
1031 "%s-out of memory for urb buffers.\n",
1032 __func__);
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001033 usb_free_urb(mos7720_port->write_urb_pool[j]);
1034 mos7720_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001035 continue;
1036 }
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001037 allocated_urbs++;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001038 }
1039
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001040 if (!allocated_urbs)
1041 return -ENOMEM;
1042
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001043 /* Initialize MCS7720 -- Write Init values to corresponding Registers
1044 *
1045 * Register Index
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001046 * 0 : THR/RHR
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001047 * 1 : IER
1048 * 2 : FCR
1049 * 3 : LCR
1050 * 4 : MCR
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001051 * 5 : LSR
1052 * 6 : MSR
1053 * 7 : SPR
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001054 *
1055 * 0x08 : SP1/2 Control Reg
1056 */
1057 port_number = port->number - port->serial->minor;
Mike Dunn63b91762010-04-15 17:02:09 -04001058 read_mos_reg(serial, port_number, LSR, &data);
1059
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001060 dev_dbg(&port->dev, "SS::%p LSR:%x\n", mos7720_port, data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001061
Mike Dunn63b91762010-04-15 17:02:09 -04001062 write_mos_reg(serial, dummy, SP1_REG, 0x02);
1063 write_mos_reg(serial, dummy, SP2_REG, 0x02);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001064
Mike Dunn63b91762010-04-15 17:02:09 -04001065 write_mos_reg(serial, port_number, IER, 0x00);
1066 write_mos_reg(serial, port_number, FCR, 0x00);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001067
Mike Dunn63b91762010-04-15 17:02:09 -04001068 write_mos_reg(serial, port_number, FCR, 0xcf);
1069 mos7720_port->shadowLCR = 0x03;
1070 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1071 mos7720_port->shadowMCR = 0x0b;
1072 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001073
Mike Dunn63b91762010-04-15 17:02:09 -04001074 write_mos_reg(serial, port_number, SP_CONTROL_REG, 0x00);
1075 read_mos_reg(serial, dummy, SP_CONTROL_REG, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001076 data = data | (port->number - port->serial->minor + 1);
Mike Dunn63b91762010-04-15 17:02:09 -04001077 write_mos_reg(serial, dummy, SP_CONTROL_REG, data);
1078 mos7720_port->shadowLCR = 0x83;
1079 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1080 write_mos_reg(serial, port_number, THR, 0x0c);
1081 write_mos_reg(serial, port_number, IER, 0x00);
1082 mos7720_port->shadowLCR = 0x03;
1083 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1084 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001085
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001086 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
1087 if (response)
Alan Cox4da1a172008-07-22 11:16:21 +01001088 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
1089 __func__, response);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001090
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001091 /* initialize our port settings */
1092 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
1093
1094 /* send a open port command */
1095 mos7720_port->open = 1;
1096
1097 return 0;
1098}
1099
1100/*
1101 * mos7720_chars_in_buffer
1102 * this function is called by the tty driver when it wants to know how many
1103 * bytes of data we currently have outstanding in the port (data that has
1104 * been written, but hasn't made it out the port yet)
1105 * If successful, we return the number of bytes left to be written in the
1106 * system,
1107 * Otherwise we return a negative error number.
1108 */
Alan Cox95da3102008-07-22 11:09:07 +01001109static int mos7720_chars_in_buffer(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001110{
Alan Cox95da3102008-07-22 11:09:07 +01001111 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001112 int i;
1113 int chars = 0;
1114 struct moschip_port *mos7720_port;
1115
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001116 mos7720_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001117 if (mos7720_port == NULL)
Alan Cox23198fd2009-07-20 16:05:27 +01001118 return 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001119
1120 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001121 if (mos7720_port->write_urb_pool[i] &&
1122 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001123 chars += URB_TRANSFER_BUFFER_SIZE;
1124 }
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001125 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001126 return chars;
1127}
1128
Alan Cox335f8512009-06-11 12:26:29 +01001129static void mos7720_close(struct usb_serial_port *port)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001130{
1131 struct usb_serial *serial;
1132 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001133 int j;
1134
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001135 serial = port->serial;
1136
1137 mos7720_port = usb_get_serial_port_data(port);
1138 if (mos7720_port == NULL)
1139 return;
1140
1141 for (j = 0; j < NUM_URBS; ++j)
1142 usb_kill_urb(mos7720_port->write_urb_pool[j]);
1143
1144 /* Freeing Write URBs */
1145 for (j = 0; j < NUM_URBS; ++j) {
1146 if (mos7720_port->write_urb_pool[j]) {
1147 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
1148 usb_free_urb(mos7720_port->write_urb_pool[j]);
1149 }
1150 }
1151
1152 /* While closing port, shutdown all bulk read, write *
Oliver Neukuma1cd7e92008-01-16 17:18:52 +01001153 * and interrupt read if they exists, otherwise nop */
Oliver Neukuma1cd7e92008-01-16 17:18:52 +01001154 usb_kill_urb(port->write_urb);
Oliver Neukuma1cd7e92008-01-16 17:18:52 +01001155 usb_kill_urb(port->read_urb);
1156
Johan Hovoldcf41aa92013-03-21 12:37:41 +01001157 write_mos_reg(serial, port->number - port->serial->minor, MCR, 0x00);
1158 write_mos_reg(serial, port->number - port->serial->minor, IER, 0x00);
1159
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001160 mos7720_port->open = 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001161}
1162
Alan Cox95da3102008-07-22 11:09:07 +01001163static void mos7720_break(struct tty_struct *tty, int break_state)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001164{
Alan Cox95da3102008-07-22 11:09:07 +01001165 struct usb_serial_port *port = tty->driver_data;
Alan Cox4da1a172008-07-22 11:16:21 +01001166 unsigned char data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001167 struct usb_serial *serial;
1168 struct moschip_port *mos7720_port;
1169
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001170 serial = port->serial;
1171
1172 mos7720_port = usb_get_serial_port_data(port);
1173 if (mos7720_port == NULL)
1174 return;
1175
1176 if (break_state == -1)
1177 data = mos7720_port->shadowLCR | UART_LCR_SBC;
1178 else
1179 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
1180
1181 mos7720_port->shadowLCR = data;
Mike Dunn63b91762010-04-15 17:02:09 -04001182 write_mos_reg(serial, port->number - port->serial->minor,
1183 LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001184}
1185
1186/*
1187 * mos7720_write_room
1188 * this function is called by the tty driver when it wants to know how many
1189 * bytes of data we can accept for a specific port.
1190 * If successful, we return the amount of room that we have for this port
1191 * Otherwise we return a negative error number.
1192 */
Alan Cox95da3102008-07-22 11:09:07 +01001193static int mos7720_write_room(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001194{
Alan Cox95da3102008-07-22 11:09:07 +01001195 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001196 struct moschip_port *mos7720_port;
1197 int room = 0;
1198 int i;
1199
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001200 mos7720_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001201 if (mos7720_port == NULL)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001202 return -ENODEV;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001203
Alan Coxa5b6f602008-04-08 17:16:06 +01001204 /* FIXME: Locking */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001205 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001206 if (mos7720_port->write_urb_pool[i] &&
1207 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001208 room += URB_TRANSFER_BUFFER_SIZE;
1209 }
1210
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001211 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001212 return room;
1213}
1214
Alan Cox95da3102008-07-22 11:09:07 +01001215static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
1216 const unsigned char *data, int count)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001217{
1218 int status;
1219 int i;
1220 int bytes_sent = 0;
1221 int transfer_size;
1222
1223 struct moschip_port *mos7720_port;
1224 struct usb_serial *serial;
1225 struct urb *urb;
1226 const unsigned char *current_position = data;
1227
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001228 serial = port->serial;
1229
1230 mos7720_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001231 if (mos7720_port == NULL)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001232 return -ENODEV;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001233
1234 /* try to find a free urb in the list */
1235 urb = NULL;
1236
1237 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001238 if (mos7720_port->write_urb_pool[i] &&
1239 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001240 urb = mos7720_port->write_urb_pool[i];
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001241 dev_dbg(&port->dev, "URB:%d\n", i);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001242 break;
1243 }
1244 }
1245
1246 if (urb == NULL) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001247 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001248 goto exit;
1249 }
1250
1251 if (urb->transfer_buffer == NULL) {
1252 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1253 GFP_KERNEL);
1254 if (urb->transfer_buffer == NULL) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001255 dev_err_console(port, "%s no more kernel memory...\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001256 __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001257 goto exit;
1258 }
1259 }
Alan Cox4da1a172008-07-22 11:16:21 +01001260 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001261
1262 memcpy(urb->transfer_buffer, current_position, transfer_size);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001263 usb_serial_debug_data(&port->dev, __func__, transfer_size,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001264 urb->transfer_buffer);
1265
1266 /* fill urb with data and submit */
1267 usb_fill_bulk_urb(urb, serial->dev,
1268 usb_sndbulkpipe(serial->dev,
Alan Cox4da1a172008-07-22 11:16:21 +01001269 port->bulk_out_endpointAddress),
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001270 urb->transfer_buffer, transfer_size,
1271 mos7720_bulk_out_data_callback, mos7720_port);
1272
1273 /* send it down the pipe */
Alan Cox4da1a172008-07-22 11:16:21 +01001274 status = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001275 if (status) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001276 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001277 "with status = %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001278 bytes_sent = status;
1279 goto exit;
1280 }
1281 bytes_sent = transfer_size;
1282
1283exit:
1284 return bytes_sent;
1285}
1286
Alan Cox95da3102008-07-22 11:09:07 +01001287static void mos7720_throttle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001288{
Alan Cox95da3102008-07-22 11:09:07 +01001289 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001290 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001291 int status;
1292
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001293 mos7720_port = usb_get_serial_port_data(port);
1294
1295 if (mos7720_port == NULL)
1296 return;
1297
1298 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001299 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001300 return;
1301 }
1302
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001303 /* if we are implementing XON/XOFF, send the stop character */
1304 if (I_IXOFF(tty)) {
1305 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001306 status = mos7720_write(tty, port, &stop_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001307 if (status <= 0)
1308 return;
1309 }
1310
1311 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001312 if (tty->termios.c_cflag & CRTSCTS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001313 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
Mike Dunn63b91762010-04-15 17:02:09 -04001314 write_mos_reg(port->serial, port->number - port->serial->minor,
1315 MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001316 if (status != 0)
1317 return;
1318 }
1319}
1320
Alan Cox95da3102008-07-22 11:09:07 +01001321static void mos7720_unthrottle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001322{
Alan Cox95da3102008-07-22 11:09:07 +01001323 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001324 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +01001325 int status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001326
1327 if (mos7720_port == NULL)
1328 return;
1329
1330 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001331 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001332 return;
1333 }
1334
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001335 /* if we are implementing XON/XOFF, send the start character */
1336 if (I_IXOFF(tty)) {
1337 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001338 status = mos7720_write(tty, port, &start_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001339 if (status <= 0)
1340 return;
1341 }
1342
1343 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001344 if (tty->termios.c_cflag & CRTSCTS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001345 mos7720_port->shadowMCR |= UART_MCR_RTS;
Mike Dunn63b91762010-04-15 17:02:09 -04001346 write_mos_reg(port->serial, port->number - port->serial->minor,
1347 MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001348 if (status != 0)
1349 return;
1350 }
1351}
1352
Mike Dunnb69578d2010-04-15 17:01:33 -04001353/* FIXME: this function does not work */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001354static int set_higher_rates(struct moschip_port *mos7720_port,
1355 unsigned int baud)
1356{
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001357 struct usb_serial_port *port;
1358 struct usb_serial *serial;
1359 int port_number;
Mike Dunn63b91762010-04-15 17:02:09 -04001360 enum mos_regs sp_reg;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001361 if (mos7720_port == NULL)
1362 return -EINVAL;
1363
1364 port = mos7720_port->port;
1365 serial = port->serial;
1366
Alan Cox4da1a172008-07-22 11:16:21 +01001367 /***********************************************
1368 * Init Sequence for higher rates
1369 ***********************************************/
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001370 dev_dbg(&port->dev, "Sending Setting Commands ..........\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001371 port_number = port->number - port->serial->minor;
1372
Mike Dunn63b91762010-04-15 17:02:09 -04001373 write_mos_reg(serial, port_number, IER, 0x00);
1374 write_mos_reg(serial, port_number, FCR, 0x00);
1375 write_mos_reg(serial, port_number, FCR, 0xcf);
1376 mos7720_port->shadowMCR = 0x0b;
1377 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1378 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x00);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001379
Alan Cox4da1a172008-07-22 11:16:21 +01001380 /***********************************************
1381 * Set for higher rates *
1382 ***********************************************/
Mike Dunnb69578d2010-04-15 17:01:33 -04001383 /* writing baud rate verbatum into uart clock field clearly not right */
Mike Dunn63b91762010-04-15 17:02:09 -04001384 if (port_number == 0)
1385 sp_reg = SP1_REG;
1386 else
1387 sp_reg = SP2_REG;
1388 write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
1389 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x03);
1390 mos7720_port->shadowMCR = 0x2b;
1391 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001392
Alan Cox4da1a172008-07-22 11:16:21 +01001393 /***********************************************
1394 * Set DLL/DLM
1395 ***********************************************/
Mike Dunn63b91762010-04-15 17:02:09 -04001396 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1397 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1398 write_mos_reg(serial, port_number, DLL, 0x01);
1399 write_mos_reg(serial, port_number, DLM, 0x00);
1400 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1401 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001402
1403 return 0;
1404}
1405
1406/* baud rate information */
Alan Cox4da1a172008-07-22 11:16:21 +01001407struct divisor_table_entry {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001408 __u32 baudrate;
1409 __u16 divisor;
1410};
1411
1412/* Define table of divisors for moschip 7720 hardware *
1413 * These assume a 3.6864MHz crystal, the standard /16, and *
1414 * MCR.7 = 0. */
1415static struct divisor_table_entry divisor_table[] = {
1416 { 50, 2304},
1417 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
1418 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
1419 { 150, 768},
1420 { 300, 384},
1421 { 600, 192},
1422 { 1200, 96},
1423 { 1800, 64},
1424 { 2400, 48},
1425 { 4800, 24},
1426 { 7200, 16},
1427 { 9600, 12},
1428 { 19200, 6},
1429 { 38400, 3},
1430 { 57600, 2},
1431 { 115200, 1},
1432};
1433
1434/*****************************************************************************
1435 * calc_baud_rate_divisor
1436 * this function calculates the proper baud rate divisor for the specified
1437 * baud rate.
1438 *****************************************************************************/
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001439static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, int *divisor)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001440{
1441 int i;
1442 __u16 custom;
1443 __u16 round1;
1444 __u16 round;
1445
1446
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001447 dev_dbg(&port->dev, "%s - %d\n", __func__, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001448
1449 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
1450 if (divisor_table[i].baudrate == baudrate) {
1451 *divisor = divisor_table[i].divisor;
1452 return 0;
1453 }
1454 }
1455
Alan Cox4da1a172008-07-22 11:16:21 +01001456 /* After trying for all the standard baud rates *
1457 * Try calculating the divisor for this baud rate */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001458 if (baudrate > 75 && baudrate < 230400) {
1459 /* get the divisor */
1460 custom = (__u16)(230400L / baudrate);
1461
1462 /* Check for round off */
1463 round1 = (__u16)(2304000L / baudrate);
1464 round = (__u16)(round1 - (custom * 10));
1465 if (round > 4)
1466 custom++;
1467 *divisor = custom;
1468
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001469 dev_dbg(&port->dev, "Baud %d = %d\n", baudrate, custom);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001470 return 0;
1471 }
1472
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001473 dev_dbg(&port->dev, "Baud calculation Failed...\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001474 return -EINVAL;
1475}
1476
1477/*
1478 * send_cmd_write_baud_rate
1479 * this function sends the proper command to change the baud rate of the
1480 * specified port.
1481 */
1482static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1483 int baudrate)
1484{
1485 struct usb_serial_port *port;
1486 struct usb_serial *serial;
1487 int divisor;
1488 int status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001489 unsigned char number;
1490
1491 if (mos7720_port == NULL)
1492 return -1;
1493
1494 port = mos7720_port->port;
1495 serial = port->serial;
1496
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001497 number = port->number - port->serial->minor;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001498 dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001499
Alan Cox4da1a172008-07-22 11:16:21 +01001500 /* Calculate the Divisor */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001501 status = calc_baud_rate_divisor(port, baudrate, &divisor);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001502 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001503 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001504 return status;
1505 }
1506
Alan Cox4da1a172008-07-22 11:16:21 +01001507 /* Enable access to divisor latch */
Mike Dunn63b91762010-04-15 17:02:09 -04001508 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1509 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001510
1511 /* Write the divisor */
Mike Dunn63b91762010-04-15 17:02:09 -04001512 write_mos_reg(serial, number, DLL, (__u8)(divisor & 0xff));
1513 write_mos_reg(serial, number, DLM, (__u8)((divisor & 0xff00) >> 8));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001514
Alan Cox4da1a172008-07-22 11:16:21 +01001515 /* Disable access to divisor latch */
Mike Dunn63b91762010-04-15 17:02:09 -04001516 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1517 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001518
1519 return status;
1520}
1521
1522/*
1523 * change_port_settings
1524 * This routine is called to set the UART on the device to match
1525 * the specified new settings.
1526 */
Alan Cox95da3102008-07-22 11:09:07 +01001527static void change_port_settings(struct tty_struct *tty,
1528 struct moschip_port *mos7720_port,
Alan Cox606d0992006-12-08 02:38:45 -08001529 struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001530{
1531 struct usb_serial_port *port;
1532 struct usb_serial *serial;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001533 int baud;
1534 unsigned cflag;
1535 unsigned iflag;
1536 __u8 mask = 0xff;
1537 __u8 lData;
1538 __u8 lParity;
1539 __u8 lStop;
1540 int status;
1541 int port_number;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001542
1543 if (mos7720_port == NULL)
1544 return ;
1545
1546 port = mos7720_port->port;
1547 serial = port->serial;
1548 port_number = port->number - port->serial->minor;
1549
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001550 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001551 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001552 return;
1553 }
1554
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001555 lData = UART_LCR_WLEN8;
1556 lStop = 0x00; /* 1 stop bit */
1557 lParity = 0x00; /* No parity */
1558
Alan Coxadc8d742012-07-14 15:31:47 +01001559 cflag = tty->termios.c_cflag;
1560 iflag = tty->termios.c_iflag;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001561
1562 /* Change the number of bits */
1563 switch (cflag & CSIZE) {
1564 case CS5:
1565 lData = UART_LCR_WLEN5;
1566 mask = 0x1f;
1567 break;
1568
1569 case CS6:
1570 lData = UART_LCR_WLEN6;
1571 mask = 0x3f;
1572 break;
1573
1574 case CS7:
1575 lData = UART_LCR_WLEN7;
1576 mask = 0x7f;
1577 break;
1578 default:
1579 case CS8:
1580 lData = UART_LCR_WLEN8;
1581 break;
1582 }
1583
1584 /* Change the Parity bit */
1585 if (cflag & PARENB) {
1586 if (cflag & PARODD) {
1587 lParity = UART_LCR_PARITY;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001588 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001589 } else {
1590 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001591 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001592 }
1593
1594 } else {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001595 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001596 }
1597
1598 if (cflag & CMSPAR)
1599 lParity = lParity | 0x20;
1600
1601 /* Change the Stop bit */
1602 if (cflag & CSTOPB) {
1603 lStop = UART_LCR_STOP;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001604 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001605 } else {
1606 lStop = 0x00;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001607 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001608 }
1609
1610#define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1611#define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1612#define LCR_PAR_MASK 0x38 /* Mask for parity field */
1613
1614 /* Update the LCR with the correct value */
Alan Cox4da1a172008-07-22 11:16:21 +01001615 mos7720_port->shadowLCR &=
Mike Dunn63b91762010-04-15 17:02:09 -04001616 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001617 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1618
1619
1620 /* Disable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001621 write_mos_reg(serial, port_number, IER, 0x00);
1622 write_mos_reg(serial, port_number, FCR, 0x00);
1623 write_mos_reg(serial, port_number, FCR, 0xcf);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001624
1625 /* Send the updated LCR value to the mos7720 */
Mike Dunn63b91762010-04-15 17:02:09 -04001626 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1627 mos7720_port->shadowMCR = 0x0b;
1628 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001629
1630 /* set up the MCR register and send it to the mos7720 */
1631 mos7720_port->shadowMCR = UART_MCR_OUT2;
1632 if (cflag & CBAUD)
1633 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1634
1635 if (cflag & CRTSCTS) {
1636 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
Alan Cox4da1a172008-07-22 11:16:21 +01001637 /* To set hardware flow control to the specified *
1638 * serial port, in SP1/2_CONTROL_REG */
Johan Hovolda26f0092013-06-04 18:50:31 +02001639 if (port_number)
Mike Dunn63b91762010-04-15 17:02:09 -04001640 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x01);
1641 else
1642 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x02);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001643
Mike Dunn63b91762010-04-15 17:02:09 -04001644 } else
1645 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1646
1647 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001648
1649 /* Determine divisor based on baud rate */
1650 baud = tty_get_baud_rate(tty);
1651 if (!baud) {
1652 /* pick a default, any default... */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001653 dev_dbg(&port->dev, "Picked default baud...\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001654 baud = 9600;
1655 }
1656
1657 if (baud >= 230400) {
1658 set_higher_rates(mos7720_port, baud);
1659 /* Enable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001660 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001661 return;
1662 }
1663
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001664 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001665 status = send_cmd_write_baud_rate(mos7720_port, baud);
Alan Cox65d063ab2008-01-03 17:01:18 +00001666 /* FIXME: needs to write actual resulting baud back not just
1667 blindly do so */
1668 if (cflag & CBAUD)
1669 tty_encode_baud_rate(tty, baud, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001670 /* Enable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001671 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001672
1673 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001674 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1675 if (status)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001676 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001677 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001678}
1679
1680/*
1681 * mos7720_set_termios
1682 * this function is called by the tty driver when it wants to change the
1683 * termios structure.
1684 */
Alan Cox95da3102008-07-22 11:09:07 +01001685static void mos7720_set_termios(struct tty_struct *tty,
1686 struct usb_serial_port *port, struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001687{
1688 int status;
1689 unsigned int cflag;
1690 struct usb_serial *serial;
1691 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001692
1693 serial = port->serial;
1694
1695 mos7720_port = usb_get_serial_port_data(port);
1696
1697 if (mos7720_port == NULL)
1698 return;
1699
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001700 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001701 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001702 return;
1703 }
1704
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001705 dev_dbg(&port->dev, "setting termios - ASPIRE\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001706
Alan Coxadc8d742012-07-14 15:31:47 +01001707 cflag = tty->termios.c_cflag;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001708
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001709 dev_dbg(&port->dev, "%s - cflag %08x iflag %08x\n", __func__,
Linus Torvaldsd9a80742012-10-01 13:23:01 -07001710 tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001711
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001712 dev_dbg(&port->dev, "%s - old cflag %08x old iflag %08x\n", __func__,
1713 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001714
1715 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001716 change_port_settings(tty, mos7720_port, old_termios);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001717
Alan Cox4da1a172008-07-22 11:16:21 +01001718 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001719 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1720 if (status)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001721 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001722 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001723}
1724
1725/*
1726 * get_lsr_info - get line status register info
1727 *
1728 * Purpose: Let user call ioctl() to get info when the UART physically
1729 * is emptied. On bus types like RS485, the transmitter must
1730 * release the bus after transmitting. This must be done when
1731 * the transmit shift register is empty, not be done when the
1732 * transmit holding register is empty. This functionality
1733 * allows an RS485 driver to be written in user space.
1734 */
Alan Cox4da1a172008-07-22 11:16:21 +01001735static int get_lsr_info(struct tty_struct *tty,
1736 struct moschip_port *mos7720_port, unsigned int __user *value)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001737{
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001738 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001739 unsigned int result = 0;
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001740 unsigned char data = 0;
1741 int port_number = port->number - port->serial->minor;
1742 int count;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001743
Alan Cox95da3102008-07-22 11:09:07 +01001744 count = mos7720_chars_in_buffer(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001745 if (count == 0) {
Mike Dunn63b91762010-04-15 17:02:09 -04001746 read_mos_reg(port->serial, port_number, LSR, &data);
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001747 if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1748 == (UART_LSR_TEMT | UART_LSR_THRE)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001749 dev_dbg(&port->dev, "%s -- Empty\n", __func__);
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001750 result = TIOCSER_TEMT;
1751 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001752 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001753 if (copy_to_user(value, &result, sizeof(int)))
1754 return -EFAULT;
1755 return 0;
1756}
1757
Alan Cox60b33c12011-02-14 16:26:14 +00001758static int mos7720_tiocmget(struct tty_struct *tty)
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001759{
1760 struct usb_serial_port *port = tty->driver_data;
1761 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1762 unsigned int result = 0;
1763 unsigned int mcr ;
1764 unsigned int msr ;
1765
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001766 mcr = mos7720_port->shadowMCR;
1767 msr = mos7720_port->shadowMSR;
1768
1769 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1770 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1771 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1772 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
1773 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1774 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
1775
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001776 return result;
1777}
1778
Alan Cox20b9d172011-02-14 16:26:50 +00001779static int mos7720_tiocmset(struct tty_struct *tty,
Mike Dunn63b91762010-04-15 17:02:09 -04001780 unsigned int set, unsigned int clear)
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001781{
1782 struct usb_serial_port *port = tty->driver_data;
1783 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1784 unsigned int mcr ;
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001785
1786 mcr = mos7720_port->shadowMCR;
1787
1788 if (set & TIOCM_RTS)
1789 mcr |= UART_MCR_RTS;
1790 if (set & TIOCM_DTR)
1791 mcr |= UART_MCR_DTR;
1792 if (set & TIOCM_LOOP)
1793 mcr |= UART_MCR_LOOP;
1794
1795 if (clear & TIOCM_RTS)
1796 mcr &= ~UART_MCR_RTS;
1797 if (clear & TIOCM_DTR)
1798 mcr &= ~UART_MCR_DTR;
1799 if (clear & TIOCM_LOOP)
1800 mcr &= ~UART_MCR_LOOP;
1801
1802 mos7720_port->shadowMCR = mcr;
Mike Dunn63b91762010-04-15 17:02:09 -04001803 write_mos_reg(port->serial, port->number - port->serial->minor,
1804 MCR, mos7720_port->shadowMCR);
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001805
1806 return 0;
1807}
1808
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001809static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1810 unsigned int __user *value)
1811{
Alan Cox0bca1b92010-09-16 18:21:40 +01001812 unsigned int mcr;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001813 unsigned int arg;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001814
1815 struct usb_serial_port *port;
1816
1817 if (mos7720_port == NULL)
1818 return -1;
1819
Alan Cox4da1a172008-07-22 11:16:21 +01001820 port = (struct usb_serial_port *)mos7720_port->port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001821 mcr = mos7720_port->shadowMCR;
1822
1823 if (copy_from_user(&arg, value, sizeof(int)))
1824 return -EFAULT;
1825
1826 switch (cmd) {
1827 case TIOCMBIS:
1828 if (arg & TIOCM_RTS)
1829 mcr |= UART_MCR_RTS;
1830 if (arg & TIOCM_DTR)
1831 mcr |= UART_MCR_RTS;
1832 if (arg & TIOCM_LOOP)
1833 mcr |= UART_MCR_LOOP;
1834 break;
1835
1836 case TIOCMBIC:
1837 if (arg & TIOCM_RTS)
1838 mcr &= ~UART_MCR_RTS;
1839 if (arg & TIOCM_DTR)
1840 mcr &= ~UART_MCR_RTS;
1841 if (arg & TIOCM_LOOP)
1842 mcr &= ~UART_MCR_LOOP;
1843 break;
1844
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001845 }
1846
1847 mos7720_port->shadowMCR = mcr;
Mike Dunn63b91762010-04-15 17:02:09 -04001848 write_mos_reg(port->serial, port->number - port->serial->minor,
1849 MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001850
1851 return 0;
1852}
1853
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001854static int get_serial_info(struct moschip_port *mos7720_port,
1855 struct serial_struct __user *retinfo)
1856{
1857 struct serial_struct tmp;
1858
1859 if (!retinfo)
1860 return -EFAULT;
1861
1862 memset(&tmp, 0, sizeof(tmp));
1863
1864 tmp.type = PORT_16550A;
1865 tmp.line = mos7720_port->port->serial->minor;
1866 tmp.port = mos7720_port->port->number;
1867 tmp.irq = 0;
1868 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
Alan Cox4da1a172008-07-22 11:16:21 +01001869 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001870 tmp.baud_base = 9600;
1871 tmp.close_delay = 5*HZ;
1872 tmp.closing_wait = 30*HZ;
1873
1874 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1875 return -EFAULT;
1876 return 0;
1877}
1878
Alan Cox00a0d0d2011-02-14 16:27:06 +00001879static int mos7720_ioctl(struct tty_struct *tty,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001880 unsigned int cmd, unsigned long arg)
1881{
Alan Cox95da3102008-07-22 11:09:07 +01001882 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001883 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001884
1885 mos7720_port = usb_get_serial_port_data(port);
1886 if (mos7720_port == NULL)
1887 return -ENODEV;
1888
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001889 dev_dbg(&port->dev, "%s - cmd = 0x%x", __func__, cmd);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001890
1891 switch (cmd) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001892 case TIOCSERGETLSR:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001893 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
Alan Cox4da1a172008-07-22 11:16:21 +01001894 return get_lsr_info(tty, mos7720_port,
1895 (unsigned int __user *)arg);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001896
Alan Cox95da3102008-07-22 11:09:07 +01001897 /* FIXME: These should be using the mode methods */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001898 case TIOCMBIS:
1899 case TIOCMBIC:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001900 dev_dbg(&port->dev, "%s TIOCMSET/TIOCMBIC/TIOCMSET\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001901 return set_modem_info(mos7720_port, cmd,
1902 (unsigned int __user *)arg);
1903
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001904 case TIOCGSERIAL:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001905 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001906 return get_serial_info(mos7720_port,
1907 (struct serial_struct __user *)arg);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001908 }
1909
1910 return -ENOIOCTLCMD;
1911}
1912
1913static int mos7720_startup(struct usb_serial *serial)
1914{
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001915 struct usb_device *dev;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001916 char data;
Huzaifa Sidhpurwala91f58ae2011-02-21 12:58:45 +05301917 u16 product;
Mike Dunnb69578d2010-04-15 17:01:33 -04001918 int ret_val;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001919
Huzaifa Sidhpurwala91f58ae2011-02-21 12:58:45 +05301920 product = le16_to_cpu(serial->dev->descriptor.idProduct);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001921 dev = serial->dev;
1922
Mike Dunnfb088e32010-01-26 12:12:12 -05001923 /*
1924 * The 7715 uses the first bulk in/out endpoint pair for the parallel
1925 * port, and the second for the serial port. Because the usbserial core
1926 * assumes both pairs are serial ports, we must engage in a bit of
1927 * subterfuge and swap the pointers for ports 0 and 1 in order to make
1928 * port 0 point to the serial port. However, both moschip devices use a
1929 * single interrupt-in endpoint for both ports (as mentioned a little
1930 * further down), and this endpoint was assigned to port 0. So after
1931 * the swap, we must copy the interrupt endpoint elements from port 1
1932 * (as newly assigned) to port 0, and null out port 1 pointers.
1933 */
1934 if (product == MOSCHIP_DEVICE_ID_7715) {
1935 struct usb_serial_port *tmp = serial->port[0];
1936 serial->port[0] = serial->port[1];
1937 serial->port[1] = tmp;
1938 serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb;
1939 serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer;
1940 serial->port[0]->interrupt_in_endpointAddress =
1941 tmp->interrupt_in_endpointAddress;
1942 serial->port[1]->interrupt_in_urb = NULL;
1943 serial->port[1]->interrupt_in_buffer = NULL;
1944 }
1945
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001946 /* setting configuration feature to one */
1947 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Johan Hovold849513a2013-05-27 14:44:43 +02001948 (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5000);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001949
Mike Dunnb69578d2010-04-15 17:01:33 -04001950 /* start the interrupt urb */
1951 ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
1952 if (ret_val)
1953 dev_err(&dev->dev,
1954 "%s - Error %d submitting control urb\n",
1955 __func__, ret_val);
1956
1957#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1958 if (product == MOSCHIP_DEVICE_ID_7715) {
1959 ret_val = mos7715_parport_init(serial);
1960 if (ret_val < 0)
1961 return ret_val;
1962 }
1963#endif
Alan Cox4da1a172008-07-22 11:16:21 +01001964 /* LSR For Port 1 */
Mike Dunn63b91762010-04-15 17:02:09 -04001965 read_mos_reg(serial, 0, LSR, &data);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001966 dev_dbg(&dev->dev, "LSR:%x\n", data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001967
1968 return 0;
1969}
1970
Alan Sternf9c99bb2009-06-02 11:53:55 -04001971static void mos7720_release(struct usb_serial *serial)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001972{
Mike Dunnb69578d2010-04-15 17:01:33 -04001973#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1974 /* close the parallel port */
1975
1976 if (le16_to_cpu(serial->dev->descriptor.idProduct)
1977 == MOSCHIP_DEVICE_ID_7715) {
1978 struct urbtracker *urbtrack;
1979 unsigned long flags;
1980 struct mos7715_parport *mos_parport =
1981 usb_get_serial_data(serial);
1982
1983 /* prevent NULL ptr dereference in port callbacks */
1984 spin_lock(&release_lock);
1985 mos_parport->pp->private_data = NULL;
1986 spin_unlock(&release_lock);
1987
1988 /* wait for synchronous usb calls to return */
1989 if (mos_parport->msg_pending)
1990 wait_for_completion_timeout(&mos_parport->syncmsg_compl,
Johan Hovold849513a2013-05-27 14:44:43 +02001991 msecs_to_jiffies(MOS_WDR_TIMEOUT));
Mike Dunnb69578d2010-04-15 17:01:33 -04001992
1993 parport_remove_port(mos_parport->pp);
1994 usb_set_serial_data(serial, NULL);
1995 mos_parport->serial = NULL;
1996
1997 /* if tasklet currently scheduled, wait for it to complete */
1998 tasklet_kill(&mos_parport->urb_tasklet);
1999
2000 /* unlink any urbs sent by the tasklet */
2001 spin_lock_irqsave(&mos_parport->listlock, flags);
2002 list_for_each_entry(urbtrack,
2003 &mos_parport->active_urbs,
2004 urblist_entry)
2005 usb_unlink_urb(urbtrack->urb);
2006 spin_unlock_irqrestore(&mos_parport->listlock, flags);
2007
2008 kref_put(&mos_parport->ref_count, destroy_mos_parport);
2009 }
2010#endif
Johan Hovold4230af52012-10-25 10:29:05 +02002011}
2012
2013static int mos7720_port_probe(struct usb_serial_port *port)
2014{
2015 struct moschip_port *mos7720_port;
2016
2017 mos7720_port = kzalloc(sizeof(*mos7720_port), GFP_KERNEL);
2018 if (!mos7720_port)
2019 return -ENOMEM;
2020
2021 /* Initialize all port interrupt end point to port 0 int endpoint.
2022 * Our device has only one interrupt endpoint common to all ports.
2023 */
2024 port->interrupt_in_endpointAddress =
2025 port->serial->port[0]->interrupt_in_endpointAddress;
2026 mos7720_port->port = port;
2027
2028 usb_set_serial_port_data(port, mos7720_port);
2029
2030 return 0;
2031}
2032
2033static int mos7720_port_remove(struct usb_serial_port *port)
2034{
2035 struct moschip_port *mos7720_port;
2036
2037 mos7720_port = usb_get_serial_port_data(port);
2038 kfree(mos7720_port);
2039
2040 return 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002041}
2042
2043static struct usb_serial_driver moschip7720_2port_driver = {
2044 .driver = {
2045 .owner = THIS_MODULE,
2046 .name = "moschip7720",
2047 },
2048 .description = "Moschip 2 port adapter",
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002049 .id_table = id_table,
Mike Dunnfb088e32010-01-26 12:12:12 -05002050 .calc_num_ports = mos77xx_calc_num_ports,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002051 .open = mos7720_open,
2052 .close = mos7720_close,
2053 .throttle = mos7720_throttle,
2054 .unthrottle = mos7720_unthrottle,
Mike Dunnfb088e32010-01-26 12:12:12 -05002055 .probe = mos77xx_probe,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002056 .attach = mos7720_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002057 .release = mos7720_release,
Johan Hovold4230af52012-10-25 10:29:05 +02002058 .port_probe = mos7720_port_probe,
2059 .port_remove = mos7720_port_remove,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002060 .ioctl = mos7720_ioctl,
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07002061 .tiocmget = mos7720_tiocmget,
2062 .tiocmset = mos7720_tiocmset,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002063 .set_termios = mos7720_set_termios,
2064 .write = mos7720_write,
2065 .write_room = mos7720_write_room,
2066 .chars_in_buffer = mos7720_chars_in_buffer,
2067 .break_ctl = mos7720_break,
2068 .read_bulk_callback = mos7720_bulk_in_callback,
Mike Dunnfb088e32010-01-26 12:12:12 -05002069 .read_int_callback = NULL /* dynamically assigned in probe() */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002070};
2071
Alan Stern4d2a7af2012-02-23 14:57:09 -05002072static struct usb_serial_driver * const serial_drivers[] = {
2073 &moschip7720_2port_driver, NULL
2074};
2075
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002076module_usb_serial_driver(serial_drivers, id_table);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002077
Alan Cox4da1a172008-07-22 11:16:21 +01002078MODULE_AUTHOR(DRIVER_AUTHOR);
2079MODULE_DESCRIPTION(DRIVER_DESC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002080MODULE_LICENSE("GPL");