blob: fc506bb71315d97a2273c81324d48d57c549a629 [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);
Mike Dunnb69578d2010-04-15 17:01:33 -0400230 int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
Mike Dunn63b91762010-04-15 17:02:09 -0400231 index, data, 1, MOS_WDR_TIMEOUT);
Mike Dunnb69578d2010-04-15 17:01:33 -0400232 if (status < 0)
233 dev_err(&usbdev->dev,
234 "mos7720: usb_control_msg() failed: %d", status);
235 return status;
236}
237
Mike Dunn63b91762010-04-15 17:02:09 -0400238#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
239
Mike Dunnb69578d2010-04-15 17:01:33 -0400240static inline int mos7715_change_mode(struct mos7715_parport *mos_parport,
241 enum mos7715_pp_modes mode)
242{
243 mos_parport->shadowECR = mode;
Mike Dunn63b91762010-04-15 17:02:09 -0400244 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400245 return 0;
246}
247
248static void destroy_mos_parport(struct kref *kref)
249{
250 struct mos7715_parport *mos_parport =
251 container_of(kref, struct mos7715_parport, ref_count);
252
Mike Dunnb69578d2010-04-15 17:01:33 -0400253 kfree(mos_parport);
254}
255
256static void destroy_urbtracker(struct kref *kref)
257{
258 struct urbtracker *urbtrack =
259 container_of(kref, struct urbtracker, ref_count);
260 struct mos7715_parport *mos_parport = urbtrack->mos_parport;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700261
Mike Dunnb69578d2010-04-15 17:01:33 -0400262 usb_free_urb(urbtrack->urb);
263 kfree(urbtrack);
264 kref_put(&mos_parport->ref_count, destroy_mos_parport);
265}
266
267/*
268 * This runs as a tasklet when sending an urb in a non-blocking parallel
269 * port callback had to be deferred because the disconnect mutex could not be
270 * obtained at the time.
271 */
272static void send_deferred_urbs(unsigned long _mos_parport)
273{
274 int ret_val;
275 unsigned long flags;
276 struct mos7715_parport *mos_parport = (void *)_mos_parport;
Wei Yongjun67990472012-08-21 11:28:45 +0800277 struct urbtracker *urbtrack, *tmp;
Mike Dunnb69578d2010-04-15 17:01:33 -0400278 struct list_head *cursor, *next;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700279 struct device *dev;
Mike Dunnb69578d2010-04-15 17:01:33 -0400280
Mike Dunnb69578d2010-04-15 17:01:33 -0400281 /* if release function ran, game over */
282 if (unlikely(mos_parport->serial == NULL))
283 return;
284
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700285 dev = &mos_parport->serial->dev->dev;
286
Mike Dunnb69578d2010-04-15 17:01:33 -0400287 /* try again to get the mutex */
288 if (!mutex_trylock(&mos_parport->serial->disc_mutex)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700289 dev_dbg(dev, "%s: rescheduling tasklet\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400290 tasklet_schedule(&mos_parport->urb_tasklet);
291 return;
292 }
293
294 /* if device disconnected, game over */
295 if (unlikely(mos_parport->serial->disconnected)) {
296 mutex_unlock(&mos_parport->serial->disc_mutex);
297 return;
298 }
299
300 spin_lock_irqsave(&mos_parport->listlock, flags);
301 if (list_empty(&mos_parport->deferred_urbs)) {
302 spin_unlock_irqrestore(&mos_parport->listlock, flags);
303 mutex_unlock(&mos_parport->serial->disc_mutex);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700304 dev_dbg(dev, "%s: deferred_urbs list empty\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400305 return;
306 }
307
308 /* move contents of deferred_urbs list to active_urbs list and submit */
309 list_for_each_safe(cursor, next, &mos_parport->deferred_urbs)
310 list_move_tail(cursor, &mos_parport->active_urbs);
Wei Yongjun67990472012-08-21 11:28:45 +0800311 list_for_each_entry_safe(urbtrack, tmp, &mos_parport->active_urbs,
Mike Dunnb69578d2010-04-15 17:01:33 -0400312 urblist_entry) {
313 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700314 dev_dbg(dev, "%s: urb submitted\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400315 if (ret_val) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700316 dev_err(dev, "usb_submit_urb() failed: %d\n", ret_val);
Mike Dunnb69578d2010-04-15 17:01:33 -0400317 list_del(&urbtrack->urblist_entry);
318 kref_put(&urbtrack->ref_count, destroy_urbtracker);
319 }
320 }
321 spin_unlock_irqrestore(&mos_parport->listlock, flags);
322 mutex_unlock(&mos_parport->serial->disc_mutex);
323}
324
325/* callback for parallel port control urbs submitted asynchronously */
326static void async_complete(struct urb *urb)
327{
328 struct urbtracker *urbtrack = urb->context;
329 int status = urb->status;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700330
Mike Dunnb69578d2010-04-15 17:01:33 -0400331 if (unlikely(status))
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700332 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
Mike Dunnb69578d2010-04-15 17:01:33 -0400333
334 /* remove the urbtracker from the active_urbs list */
335 spin_lock(&urbtrack->mos_parport->listlock);
336 list_del(&urbtrack->urblist_entry);
337 spin_unlock(&urbtrack->mos_parport->listlock);
338 kref_put(&urbtrack->ref_count, destroy_urbtracker);
339}
340
341static int write_parport_reg_nonblock(struct mos7715_parport *mos_parport,
342 enum mos_regs reg, __u8 data)
343{
344 struct urbtracker *urbtrack;
345 int ret_val;
346 unsigned long flags;
347 struct usb_ctrlrequest setup;
348 struct usb_serial *serial = mos_parport->serial;
349 struct usb_device *usbdev = serial->dev;
Mike Dunnb69578d2010-04-15 17:01:33 -0400350
351 /* create and initialize the control urb and containing urbtracker */
352 urbtrack = kmalloc(sizeof(struct urbtracker), GFP_ATOMIC);
353 if (urbtrack == NULL) {
354 dev_err(&usbdev->dev, "out of memory");
355 return -ENOMEM;
356 }
357 kref_get(&mos_parport->ref_count);
358 urbtrack->mos_parport = mos_parport;
359 urbtrack->urb = usb_alloc_urb(0, GFP_ATOMIC);
360 if (urbtrack->urb == NULL) {
361 dev_err(&usbdev->dev, "out of urbs");
362 kfree(urbtrack);
363 return -ENOMEM;
364 }
365 setup.bRequestType = (__u8)0x40;
366 setup.bRequest = (__u8)0x0e;
Mike Dunn63b91762010-04-15 17:02:09 -0400367 setup.wValue = get_reg_value(reg, dummy);
Mike Dunnb69578d2010-04-15 17:01:33 -0400368 setup.wIndex = get_reg_index(reg);
369 setup.wLength = 0;
370 usb_fill_control_urb(urbtrack->urb, usbdev,
371 usb_sndctrlpipe(usbdev, 0),
372 (unsigned char *)&setup,
373 NULL, 0, async_complete, urbtrack);
374 kref_init(&urbtrack->ref_count);
375 INIT_LIST_HEAD(&urbtrack->urblist_entry);
376
377 /*
378 * get the disconnect mutex, or add tracker to the deferred_urbs list
379 * and schedule a tasklet to try again later
380 */
381 if (!mutex_trylock(&serial->disc_mutex)) {
382 spin_lock_irqsave(&mos_parport->listlock, flags);
383 list_add_tail(&urbtrack->urblist_entry,
384 &mos_parport->deferred_urbs);
385 spin_unlock_irqrestore(&mos_parport->listlock, flags);
386 tasklet_schedule(&mos_parport->urb_tasklet);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700387 dev_dbg(&usbdev->dev, "tasklet scheduled");
Mike Dunnb69578d2010-04-15 17:01:33 -0400388 return 0;
389 }
390
391 /* bail if device disconnected */
392 if (serial->disconnected) {
393 kref_put(&urbtrack->ref_count, destroy_urbtracker);
394 mutex_unlock(&serial->disc_mutex);
395 return -ENODEV;
396 }
397
398 /* add the tracker to the active_urbs list and submit */
399 spin_lock_irqsave(&mos_parport->listlock, flags);
400 list_add_tail(&urbtrack->urblist_entry, &mos_parport->active_urbs);
401 spin_unlock_irqrestore(&mos_parport->listlock, flags);
402 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
403 mutex_unlock(&serial->disc_mutex);
404 if (ret_val) {
405 dev_err(&usbdev->dev,
406 "%s: submit_urb() failed: %d", __func__, ret_val);
407 spin_lock_irqsave(&mos_parport->listlock, flags);
408 list_del(&urbtrack->urblist_entry);
409 spin_unlock_irqrestore(&mos_parport->listlock, flags);
410 kref_put(&urbtrack->ref_count, destroy_urbtracker);
411 return ret_val;
412 }
413 return 0;
414}
415
416/*
417 * This is the the common top part of all parallel port callback operations that
418 * send synchronous messages to the device. This implements convoluted locking
419 * that avoids two scenarios: (1) a port operation is called after usbserial
420 * has called our release function, at which point struct mos7715_parport has
421 * been destroyed, and (2) the device has been disconnected, but usbserial has
422 * not called the release function yet because someone has a serial port open.
423 * The shared release_lock prevents the first, and the mutex and disconnected
424 * flag maintained by usbserial covers the second. We also use the msg_pending
425 * flag to ensure that all synchronous usb messgage calls have completed before
426 * our release function can return.
427 */
428static int parport_prologue(struct parport *pp)
429{
430 struct mos7715_parport *mos_parport;
431
432 spin_lock(&release_lock);
433 mos_parport = pp->private_data;
434 if (unlikely(mos_parport == NULL)) {
435 /* release fn called, port struct destroyed */
436 spin_unlock(&release_lock);
437 return -1;
438 }
439 mos_parport->msg_pending = true; /* synch usb call pending */
440 INIT_COMPLETION(mos_parport->syncmsg_compl);
441 spin_unlock(&release_lock);
442
443 mutex_lock(&mos_parport->serial->disc_mutex);
444 if (mos_parport->serial->disconnected) {
445 /* device disconnected */
446 mutex_unlock(&mos_parport->serial->disc_mutex);
447 mos_parport->msg_pending = false;
448 complete(&mos_parport->syncmsg_compl);
449 return -1;
450 }
451
452 return 0;
453}
454
455/*
456 * This is the the common bottom part of all parallel port functions that send
457 * synchronous messages to the device.
458 */
459static inline void parport_epilogue(struct parport *pp)
460{
461 struct mos7715_parport *mos_parport = pp->private_data;
462 mutex_unlock(&mos_parport->serial->disc_mutex);
463 mos_parport->msg_pending = false;
464 complete(&mos_parport->syncmsg_compl);
465}
466
467static void parport_mos7715_write_data(struct parport *pp, unsigned char d)
468{
469 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700470
Mike Dunnb69578d2010-04-15 17:01:33 -0400471 if (parport_prologue(pp) < 0)
472 return;
473 mos7715_change_mode(mos_parport, SPP);
Mike Dunn63b91762010-04-15 17:02:09 -0400474 write_mos_reg(mos_parport->serial, dummy, DPR, (__u8)d);
Mike Dunnb69578d2010-04-15 17:01:33 -0400475 parport_epilogue(pp);
476}
477
478static unsigned char parport_mos7715_read_data(struct parport *pp)
479{
480 struct mos7715_parport *mos_parport = pp->private_data;
481 unsigned char d;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700482
Mike Dunnb69578d2010-04-15 17:01:33 -0400483 if (parport_prologue(pp) < 0)
484 return 0;
Mike Dunn63b91762010-04-15 17:02:09 -0400485 read_mos_reg(mos_parport->serial, dummy, DPR, &d);
Mike Dunnb69578d2010-04-15 17:01:33 -0400486 parport_epilogue(pp);
487 return d;
488}
489
490static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
491{
492 struct mos7715_parport *mos_parport = pp->private_data;
493 __u8 data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700494
Mike Dunnb69578d2010-04-15 17:01:33 -0400495 if (parport_prologue(pp) < 0)
496 return;
497 data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
Mike Dunn63b91762010-04-15 17:02:09 -0400498 write_mos_reg(mos_parport->serial, dummy, DCR, data);
Mike Dunnb69578d2010-04-15 17:01:33 -0400499 mos_parport->shadowDCR = data;
500 parport_epilogue(pp);
501}
502
503static unsigned char parport_mos7715_read_control(struct parport *pp)
504{
505 struct mos7715_parport *mos_parport = pp->private_data;
506 __u8 dcr;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700507
Mike Dunnb69578d2010-04-15 17:01:33 -0400508 spin_lock(&release_lock);
509 mos_parport = pp->private_data;
510 if (unlikely(mos_parport == NULL)) {
511 spin_unlock(&release_lock);
512 return 0;
513 }
514 dcr = mos_parport->shadowDCR & 0x0f;
515 spin_unlock(&release_lock);
516 return dcr;
517}
518
519static unsigned char parport_mos7715_frob_control(struct parport *pp,
520 unsigned char mask,
521 unsigned char val)
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 mask &= 0x0f;
527 val &= 0x0f;
528 if (parport_prologue(pp) < 0)
529 return 0;
530 mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
Mike Dunn63b91762010-04-15 17:02:09 -0400531 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400532 dcr = mos_parport->shadowDCR & 0x0f;
533 parport_epilogue(pp);
534 return dcr;
535}
536
537static unsigned char parport_mos7715_read_status(struct parport *pp)
538{
539 unsigned char status;
540 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700541
Mike Dunnb69578d2010-04-15 17:01:33 -0400542 spin_lock(&release_lock);
543 mos_parport = pp->private_data;
544 if (unlikely(mos_parport == NULL)) { /* release called */
545 spin_unlock(&release_lock);
546 return 0;
547 }
548 status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
549 spin_unlock(&release_lock);
550 return status;
551}
552
553static void parport_mos7715_enable_irq(struct parport *pp)
554{
Mike Dunnb69578d2010-04-15 17:01:33 -0400555}
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700556
Mike Dunnb69578d2010-04-15 17:01:33 -0400557static void parport_mos7715_disable_irq(struct parport *pp)
558{
Mike Dunnb69578d2010-04-15 17:01:33 -0400559}
560
561static void parport_mos7715_data_forward(struct parport *pp)
562{
563 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700564
Mike Dunnb69578d2010-04-15 17:01:33 -0400565 if (parport_prologue(pp) < 0)
566 return;
567 mos7715_change_mode(mos_parport, PS2);
568 mos_parport->shadowDCR &= ~0x20;
Mike Dunn63b91762010-04-15 17:02:09 -0400569 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400570 parport_epilogue(pp);
571}
572
573static void parport_mos7715_data_reverse(struct parport *pp)
574{
575 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700576
Mike Dunnb69578d2010-04-15 17:01:33 -0400577 if (parport_prologue(pp) < 0)
578 return;
579 mos7715_change_mode(mos_parport, PS2);
580 mos_parport->shadowDCR |= 0x20;
Mike Dunn63b91762010-04-15 17:02:09 -0400581 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400582 parport_epilogue(pp);
583}
584
585static void parport_mos7715_init_state(struct pardevice *dev,
586 struct parport_state *s)
587{
Mike Dunnb69578d2010-04-15 17:01:33 -0400588 s->u.pc.ctr = DCR_INIT_VAL;
589 s->u.pc.ecr = ECR_INIT_VAL;
590}
591
592/* N.B. Parport core code requires that this function not block */
593static void parport_mos7715_save_state(struct parport *pp,
594 struct parport_state *s)
595{
596 struct mos7715_parport *mos_parport;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700597
Mike Dunnb69578d2010-04-15 17:01:33 -0400598 spin_lock(&release_lock);
599 mos_parport = pp->private_data;
600 if (unlikely(mos_parport == NULL)) { /* release called */
601 spin_unlock(&release_lock);
602 return;
603 }
604 s->u.pc.ctr = mos_parport->shadowDCR;
605 s->u.pc.ecr = mos_parport->shadowECR;
606 spin_unlock(&release_lock);
607}
608
609/* N.B. Parport core code requires that this function not block */
610static void parport_mos7715_restore_state(struct parport *pp,
611 struct parport_state *s)
612{
613 struct mos7715_parport *mos_parport;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700614
Mike Dunnb69578d2010-04-15 17:01:33 -0400615 spin_lock(&release_lock);
616 mos_parport = pp->private_data;
617 if (unlikely(mos_parport == NULL)) { /* release called */
618 spin_unlock(&release_lock);
619 return;
620 }
621 write_parport_reg_nonblock(mos_parport, DCR, mos_parport->shadowDCR);
622 write_parport_reg_nonblock(mos_parport, ECR, mos_parport->shadowECR);
623 spin_unlock(&release_lock);
624}
625
626static size_t parport_mos7715_write_compat(struct parport *pp,
627 const void *buffer,
628 size_t len, int flags)
629{
630 int retval;
631 struct mos7715_parport *mos_parport = pp->private_data;
632 int actual_len;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700633
Mike Dunnb69578d2010-04-15 17:01:33 -0400634 if (parport_prologue(pp) < 0)
635 return 0;
636 mos7715_change_mode(mos_parport, PPF);
637 retval = usb_bulk_msg(mos_parport->serial->dev,
638 usb_sndbulkpipe(mos_parport->serial->dev, 2),
639 (void *)buffer, len, &actual_len,
640 MOS_WDR_TIMEOUT);
641 parport_epilogue(pp);
642 if (retval) {
643 dev_err(&mos_parport->serial->dev->dev,
644 "mos7720: usb_bulk_msg() failed: %d", retval);
645 return 0;
646 }
647 return actual_len;
648}
649
650static struct parport_operations parport_mos7715_ops = {
651 .owner = THIS_MODULE,
652 .write_data = parport_mos7715_write_data,
653 .read_data = parport_mos7715_read_data,
654
655 .write_control = parport_mos7715_write_control,
656 .read_control = parport_mos7715_read_control,
657 .frob_control = parport_mos7715_frob_control,
658
659 .read_status = parport_mos7715_read_status,
660
661 .enable_irq = parport_mos7715_enable_irq,
662 .disable_irq = parport_mos7715_disable_irq,
663
664 .data_forward = parport_mos7715_data_forward,
665 .data_reverse = parport_mos7715_data_reverse,
666
667 .init_state = parport_mos7715_init_state,
668 .save_state = parport_mos7715_save_state,
669 .restore_state = parport_mos7715_restore_state,
670
671 .compat_write_data = parport_mos7715_write_compat,
672
673 .nibble_read_data = parport_ieee1284_read_nibble,
674 .byte_read_data = parport_ieee1284_read_byte,
675};
676
677/*
678 * Allocate and initialize parallel port control struct, initialize
679 * the parallel port hardware device, and register with the parport subsystem.
680 */
681static int mos7715_parport_init(struct usb_serial *serial)
682{
683 struct mos7715_parport *mos_parport;
684
685 /* allocate and initialize parallel port control struct */
686 mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
687 if (mos_parport == NULL) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700688 dev_dbg(&serial->dev->dev, "%s: kzalloc failed\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400689 return -ENOMEM;
690 }
691 mos_parport->msg_pending = false;
692 kref_init(&mos_parport->ref_count);
693 spin_lock_init(&mos_parport->listlock);
694 INIT_LIST_HEAD(&mos_parport->active_urbs);
695 INIT_LIST_HEAD(&mos_parport->deferred_urbs);
696 usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
697 mos_parport->serial = serial;
698 tasklet_init(&mos_parport->urb_tasklet, send_deferred_urbs,
699 (unsigned long) mos_parport);
700 init_completion(&mos_parport->syncmsg_compl);
701
702 /* cycle parallel port reset bit */
Mike Dunn63b91762010-04-15 17:02:09 -0400703 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x80);
704 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x00);
Mike Dunnb69578d2010-04-15 17:01:33 -0400705
706 /* initialize device registers */
707 mos_parport->shadowDCR = DCR_INIT_VAL;
Mike Dunn63b91762010-04-15 17:02:09 -0400708 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400709 mos_parport->shadowECR = ECR_INIT_VAL;
Mike Dunn63b91762010-04-15 17:02:09 -0400710 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400711
712 /* register with parport core */
713 mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE,
714 PARPORT_DMA_NONE,
715 &parport_mos7715_ops);
716 if (mos_parport->pp == NULL) {
717 dev_err(&serial->interface->dev,
718 "Could not register parport\n");
719 kref_put(&mos_parport->ref_count, destroy_mos_parport);
720 return -EIO;
721 }
722 mos_parport->pp->private_data = mos_parport;
723 mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP;
724 mos_parport->pp->dev = &serial->interface->dev;
725 parport_announce_port(mos_parport->pp);
726
727 return 0;
728}
729#endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700730
731/*
732 * mos7720_interrupt_callback
733 * this is the callback function for when we have received data on the
734 * interrupt endpoint.
735 */
736static void mos7720_interrupt_callback(struct urb *urb)
737{
738 int result;
739 int length;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700740 int status = urb->status;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700741 struct device *dev = &urb->dev->dev;
Oliver Neukum325b70c2007-03-19 13:58:29 +0100742 __u8 *data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700743 __u8 sp1;
744 __u8 sp2;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700745
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700746 switch (status) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700747 case 0:
748 /* success */
749 break;
750 case -ECONNRESET:
751 case -ENOENT:
752 case -ESHUTDOWN:
753 /* this urb is terminated, clean up */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700754 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700755 return;
756 default:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700757 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700758 goto exit;
759 }
760
761 length = urb->actual_length;
762 data = urb->transfer_buffer;
763
764 /* Moschip get 4 bytes
765 * Byte 1 IIR Port 1 (port.number is 0)
766 * Byte 2 IIR Port 2 (port.number is 1)
767 * Byte 3 --------------
768 * Byte 4 FIFO status for both */
Oliver Neukum325b70c2007-03-19 13:58:29 +0100769
770 /* the above description is inverted
771 * oneukum 2007-03-14 */
772
773 if (unlikely(length != 4)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700774 dev_dbg(dev, "Wrong data !!!\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700775 return;
776 }
777
Oliver Neukum325b70c2007-03-19 13:58:29 +0100778 sp1 = data[3];
779 sp2 = data[2];
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700780
Oliver Neukum325b70c2007-03-19 13:58:29 +0100781 if ((sp1 | sp2) & 0x01) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700782 /* No Interrupt Pending in both the ports */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700783 dev_dbg(dev, "No Interrupt !!!\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700784 } else {
785 switch (sp1 & 0x0f) {
786 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700787 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 -0700788 break;
789 case SERIAL_IIR_CTI:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700790 dev_dbg(dev, "Serial Port 1: Receiver time out\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700791 break;
792 case SERIAL_IIR_MS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700793 /* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700794 break;
795 }
796
797 switch (sp2 & 0x0f) {
798 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700799 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 -0700800 break;
801 case SERIAL_IIR_CTI:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700802 dev_dbg(dev, "Serial Port 2: Receiver time out\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700803 break;
804 case SERIAL_IIR_MS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700805 /* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700806 break;
807 }
808 }
809
810exit:
811 result = usb_submit_urb(urb, GFP_ATOMIC);
812 if (result)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700813 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700814}
815
816/*
Mike Dunnfb088e32010-01-26 12:12:12 -0500817 * mos7715_interrupt_callback
818 * this is the 7715's callback function for when we have received data on
819 * the interrupt endpoint.
820 */
821static void mos7715_interrupt_callback(struct urb *urb)
822{
823 int result;
824 int length;
825 int status = urb->status;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700826 struct device *dev = &urb->dev->dev;
Mike Dunnfb088e32010-01-26 12:12:12 -0500827 __u8 *data;
828 __u8 iir;
829
830 switch (status) {
831 case 0:
832 /* success */
833 break;
834 case -ECONNRESET:
835 case -ENOENT:
836 case -ESHUTDOWN:
Mike Dunnb69578d2010-04-15 17:01:33 -0400837 case -ENODEV:
Mike Dunnfb088e32010-01-26 12:12:12 -0500838 /* this urb is terminated, clean up */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700839 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
Mike Dunnfb088e32010-01-26 12:12:12 -0500840 return;
841 default:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700842 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
Mike Dunnfb088e32010-01-26 12:12:12 -0500843 goto exit;
844 }
845
846 length = urb->actual_length;
847 data = urb->transfer_buffer;
848
849 /* Structure of data from 7715 device:
850 * Byte 1: IIR serial Port
851 * Byte 2: unused
852 * Byte 2: DSR parallel port
853 * Byte 4: FIFO status for both */
854
855 if (unlikely(length != 4)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700856 dev_dbg(dev, "Wrong data !!!\n");
Mike Dunnfb088e32010-01-26 12:12:12 -0500857 return;
858 }
859
860 iir = data[0];
861 if (!(iir & 0x01)) { /* serial port interrupt pending */
862 switch (iir & 0x0f) {
863 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700864 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 -0500865 break;
866 case SERIAL_IIR_CTI:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700867 dev_dbg(dev, "Serial Port: Receiver time out\n");
Mike Dunnfb088e32010-01-26 12:12:12 -0500868 break;
869 case SERIAL_IIR_MS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700870 /* dev_dbg(dev, "Serial Port: Modem status change\n"); */
Mike Dunnfb088e32010-01-26 12:12:12 -0500871 break;
872 }
873 }
874
Mike Dunnb69578d2010-04-15 17:01:33 -0400875#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
876 { /* update local copy of DSR reg */
877 struct usb_serial_port *port = urb->context;
878 struct mos7715_parport *mos_parport = port->serial->private;
879 if (unlikely(mos_parport == NULL))
880 return;
881 atomic_set(&mos_parport->shadowDSR, data[2]);
882 }
883#endif
884
Mike Dunnfb088e32010-01-26 12:12:12 -0500885exit:
886 result = usb_submit_urb(urb, GFP_ATOMIC);
887 if (result)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700888 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
Mike Dunnfb088e32010-01-26 12:12:12 -0500889}
890
891/*
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700892 * mos7720_bulk_in_callback
893 * this is the callback function for when we have received data on the
894 * bulk in endpoint.
895 */
896static void mos7720_bulk_in_callback(struct urb *urb)
897{
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700898 int retval;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700899 unsigned char *data ;
900 struct usb_serial_port *port;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700901 int status = urb->status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700902
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700903 if (status) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700904 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700905 return;
906 }
907
Mike Dunnb69578d2010-04-15 17:01:33 -0400908 port = urb->context;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700909
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700910 dev_dbg(&port->dev, "Entering...%s\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700911
912 data = urb->transfer_buffer;
913
Jiri Slaby2e124b42013-01-03 15:53:06 +0100914 if (urb->actual_length) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100915 tty_insert_flip_string(&port->port, data, urb->actual_length);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100916 tty_flip_buffer_push(&port->port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700917 }
918
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700919 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700920 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
921 if (retval)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700922 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700923 }
924}
925
926/*
927 * mos7720_bulk_out_data_callback
928 * this is the callback function for when we have finished sending serial
929 * data on the bulk out endpoint.
930 */
931static void mos7720_bulk_out_data_callback(struct urb *urb)
932{
933 struct moschip_port *mos7720_port;
934 struct tty_struct *tty;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700935 int status = urb->status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700936
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700937 if (status) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700938 dev_dbg(&urb->dev->dev, "nonzero write bulk status received:%d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700939 return;
940 }
941
942 mos7720_port = urb->context;
943 if (!mos7720_port) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700944 dev_dbg(&urb->dev->dev, "NULL mos7720_port pointer\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700945 return ;
946 }
947
Alan Cox4a90f092008-10-13 10:39:46 +0100948 tty = tty_port_tty_get(&mos7720_port->port->port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700949
Jiri Slabyb963a842007-02-10 01:44:55 -0800950 if (tty && mos7720_port->open)
951 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100952 tty_kref_put(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700953}
954
955/*
Mike Dunnfb088e32010-01-26 12:12:12 -0500956 * mos77xx_probe
957 * this function installs the appropriate read interrupt endpoint callback
958 * depending on whether the device is a 7720 or 7715, thus avoiding costly
959 * run-time checks in the high-frequency callback routine itself.
960 */
961static int mos77xx_probe(struct usb_serial *serial,
962 const struct usb_device_id *id)
963{
964 if (id->idProduct == MOSCHIP_DEVICE_ID_7715)
965 moschip7720_2port_driver.read_int_callback =
966 mos7715_interrupt_callback;
967 else
968 moschip7720_2port_driver.read_int_callback =
969 mos7720_interrupt_callback;
970
971 return 0;
972}
973
974static int mos77xx_calc_num_ports(struct usb_serial *serial)
975{
976 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
977 if (product == MOSCHIP_DEVICE_ID_7715)
978 return 1;
979
980 return 2;
981}
982
Alan Coxa509a7e2009-09-19 13:13:26 -0700983static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700984{
985 struct usb_serial *serial;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700986 struct urb *urb;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700987 struct moschip_port *mos7720_port;
988 int response;
989 int port_number;
Mike Dunn63b91762010-04-15 17:02:09 -0400990 __u8 data;
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100991 int allocated_urbs = 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700992 int j;
993
994 serial = port->serial;
995
996 mos7720_port = usb_get_serial_port_data(port);
997 if (mos7720_port == NULL)
998 return -ENODEV;
999
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001000 usb_clear_halt(serial->dev, port->write_urb->pipe);
1001 usb_clear_halt(serial->dev, port->read_urb->pipe);
1002
1003 /* Initialising the write urb pool */
1004 for (j = 0; j < NUM_URBS; ++j) {
Alan Cox4da1a172008-07-22 11:16:21 +01001005 urb = usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001006 mos7720_port->write_urb_pool[j] = urb;
1007
1008 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001009 dev_err(&port->dev, "No more urbs???\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001010 continue;
1011 }
1012
1013 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1014 GFP_KERNEL);
1015 if (!urb->transfer_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001016 dev_err(&port->dev,
1017 "%s-out of memory for urb buffers.\n",
1018 __func__);
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001019 usb_free_urb(mos7720_port->write_urb_pool[j]);
1020 mos7720_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001021 continue;
1022 }
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001023 allocated_urbs++;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001024 }
1025
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001026 if (!allocated_urbs)
1027 return -ENOMEM;
1028
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001029 /* Initialize MCS7720 -- Write Init values to corresponding Registers
1030 *
1031 * Register Index
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001032 * 0 : THR/RHR
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001033 * 1 : IER
1034 * 2 : FCR
1035 * 3 : LCR
1036 * 4 : MCR
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001037 * 5 : LSR
1038 * 6 : MSR
1039 * 7 : SPR
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001040 *
1041 * 0x08 : SP1/2 Control Reg
1042 */
1043 port_number = port->number - port->serial->minor;
Mike Dunn63b91762010-04-15 17:02:09 -04001044 read_mos_reg(serial, port_number, LSR, &data);
1045
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001046 dev_dbg(&port->dev, "SS::%p LSR:%x\n", mos7720_port, data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001047
Mike Dunn63b91762010-04-15 17:02:09 -04001048 write_mos_reg(serial, dummy, SP1_REG, 0x02);
1049 write_mos_reg(serial, dummy, SP2_REG, 0x02);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001050
Mike Dunn63b91762010-04-15 17:02:09 -04001051 write_mos_reg(serial, port_number, IER, 0x00);
1052 write_mos_reg(serial, port_number, FCR, 0x00);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001053
Mike Dunn63b91762010-04-15 17:02:09 -04001054 write_mos_reg(serial, port_number, FCR, 0xcf);
1055 mos7720_port->shadowLCR = 0x03;
1056 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1057 mos7720_port->shadowMCR = 0x0b;
1058 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001059
Mike Dunn63b91762010-04-15 17:02:09 -04001060 write_mos_reg(serial, port_number, SP_CONTROL_REG, 0x00);
1061 read_mos_reg(serial, dummy, SP_CONTROL_REG, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001062 data = data | (port->number - port->serial->minor + 1);
Mike Dunn63b91762010-04-15 17:02:09 -04001063 write_mos_reg(serial, dummy, SP_CONTROL_REG, data);
1064 mos7720_port->shadowLCR = 0x83;
1065 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1066 write_mos_reg(serial, port_number, THR, 0x0c);
1067 write_mos_reg(serial, port_number, IER, 0x00);
1068 mos7720_port->shadowLCR = 0x03;
1069 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1070 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001071
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001072 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
1073 if (response)
Alan Cox4da1a172008-07-22 11:16:21 +01001074 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
1075 __func__, response);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001076
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001077 /* initialize our port settings */
1078 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
1079
1080 /* send a open port command */
1081 mos7720_port->open = 1;
1082
1083 return 0;
1084}
1085
1086/*
1087 * mos7720_chars_in_buffer
1088 * this function is called by the tty driver when it wants to know how many
1089 * bytes of data we currently have outstanding in the port (data that has
1090 * been written, but hasn't made it out the port yet)
1091 * If successful, we return the number of bytes left to be written in the
1092 * system,
1093 * Otherwise we return a negative error number.
1094 */
Alan Cox95da3102008-07-22 11:09:07 +01001095static int mos7720_chars_in_buffer(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001096{
Alan Cox95da3102008-07-22 11:09:07 +01001097 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001098 int i;
1099 int chars = 0;
1100 struct moschip_port *mos7720_port;
1101
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001102 mos7720_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001103 if (mos7720_port == NULL)
Alan Cox23198fd2009-07-20 16:05:27 +01001104 return 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001105
1106 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001107 if (mos7720_port->write_urb_pool[i] &&
1108 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001109 chars += URB_TRANSFER_BUFFER_SIZE;
1110 }
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001111 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001112 return chars;
1113}
1114
Alan Cox335f8512009-06-11 12:26:29 +01001115static void mos7720_close(struct usb_serial_port *port)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001116{
1117 struct usb_serial *serial;
1118 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001119 int j;
1120
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001121 serial = port->serial;
1122
1123 mos7720_port = usb_get_serial_port_data(port);
1124 if (mos7720_port == NULL)
1125 return;
1126
1127 for (j = 0; j < NUM_URBS; ++j)
1128 usb_kill_urb(mos7720_port->write_urb_pool[j]);
1129
1130 /* Freeing Write URBs */
1131 for (j = 0; j < NUM_URBS; ++j) {
1132 if (mos7720_port->write_urb_pool[j]) {
1133 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
1134 usb_free_urb(mos7720_port->write_urb_pool[j]);
1135 }
1136 }
1137
1138 /* While closing port, shutdown all bulk read, write *
Oliver Neukuma1cd7e92008-01-16 17:18:52 +01001139 * and interrupt read if they exists, otherwise nop */
Oliver Neukuma1cd7e92008-01-16 17:18:52 +01001140 usb_kill_urb(port->write_urb);
Oliver Neukuma1cd7e92008-01-16 17:18:52 +01001141 usb_kill_urb(port->read_urb);
1142
Johan Hovoldcf41aa92013-03-21 12:37:41 +01001143 write_mos_reg(serial, port->number - port->serial->minor, MCR, 0x00);
1144 write_mos_reg(serial, port->number - port->serial->minor, IER, 0x00);
1145
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001146 mos7720_port->open = 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001147}
1148
Alan Cox95da3102008-07-22 11:09:07 +01001149static void mos7720_break(struct tty_struct *tty, int break_state)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001150{
Alan Cox95da3102008-07-22 11:09:07 +01001151 struct usb_serial_port *port = tty->driver_data;
Alan Cox4da1a172008-07-22 11:16:21 +01001152 unsigned char data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001153 struct usb_serial *serial;
1154 struct moschip_port *mos7720_port;
1155
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001156 serial = port->serial;
1157
1158 mos7720_port = usb_get_serial_port_data(port);
1159 if (mos7720_port == NULL)
1160 return;
1161
1162 if (break_state == -1)
1163 data = mos7720_port->shadowLCR | UART_LCR_SBC;
1164 else
1165 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
1166
1167 mos7720_port->shadowLCR = data;
Mike Dunn63b91762010-04-15 17:02:09 -04001168 write_mos_reg(serial, port->number - port->serial->minor,
1169 LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001170}
1171
1172/*
1173 * mos7720_write_room
1174 * this function is called by the tty driver when it wants to know how many
1175 * bytes of data we can accept for a specific port.
1176 * If successful, we return the amount of room that we have for this port
1177 * Otherwise we return a negative error number.
1178 */
Alan Cox95da3102008-07-22 11:09:07 +01001179static int mos7720_write_room(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001180{
Alan Cox95da3102008-07-22 11:09:07 +01001181 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001182 struct moschip_port *mos7720_port;
1183 int room = 0;
1184 int i;
1185
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001186 mos7720_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001187 if (mos7720_port == NULL)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001188 return -ENODEV;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001189
Alan Coxa5b6f602008-04-08 17:16:06 +01001190 /* FIXME: Locking */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001191 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001192 if (mos7720_port->write_urb_pool[i] &&
1193 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001194 room += URB_TRANSFER_BUFFER_SIZE;
1195 }
1196
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001197 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001198 return room;
1199}
1200
Alan Cox95da3102008-07-22 11:09:07 +01001201static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
1202 const unsigned char *data, int count)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001203{
1204 int status;
1205 int i;
1206 int bytes_sent = 0;
1207 int transfer_size;
1208
1209 struct moschip_port *mos7720_port;
1210 struct usb_serial *serial;
1211 struct urb *urb;
1212 const unsigned char *current_position = data;
1213
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001214 serial = port->serial;
1215
1216 mos7720_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001217 if (mos7720_port == NULL)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001218 return -ENODEV;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001219
1220 /* try to find a free urb in the list */
1221 urb = NULL;
1222
1223 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001224 if (mos7720_port->write_urb_pool[i] &&
1225 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001226 urb = mos7720_port->write_urb_pool[i];
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001227 dev_dbg(&port->dev, "URB:%d\n", i);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001228 break;
1229 }
1230 }
1231
1232 if (urb == NULL) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001233 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001234 goto exit;
1235 }
1236
1237 if (urb->transfer_buffer == NULL) {
1238 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1239 GFP_KERNEL);
1240 if (urb->transfer_buffer == NULL) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001241 dev_err_console(port, "%s no more kernel memory...\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001242 __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001243 goto exit;
1244 }
1245 }
Alan Cox4da1a172008-07-22 11:16:21 +01001246 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001247
1248 memcpy(urb->transfer_buffer, current_position, transfer_size);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001249 usb_serial_debug_data(&port->dev, __func__, transfer_size,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001250 urb->transfer_buffer);
1251
1252 /* fill urb with data and submit */
1253 usb_fill_bulk_urb(urb, serial->dev,
1254 usb_sndbulkpipe(serial->dev,
Alan Cox4da1a172008-07-22 11:16:21 +01001255 port->bulk_out_endpointAddress),
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001256 urb->transfer_buffer, transfer_size,
1257 mos7720_bulk_out_data_callback, mos7720_port);
1258
1259 /* send it down the pipe */
Alan Cox4da1a172008-07-22 11:16:21 +01001260 status = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001261 if (status) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001262 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001263 "with status = %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001264 bytes_sent = status;
1265 goto exit;
1266 }
1267 bytes_sent = transfer_size;
1268
1269exit:
1270 return bytes_sent;
1271}
1272
Alan Cox95da3102008-07-22 11:09:07 +01001273static void mos7720_throttle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001274{
Alan Cox95da3102008-07-22 11:09:07 +01001275 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001276 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001277 int status;
1278
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001279 mos7720_port = usb_get_serial_port_data(port);
1280
1281 if (mos7720_port == NULL)
1282 return;
1283
1284 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001285 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001286 return;
1287 }
1288
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001289 /* if we are implementing XON/XOFF, send the stop character */
1290 if (I_IXOFF(tty)) {
1291 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001292 status = mos7720_write(tty, port, &stop_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001293 if (status <= 0)
1294 return;
1295 }
1296
1297 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001298 if (tty->termios.c_cflag & CRTSCTS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001299 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
Mike Dunn63b91762010-04-15 17:02:09 -04001300 write_mos_reg(port->serial, port->number - port->serial->minor,
1301 MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001302 if (status != 0)
1303 return;
1304 }
1305}
1306
Alan Cox95da3102008-07-22 11:09:07 +01001307static void mos7720_unthrottle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001308{
Alan Cox95da3102008-07-22 11:09:07 +01001309 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001310 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +01001311 int status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001312
1313 if (mos7720_port == NULL)
1314 return;
1315
1316 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001317 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001318 return;
1319 }
1320
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001321 /* if we are implementing XON/XOFF, send the start character */
1322 if (I_IXOFF(tty)) {
1323 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001324 status = mos7720_write(tty, port, &start_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001325 if (status <= 0)
1326 return;
1327 }
1328
1329 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001330 if (tty->termios.c_cflag & CRTSCTS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001331 mos7720_port->shadowMCR |= UART_MCR_RTS;
Mike Dunn63b91762010-04-15 17:02:09 -04001332 write_mos_reg(port->serial, port->number - port->serial->minor,
1333 MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001334 if (status != 0)
1335 return;
1336 }
1337}
1338
Mike Dunnb69578d2010-04-15 17:01:33 -04001339/* FIXME: this function does not work */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001340static int set_higher_rates(struct moschip_port *mos7720_port,
1341 unsigned int baud)
1342{
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001343 struct usb_serial_port *port;
1344 struct usb_serial *serial;
1345 int port_number;
Mike Dunn63b91762010-04-15 17:02:09 -04001346 enum mos_regs sp_reg;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001347 if (mos7720_port == NULL)
1348 return -EINVAL;
1349
1350 port = mos7720_port->port;
1351 serial = port->serial;
1352
Alan Cox4da1a172008-07-22 11:16:21 +01001353 /***********************************************
1354 * Init Sequence for higher rates
1355 ***********************************************/
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001356 dev_dbg(&port->dev, "Sending Setting Commands ..........\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001357 port_number = port->number - port->serial->minor;
1358
Mike Dunn63b91762010-04-15 17:02:09 -04001359 write_mos_reg(serial, port_number, IER, 0x00);
1360 write_mos_reg(serial, port_number, FCR, 0x00);
1361 write_mos_reg(serial, port_number, FCR, 0xcf);
1362 mos7720_port->shadowMCR = 0x0b;
1363 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1364 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x00);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001365
Alan Cox4da1a172008-07-22 11:16:21 +01001366 /***********************************************
1367 * Set for higher rates *
1368 ***********************************************/
Mike Dunnb69578d2010-04-15 17:01:33 -04001369 /* writing baud rate verbatum into uart clock field clearly not right */
Mike Dunn63b91762010-04-15 17:02:09 -04001370 if (port_number == 0)
1371 sp_reg = SP1_REG;
1372 else
1373 sp_reg = SP2_REG;
1374 write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
1375 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x03);
1376 mos7720_port->shadowMCR = 0x2b;
1377 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001378
Alan Cox4da1a172008-07-22 11:16:21 +01001379 /***********************************************
1380 * Set DLL/DLM
1381 ***********************************************/
Mike Dunn63b91762010-04-15 17:02:09 -04001382 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1383 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1384 write_mos_reg(serial, port_number, DLL, 0x01);
1385 write_mos_reg(serial, port_number, DLM, 0x00);
1386 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1387 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001388
1389 return 0;
1390}
1391
1392/* baud rate information */
Alan Cox4da1a172008-07-22 11:16:21 +01001393struct divisor_table_entry {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001394 __u32 baudrate;
1395 __u16 divisor;
1396};
1397
1398/* Define table of divisors for moschip 7720 hardware *
1399 * These assume a 3.6864MHz crystal, the standard /16, and *
1400 * MCR.7 = 0. */
1401static struct divisor_table_entry divisor_table[] = {
1402 { 50, 2304},
1403 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
1404 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
1405 { 150, 768},
1406 { 300, 384},
1407 { 600, 192},
1408 { 1200, 96},
1409 { 1800, 64},
1410 { 2400, 48},
1411 { 4800, 24},
1412 { 7200, 16},
1413 { 9600, 12},
1414 { 19200, 6},
1415 { 38400, 3},
1416 { 57600, 2},
1417 { 115200, 1},
1418};
1419
1420/*****************************************************************************
1421 * calc_baud_rate_divisor
1422 * this function calculates the proper baud rate divisor for the specified
1423 * baud rate.
1424 *****************************************************************************/
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001425static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, int *divisor)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001426{
1427 int i;
1428 __u16 custom;
1429 __u16 round1;
1430 __u16 round;
1431
1432
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001433 dev_dbg(&port->dev, "%s - %d\n", __func__, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001434
1435 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
1436 if (divisor_table[i].baudrate == baudrate) {
1437 *divisor = divisor_table[i].divisor;
1438 return 0;
1439 }
1440 }
1441
Alan Cox4da1a172008-07-22 11:16:21 +01001442 /* After trying for all the standard baud rates *
1443 * Try calculating the divisor for this baud rate */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001444 if (baudrate > 75 && baudrate < 230400) {
1445 /* get the divisor */
1446 custom = (__u16)(230400L / baudrate);
1447
1448 /* Check for round off */
1449 round1 = (__u16)(2304000L / baudrate);
1450 round = (__u16)(round1 - (custom * 10));
1451 if (round > 4)
1452 custom++;
1453 *divisor = custom;
1454
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001455 dev_dbg(&port->dev, "Baud %d = %d\n", baudrate, custom);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001456 return 0;
1457 }
1458
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001459 dev_dbg(&port->dev, "Baud calculation Failed...\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001460 return -EINVAL;
1461}
1462
1463/*
1464 * send_cmd_write_baud_rate
1465 * this function sends the proper command to change the baud rate of the
1466 * specified port.
1467 */
1468static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1469 int baudrate)
1470{
1471 struct usb_serial_port *port;
1472 struct usb_serial *serial;
1473 int divisor;
1474 int status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001475 unsigned char number;
1476
1477 if (mos7720_port == NULL)
1478 return -1;
1479
1480 port = mos7720_port->port;
1481 serial = port->serial;
1482
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001483 number = port->number - port->serial->minor;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001484 dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001485
Alan Cox4da1a172008-07-22 11:16:21 +01001486 /* Calculate the Divisor */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001487 status = calc_baud_rate_divisor(port, baudrate, &divisor);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001488 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001489 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001490 return status;
1491 }
1492
Alan Cox4da1a172008-07-22 11:16:21 +01001493 /* Enable access to divisor latch */
Mike Dunn63b91762010-04-15 17:02:09 -04001494 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1495 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001496
1497 /* Write the divisor */
Mike Dunn63b91762010-04-15 17:02:09 -04001498 write_mos_reg(serial, number, DLL, (__u8)(divisor & 0xff));
1499 write_mos_reg(serial, number, DLM, (__u8)((divisor & 0xff00) >> 8));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001500
Alan Cox4da1a172008-07-22 11:16:21 +01001501 /* Disable access to divisor latch */
Mike Dunn63b91762010-04-15 17:02:09 -04001502 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1503 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001504
1505 return status;
1506}
1507
1508/*
1509 * change_port_settings
1510 * This routine is called to set the UART on the device to match
1511 * the specified new settings.
1512 */
Alan Cox95da3102008-07-22 11:09:07 +01001513static void change_port_settings(struct tty_struct *tty,
1514 struct moschip_port *mos7720_port,
Alan Cox606d0992006-12-08 02:38:45 -08001515 struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001516{
1517 struct usb_serial_port *port;
1518 struct usb_serial *serial;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001519 int baud;
1520 unsigned cflag;
1521 unsigned iflag;
1522 __u8 mask = 0xff;
1523 __u8 lData;
1524 __u8 lParity;
1525 __u8 lStop;
1526 int status;
1527 int port_number;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001528
1529 if (mos7720_port == NULL)
1530 return ;
1531
1532 port = mos7720_port->port;
1533 serial = port->serial;
1534 port_number = port->number - port->serial->minor;
1535
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001536 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001537 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001538 return;
1539 }
1540
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001541 lData = UART_LCR_WLEN8;
1542 lStop = 0x00; /* 1 stop bit */
1543 lParity = 0x00; /* No parity */
1544
Alan Coxadc8d742012-07-14 15:31:47 +01001545 cflag = tty->termios.c_cflag;
1546 iflag = tty->termios.c_iflag;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001547
1548 /* Change the number of bits */
1549 switch (cflag & CSIZE) {
1550 case CS5:
1551 lData = UART_LCR_WLEN5;
1552 mask = 0x1f;
1553 break;
1554
1555 case CS6:
1556 lData = UART_LCR_WLEN6;
1557 mask = 0x3f;
1558 break;
1559
1560 case CS7:
1561 lData = UART_LCR_WLEN7;
1562 mask = 0x7f;
1563 break;
1564 default:
1565 case CS8:
1566 lData = UART_LCR_WLEN8;
1567 break;
1568 }
1569
1570 /* Change the Parity bit */
1571 if (cflag & PARENB) {
1572 if (cflag & PARODD) {
1573 lParity = UART_LCR_PARITY;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001574 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001575 } else {
1576 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001577 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001578 }
1579
1580 } else {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001581 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001582 }
1583
1584 if (cflag & CMSPAR)
1585 lParity = lParity | 0x20;
1586
1587 /* Change the Stop bit */
1588 if (cflag & CSTOPB) {
1589 lStop = UART_LCR_STOP;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001590 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001591 } else {
1592 lStop = 0x00;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001593 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001594 }
1595
1596#define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1597#define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1598#define LCR_PAR_MASK 0x38 /* Mask for parity field */
1599
1600 /* Update the LCR with the correct value */
Alan Cox4da1a172008-07-22 11:16:21 +01001601 mos7720_port->shadowLCR &=
Mike Dunn63b91762010-04-15 17:02:09 -04001602 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001603 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1604
1605
1606 /* Disable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001607 write_mos_reg(serial, port_number, IER, 0x00);
1608 write_mos_reg(serial, port_number, FCR, 0x00);
1609 write_mos_reg(serial, port_number, FCR, 0xcf);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001610
1611 /* Send the updated LCR value to the mos7720 */
Mike Dunn63b91762010-04-15 17:02:09 -04001612 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1613 mos7720_port->shadowMCR = 0x0b;
1614 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001615
1616 /* set up the MCR register and send it to the mos7720 */
1617 mos7720_port->shadowMCR = UART_MCR_OUT2;
1618 if (cflag & CBAUD)
1619 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1620
1621 if (cflag & CRTSCTS) {
1622 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
Alan Cox4da1a172008-07-22 11:16:21 +01001623 /* To set hardware flow control to the specified *
1624 * serial port, in SP1/2_CONTROL_REG */
Mike Dunn63b91762010-04-15 17:02:09 -04001625 if (port->number)
1626 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x01);
1627 else
1628 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x02);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001629
Mike Dunn63b91762010-04-15 17:02:09 -04001630 } else
1631 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1632
1633 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001634
1635 /* Determine divisor based on baud rate */
1636 baud = tty_get_baud_rate(tty);
1637 if (!baud) {
1638 /* pick a default, any default... */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001639 dev_dbg(&port->dev, "Picked default baud...\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001640 baud = 9600;
1641 }
1642
1643 if (baud >= 230400) {
1644 set_higher_rates(mos7720_port, baud);
1645 /* Enable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001646 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001647 return;
1648 }
1649
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001650 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001651 status = send_cmd_write_baud_rate(mos7720_port, baud);
Alan Cox65d063a2008-01-03 17:01:18 +00001652 /* FIXME: needs to write actual resulting baud back not just
1653 blindly do so */
1654 if (cflag & CBAUD)
1655 tty_encode_baud_rate(tty, baud, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001656 /* Enable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001657 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001658
1659 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001660 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1661 if (status)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001662 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001663 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001664}
1665
1666/*
1667 * mos7720_set_termios
1668 * this function is called by the tty driver when it wants to change the
1669 * termios structure.
1670 */
Alan Cox95da3102008-07-22 11:09:07 +01001671static void mos7720_set_termios(struct tty_struct *tty,
1672 struct usb_serial_port *port, struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001673{
1674 int status;
1675 unsigned int cflag;
1676 struct usb_serial *serial;
1677 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001678
1679 serial = port->serial;
1680
1681 mos7720_port = usb_get_serial_port_data(port);
1682
1683 if (mos7720_port == NULL)
1684 return;
1685
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001686 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001687 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001688 return;
1689 }
1690
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001691 dev_dbg(&port->dev, "setting termios - ASPIRE\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001692
Alan Coxadc8d742012-07-14 15:31:47 +01001693 cflag = tty->termios.c_cflag;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001694
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001695 dev_dbg(&port->dev, "%s - cflag %08x iflag %08x\n", __func__,
Linus Torvaldsd9a80742012-10-01 13:23:01 -07001696 tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001697
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001698 dev_dbg(&port->dev, "%s - old cflag %08x old iflag %08x\n", __func__,
1699 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001700
1701 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001702 change_port_settings(tty, mos7720_port, old_termios);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001703
Alan Cox4da1a172008-07-22 11:16:21 +01001704 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001705 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1706 if (status)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001707 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001708 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001709}
1710
1711/*
1712 * get_lsr_info - get line status register info
1713 *
1714 * Purpose: Let user call ioctl() to get info when the UART physically
1715 * is emptied. On bus types like RS485, the transmitter must
1716 * release the bus after transmitting. This must be done when
1717 * the transmit shift register is empty, not be done when the
1718 * transmit holding register is empty. This functionality
1719 * allows an RS485 driver to be written in user space.
1720 */
Alan Cox4da1a172008-07-22 11:16:21 +01001721static int get_lsr_info(struct tty_struct *tty,
1722 struct moschip_port *mos7720_port, unsigned int __user *value)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001723{
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001724 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001725 unsigned int result = 0;
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001726 unsigned char data = 0;
1727 int port_number = port->number - port->serial->minor;
1728 int count;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001729
Alan Cox95da3102008-07-22 11:09:07 +01001730 count = mos7720_chars_in_buffer(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001731 if (count == 0) {
Mike Dunn63b91762010-04-15 17:02:09 -04001732 read_mos_reg(port->serial, port_number, LSR, &data);
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001733 if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1734 == (UART_LSR_TEMT | UART_LSR_THRE)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001735 dev_dbg(&port->dev, "%s -- Empty\n", __func__);
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001736 result = TIOCSER_TEMT;
1737 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001738 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001739 if (copy_to_user(value, &result, sizeof(int)))
1740 return -EFAULT;
1741 return 0;
1742}
1743
Alan Cox60b33c12011-02-14 16:26:14 +00001744static int mos7720_tiocmget(struct tty_struct *tty)
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001745{
1746 struct usb_serial_port *port = tty->driver_data;
1747 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1748 unsigned int result = 0;
1749 unsigned int mcr ;
1750 unsigned int msr ;
1751
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001752 mcr = mos7720_port->shadowMCR;
1753 msr = mos7720_port->shadowMSR;
1754
1755 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1756 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1757 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1758 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
1759 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1760 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
1761
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001762 return result;
1763}
1764
Alan Cox20b9d172011-02-14 16:26:50 +00001765static int mos7720_tiocmset(struct tty_struct *tty,
Mike Dunn63b91762010-04-15 17:02:09 -04001766 unsigned int set, unsigned int clear)
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001767{
1768 struct usb_serial_port *port = tty->driver_data;
1769 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1770 unsigned int mcr ;
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001771
1772 mcr = mos7720_port->shadowMCR;
1773
1774 if (set & TIOCM_RTS)
1775 mcr |= UART_MCR_RTS;
1776 if (set & TIOCM_DTR)
1777 mcr |= UART_MCR_DTR;
1778 if (set & TIOCM_LOOP)
1779 mcr |= UART_MCR_LOOP;
1780
1781 if (clear & TIOCM_RTS)
1782 mcr &= ~UART_MCR_RTS;
1783 if (clear & TIOCM_DTR)
1784 mcr &= ~UART_MCR_DTR;
1785 if (clear & TIOCM_LOOP)
1786 mcr &= ~UART_MCR_LOOP;
1787
1788 mos7720_port->shadowMCR = mcr;
Mike Dunn63b91762010-04-15 17:02:09 -04001789 write_mos_reg(port->serial, port->number - port->serial->minor,
1790 MCR, mos7720_port->shadowMCR);
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001791
1792 return 0;
1793}
1794
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001795static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1796 unsigned int __user *value)
1797{
Alan Cox0bca1b92010-09-16 18:21:40 +01001798 unsigned int mcr;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001799 unsigned int arg;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001800
1801 struct usb_serial_port *port;
1802
1803 if (mos7720_port == NULL)
1804 return -1;
1805
Alan Cox4da1a172008-07-22 11:16:21 +01001806 port = (struct usb_serial_port *)mos7720_port->port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001807 mcr = mos7720_port->shadowMCR;
1808
1809 if (copy_from_user(&arg, value, sizeof(int)))
1810 return -EFAULT;
1811
1812 switch (cmd) {
1813 case TIOCMBIS:
1814 if (arg & TIOCM_RTS)
1815 mcr |= UART_MCR_RTS;
1816 if (arg & TIOCM_DTR)
1817 mcr |= UART_MCR_RTS;
1818 if (arg & TIOCM_LOOP)
1819 mcr |= UART_MCR_LOOP;
1820 break;
1821
1822 case TIOCMBIC:
1823 if (arg & TIOCM_RTS)
1824 mcr &= ~UART_MCR_RTS;
1825 if (arg & TIOCM_DTR)
1826 mcr &= ~UART_MCR_RTS;
1827 if (arg & TIOCM_LOOP)
1828 mcr &= ~UART_MCR_LOOP;
1829 break;
1830
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001831 }
1832
1833 mos7720_port->shadowMCR = mcr;
Mike Dunn63b91762010-04-15 17:02:09 -04001834 write_mos_reg(port->serial, port->number - port->serial->minor,
1835 MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001836
1837 return 0;
1838}
1839
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001840static int get_serial_info(struct moschip_port *mos7720_port,
1841 struct serial_struct __user *retinfo)
1842{
1843 struct serial_struct tmp;
1844
1845 if (!retinfo)
1846 return -EFAULT;
1847
1848 memset(&tmp, 0, sizeof(tmp));
1849
1850 tmp.type = PORT_16550A;
1851 tmp.line = mos7720_port->port->serial->minor;
1852 tmp.port = mos7720_port->port->number;
1853 tmp.irq = 0;
1854 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
Alan Cox4da1a172008-07-22 11:16:21 +01001855 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001856 tmp.baud_base = 9600;
1857 tmp.close_delay = 5*HZ;
1858 tmp.closing_wait = 30*HZ;
1859
1860 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1861 return -EFAULT;
1862 return 0;
1863}
1864
Alan Cox00a0d0d2011-02-14 16:27:06 +00001865static int mos7720_ioctl(struct tty_struct *tty,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001866 unsigned int cmd, unsigned long arg)
1867{
Alan Cox95da3102008-07-22 11:09:07 +01001868 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001869 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001870
1871 mos7720_port = usb_get_serial_port_data(port);
1872 if (mos7720_port == NULL)
1873 return -ENODEV;
1874
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001875 dev_dbg(&port->dev, "%s - cmd = 0x%x", __func__, cmd);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001876
1877 switch (cmd) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001878 case TIOCSERGETLSR:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001879 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
Alan Cox4da1a172008-07-22 11:16:21 +01001880 return get_lsr_info(tty, mos7720_port,
1881 (unsigned int __user *)arg);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001882
Alan Cox95da3102008-07-22 11:09:07 +01001883 /* FIXME: These should be using the mode methods */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001884 case TIOCMBIS:
1885 case TIOCMBIC:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001886 dev_dbg(&port->dev, "%s TIOCMSET/TIOCMBIC/TIOCMSET\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001887 return set_modem_info(mos7720_port, cmd,
1888 (unsigned int __user *)arg);
1889
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001890 case TIOCGSERIAL:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001891 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001892 return get_serial_info(mos7720_port,
1893 (struct serial_struct __user *)arg);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001894 }
1895
1896 return -ENOIOCTLCMD;
1897}
1898
1899static int mos7720_startup(struct usb_serial *serial)
1900{
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001901 struct usb_device *dev;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001902 char data;
Huzaifa Sidhpurwala91f58ae2011-02-21 12:58:45 +05301903 u16 product;
Mike Dunnb69578d2010-04-15 17:01:33 -04001904 int ret_val;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001905
Huzaifa Sidhpurwala91f58ae2011-02-21 12:58:45 +05301906 product = le16_to_cpu(serial->dev->descriptor.idProduct);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001907 dev = serial->dev;
1908
Mike Dunnfb088e32010-01-26 12:12:12 -05001909 /*
1910 * The 7715 uses the first bulk in/out endpoint pair for the parallel
1911 * port, and the second for the serial port. Because the usbserial core
1912 * assumes both pairs are serial ports, we must engage in a bit of
1913 * subterfuge and swap the pointers for ports 0 and 1 in order to make
1914 * port 0 point to the serial port. However, both moschip devices use a
1915 * single interrupt-in endpoint for both ports (as mentioned a little
1916 * further down), and this endpoint was assigned to port 0. So after
1917 * the swap, we must copy the interrupt endpoint elements from port 1
1918 * (as newly assigned) to port 0, and null out port 1 pointers.
1919 */
1920 if (product == MOSCHIP_DEVICE_ID_7715) {
1921 struct usb_serial_port *tmp = serial->port[0];
1922 serial->port[0] = serial->port[1];
1923 serial->port[1] = tmp;
1924 serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb;
1925 serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer;
1926 serial->port[0]->interrupt_in_endpointAddress =
1927 tmp->interrupt_in_endpointAddress;
1928 serial->port[1]->interrupt_in_urb = NULL;
1929 serial->port[1]->interrupt_in_buffer = NULL;
1930 }
1931
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001932 /* setting configuration feature to one */
1933 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Alan Cox4da1a172008-07-22 11:16:21 +01001934 (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5*HZ);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001935
Mike Dunnb69578d2010-04-15 17:01:33 -04001936 /* start the interrupt urb */
1937 ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
1938 if (ret_val)
1939 dev_err(&dev->dev,
1940 "%s - Error %d submitting control urb\n",
1941 __func__, ret_val);
1942
1943#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1944 if (product == MOSCHIP_DEVICE_ID_7715) {
1945 ret_val = mos7715_parport_init(serial);
1946 if (ret_val < 0)
1947 return ret_val;
1948 }
1949#endif
Alan Cox4da1a172008-07-22 11:16:21 +01001950 /* LSR For Port 1 */
Mike Dunn63b91762010-04-15 17:02:09 -04001951 read_mos_reg(serial, 0, LSR, &data);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001952 dev_dbg(&dev->dev, "LSR:%x\n", data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001953
1954 return 0;
1955}
1956
Alan Sternf9c99bb2009-06-02 11:53:55 -04001957static void mos7720_release(struct usb_serial *serial)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001958{
Mike Dunnb69578d2010-04-15 17:01:33 -04001959#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1960 /* close the parallel port */
1961
1962 if (le16_to_cpu(serial->dev->descriptor.idProduct)
1963 == MOSCHIP_DEVICE_ID_7715) {
1964 struct urbtracker *urbtrack;
1965 unsigned long flags;
1966 struct mos7715_parport *mos_parport =
1967 usb_get_serial_data(serial);
1968
1969 /* prevent NULL ptr dereference in port callbacks */
1970 spin_lock(&release_lock);
1971 mos_parport->pp->private_data = NULL;
1972 spin_unlock(&release_lock);
1973
1974 /* wait for synchronous usb calls to return */
1975 if (mos_parport->msg_pending)
1976 wait_for_completion_timeout(&mos_parport->syncmsg_compl,
1977 MOS_WDR_TIMEOUT);
1978
1979 parport_remove_port(mos_parport->pp);
1980 usb_set_serial_data(serial, NULL);
1981 mos_parport->serial = NULL;
1982
1983 /* if tasklet currently scheduled, wait for it to complete */
1984 tasklet_kill(&mos_parport->urb_tasklet);
1985
1986 /* unlink any urbs sent by the tasklet */
1987 spin_lock_irqsave(&mos_parport->listlock, flags);
1988 list_for_each_entry(urbtrack,
1989 &mos_parport->active_urbs,
1990 urblist_entry)
1991 usb_unlink_urb(urbtrack->urb);
1992 spin_unlock_irqrestore(&mos_parport->listlock, flags);
1993
1994 kref_put(&mos_parport->ref_count, destroy_mos_parport);
1995 }
1996#endif
Johan Hovold4230af52012-10-25 10:29:05 +02001997}
1998
1999static int mos7720_port_probe(struct usb_serial_port *port)
2000{
2001 struct moschip_port *mos7720_port;
2002
2003 mos7720_port = kzalloc(sizeof(*mos7720_port), GFP_KERNEL);
2004 if (!mos7720_port)
2005 return -ENOMEM;
2006
2007 /* Initialize all port interrupt end point to port 0 int endpoint.
2008 * Our device has only one interrupt endpoint common to all ports.
2009 */
2010 port->interrupt_in_endpointAddress =
2011 port->serial->port[0]->interrupt_in_endpointAddress;
2012 mos7720_port->port = port;
2013
2014 usb_set_serial_port_data(port, mos7720_port);
2015
2016 return 0;
2017}
2018
2019static int mos7720_port_remove(struct usb_serial_port *port)
2020{
2021 struct moschip_port *mos7720_port;
2022
2023 mos7720_port = usb_get_serial_port_data(port);
2024 kfree(mos7720_port);
2025
2026 return 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002027}
2028
2029static struct usb_serial_driver moschip7720_2port_driver = {
2030 .driver = {
2031 .owner = THIS_MODULE,
2032 .name = "moschip7720",
2033 },
2034 .description = "Moschip 2 port adapter",
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002035 .id_table = id_table,
Mike Dunnfb088e32010-01-26 12:12:12 -05002036 .calc_num_ports = mos77xx_calc_num_ports,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002037 .open = mos7720_open,
2038 .close = mos7720_close,
2039 .throttle = mos7720_throttle,
2040 .unthrottle = mos7720_unthrottle,
Mike Dunnfb088e32010-01-26 12:12:12 -05002041 .probe = mos77xx_probe,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002042 .attach = mos7720_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002043 .release = mos7720_release,
Johan Hovold4230af52012-10-25 10:29:05 +02002044 .port_probe = mos7720_port_probe,
2045 .port_remove = mos7720_port_remove,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002046 .ioctl = mos7720_ioctl,
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07002047 .tiocmget = mos7720_tiocmget,
2048 .tiocmset = mos7720_tiocmset,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002049 .set_termios = mos7720_set_termios,
2050 .write = mos7720_write,
2051 .write_room = mos7720_write_room,
2052 .chars_in_buffer = mos7720_chars_in_buffer,
2053 .break_ctl = mos7720_break,
2054 .read_bulk_callback = mos7720_bulk_in_callback,
Mike Dunnfb088e32010-01-26 12:12:12 -05002055 .read_int_callback = NULL /* dynamically assigned in probe() */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002056};
2057
Alan Stern4d2a7af2012-02-23 14:57:09 -05002058static struct usb_serial_driver * const serial_drivers[] = {
2059 &moschip7720_2port_driver, NULL
2060};
2061
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002062module_usb_serial_driver(serial_drivers, id_table);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002063
Alan Cox4da1a172008-07-22 11:16:21 +01002064MODULE_AUTHOR(DRIVER_AUTHOR);
2065MODULE_DESCRIPTION(DRIVER_DESC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002066MODULE_LICENSE("GPL");