blob: 7752cffbf2bc3ea24e72d91579b50f0c33c72220 [file] [log] [blame]
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001/*
2 * mos7720.c
3 * Controls the Moschip 7720 usb to dual port serial convertor
4 *
5 * Copyright 2006 Moschip Semiconductor Tech. Ltd.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, version 2 of the License.
10 *
11 * Developed by:
Greg Kroah-Hartman50d2dc72007-06-25 01:08:01 -070012 * Vijaya Kumar <vijaykumar.gn@gmail.com>
13 * Ajay Kumar <naanuajay@yahoo.com>
14 * Gurudeva <ngurudeva@yahoo.com>
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070015 *
16 * Cleaned up from the original by:
17 * Greg Kroah-Hartman <gregkh@suse.de>
18 *
19 * Originally based on drivers/usb/serial/io_edgeport.c which is:
20 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
21 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
22 */
23#include <linux/kernel.h>
24#include <linux/errno.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/tty.h>
28#include <linux/tty_driver.h>
29#include <linux/tty_flip.h>
30#include <linux/module.h>
31#include <linux/spinlock.h>
32#include <linux/serial.h>
33#include <linux/serial_reg.h>
34#include <linux/usb.h>
35#include <linux/usb/serial.h>
Alan Cox4da1a172008-07-22 11:16:21 +010036#include <linux/uaccess.h>
Mike Dunnb69578d2010-04-15 17:01:33 -040037#include <linux/parport.h>
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070038
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070039#define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
40#define DRIVER_DESC "Moschip USB Serial Driver"
41
42/* default urb timeout */
43#define MOS_WDR_TIMEOUT (HZ * 5)
44
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070045#define MOS_MAX_PORT 0x02
46#define MOS_WRITE 0x0E
47#define MOS_READ 0x0D
48
49/* Interrupt Rotinue Defines */
50#define SERIAL_IIR_RLS 0x06
51#define SERIAL_IIR_RDA 0x04
52#define SERIAL_IIR_CTI 0x0c
53#define SERIAL_IIR_THR 0x02
54#define SERIAL_IIR_MS 0x00
55
56#define NUM_URBS 16 /* URB Count */
57#define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
58
Mike Dunnb69578d2010-04-15 17:01:33 -040059/* This structure holds all of the local serial port information */
Alan Cox4da1a172008-07-22 11:16:21 +010060struct moschip_port {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070061 __u8 shadowLCR; /* last LCR value received */
62 __u8 shadowMCR; /* last MCR value received */
63 __u8 shadowMSR; /* last MSR value received */
64 char open;
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;
93};
94
95enum mos7715_pp_modes {
96 SPP = 0<<5,
97 PS2 = 1<<5, /* moschip calls this 'NIBBLE' mode */
98 PPF = 2<<5, /* moschip calls this 'CB-FIFO mode */
99};
100
101struct mos7715_parport {
102 struct parport *pp; /* back to containing struct */
103 struct kref ref_count; /* to instance of this struct */
104 struct list_head deferred_urbs; /* list deferred async urbs */
105 struct list_head active_urbs; /* list async urbs in flight */
106 spinlock_t listlock; /* protects list access */
107 bool msg_pending; /* usb sync call pending */
108 struct completion syncmsg_compl; /* usb sync call completed */
109 struct tasklet_struct urb_tasklet; /* for sending deferred urbs */
110 struct usb_serial *serial; /* back to containing struct */
111 __u8 shadowECR; /* parallel port regs... */
112 __u8 shadowDCR;
113 atomic_t shadowDSR; /* updated in int-in callback */
114};
115
116/* lock guards against dereferencing NULL ptr in parport ops callbacks */
117static DEFINE_SPINLOCK(release_lock);
118
Mike Dunn63b91762010-04-15 17:02:09 -0400119#endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
120
121static const unsigned int dummy; /* for clarity in register access fns */
122
Mike Dunnb69578d2010-04-15 17:01:33 -0400123enum mos_regs {
124 THR, /* serial port regs */
125 RHR,
126 IER,
127 FCR,
128 ISR,
129 LCR,
130 MCR,
131 LSR,
132 MSR,
133 SPR,
134 DLL,
135 DLM,
136 DPR, /* parallel port regs */
137 DSR,
138 DCR,
139 ECR,
140 SP1_REG, /* device control regs */
141 SP2_REG, /* serial port 2 (7720 only) */
142 PP_REG,
143 SP_CONTROL_REG,
144};
145
146/*
147 * Return the correct value for the Windex field of the setup packet
148 * for a control endpoint message. See the 7715 datasheet.
149 */
150static inline __u16 get_reg_index(enum mos_regs reg)
151{
152 static const __u16 mos7715_index_lookup_table[] = {
153 0x00, /* THR */
154 0x00, /* RHR */
155 0x01, /* IER */
156 0x02, /* FCR */
157 0x02, /* ISR */
158 0x03, /* LCR */
159 0x04, /* MCR */
160 0x05, /* LSR */
161 0x06, /* MSR */
162 0x07, /* SPR */
163 0x00, /* DLL */
164 0x01, /* DLM */
165 0x00, /* DPR */
166 0x01, /* DSR */
167 0x02, /* DCR */
168 0x0a, /* ECR */
169 0x01, /* SP1_REG */
170 0x02, /* SP2_REG (7720 only) */
171 0x04, /* PP_REG (7715 only) */
172 0x08, /* SP_CONTROL_REG */
173 };
174 return mos7715_index_lookup_table[reg];
175}
176
177/*
178 * Return the correct value for the upper byte of the Wvalue field of
179 * the setup packet for a control endpoint message.
180 */
Mike Dunn63b91762010-04-15 17:02:09 -0400181static inline __u16 get_reg_value(enum mos_regs reg,
182 unsigned int serial_portnum)
Mike Dunnb69578d2010-04-15 17:01:33 -0400183{
184 if (reg >= SP1_REG) /* control reg */
185 return 0x0000;
Mike Dunn63b91762010-04-15 17:02:09 -0400186
187 else if (reg >= DPR) /* parallel port reg (7715 only) */
Mike Dunnb69578d2010-04-15 17:01:33 -0400188 return 0x0100;
Mike Dunn63b91762010-04-15 17:02:09 -0400189
190 else /* serial port reg */
191 return (serial_portnum + 2) << 8;
Mike Dunnb69578d2010-04-15 17:01:33 -0400192}
193
194/*
195 * Write data byte to the specified device register. The data is embedded in
Mike Dunn63b91762010-04-15 17:02:09 -0400196 * the value field of the setup packet. serial_portnum is ignored for registers
197 * not specific to a particular serial port.
Mike Dunnb69578d2010-04-15 17:01:33 -0400198 */
Mike Dunn63b91762010-04-15 17:02:09 -0400199static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
200 enum mos_regs reg, __u8 data)
Mike Dunnb69578d2010-04-15 17:01:33 -0400201{
Mike Dunnb69578d2010-04-15 17:01:33 -0400202 struct usb_device *usbdev = serial->dev;
203 unsigned int pipe = usb_sndctrlpipe(usbdev, 0);
204 __u8 request = (__u8)0x0e;
205 __u8 requesttype = (__u8)0x40;
Mike Dunnb69578d2010-04-15 17:01:33 -0400206 __u16 index = get_reg_index(reg);
Mike Dunn63b91762010-04-15 17:02:09 -0400207 __u16 value = get_reg_value(reg, serial_portnum) + data;
208 int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
209 index, NULL, 0, MOS_WDR_TIMEOUT);
Mike Dunnb69578d2010-04-15 17:01:33 -0400210 if (status < 0)
211 dev_err(&usbdev->dev,
212 "mos7720: usb_control_msg() failed: %d", status);
213 return status;
214}
215
216/*
217 * Read data byte from the specified device register. The data returned by the
Mike Dunn63b91762010-04-15 17:02:09 -0400218 * device is embedded in the value field of the setup packet. serial_portnum is
219 * ignored for registers that are not specific to a particular serial port.
Mike Dunnb69578d2010-04-15 17:01:33 -0400220 */
Mike Dunn63b91762010-04-15 17:02:09 -0400221static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
222 enum mos_regs reg, __u8 *data)
Mike Dunnb69578d2010-04-15 17:01:33 -0400223{
Mike Dunn63b91762010-04-15 17:02:09 -0400224 struct usb_device *usbdev = serial->dev;
Mike Dunnb69578d2010-04-15 17:01:33 -0400225 unsigned int pipe = usb_rcvctrlpipe(usbdev, 0);
226 __u8 request = (__u8)0x0d;
227 __u8 requesttype = (__u8)0xc0;
Mike Dunnb69578d2010-04-15 17:01:33 -0400228 __u16 index = get_reg_index(reg);
Mike Dunn63b91762010-04-15 17:02:09 -0400229 __u16 value = get_reg_value(reg, serial_portnum);
Johan Hovold72ea18a2013-05-27 14:44:39 +0200230 u8 *buf;
231 int status;
232
233 buf = kmalloc(1, GFP_KERNEL);
234 if (!buf)
235 return -ENOMEM;
236
237 status = usb_control_msg(usbdev, pipe, request, requesttype, value,
238 index, buf, 1, MOS_WDR_TIMEOUT);
239 if (status == 1)
240 *data = *buf;
241 else if (status < 0)
Mike Dunnb69578d2010-04-15 17:01:33 -0400242 dev_err(&usbdev->dev,
243 "mos7720: usb_control_msg() failed: %d", status);
Johan Hovold72ea18a2013-05-27 14:44:39 +0200244 kfree(buf);
245
Mike Dunnb69578d2010-04-15 17:01:33 -0400246 return status;
247}
248
Mike Dunn63b91762010-04-15 17:02:09 -0400249#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
250
Mike Dunnb69578d2010-04-15 17:01:33 -0400251static inline int mos7715_change_mode(struct mos7715_parport *mos_parport,
252 enum mos7715_pp_modes mode)
253{
254 mos_parport->shadowECR = mode;
Mike Dunn63b91762010-04-15 17:02:09 -0400255 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400256 return 0;
257}
258
259static void destroy_mos_parport(struct kref *kref)
260{
261 struct mos7715_parport *mos_parport =
262 container_of(kref, struct mos7715_parport, ref_count);
263
Mike Dunnb69578d2010-04-15 17:01:33 -0400264 kfree(mos_parport);
265}
266
267static void destroy_urbtracker(struct kref *kref)
268{
269 struct urbtracker *urbtrack =
270 container_of(kref, struct urbtracker, ref_count);
271 struct mos7715_parport *mos_parport = urbtrack->mos_parport;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700272
Mike Dunnb69578d2010-04-15 17:01:33 -0400273 usb_free_urb(urbtrack->urb);
274 kfree(urbtrack);
275 kref_put(&mos_parport->ref_count, destroy_mos_parport);
276}
277
278/*
279 * This runs as a tasklet when sending an urb in a non-blocking parallel
280 * port callback had to be deferred because the disconnect mutex could not be
281 * obtained at the time.
282 */
283static void send_deferred_urbs(unsigned long _mos_parport)
284{
285 int ret_val;
286 unsigned long flags;
287 struct mos7715_parport *mos_parport = (void *)_mos_parport;
Wei Yongjun67990472012-08-21 11:28:45 +0800288 struct urbtracker *urbtrack, *tmp;
Mike Dunnb69578d2010-04-15 17:01:33 -0400289 struct list_head *cursor, *next;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700290 struct device *dev;
Mike Dunnb69578d2010-04-15 17:01:33 -0400291
Mike Dunnb69578d2010-04-15 17:01:33 -0400292 /* if release function ran, game over */
293 if (unlikely(mos_parport->serial == NULL))
294 return;
295
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700296 dev = &mos_parport->serial->dev->dev;
297
Mike Dunnb69578d2010-04-15 17:01:33 -0400298 /* try again to get the mutex */
299 if (!mutex_trylock(&mos_parport->serial->disc_mutex)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700300 dev_dbg(dev, "%s: rescheduling tasklet\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400301 tasklet_schedule(&mos_parport->urb_tasklet);
302 return;
303 }
304
305 /* if device disconnected, game over */
306 if (unlikely(mos_parport->serial->disconnected)) {
307 mutex_unlock(&mos_parport->serial->disc_mutex);
308 return;
309 }
310
311 spin_lock_irqsave(&mos_parport->listlock, flags);
312 if (list_empty(&mos_parport->deferred_urbs)) {
313 spin_unlock_irqrestore(&mos_parport->listlock, flags);
314 mutex_unlock(&mos_parport->serial->disc_mutex);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700315 dev_dbg(dev, "%s: deferred_urbs list empty\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400316 return;
317 }
318
319 /* move contents of deferred_urbs list to active_urbs list and submit */
320 list_for_each_safe(cursor, next, &mos_parport->deferred_urbs)
321 list_move_tail(cursor, &mos_parport->active_urbs);
Wei Yongjun67990472012-08-21 11:28:45 +0800322 list_for_each_entry_safe(urbtrack, tmp, &mos_parport->active_urbs,
Mike Dunnb69578d2010-04-15 17:01:33 -0400323 urblist_entry) {
324 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700325 dev_dbg(dev, "%s: urb submitted\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400326 if (ret_val) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700327 dev_err(dev, "usb_submit_urb() failed: %d\n", ret_val);
Mike Dunnb69578d2010-04-15 17:01:33 -0400328 list_del(&urbtrack->urblist_entry);
329 kref_put(&urbtrack->ref_count, destroy_urbtracker);
330 }
331 }
332 spin_unlock_irqrestore(&mos_parport->listlock, flags);
333 mutex_unlock(&mos_parport->serial->disc_mutex);
334}
335
336/* callback for parallel port control urbs submitted asynchronously */
337static void async_complete(struct urb *urb)
338{
339 struct urbtracker *urbtrack = urb->context;
340 int status = urb->status;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700341
Mike Dunnb69578d2010-04-15 17:01:33 -0400342 if (unlikely(status))
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700343 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
Mike Dunnb69578d2010-04-15 17:01:33 -0400344
345 /* remove the urbtracker from the active_urbs list */
346 spin_lock(&urbtrack->mos_parport->listlock);
347 list_del(&urbtrack->urblist_entry);
348 spin_unlock(&urbtrack->mos_parport->listlock);
349 kref_put(&urbtrack->ref_count, destroy_urbtracker);
350}
351
352static int write_parport_reg_nonblock(struct mos7715_parport *mos_parport,
353 enum mos_regs reg, __u8 data)
354{
355 struct urbtracker *urbtrack;
356 int ret_val;
357 unsigned long flags;
358 struct usb_ctrlrequest setup;
359 struct usb_serial *serial = mos_parport->serial;
360 struct usb_device *usbdev = serial->dev;
Mike Dunnb69578d2010-04-15 17:01:33 -0400361
362 /* create and initialize the control urb and containing urbtracker */
363 urbtrack = kmalloc(sizeof(struct urbtracker), GFP_ATOMIC);
364 if (urbtrack == NULL) {
365 dev_err(&usbdev->dev, "out of memory");
366 return -ENOMEM;
367 }
368 kref_get(&mos_parport->ref_count);
369 urbtrack->mos_parport = mos_parport;
370 urbtrack->urb = usb_alloc_urb(0, GFP_ATOMIC);
371 if (urbtrack->urb == NULL) {
372 dev_err(&usbdev->dev, "out of urbs");
373 kfree(urbtrack);
374 return -ENOMEM;
375 }
376 setup.bRequestType = (__u8)0x40;
377 setup.bRequest = (__u8)0x0e;
Mike Dunn63b91762010-04-15 17:02:09 -0400378 setup.wValue = get_reg_value(reg, dummy);
Mike Dunnb69578d2010-04-15 17:01:33 -0400379 setup.wIndex = get_reg_index(reg);
380 setup.wLength = 0;
381 usb_fill_control_urb(urbtrack->urb, usbdev,
382 usb_sndctrlpipe(usbdev, 0),
383 (unsigned char *)&setup,
384 NULL, 0, async_complete, urbtrack);
385 kref_init(&urbtrack->ref_count);
386 INIT_LIST_HEAD(&urbtrack->urblist_entry);
387
388 /*
389 * get the disconnect mutex, or add tracker to the deferred_urbs list
390 * and schedule a tasklet to try again later
391 */
392 if (!mutex_trylock(&serial->disc_mutex)) {
393 spin_lock_irqsave(&mos_parport->listlock, flags);
394 list_add_tail(&urbtrack->urblist_entry,
395 &mos_parport->deferred_urbs);
396 spin_unlock_irqrestore(&mos_parport->listlock, flags);
397 tasklet_schedule(&mos_parport->urb_tasklet);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700398 dev_dbg(&usbdev->dev, "tasklet scheduled");
Mike Dunnb69578d2010-04-15 17:01:33 -0400399 return 0;
400 }
401
402 /* bail if device disconnected */
403 if (serial->disconnected) {
404 kref_put(&urbtrack->ref_count, destroy_urbtracker);
405 mutex_unlock(&serial->disc_mutex);
406 return -ENODEV;
407 }
408
409 /* add the tracker to the active_urbs list and submit */
410 spin_lock_irqsave(&mos_parport->listlock, flags);
411 list_add_tail(&urbtrack->urblist_entry, &mos_parport->active_urbs);
412 spin_unlock_irqrestore(&mos_parport->listlock, flags);
413 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
414 mutex_unlock(&serial->disc_mutex);
415 if (ret_val) {
416 dev_err(&usbdev->dev,
417 "%s: submit_urb() failed: %d", __func__, ret_val);
418 spin_lock_irqsave(&mos_parport->listlock, flags);
419 list_del(&urbtrack->urblist_entry);
420 spin_unlock_irqrestore(&mos_parport->listlock, flags);
421 kref_put(&urbtrack->ref_count, destroy_urbtracker);
422 return ret_val;
423 }
424 return 0;
425}
426
427/*
428 * This is the the common top part of all parallel port callback operations that
429 * send synchronous messages to the device. This implements convoluted locking
430 * that avoids two scenarios: (1) a port operation is called after usbserial
431 * has called our release function, at which point struct mos7715_parport has
432 * been destroyed, and (2) the device has been disconnected, but usbserial has
433 * not called the release function yet because someone has a serial port open.
434 * The shared release_lock prevents the first, and the mutex and disconnected
435 * flag maintained by usbserial covers the second. We also use the msg_pending
436 * flag to ensure that all synchronous usb messgage calls have completed before
437 * our release function can return.
438 */
439static int parport_prologue(struct parport *pp)
440{
441 struct mos7715_parport *mos_parport;
442
443 spin_lock(&release_lock);
444 mos_parport = pp->private_data;
445 if (unlikely(mos_parport == NULL)) {
446 /* release fn called, port struct destroyed */
447 spin_unlock(&release_lock);
448 return -1;
449 }
450 mos_parport->msg_pending = true; /* synch usb call pending */
451 INIT_COMPLETION(mos_parport->syncmsg_compl);
452 spin_unlock(&release_lock);
453
454 mutex_lock(&mos_parport->serial->disc_mutex);
455 if (mos_parport->serial->disconnected) {
456 /* device disconnected */
457 mutex_unlock(&mos_parport->serial->disc_mutex);
458 mos_parport->msg_pending = false;
459 complete(&mos_parport->syncmsg_compl);
460 return -1;
461 }
462
463 return 0;
464}
465
466/*
467 * This is the the common bottom part of all parallel port functions that send
468 * synchronous messages to the device.
469 */
470static inline void parport_epilogue(struct parport *pp)
471{
472 struct mos7715_parport *mos_parport = pp->private_data;
473 mutex_unlock(&mos_parport->serial->disc_mutex);
474 mos_parport->msg_pending = false;
475 complete(&mos_parport->syncmsg_compl);
476}
477
478static void parport_mos7715_write_data(struct parport *pp, unsigned char d)
479{
480 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700481
Mike Dunnb69578d2010-04-15 17:01:33 -0400482 if (parport_prologue(pp) < 0)
483 return;
484 mos7715_change_mode(mos_parport, SPP);
Mike Dunn63b91762010-04-15 17:02:09 -0400485 write_mos_reg(mos_parport->serial, dummy, DPR, (__u8)d);
Mike Dunnb69578d2010-04-15 17:01:33 -0400486 parport_epilogue(pp);
487}
488
489static unsigned char parport_mos7715_read_data(struct parport *pp)
490{
491 struct mos7715_parport *mos_parport = pp->private_data;
492 unsigned char d;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700493
Mike Dunnb69578d2010-04-15 17:01:33 -0400494 if (parport_prologue(pp) < 0)
495 return 0;
Mike Dunn63b91762010-04-15 17:02:09 -0400496 read_mos_reg(mos_parport->serial, dummy, DPR, &d);
Mike Dunnb69578d2010-04-15 17:01:33 -0400497 parport_epilogue(pp);
498 return d;
499}
500
501static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
502{
503 struct mos7715_parport *mos_parport = pp->private_data;
504 __u8 data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700505
Mike Dunnb69578d2010-04-15 17:01:33 -0400506 if (parport_prologue(pp) < 0)
507 return;
508 data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
Mike Dunn63b91762010-04-15 17:02:09 -0400509 write_mos_reg(mos_parport->serial, dummy, DCR, data);
Mike Dunnb69578d2010-04-15 17:01:33 -0400510 mos_parport->shadowDCR = data;
511 parport_epilogue(pp);
512}
513
514static unsigned char parport_mos7715_read_control(struct parport *pp)
515{
516 struct mos7715_parport *mos_parport = pp->private_data;
517 __u8 dcr;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700518
Mike Dunnb69578d2010-04-15 17:01:33 -0400519 spin_lock(&release_lock);
520 mos_parport = pp->private_data;
521 if (unlikely(mos_parport == NULL)) {
522 spin_unlock(&release_lock);
523 return 0;
524 }
525 dcr = mos_parport->shadowDCR & 0x0f;
526 spin_unlock(&release_lock);
527 return dcr;
528}
529
530static unsigned char parport_mos7715_frob_control(struct parport *pp,
531 unsigned char mask,
532 unsigned char val)
533{
534 struct mos7715_parport *mos_parport = pp->private_data;
535 __u8 dcr;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700536
Mike Dunnb69578d2010-04-15 17:01:33 -0400537 mask &= 0x0f;
538 val &= 0x0f;
539 if (parport_prologue(pp) < 0)
540 return 0;
541 mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
Mike Dunn63b91762010-04-15 17:02:09 -0400542 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400543 dcr = mos_parport->shadowDCR & 0x0f;
544 parport_epilogue(pp);
545 return dcr;
546}
547
548static unsigned char parport_mos7715_read_status(struct parport *pp)
549{
550 unsigned char status;
551 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700552
Mike Dunnb69578d2010-04-15 17:01:33 -0400553 spin_lock(&release_lock);
554 mos_parport = pp->private_data;
555 if (unlikely(mos_parport == NULL)) { /* release called */
556 spin_unlock(&release_lock);
557 return 0;
558 }
559 status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
560 spin_unlock(&release_lock);
561 return status;
562}
563
564static void parport_mos7715_enable_irq(struct parport *pp)
565{
Mike Dunnb69578d2010-04-15 17:01:33 -0400566}
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700567
Mike Dunnb69578d2010-04-15 17:01:33 -0400568static void parport_mos7715_disable_irq(struct parport *pp)
569{
Mike Dunnb69578d2010-04-15 17:01:33 -0400570}
571
572static void parport_mos7715_data_forward(struct parport *pp)
573{
574 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700575
Mike Dunnb69578d2010-04-15 17:01:33 -0400576 if (parport_prologue(pp) < 0)
577 return;
578 mos7715_change_mode(mos_parport, PS2);
579 mos_parport->shadowDCR &= ~0x20;
Mike Dunn63b91762010-04-15 17:02:09 -0400580 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400581 parport_epilogue(pp);
582}
583
584static void parport_mos7715_data_reverse(struct parport *pp)
585{
586 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700587
Mike Dunnb69578d2010-04-15 17:01:33 -0400588 if (parport_prologue(pp) < 0)
589 return;
590 mos7715_change_mode(mos_parport, PS2);
591 mos_parport->shadowDCR |= 0x20;
Mike Dunn63b91762010-04-15 17:02:09 -0400592 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400593 parport_epilogue(pp);
594}
595
596static void parport_mos7715_init_state(struct pardevice *dev,
597 struct parport_state *s)
598{
Mike Dunnb69578d2010-04-15 17:01:33 -0400599 s->u.pc.ctr = DCR_INIT_VAL;
600 s->u.pc.ecr = ECR_INIT_VAL;
601}
602
603/* N.B. Parport core code requires that this function not block */
604static void parport_mos7715_save_state(struct parport *pp,
605 struct parport_state *s)
606{
607 struct mos7715_parport *mos_parport;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700608
Mike Dunnb69578d2010-04-15 17:01:33 -0400609 spin_lock(&release_lock);
610 mos_parport = pp->private_data;
611 if (unlikely(mos_parport == NULL)) { /* release called */
612 spin_unlock(&release_lock);
613 return;
614 }
615 s->u.pc.ctr = mos_parport->shadowDCR;
616 s->u.pc.ecr = mos_parport->shadowECR;
617 spin_unlock(&release_lock);
618}
619
620/* N.B. Parport core code requires that this function not block */
621static void parport_mos7715_restore_state(struct parport *pp,
622 struct parport_state *s)
623{
624 struct mos7715_parport *mos_parport;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700625
Mike Dunnb69578d2010-04-15 17:01:33 -0400626 spin_lock(&release_lock);
627 mos_parport = pp->private_data;
628 if (unlikely(mos_parport == NULL)) { /* release called */
629 spin_unlock(&release_lock);
630 return;
631 }
632 write_parport_reg_nonblock(mos_parport, DCR, mos_parport->shadowDCR);
633 write_parport_reg_nonblock(mos_parport, ECR, mos_parport->shadowECR);
634 spin_unlock(&release_lock);
635}
636
637static size_t parport_mos7715_write_compat(struct parport *pp,
638 const void *buffer,
639 size_t len, int flags)
640{
641 int retval;
642 struct mos7715_parport *mos_parport = pp->private_data;
643 int actual_len;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700644
Mike Dunnb69578d2010-04-15 17:01:33 -0400645 if (parport_prologue(pp) < 0)
646 return 0;
647 mos7715_change_mode(mos_parport, PPF);
648 retval = usb_bulk_msg(mos_parport->serial->dev,
649 usb_sndbulkpipe(mos_parport->serial->dev, 2),
650 (void *)buffer, len, &actual_len,
651 MOS_WDR_TIMEOUT);
652 parport_epilogue(pp);
653 if (retval) {
654 dev_err(&mos_parport->serial->dev->dev,
655 "mos7720: usb_bulk_msg() failed: %d", retval);
656 return 0;
657 }
658 return actual_len;
659}
660
661static struct parport_operations parport_mos7715_ops = {
662 .owner = THIS_MODULE,
663 .write_data = parport_mos7715_write_data,
664 .read_data = parport_mos7715_read_data,
665
666 .write_control = parport_mos7715_write_control,
667 .read_control = parport_mos7715_read_control,
668 .frob_control = parport_mos7715_frob_control,
669
670 .read_status = parport_mos7715_read_status,
671
672 .enable_irq = parport_mos7715_enable_irq,
673 .disable_irq = parport_mos7715_disable_irq,
674
675 .data_forward = parport_mos7715_data_forward,
676 .data_reverse = parport_mos7715_data_reverse,
677
678 .init_state = parport_mos7715_init_state,
679 .save_state = parport_mos7715_save_state,
680 .restore_state = parport_mos7715_restore_state,
681
682 .compat_write_data = parport_mos7715_write_compat,
683
684 .nibble_read_data = parport_ieee1284_read_nibble,
685 .byte_read_data = parport_ieee1284_read_byte,
686};
687
688/*
689 * Allocate and initialize parallel port control struct, initialize
690 * the parallel port hardware device, and register with the parport subsystem.
691 */
692static int mos7715_parport_init(struct usb_serial *serial)
693{
694 struct mos7715_parport *mos_parport;
695
696 /* allocate and initialize parallel port control struct */
697 mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
698 if (mos_parport == NULL) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700699 dev_dbg(&serial->dev->dev, "%s: kzalloc failed\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400700 return -ENOMEM;
701 }
702 mos_parport->msg_pending = false;
703 kref_init(&mos_parport->ref_count);
704 spin_lock_init(&mos_parport->listlock);
705 INIT_LIST_HEAD(&mos_parport->active_urbs);
706 INIT_LIST_HEAD(&mos_parport->deferred_urbs);
707 usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
708 mos_parport->serial = serial;
709 tasklet_init(&mos_parport->urb_tasklet, send_deferred_urbs,
710 (unsigned long) mos_parport);
711 init_completion(&mos_parport->syncmsg_compl);
712
713 /* cycle parallel port reset bit */
Mike Dunn63b91762010-04-15 17:02:09 -0400714 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x80);
715 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x00);
Mike Dunnb69578d2010-04-15 17:01:33 -0400716
717 /* initialize device registers */
718 mos_parport->shadowDCR = DCR_INIT_VAL;
Mike Dunn63b91762010-04-15 17:02:09 -0400719 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400720 mos_parport->shadowECR = ECR_INIT_VAL;
Mike Dunn63b91762010-04-15 17:02:09 -0400721 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400722
723 /* register with parport core */
724 mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE,
725 PARPORT_DMA_NONE,
726 &parport_mos7715_ops);
727 if (mos_parport->pp == NULL) {
728 dev_err(&serial->interface->dev,
729 "Could not register parport\n");
730 kref_put(&mos_parport->ref_count, destroy_mos_parport);
731 return -EIO;
732 }
733 mos_parport->pp->private_data = mos_parport;
734 mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP;
735 mos_parport->pp->dev = &serial->interface->dev;
736 parport_announce_port(mos_parport->pp);
737
738 return 0;
739}
740#endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700741
742/*
743 * mos7720_interrupt_callback
744 * this is the callback function for when we have received data on the
745 * interrupt endpoint.
746 */
747static void mos7720_interrupt_callback(struct urb *urb)
748{
749 int result;
750 int length;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700751 int status = urb->status;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700752 struct device *dev = &urb->dev->dev;
Oliver Neukum325b70c2007-03-19 13:58:29 +0100753 __u8 *data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700754 __u8 sp1;
755 __u8 sp2;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700756
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700757 switch (status) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700758 case 0:
759 /* success */
760 break;
761 case -ECONNRESET:
762 case -ENOENT:
763 case -ESHUTDOWN:
764 /* this urb is terminated, clean up */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700765 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700766 return;
767 default:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700768 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700769 goto exit;
770 }
771
772 length = urb->actual_length;
773 data = urb->transfer_buffer;
774
775 /* Moschip get 4 bytes
776 * Byte 1 IIR Port 1 (port.number is 0)
777 * Byte 2 IIR Port 2 (port.number is 1)
778 * Byte 3 --------------
779 * Byte 4 FIFO status for both */
Oliver Neukum325b70c2007-03-19 13:58:29 +0100780
781 /* the above description is inverted
782 * oneukum 2007-03-14 */
783
784 if (unlikely(length != 4)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700785 dev_dbg(dev, "Wrong data !!!\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700786 return;
787 }
788
Oliver Neukum325b70c2007-03-19 13:58:29 +0100789 sp1 = data[3];
790 sp2 = data[2];
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700791
Oliver Neukum325b70c2007-03-19 13:58:29 +0100792 if ((sp1 | sp2) & 0x01) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700793 /* No Interrupt Pending in both the ports */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700794 dev_dbg(dev, "No Interrupt !!!\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700795 } else {
796 switch (sp1 & 0x0f) {
797 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700798 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 -0700799 break;
800 case SERIAL_IIR_CTI:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700801 dev_dbg(dev, "Serial Port 1: Receiver time out\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700802 break;
803 case SERIAL_IIR_MS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700804 /* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700805 break;
806 }
807
808 switch (sp2 & 0x0f) {
809 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700810 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 -0700811 break;
812 case SERIAL_IIR_CTI:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700813 dev_dbg(dev, "Serial Port 2: Receiver time out\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700814 break;
815 case SERIAL_IIR_MS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700816 /* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700817 break;
818 }
819 }
820
821exit:
822 result = usb_submit_urb(urb, GFP_ATOMIC);
823 if (result)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700824 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700825}
826
827/*
Mike Dunnfb088e32010-01-26 12:12:12 -0500828 * mos7715_interrupt_callback
829 * this is the 7715's callback function for when we have received data on
830 * the interrupt endpoint.
831 */
832static void mos7715_interrupt_callback(struct urb *urb)
833{
834 int result;
835 int length;
836 int status = urb->status;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700837 struct device *dev = &urb->dev->dev;
Mike Dunnfb088e32010-01-26 12:12:12 -0500838 __u8 *data;
839 __u8 iir;
840
841 switch (status) {
842 case 0:
843 /* success */
844 break;
845 case -ECONNRESET:
846 case -ENOENT:
847 case -ESHUTDOWN:
Mike Dunnb69578d2010-04-15 17:01:33 -0400848 case -ENODEV:
Mike Dunnfb088e32010-01-26 12:12:12 -0500849 /* this urb is terminated, clean up */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700850 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
Mike Dunnfb088e32010-01-26 12:12:12 -0500851 return;
852 default:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700853 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
Mike Dunnfb088e32010-01-26 12:12:12 -0500854 goto exit;
855 }
856
857 length = urb->actual_length;
858 data = urb->transfer_buffer;
859
860 /* Structure of data from 7715 device:
861 * Byte 1: IIR serial Port
862 * Byte 2: unused
863 * Byte 2: DSR parallel port
864 * Byte 4: FIFO status for both */
865
866 if (unlikely(length != 4)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700867 dev_dbg(dev, "Wrong data !!!\n");
Mike Dunnfb088e32010-01-26 12:12:12 -0500868 return;
869 }
870
871 iir = data[0];
872 if (!(iir & 0x01)) { /* serial port interrupt pending */
873 switch (iir & 0x0f) {
874 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700875 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 -0500876 break;
877 case SERIAL_IIR_CTI:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700878 dev_dbg(dev, "Serial Port: Receiver time out\n");
Mike Dunnfb088e32010-01-26 12:12:12 -0500879 break;
880 case SERIAL_IIR_MS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700881 /* dev_dbg(dev, "Serial Port: Modem status change\n"); */
Mike Dunnfb088e32010-01-26 12:12:12 -0500882 break;
883 }
884 }
885
Mike Dunnb69578d2010-04-15 17:01:33 -0400886#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
887 { /* update local copy of DSR reg */
888 struct usb_serial_port *port = urb->context;
889 struct mos7715_parport *mos_parport = port->serial->private;
890 if (unlikely(mos_parport == NULL))
891 return;
892 atomic_set(&mos_parport->shadowDSR, data[2]);
893 }
894#endif
895
Mike Dunnfb088e32010-01-26 12:12:12 -0500896exit:
897 result = usb_submit_urb(urb, GFP_ATOMIC);
898 if (result)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700899 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
Mike Dunnfb088e32010-01-26 12:12:12 -0500900}
901
902/*
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700903 * mos7720_bulk_in_callback
904 * this is the callback function for when we have received data on the
905 * bulk in endpoint.
906 */
907static void mos7720_bulk_in_callback(struct urb *urb)
908{
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700909 int retval;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700910 unsigned char *data ;
911 struct usb_serial_port *port;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700912 int status = urb->status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700913
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700914 if (status) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700915 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700916 return;
917 }
918
Mike Dunnb69578d2010-04-15 17:01:33 -0400919 port = urb->context;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700920
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700921 dev_dbg(&port->dev, "Entering...%s\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700922
923 data = urb->transfer_buffer;
924
Jiri Slaby2e124b42013-01-03 15:53:06 +0100925 if (urb->actual_length) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100926 tty_insert_flip_string(&port->port, data, urb->actual_length);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100927 tty_flip_buffer_push(&port->port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700928 }
929
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700930 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700931 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
932 if (retval)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700933 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700934 }
935}
936
937/*
938 * mos7720_bulk_out_data_callback
939 * this is the callback function for when we have finished sending serial
940 * data on the bulk out endpoint.
941 */
942static void mos7720_bulk_out_data_callback(struct urb *urb)
943{
944 struct moschip_port *mos7720_port;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700945 int status = urb->status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700946
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700947 if (status) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700948 dev_dbg(&urb->dev->dev, "nonzero write bulk status received:%d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700949 return;
950 }
951
952 mos7720_port = urb->context;
953 if (!mos7720_port) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700954 dev_dbg(&urb->dev->dev, "NULL mos7720_port pointer\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700955 return ;
956 }
957
Jiri Slaby6aad04f2013-03-07 13:12:29 +0100958 if (mos7720_port->open)
959 tty_port_tty_wakeup(&mos7720_port->port->port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700960}
961
962/*
Mike Dunnfb088e32010-01-26 12:12:12 -0500963 * mos77xx_probe
964 * this function installs the appropriate read interrupt endpoint callback
965 * depending on whether the device is a 7720 or 7715, thus avoiding costly
966 * run-time checks in the high-frequency callback routine itself.
967 */
968static int mos77xx_probe(struct usb_serial *serial,
969 const struct usb_device_id *id)
970{
971 if (id->idProduct == MOSCHIP_DEVICE_ID_7715)
972 moschip7720_2port_driver.read_int_callback =
973 mos7715_interrupt_callback;
974 else
975 moschip7720_2port_driver.read_int_callback =
976 mos7720_interrupt_callback;
977
978 return 0;
979}
980
981static int mos77xx_calc_num_ports(struct usb_serial *serial)
982{
983 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
984 if (product == MOSCHIP_DEVICE_ID_7715)
985 return 1;
986
987 return 2;
988}
989
Alan Coxa509a7e2009-09-19 13:13:26 -0700990static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700991{
992 struct usb_serial *serial;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700993 struct urb *urb;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700994 struct moschip_port *mos7720_port;
995 int response;
996 int port_number;
Mike Dunn63b91762010-04-15 17:02:09 -0400997 __u8 data;
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100998 int allocated_urbs = 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700999 int j;
1000
1001 serial = port->serial;
1002
1003 mos7720_port = usb_get_serial_port_data(port);
1004 if (mos7720_port == NULL)
1005 return -ENODEV;
1006
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001007 usb_clear_halt(serial->dev, port->write_urb->pipe);
1008 usb_clear_halt(serial->dev, port->read_urb->pipe);
1009
1010 /* Initialising the write urb pool */
1011 for (j = 0; j < NUM_URBS; ++j) {
Alan Cox4da1a172008-07-22 11:16:21 +01001012 urb = usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001013 mos7720_port->write_urb_pool[j] = urb;
1014
1015 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001016 dev_err(&port->dev, "No more urbs???\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001017 continue;
1018 }
1019
1020 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1021 GFP_KERNEL);
1022 if (!urb->transfer_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001023 dev_err(&port->dev,
1024 "%s-out of memory for urb buffers.\n",
1025 __func__);
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001026 usb_free_urb(mos7720_port->write_urb_pool[j]);
1027 mos7720_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001028 continue;
1029 }
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001030 allocated_urbs++;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001031 }
1032
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001033 if (!allocated_urbs)
1034 return -ENOMEM;
1035
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001036 /* Initialize MCS7720 -- Write Init values to corresponding Registers
1037 *
1038 * Register Index
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001039 * 0 : THR/RHR
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001040 * 1 : IER
1041 * 2 : FCR
1042 * 3 : LCR
1043 * 4 : MCR
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001044 * 5 : LSR
1045 * 6 : MSR
1046 * 7 : SPR
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001047 *
1048 * 0x08 : SP1/2 Control Reg
1049 */
1050 port_number = port->number - port->serial->minor;
Mike Dunn63b91762010-04-15 17:02:09 -04001051 read_mos_reg(serial, port_number, LSR, &data);
1052
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001053 dev_dbg(&port->dev, "SS::%p LSR:%x\n", mos7720_port, data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001054
Mike Dunn63b91762010-04-15 17:02:09 -04001055 write_mos_reg(serial, dummy, SP1_REG, 0x02);
1056 write_mos_reg(serial, dummy, SP2_REG, 0x02);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001057
Mike Dunn63b91762010-04-15 17:02:09 -04001058 write_mos_reg(serial, port_number, IER, 0x00);
1059 write_mos_reg(serial, port_number, FCR, 0x00);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001060
Mike Dunn63b91762010-04-15 17:02:09 -04001061 write_mos_reg(serial, port_number, FCR, 0xcf);
1062 mos7720_port->shadowLCR = 0x03;
1063 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1064 mos7720_port->shadowMCR = 0x0b;
1065 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001066
Mike Dunn63b91762010-04-15 17:02:09 -04001067 write_mos_reg(serial, port_number, SP_CONTROL_REG, 0x00);
1068 read_mos_reg(serial, dummy, SP_CONTROL_REG, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001069 data = data | (port->number - port->serial->minor + 1);
Mike Dunn63b91762010-04-15 17:02:09 -04001070 write_mos_reg(serial, dummy, SP_CONTROL_REG, data);
1071 mos7720_port->shadowLCR = 0x83;
1072 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1073 write_mos_reg(serial, port_number, THR, 0x0c);
1074 write_mos_reg(serial, port_number, IER, 0x00);
1075 mos7720_port->shadowLCR = 0x03;
1076 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1077 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001078
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001079 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
1080 if (response)
Alan Cox4da1a172008-07-22 11:16:21 +01001081 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
1082 __func__, response);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001083
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001084 /* initialize our port settings */
1085 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
1086
1087 /* send a open port command */
1088 mos7720_port->open = 1;
1089
1090 return 0;
1091}
1092
1093/*
1094 * mos7720_chars_in_buffer
1095 * this function is called by the tty driver when it wants to know how many
1096 * bytes of data we currently have outstanding in the port (data that has
1097 * been written, but hasn't made it out the port yet)
1098 * If successful, we return the number of bytes left to be written in the
1099 * system,
1100 * Otherwise we return a negative error number.
1101 */
Alan Cox95da3102008-07-22 11:09:07 +01001102static int mos7720_chars_in_buffer(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001103{
Alan Cox95da3102008-07-22 11:09:07 +01001104 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001105 int i;
1106 int chars = 0;
1107 struct moschip_port *mos7720_port;
1108
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001109 mos7720_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001110 if (mos7720_port == NULL)
Alan Cox23198fd2009-07-20 16:05:27 +01001111 return 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001112
1113 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001114 if (mos7720_port->write_urb_pool[i] &&
1115 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001116 chars += URB_TRANSFER_BUFFER_SIZE;
1117 }
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001118 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001119 return chars;
1120}
1121
Alan Cox335f8512009-06-11 12:26:29 +01001122static void mos7720_close(struct usb_serial_port *port)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001123{
1124 struct usb_serial *serial;
1125 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001126 int j;
1127
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001128 serial = port->serial;
1129
1130 mos7720_port = usb_get_serial_port_data(port);
1131 if (mos7720_port == NULL)
1132 return;
1133
1134 for (j = 0; j < NUM_URBS; ++j)
1135 usb_kill_urb(mos7720_port->write_urb_pool[j]);
1136
1137 /* Freeing Write URBs */
1138 for (j = 0; j < NUM_URBS; ++j) {
1139 if (mos7720_port->write_urb_pool[j]) {
1140 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
1141 usb_free_urb(mos7720_port->write_urb_pool[j]);
1142 }
1143 }
1144
1145 /* While closing port, shutdown all bulk read, write *
Oliver Neukuma1cd7e92008-01-16 17:18:52 +01001146 * and interrupt read if they exists, otherwise nop */
Oliver Neukuma1cd7e92008-01-16 17:18:52 +01001147 usb_kill_urb(port->write_urb);
Oliver Neukuma1cd7e92008-01-16 17:18:52 +01001148 usb_kill_urb(port->read_urb);
1149
Johan Hovoldcf41aa92013-03-21 12:37:41 +01001150 write_mos_reg(serial, port->number - port->serial->minor, MCR, 0x00);
1151 write_mos_reg(serial, port->number - port->serial->minor, IER, 0x00);
1152
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001153 mos7720_port->open = 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001154}
1155
Alan Cox95da3102008-07-22 11:09:07 +01001156static void mos7720_break(struct tty_struct *tty, int break_state)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001157{
Alan Cox95da3102008-07-22 11:09:07 +01001158 struct usb_serial_port *port = tty->driver_data;
Alan Cox4da1a172008-07-22 11:16:21 +01001159 unsigned char data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001160 struct usb_serial *serial;
1161 struct moschip_port *mos7720_port;
1162
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001163 serial = port->serial;
1164
1165 mos7720_port = usb_get_serial_port_data(port);
1166 if (mos7720_port == NULL)
1167 return;
1168
1169 if (break_state == -1)
1170 data = mos7720_port->shadowLCR | UART_LCR_SBC;
1171 else
1172 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
1173
1174 mos7720_port->shadowLCR = data;
Mike Dunn63b91762010-04-15 17:02:09 -04001175 write_mos_reg(serial, port->number - port->serial->minor,
1176 LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001177}
1178
1179/*
1180 * mos7720_write_room
1181 * this function is called by the tty driver when it wants to know how many
1182 * bytes of data we can accept for a specific port.
1183 * If successful, we return the amount of room that we have for this port
1184 * Otherwise we return a negative error number.
1185 */
Alan Cox95da3102008-07-22 11:09:07 +01001186static int mos7720_write_room(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001187{
Alan Cox95da3102008-07-22 11:09:07 +01001188 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001189 struct moschip_port *mos7720_port;
1190 int room = 0;
1191 int i;
1192
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001193 mos7720_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001194 if (mos7720_port == NULL)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001195 return -ENODEV;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001196
Alan Coxa5b6f602008-04-08 17:16:06 +01001197 /* FIXME: Locking */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001198 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001199 if (mos7720_port->write_urb_pool[i] &&
1200 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001201 room += URB_TRANSFER_BUFFER_SIZE;
1202 }
1203
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001204 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001205 return room;
1206}
1207
Alan Cox95da3102008-07-22 11:09:07 +01001208static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
1209 const unsigned char *data, int count)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001210{
1211 int status;
1212 int i;
1213 int bytes_sent = 0;
1214 int transfer_size;
1215
1216 struct moschip_port *mos7720_port;
1217 struct usb_serial *serial;
1218 struct urb *urb;
1219 const unsigned char *current_position = data;
1220
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001221 serial = port->serial;
1222
1223 mos7720_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001224 if (mos7720_port == NULL)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001225 return -ENODEV;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001226
1227 /* try to find a free urb in the list */
1228 urb = NULL;
1229
1230 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001231 if (mos7720_port->write_urb_pool[i] &&
1232 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001233 urb = mos7720_port->write_urb_pool[i];
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001234 dev_dbg(&port->dev, "URB:%d\n", i);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001235 break;
1236 }
1237 }
1238
1239 if (urb == NULL) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001240 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001241 goto exit;
1242 }
1243
1244 if (urb->transfer_buffer == NULL) {
1245 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1246 GFP_KERNEL);
1247 if (urb->transfer_buffer == NULL) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001248 dev_err_console(port, "%s no more kernel memory...\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001249 __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001250 goto exit;
1251 }
1252 }
Alan Cox4da1a172008-07-22 11:16:21 +01001253 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001254
1255 memcpy(urb->transfer_buffer, current_position, transfer_size);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001256 usb_serial_debug_data(&port->dev, __func__, transfer_size,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001257 urb->transfer_buffer);
1258
1259 /* fill urb with data and submit */
1260 usb_fill_bulk_urb(urb, serial->dev,
1261 usb_sndbulkpipe(serial->dev,
Alan Cox4da1a172008-07-22 11:16:21 +01001262 port->bulk_out_endpointAddress),
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001263 urb->transfer_buffer, transfer_size,
1264 mos7720_bulk_out_data_callback, mos7720_port);
1265
1266 /* send it down the pipe */
Alan Cox4da1a172008-07-22 11:16:21 +01001267 status = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001268 if (status) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001269 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001270 "with status = %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001271 bytes_sent = status;
1272 goto exit;
1273 }
1274 bytes_sent = transfer_size;
1275
1276exit:
1277 return bytes_sent;
1278}
1279
Alan Cox95da3102008-07-22 11:09:07 +01001280static void mos7720_throttle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001281{
Alan Cox95da3102008-07-22 11:09:07 +01001282 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001283 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001284 int status;
1285
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001286 mos7720_port = usb_get_serial_port_data(port);
1287
1288 if (mos7720_port == NULL)
1289 return;
1290
1291 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001292 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001293 return;
1294 }
1295
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001296 /* if we are implementing XON/XOFF, send the stop character */
1297 if (I_IXOFF(tty)) {
1298 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001299 status = mos7720_write(tty, port, &stop_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001300 if (status <= 0)
1301 return;
1302 }
1303
1304 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001305 if (tty->termios.c_cflag & CRTSCTS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001306 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
Mike Dunn63b91762010-04-15 17:02:09 -04001307 write_mos_reg(port->serial, port->number - port->serial->minor,
1308 MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001309 if (status != 0)
1310 return;
1311 }
1312}
1313
Alan Cox95da3102008-07-22 11:09:07 +01001314static void mos7720_unthrottle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001315{
Alan Cox95da3102008-07-22 11:09:07 +01001316 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001317 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +01001318 int status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001319
1320 if (mos7720_port == NULL)
1321 return;
1322
1323 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001324 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001325 return;
1326 }
1327
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001328 /* if we are implementing XON/XOFF, send the start character */
1329 if (I_IXOFF(tty)) {
1330 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001331 status = mos7720_write(tty, port, &start_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001332 if (status <= 0)
1333 return;
1334 }
1335
1336 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001337 if (tty->termios.c_cflag & CRTSCTS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001338 mos7720_port->shadowMCR |= UART_MCR_RTS;
Mike Dunn63b91762010-04-15 17:02:09 -04001339 write_mos_reg(port->serial, port->number - port->serial->minor,
1340 MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001341 if (status != 0)
1342 return;
1343 }
1344}
1345
Mike Dunnb69578d2010-04-15 17:01:33 -04001346/* FIXME: this function does not work */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001347static int set_higher_rates(struct moschip_port *mos7720_port,
1348 unsigned int baud)
1349{
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001350 struct usb_serial_port *port;
1351 struct usb_serial *serial;
1352 int port_number;
Mike Dunn63b91762010-04-15 17:02:09 -04001353 enum mos_regs sp_reg;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001354 if (mos7720_port == NULL)
1355 return -EINVAL;
1356
1357 port = mos7720_port->port;
1358 serial = port->serial;
1359
Alan Cox4da1a172008-07-22 11:16:21 +01001360 /***********************************************
1361 * Init Sequence for higher rates
1362 ***********************************************/
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001363 dev_dbg(&port->dev, "Sending Setting Commands ..........\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001364 port_number = port->number - port->serial->minor;
1365
Mike Dunn63b91762010-04-15 17:02:09 -04001366 write_mos_reg(serial, port_number, IER, 0x00);
1367 write_mos_reg(serial, port_number, FCR, 0x00);
1368 write_mos_reg(serial, port_number, FCR, 0xcf);
1369 mos7720_port->shadowMCR = 0x0b;
1370 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1371 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x00);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001372
Alan Cox4da1a172008-07-22 11:16:21 +01001373 /***********************************************
1374 * Set for higher rates *
1375 ***********************************************/
Mike Dunnb69578d2010-04-15 17:01:33 -04001376 /* writing baud rate verbatum into uart clock field clearly not right */
Mike Dunn63b91762010-04-15 17:02:09 -04001377 if (port_number == 0)
1378 sp_reg = SP1_REG;
1379 else
1380 sp_reg = SP2_REG;
1381 write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
1382 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x03);
1383 mos7720_port->shadowMCR = 0x2b;
1384 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001385
Alan Cox4da1a172008-07-22 11:16:21 +01001386 /***********************************************
1387 * Set DLL/DLM
1388 ***********************************************/
Mike Dunn63b91762010-04-15 17:02:09 -04001389 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1390 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1391 write_mos_reg(serial, port_number, DLL, 0x01);
1392 write_mos_reg(serial, port_number, DLM, 0x00);
1393 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1394 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001395
1396 return 0;
1397}
1398
1399/* baud rate information */
Alan Cox4da1a172008-07-22 11:16:21 +01001400struct divisor_table_entry {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001401 __u32 baudrate;
1402 __u16 divisor;
1403};
1404
1405/* Define table of divisors for moschip 7720 hardware *
1406 * These assume a 3.6864MHz crystal, the standard /16, and *
1407 * MCR.7 = 0. */
1408static struct divisor_table_entry divisor_table[] = {
1409 { 50, 2304},
1410 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
1411 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
1412 { 150, 768},
1413 { 300, 384},
1414 { 600, 192},
1415 { 1200, 96},
1416 { 1800, 64},
1417 { 2400, 48},
1418 { 4800, 24},
1419 { 7200, 16},
1420 { 9600, 12},
1421 { 19200, 6},
1422 { 38400, 3},
1423 { 57600, 2},
1424 { 115200, 1},
1425};
1426
1427/*****************************************************************************
1428 * calc_baud_rate_divisor
1429 * this function calculates the proper baud rate divisor for the specified
1430 * baud rate.
1431 *****************************************************************************/
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001432static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, int *divisor)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001433{
1434 int i;
1435 __u16 custom;
1436 __u16 round1;
1437 __u16 round;
1438
1439
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001440 dev_dbg(&port->dev, "%s - %d\n", __func__, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001441
1442 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
1443 if (divisor_table[i].baudrate == baudrate) {
1444 *divisor = divisor_table[i].divisor;
1445 return 0;
1446 }
1447 }
1448
Alan Cox4da1a172008-07-22 11:16:21 +01001449 /* After trying for all the standard baud rates *
1450 * Try calculating the divisor for this baud rate */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001451 if (baudrate > 75 && baudrate < 230400) {
1452 /* get the divisor */
1453 custom = (__u16)(230400L / baudrate);
1454
1455 /* Check for round off */
1456 round1 = (__u16)(2304000L / baudrate);
1457 round = (__u16)(round1 - (custom * 10));
1458 if (round > 4)
1459 custom++;
1460 *divisor = custom;
1461
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001462 dev_dbg(&port->dev, "Baud %d = %d\n", baudrate, custom);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001463 return 0;
1464 }
1465
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001466 dev_dbg(&port->dev, "Baud calculation Failed...\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001467 return -EINVAL;
1468}
1469
1470/*
1471 * send_cmd_write_baud_rate
1472 * this function sends the proper command to change the baud rate of the
1473 * specified port.
1474 */
1475static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1476 int baudrate)
1477{
1478 struct usb_serial_port *port;
1479 struct usb_serial *serial;
1480 int divisor;
1481 int status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001482 unsigned char number;
1483
1484 if (mos7720_port == NULL)
1485 return -1;
1486
1487 port = mos7720_port->port;
1488 serial = port->serial;
1489
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001490 number = port->number - port->serial->minor;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001491 dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001492
Alan Cox4da1a172008-07-22 11:16:21 +01001493 /* Calculate the Divisor */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001494 status = calc_baud_rate_divisor(port, baudrate, &divisor);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001495 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001496 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001497 return status;
1498 }
1499
Alan Cox4da1a172008-07-22 11:16:21 +01001500 /* Enable access to divisor latch */
Mike Dunn63b91762010-04-15 17:02:09 -04001501 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1502 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001503
1504 /* Write the divisor */
Mike Dunn63b91762010-04-15 17:02:09 -04001505 write_mos_reg(serial, number, DLL, (__u8)(divisor & 0xff));
1506 write_mos_reg(serial, number, DLM, (__u8)((divisor & 0xff00) >> 8));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001507
Alan Cox4da1a172008-07-22 11:16:21 +01001508 /* Disable access to divisor latch */
Mike Dunn63b91762010-04-15 17:02:09 -04001509 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1510 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001511
1512 return status;
1513}
1514
1515/*
1516 * change_port_settings
1517 * This routine is called to set the UART on the device to match
1518 * the specified new settings.
1519 */
Alan Cox95da3102008-07-22 11:09:07 +01001520static void change_port_settings(struct tty_struct *tty,
1521 struct moschip_port *mos7720_port,
Alan Cox606d0992006-12-08 02:38:45 -08001522 struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001523{
1524 struct usb_serial_port *port;
1525 struct usb_serial *serial;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001526 int baud;
1527 unsigned cflag;
1528 unsigned iflag;
1529 __u8 mask = 0xff;
1530 __u8 lData;
1531 __u8 lParity;
1532 __u8 lStop;
1533 int status;
1534 int port_number;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001535
1536 if (mos7720_port == NULL)
1537 return ;
1538
1539 port = mos7720_port->port;
1540 serial = port->serial;
1541 port_number = port->number - port->serial->minor;
1542
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001543 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001544 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001545 return;
1546 }
1547
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001548 lData = UART_LCR_WLEN8;
1549 lStop = 0x00; /* 1 stop bit */
1550 lParity = 0x00; /* No parity */
1551
Alan Coxadc8d742012-07-14 15:31:47 +01001552 cflag = tty->termios.c_cflag;
1553 iflag = tty->termios.c_iflag;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001554
1555 /* Change the number of bits */
1556 switch (cflag & CSIZE) {
1557 case CS5:
1558 lData = UART_LCR_WLEN5;
1559 mask = 0x1f;
1560 break;
1561
1562 case CS6:
1563 lData = UART_LCR_WLEN6;
1564 mask = 0x3f;
1565 break;
1566
1567 case CS7:
1568 lData = UART_LCR_WLEN7;
1569 mask = 0x7f;
1570 break;
1571 default:
1572 case CS8:
1573 lData = UART_LCR_WLEN8;
1574 break;
1575 }
1576
1577 /* Change the Parity bit */
1578 if (cflag & PARENB) {
1579 if (cflag & PARODD) {
1580 lParity = UART_LCR_PARITY;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001581 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001582 } else {
1583 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001584 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001585 }
1586
1587 } else {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001588 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001589 }
1590
1591 if (cflag & CMSPAR)
1592 lParity = lParity | 0x20;
1593
1594 /* Change the Stop bit */
1595 if (cflag & CSTOPB) {
1596 lStop = UART_LCR_STOP;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001597 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001598 } else {
1599 lStop = 0x00;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001600 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001601 }
1602
1603#define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1604#define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1605#define LCR_PAR_MASK 0x38 /* Mask for parity field */
1606
1607 /* Update the LCR with the correct value */
Alan Cox4da1a172008-07-22 11:16:21 +01001608 mos7720_port->shadowLCR &=
Mike Dunn63b91762010-04-15 17:02:09 -04001609 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001610 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1611
1612
1613 /* Disable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001614 write_mos_reg(serial, port_number, IER, 0x00);
1615 write_mos_reg(serial, port_number, FCR, 0x00);
1616 write_mos_reg(serial, port_number, FCR, 0xcf);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001617
1618 /* Send the updated LCR value to the mos7720 */
Mike Dunn63b91762010-04-15 17:02:09 -04001619 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1620 mos7720_port->shadowMCR = 0x0b;
1621 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001622
1623 /* set up the MCR register and send it to the mos7720 */
1624 mos7720_port->shadowMCR = UART_MCR_OUT2;
1625 if (cflag & CBAUD)
1626 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1627
1628 if (cflag & CRTSCTS) {
1629 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
Alan Cox4da1a172008-07-22 11:16:21 +01001630 /* To set hardware flow control to the specified *
1631 * serial port, in SP1/2_CONTROL_REG */
Mike Dunn63b91762010-04-15 17:02:09 -04001632 if (port->number)
1633 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x01);
1634 else
1635 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x02);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001636
Mike Dunn63b91762010-04-15 17:02:09 -04001637 } else
1638 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1639
1640 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001641
1642 /* Determine divisor based on baud rate */
1643 baud = tty_get_baud_rate(tty);
1644 if (!baud) {
1645 /* pick a default, any default... */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001646 dev_dbg(&port->dev, "Picked default baud...\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001647 baud = 9600;
1648 }
1649
1650 if (baud >= 230400) {
1651 set_higher_rates(mos7720_port, baud);
1652 /* Enable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001653 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001654 return;
1655 }
1656
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001657 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001658 status = send_cmd_write_baud_rate(mos7720_port, baud);
Alan Cox65d063ab2008-01-03 17:01:18 +00001659 /* FIXME: needs to write actual resulting baud back not just
1660 blindly do so */
1661 if (cflag & CBAUD)
1662 tty_encode_baud_rate(tty, baud, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001663 /* Enable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001664 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001665
1666 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001667 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1668 if (status)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001669 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001670 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001671}
1672
1673/*
1674 * mos7720_set_termios
1675 * this function is called by the tty driver when it wants to change the
1676 * termios structure.
1677 */
Alan Cox95da3102008-07-22 11:09:07 +01001678static void mos7720_set_termios(struct tty_struct *tty,
1679 struct usb_serial_port *port, struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001680{
1681 int status;
1682 unsigned int cflag;
1683 struct usb_serial *serial;
1684 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001685
1686 serial = port->serial;
1687
1688 mos7720_port = usb_get_serial_port_data(port);
1689
1690 if (mos7720_port == NULL)
1691 return;
1692
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001693 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001694 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001695 return;
1696 }
1697
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001698 dev_dbg(&port->dev, "setting termios - ASPIRE\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001699
Alan Coxadc8d742012-07-14 15:31:47 +01001700 cflag = tty->termios.c_cflag;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001701
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001702 dev_dbg(&port->dev, "%s - cflag %08x iflag %08x\n", __func__,
Linus Torvaldsd9a80742012-10-01 13:23:01 -07001703 tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001704
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001705 dev_dbg(&port->dev, "%s - old cflag %08x old iflag %08x\n", __func__,
1706 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001707
1708 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001709 change_port_settings(tty, mos7720_port, old_termios);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001710
Alan Cox4da1a172008-07-22 11:16:21 +01001711 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001712 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1713 if (status)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001714 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001715 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001716}
1717
1718/*
1719 * get_lsr_info - get line status register info
1720 *
1721 * Purpose: Let user call ioctl() to get info when the UART physically
1722 * is emptied. On bus types like RS485, the transmitter must
1723 * release the bus after transmitting. This must be done when
1724 * the transmit shift register is empty, not be done when the
1725 * transmit holding register is empty. This functionality
1726 * allows an RS485 driver to be written in user space.
1727 */
Alan Cox4da1a172008-07-22 11:16:21 +01001728static int get_lsr_info(struct tty_struct *tty,
1729 struct moschip_port *mos7720_port, unsigned int __user *value)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001730{
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001731 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001732 unsigned int result = 0;
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001733 unsigned char data = 0;
1734 int port_number = port->number - port->serial->minor;
1735 int count;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001736
Alan Cox95da3102008-07-22 11:09:07 +01001737 count = mos7720_chars_in_buffer(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001738 if (count == 0) {
Mike Dunn63b91762010-04-15 17:02:09 -04001739 read_mos_reg(port->serial, port_number, LSR, &data);
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001740 if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1741 == (UART_LSR_TEMT | UART_LSR_THRE)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001742 dev_dbg(&port->dev, "%s -- Empty\n", __func__);
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001743 result = TIOCSER_TEMT;
1744 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001745 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001746 if (copy_to_user(value, &result, sizeof(int)))
1747 return -EFAULT;
1748 return 0;
1749}
1750
Alan Cox60b33c12011-02-14 16:26:14 +00001751static int mos7720_tiocmget(struct tty_struct *tty)
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001752{
1753 struct usb_serial_port *port = tty->driver_data;
1754 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1755 unsigned int result = 0;
1756 unsigned int mcr ;
1757 unsigned int msr ;
1758
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001759 mcr = mos7720_port->shadowMCR;
1760 msr = mos7720_port->shadowMSR;
1761
1762 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1763 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1764 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1765 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
1766 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1767 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
1768
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001769 return result;
1770}
1771
Alan Cox20b9d172011-02-14 16:26:50 +00001772static int mos7720_tiocmset(struct tty_struct *tty,
Mike Dunn63b91762010-04-15 17:02:09 -04001773 unsigned int set, unsigned int clear)
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001774{
1775 struct usb_serial_port *port = tty->driver_data;
1776 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1777 unsigned int mcr ;
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001778
1779 mcr = mos7720_port->shadowMCR;
1780
1781 if (set & TIOCM_RTS)
1782 mcr |= UART_MCR_RTS;
1783 if (set & TIOCM_DTR)
1784 mcr |= UART_MCR_DTR;
1785 if (set & TIOCM_LOOP)
1786 mcr |= UART_MCR_LOOP;
1787
1788 if (clear & TIOCM_RTS)
1789 mcr &= ~UART_MCR_RTS;
1790 if (clear & TIOCM_DTR)
1791 mcr &= ~UART_MCR_DTR;
1792 if (clear & TIOCM_LOOP)
1793 mcr &= ~UART_MCR_LOOP;
1794
1795 mos7720_port->shadowMCR = mcr;
Mike Dunn63b91762010-04-15 17:02:09 -04001796 write_mos_reg(port->serial, port->number - port->serial->minor,
1797 MCR, mos7720_port->shadowMCR);
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001798
1799 return 0;
1800}
1801
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001802static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1803 unsigned int __user *value)
1804{
Alan Cox0bca1b92010-09-16 18:21:40 +01001805 unsigned int mcr;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001806 unsigned int arg;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001807
1808 struct usb_serial_port *port;
1809
1810 if (mos7720_port == NULL)
1811 return -1;
1812
Alan Cox4da1a172008-07-22 11:16:21 +01001813 port = (struct usb_serial_port *)mos7720_port->port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001814 mcr = mos7720_port->shadowMCR;
1815
1816 if (copy_from_user(&arg, value, sizeof(int)))
1817 return -EFAULT;
1818
1819 switch (cmd) {
1820 case TIOCMBIS:
1821 if (arg & TIOCM_RTS)
1822 mcr |= UART_MCR_RTS;
1823 if (arg & TIOCM_DTR)
1824 mcr |= UART_MCR_RTS;
1825 if (arg & TIOCM_LOOP)
1826 mcr |= UART_MCR_LOOP;
1827 break;
1828
1829 case TIOCMBIC:
1830 if (arg & TIOCM_RTS)
1831 mcr &= ~UART_MCR_RTS;
1832 if (arg & TIOCM_DTR)
1833 mcr &= ~UART_MCR_RTS;
1834 if (arg & TIOCM_LOOP)
1835 mcr &= ~UART_MCR_LOOP;
1836 break;
1837
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001838 }
1839
1840 mos7720_port->shadowMCR = mcr;
Mike Dunn63b91762010-04-15 17:02:09 -04001841 write_mos_reg(port->serial, port->number - port->serial->minor,
1842 MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001843
1844 return 0;
1845}
1846
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001847static int get_serial_info(struct moschip_port *mos7720_port,
1848 struct serial_struct __user *retinfo)
1849{
1850 struct serial_struct tmp;
1851
1852 if (!retinfo)
1853 return -EFAULT;
1854
1855 memset(&tmp, 0, sizeof(tmp));
1856
1857 tmp.type = PORT_16550A;
1858 tmp.line = mos7720_port->port->serial->minor;
1859 tmp.port = mos7720_port->port->number;
1860 tmp.irq = 0;
1861 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
Alan Cox4da1a172008-07-22 11:16:21 +01001862 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001863 tmp.baud_base = 9600;
1864 tmp.close_delay = 5*HZ;
1865 tmp.closing_wait = 30*HZ;
1866
1867 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1868 return -EFAULT;
1869 return 0;
1870}
1871
Alan Cox00a0d0d2011-02-14 16:27:06 +00001872static int mos7720_ioctl(struct tty_struct *tty,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001873 unsigned int cmd, unsigned long arg)
1874{
Alan Cox95da3102008-07-22 11:09:07 +01001875 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001876 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001877
1878 mos7720_port = usb_get_serial_port_data(port);
1879 if (mos7720_port == NULL)
1880 return -ENODEV;
1881
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001882 dev_dbg(&port->dev, "%s - cmd = 0x%x", __func__, cmd);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001883
1884 switch (cmd) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001885 case TIOCSERGETLSR:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001886 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
Alan Cox4da1a172008-07-22 11:16:21 +01001887 return get_lsr_info(tty, mos7720_port,
1888 (unsigned int __user *)arg);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001889
Alan Cox95da3102008-07-22 11:09:07 +01001890 /* FIXME: These should be using the mode methods */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001891 case TIOCMBIS:
1892 case TIOCMBIC:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001893 dev_dbg(&port->dev, "%s TIOCMSET/TIOCMBIC/TIOCMSET\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001894 return set_modem_info(mos7720_port, cmd,
1895 (unsigned int __user *)arg);
1896
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001897 case TIOCGSERIAL:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001898 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001899 return get_serial_info(mos7720_port,
1900 (struct serial_struct __user *)arg);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001901 }
1902
1903 return -ENOIOCTLCMD;
1904}
1905
1906static int mos7720_startup(struct usb_serial *serial)
1907{
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001908 struct usb_device *dev;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001909 char data;
Huzaifa Sidhpurwala91f58ae2011-02-21 12:58:45 +05301910 u16 product;
Mike Dunnb69578d2010-04-15 17:01:33 -04001911 int ret_val;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001912
Huzaifa Sidhpurwala91f58ae2011-02-21 12:58:45 +05301913 product = le16_to_cpu(serial->dev->descriptor.idProduct);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001914 dev = serial->dev;
1915
Mike Dunnfb088e32010-01-26 12:12:12 -05001916 /*
1917 * The 7715 uses the first bulk in/out endpoint pair for the parallel
1918 * port, and the second for the serial port. Because the usbserial core
1919 * assumes both pairs are serial ports, we must engage in a bit of
1920 * subterfuge and swap the pointers for ports 0 and 1 in order to make
1921 * port 0 point to the serial port. However, both moschip devices use a
1922 * single interrupt-in endpoint for both ports (as mentioned a little
1923 * further down), and this endpoint was assigned to port 0. So after
1924 * the swap, we must copy the interrupt endpoint elements from port 1
1925 * (as newly assigned) to port 0, and null out port 1 pointers.
1926 */
1927 if (product == MOSCHIP_DEVICE_ID_7715) {
1928 struct usb_serial_port *tmp = serial->port[0];
1929 serial->port[0] = serial->port[1];
1930 serial->port[1] = tmp;
1931 serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb;
1932 serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer;
1933 serial->port[0]->interrupt_in_endpointAddress =
1934 tmp->interrupt_in_endpointAddress;
1935 serial->port[1]->interrupt_in_urb = NULL;
1936 serial->port[1]->interrupt_in_buffer = NULL;
1937 }
1938
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001939 /* setting configuration feature to one */
1940 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Alan Cox4da1a172008-07-22 11:16:21 +01001941 (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5*HZ);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001942
Mike Dunnb69578d2010-04-15 17:01:33 -04001943 /* start the interrupt urb */
1944 ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
1945 if (ret_val)
1946 dev_err(&dev->dev,
1947 "%s - Error %d submitting control urb\n",
1948 __func__, ret_val);
1949
1950#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1951 if (product == MOSCHIP_DEVICE_ID_7715) {
1952 ret_val = mos7715_parport_init(serial);
1953 if (ret_val < 0)
1954 return ret_val;
1955 }
1956#endif
Alan Cox4da1a172008-07-22 11:16:21 +01001957 /* LSR For Port 1 */
Mike Dunn63b91762010-04-15 17:02:09 -04001958 read_mos_reg(serial, 0, LSR, &data);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001959 dev_dbg(&dev->dev, "LSR:%x\n", data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001960
1961 return 0;
1962}
1963
Alan Sternf9c99bb2009-06-02 11:53:55 -04001964static void mos7720_release(struct usb_serial *serial)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001965{
Mike Dunnb69578d2010-04-15 17:01:33 -04001966#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1967 /* close the parallel port */
1968
1969 if (le16_to_cpu(serial->dev->descriptor.idProduct)
1970 == MOSCHIP_DEVICE_ID_7715) {
1971 struct urbtracker *urbtrack;
1972 unsigned long flags;
1973 struct mos7715_parport *mos_parport =
1974 usb_get_serial_data(serial);
1975
1976 /* prevent NULL ptr dereference in port callbacks */
1977 spin_lock(&release_lock);
1978 mos_parport->pp->private_data = NULL;
1979 spin_unlock(&release_lock);
1980
1981 /* wait for synchronous usb calls to return */
1982 if (mos_parport->msg_pending)
1983 wait_for_completion_timeout(&mos_parport->syncmsg_compl,
1984 MOS_WDR_TIMEOUT);
1985
1986 parport_remove_port(mos_parport->pp);
1987 usb_set_serial_data(serial, NULL);
1988 mos_parport->serial = NULL;
1989
1990 /* if tasklet currently scheduled, wait for it to complete */
1991 tasklet_kill(&mos_parport->urb_tasklet);
1992
1993 /* unlink any urbs sent by the tasklet */
1994 spin_lock_irqsave(&mos_parport->listlock, flags);
1995 list_for_each_entry(urbtrack,
1996 &mos_parport->active_urbs,
1997 urblist_entry)
1998 usb_unlink_urb(urbtrack->urb);
1999 spin_unlock_irqrestore(&mos_parport->listlock, flags);
2000
2001 kref_put(&mos_parport->ref_count, destroy_mos_parport);
2002 }
2003#endif
Johan Hovold4230af52012-10-25 10:29:05 +02002004}
2005
2006static int mos7720_port_probe(struct usb_serial_port *port)
2007{
2008 struct moschip_port *mos7720_port;
2009
2010 mos7720_port = kzalloc(sizeof(*mos7720_port), GFP_KERNEL);
2011 if (!mos7720_port)
2012 return -ENOMEM;
2013
2014 /* Initialize all port interrupt end point to port 0 int endpoint.
2015 * Our device has only one interrupt endpoint common to all ports.
2016 */
2017 port->interrupt_in_endpointAddress =
2018 port->serial->port[0]->interrupt_in_endpointAddress;
2019 mos7720_port->port = port;
2020
2021 usb_set_serial_port_data(port, mos7720_port);
2022
2023 return 0;
2024}
2025
2026static int mos7720_port_remove(struct usb_serial_port *port)
2027{
2028 struct moschip_port *mos7720_port;
2029
2030 mos7720_port = usb_get_serial_port_data(port);
2031 kfree(mos7720_port);
2032
2033 return 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002034}
2035
2036static struct usb_serial_driver moschip7720_2port_driver = {
2037 .driver = {
2038 .owner = THIS_MODULE,
2039 .name = "moschip7720",
2040 },
2041 .description = "Moschip 2 port adapter",
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002042 .id_table = id_table,
Mike Dunnfb088e32010-01-26 12:12:12 -05002043 .calc_num_ports = mos77xx_calc_num_ports,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002044 .open = mos7720_open,
2045 .close = mos7720_close,
2046 .throttle = mos7720_throttle,
2047 .unthrottle = mos7720_unthrottle,
Mike Dunnfb088e32010-01-26 12:12:12 -05002048 .probe = mos77xx_probe,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002049 .attach = mos7720_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002050 .release = mos7720_release,
Johan Hovold4230af52012-10-25 10:29:05 +02002051 .port_probe = mos7720_port_probe,
2052 .port_remove = mos7720_port_remove,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002053 .ioctl = mos7720_ioctl,
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07002054 .tiocmget = mos7720_tiocmget,
2055 .tiocmset = mos7720_tiocmset,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002056 .set_termios = mos7720_set_termios,
2057 .write = mos7720_write,
2058 .write_room = mos7720_write_room,
2059 .chars_in_buffer = mos7720_chars_in_buffer,
2060 .break_ctl = mos7720_break,
2061 .read_bulk_callback = mos7720_bulk_in_callback,
Mike Dunnfb088e32010-01-26 12:12:12 -05002062 .read_int_callback = NULL /* dynamically assigned in probe() */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002063};
2064
Alan Stern4d2a7af2012-02-23 14:57:09 -05002065static struct usb_serial_driver * const serial_drivers[] = {
2066 &moschip7720_2port_driver, NULL
2067};
2068
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002069module_usb_serial_driver(serial_drivers, id_table);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002070
Alan Cox4da1a172008-07-22 11:16:21 +01002071MODULE_AUTHOR(DRIVER_AUTHOR);
2072MODULE_DESCRIPTION(DRIVER_DESC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002073MODULE_LICENSE("GPL");