blob: 049bf6fa283cad4b90c2d753f004ebc2e71a9aa3 [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
39/*
40 * Version Information
41 */
Mike Dunn63b91762010-04-15 17:02:09 -040042#define DRIVER_VERSION "2.1"
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070043#define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
44#define DRIVER_DESC "Moschip USB Serial Driver"
45
46/* default urb timeout */
47#define MOS_WDR_TIMEOUT (HZ * 5)
48
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070049#define MOS_MAX_PORT 0x02
50#define MOS_WRITE 0x0E
51#define MOS_READ 0x0D
52
53/* Interrupt Rotinue Defines */
54#define SERIAL_IIR_RLS 0x06
55#define SERIAL_IIR_RDA 0x04
56#define SERIAL_IIR_CTI 0x0c
57#define SERIAL_IIR_THR 0x02
58#define SERIAL_IIR_MS 0x00
59
60#define NUM_URBS 16 /* URB Count */
61#define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
62
Mike Dunnb69578d2010-04-15 17:01:33 -040063/* This structure holds all of the local serial port information */
Alan Cox4da1a172008-07-22 11:16:21 +010064struct moschip_port {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070065 __u8 shadowLCR; /* last LCR value received */
66 __u8 shadowMCR; /* last MCR value received */
67 __u8 shadowMSR; /* last MSR value received */
68 char open;
69 struct async_icount icount;
70 struct usb_serial_port *port; /* loop back to the owner */
71 struct urb *write_urb_pool[NUM_URBS];
72};
73
Rusty Russell90ab5ee2012-01-13 09:32:20 +103074static bool debug;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070075
Mike Dunnfb088e32010-01-26 12:12:12 -050076static struct usb_serial_driver moschip7720_2port_driver;
77
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070078#define USB_VENDOR_ID_MOSCHIP 0x9710
79#define MOSCHIP_DEVICE_ID_7720 0x7720
80#define MOSCHIP_DEVICE_ID_7715 0x7715
81
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -070082static const struct usb_device_id id_table[] = {
Alan Cox4da1a172008-07-22 11:16:21 +010083 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
Mike Dunnfb088e32010-01-26 12:12:12 -050084 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) },
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070085 { } /* terminating entry */
86};
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -070087MODULE_DEVICE_TABLE(usb, id_table);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070088
Mike Dunnb69578d2010-04-15 17:01:33 -040089#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
90
91/* initial values for parport regs */
92#define DCR_INIT_VAL 0x0c /* SLCTIN, nINIT */
93#define ECR_INIT_VAL 0x00 /* SPP mode */
94
95struct urbtracker {
96 struct mos7715_parport *mos_parport;
97 struct list_head urblist_entry;
98 struct kref ref_count;
99 struct urb *urb;
100};
101
102enum mos7715_pp_modes {
103 SPP = 0<<5,
104 PS2 = 1<<5, /* moschip calls this 'NIBBLE' mode */
105 PPF = 2<<5, /* moschip calls this 'CB-FIFO mode */
106};
107
108struct mos7715_parport {
109 struct parport *pp; /* back to containing struct */
110 struct kref ref_count; /* to instance of this struct */
111 struct list_head deferred_urbs; /* list deferred async urbs */
112 struct list_head active_urbs; /* list async urbs in flight */
113 spinlock_t listlock; /* protects list access */
114 bool msg_pending; /* usb sync call pending */
115 struct completion syncmsg_compl; /* usb sync call completed */
116 struct tasklet_struct urb_tasklet; /* for sending deferred urbs */
117 struct usb_serial *serial; /* back to containing struct */
118 __u8 shadowECR; /* parallel port regs... */
119 __u8 shadowDCR;
120 atomic_t shadowDSR; /* updated in int-in callback */
121};
122
123/* lock guards against dereferencing NULL ptr in parport ops callbacks */
124static DEFINE_SPINLOCK(release_lock);
125
Mike Dunn63b91762010-04-15 17:02:09 -0400126#endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
127
128static const unsigned int dummy; /* for clarity in register access fns */
129
Mike Dunnb69578d2010-04-15 17:01:33 -0400130enum mos_regs {
131 THR, /* serial port regs */
132 RHR,
133 IER,
134 FCR,
135 ISR,
136 LCR,
137 MCR,
138 LSR,
139 MSR,
140 SPR,
141 DLL,
142 DLM,
143 DPR, /* parallel port regs */
144 DSR,
145 DCR,
146 ECR,
147 SP1_REG, /* device control regs */
148 SP2_REG, /* serial port 2 (7720 only) */
149 PP_REG,
150 SP_CONTROL_REG,
151};
152
153/*
154 * Return the correct value for the Windex field of the setup packet
155 * for a control endpoint message. See the 7715 datasheet.
156 */
157static inline __u16 get_reg_index(enum mos_regs reg)
158{
159 static const __u16 mos7715_index_lookup_table[] = {
160 0x00, /* THR */
161 0x00, /* RHR */
162 0x01, /* IER */
163 0x02, /* FCR */
164 0x02, /* ISR */
165 0x03, /* LCR */
166 0x04, /* MCR */
167 0x05, /* LSR */
168 0x06, /* MSR */
169 0x07, /* SPR */
170 0x00, /* DLL */
171 0x01, /* DLM */
172 0x00, /* DPR */
173 0x01, /* DSR */
174 0x02, /* DCR */
175 0x0a, /* ECR */
176 0x01, /* SP1_REG */
177 0x02, /* SP2_REG (7720 only) */
178 0x04, /* PP_REG (7715 only) */
179 0x08, /* SP_CONTROL_REG */
180 };
181 return mos7715_index_lookup_table[reg];
182}
183
184/*
185 * Return the correct value for the upper byte of the Wvalue field of
186 * the setup packet for a control endpoint message.
187 */
Mike Dunn63b91762010-04-15 17:02:09 -0400188static inline __u16 get_reg_value(enum mos_regs reg,
189 unsigned int serial_portnum)
Mike Dunnb69578d2010-04-15 17:01:33 -0400190{
191 if (reg >= SP1_REG) /* control reg */
192 return 0x0000;
Mike Dunn63b91762010-04-15 17:02:09 -0400193
194 else if (reg >= DPR) /* parallel port reg (7715 only) */
Mike Dunnb69578d2010-04-15 17:01:33 -0400195 return 0x0100;
Mike Dunn63b91762010-04-15 17:02:09 -0400196
197 else /* serial port reg */
198 return (serial_portnum + 2) << 8;
Mike Dunnb69578d2010-04-15 17:01:33 -0400199}
200
201/*
202 * Write data byte to the specified device register. The data is embedded in
Mike Dunn63b91762010-04-15 17:02:09 -0400203 * the value field of the setup packet. serial_portnum is ignored for registers
204 * not specific to a particular serial port.
Mike Dunnb69578d2010-04-15 17:01:33 -0400205 */
Mike Dunn63b91762010-04-15 17:02:09 -0400206static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
207 enum mos_regs reg, __u8 data)
Mike Dunnb69578d2010-04-15 17:01:33 -0400208{
Mike Dunnb69578d2010-04-15 17:01:33 -0400209 struct usb_device *usbdev = serial->dev;
210 unsigned int pipe = usb_sndctrlpipe(usbdev, 0);
211 __u8 request = (__u8)0x0e;
212 __u8 requesttype = (__u8)0x40;
Mike Dunnb69578d2010-04-15 17:01:33 -0400213 __u16 index = get_reg_index(reg);
Mike Dunn63b91762010-04-15 17:02:09 -0400214 __u16 value = get_reg_value(reg, serial_portnum) + data;
215 int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
216 index, NULL, 0, MOS_WDR_TIMEOUT);
Mike Dunnb69578d2010-04-15 17:01:33 -0400217 if (status < 0)
218 dev_err(&usbdev->dev,
219 "mos7720: usb_control_msg() failed: %d", status);
220 return status;
221}
222
223/*
224 * Read data byte from the specified device register. The data returned by the
Mike Dunn63b91762010-04-15 17:02:09 -0400225 * device is embedded in the value field of the setup packet. serial_portnum is
226 * ignored for registers that are not specific to a particular serial port.
Mike Dunnb69578d2010-04-15 17:01:33 -0400227 */
Mike Dunn63b91762010-04-15 17:02:09 -0400228static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
229 enum mos_regs reg, __u8 *data)
Mike Dunnb69578d2010-04-15 17:01:33 -0400230{
Mike Dunn63b91762010-04-15 17:02:09 -0400231 struct usb_device *usbdev = serial->dev;
Mike Dunnb69578d2010-04-15 17:01:33 -0400232 unsigned int pipe = usb_rcvctrlpipe(usbdev, 0);
233 __u8 request = (__u8)0x0d;
234 __u8 requesttype = (__u8)0xc0;
Mike Dunnb69578d2010-04-15 17:01:33 -0400235 __u16 index = get_reg_index(reg);
Mike Dunn63b91762010-04-15 17:02:09 -0400236 __u16 value = get_reg_value(reg, serial_portnum);
Mike Dunnb69578d2010-04-15 17:01:33 -0400237 int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
Mike Dunn63b91762010-04-15 17:02:09 -0400238 index, data, 1, MOS_WDR_TIMEOUT);
Mike Dunnb69578d2010-04-15 17:01:33 -0400239 if (status < 0)
240 dev_err(&usbdev->dev,
241 "mos7720: usb_control_msg() failed: %d", status);
242 return status;
243}
244
Mike Dunn63b91762010-04-15 17:02:09 -0400245#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
246
Mike Dunnb69578d2010-04-15 17:01:33 -0400247static inline int mos7715_change_mode(struct mos7715_parport *mos_parport,
248 enum mos7715_pp_modes mode)
249{
250 mos_parport->shadowECR = mode;
Mike Dunn63b91762010-04-15 17:02:09 -0400251 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400252 return 0;
253}
254
255static void destroy_mos_parport(struct kref *kref)
256{
257 struct mos7715_parport *mos_parport =
258 container_of(kref, struct mos7715_parport, ref_count);
259
Mike Dunnb69578d2010-04-15 17:01:33 -0400260 kfree(mos_parport);
261}
262
263static void destroy_urbtracker(struct kref *kref)
264{
265 struct urbtracker *urbtrack =
266 container_of(kref, struct urbtracker, ref_count);
267 struct mos7715_parport *mos_parport = urbtrack->mos_parport;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700268
Mike Dunnb69578d2010-04-15 17:01:33 -0400269 usb_free_urb(urbtrack->urb);
270 kfree(urbtrack);
271 kref_put(&mos_parport->ref_count, destroy_mos_parport);
272}
273
274/*
275 * This runs as a tasklet when sending an urb in a non-blocking parallel
276 * port callback had to be deferred because the disconnect mutex could not be
277 * obtained at the time.
278 */
279static void send_deferred_urbs(unsigned long _mos_parport)
280{
281 int ret_val;
282 unsigned long flags;
283 struct mos7715_parport *mos_parport = (void *)_mos_parport;
Wei Yongjun67990472012-08-21 11:28:45 +0800284 struct urbtracker *urbtrack, *tmp;
Mike Dunnb69578d2010-04-15 17:01:33 -0400285 struct list_head *cursor, *next;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700286 struct device *dev;
Mike Dunnb69578d2010-04-15 17:01:33 -0400287
Mike Dunnb69578d2010-04-15 17:01:33 -0400288 /* if release function ran, game over */
289 if (unlikely(mos_parport->serial == NULL))
290 return;
291
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700292 dev = &mos_parport->serial->dev->dev;
293
Mike Dunnb69578d2010-04-15 17:01:33 -0400294 /* try again to get the mutex */
295 if (!mutex_trylock(&mos_parport->serial->disc_mutex)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700296 dev_dbg(dev, "%s: rescheduling tasklet\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400297 tasklet_schedule(&mos_parport->urb_tasklet);
298 return;
299 }
300
301 /* if device disconnected, game over */
302 if (unlikely(mos_parport->serial->disconnected)) {
303 mutex_unlock(&mos_parport->serial->disc_mutex);
304 return;
305 }
306
307 spin_lock_irqsave(&mos_parport->listlock, flags);
308 if (list_empty(&mos_parport->deferred_urbs)) {
309 spin_unlock_irqrestore(&mos_parport->listlock, flags);
310 mutex_unlock(&mos_parport->serial->disc_mutex);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700311 dev_dbg(dev, "%s: deferred_urbs list empty\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400312 return;
313 }
314
315 /* move contents of deferred_urbs list to active_urbs list and submit */
316 list_for_each_safe(cursor, next, &mos_parport->deferred_urbs)
317 list_move_tail(cursor, &mos_parport->active_urbs);
Wei Yongjun67990472012-08-21 11:28:45 +0800318 list_for_each_entry_safe(urbtrack, tmp, &mos_parport->active_urbs,
Mike Dunnb69578d2010-04-15 17:01:33 -0400319 urblist_entry) {
320 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700321 dev_dbg(dev, "%s: urb submitted\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400322 if (ret_val) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700323 dev_err(dev, "usb_submit_urb() failed: %d\n", ret_val);
Mike Dunnb69578d2010-04-15 17:01:33 -0400324 list_del(&urbtrack->urblist_entry);
325 kref_put(&urbtrack->ref_count, destroy_urbtracker);
326 }
327 }
328 spin_unlock_irqrestore(&mos_parport->listlock, flags);
329 mutex_unlock(&mos_parport->serial->disc_mutex);
330}
331
332/* callback for parallel port control urbs submitted asynchronously */
333static void async_complete(struct urb *urb)
334{
335 struct urbtracker *urbtrack = urb->context;
336 int status = urb->status;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700337
Mike Dunnb69578d2010-04-15 17:01:33 -0400338 if (unlikely(status))
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700339 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
Mike Dunnb69578d2010-04-15 17:01:33 -0400340
341 /* remove the urbtracker from the active_urbs list */
342 spin_lock(&urbtrack->mos_parport->listlock);
343 list_del(&urbtrack->urblist_entry);
344 spin_unlock(&urbtrack->mos_parport->listlock);
345 kref_put(&urbtrack->ref_count, destroy_urbtracker);
346}
347
348static int write_parport_reg_nonblock(struct mos7715_parport *mos_parport,
349 enum mos_regs reg, __u8 data)
350{
351 struct urbtracker *urbtrack;
352 int ret_val;
353 unsigned long flags;
354 struct usb_ctrlrequest setup;
355 struct usb_serial *serial = mos_parport->serial;
356 struct usb_device *usbdev = serial->dev;
Mike Dunnb69578d2010-04-15 17:01:33 -0400357
358 /* create and initialize the control urb and containing urbtracker */
359 urbtrack = kmalloc(sizeof(struct urbtracker), GFP_ATOMIC);
360 if (urbtrack == NULL) {
361 dev_err(&usbdev->dev, "out of memory");
362 return -ENOMEM;
363 }
364 kref_get(&mos_parport->ref_count);
365 urbtrack->mos_parport = mos_parport;
366 urbtrack->urb = usb_alloc_urb(0, GFP_ATOMIC);
367 if (urbtrack->urb == NULL) {
368 dev_err(&usbdev->dev, "out of urbs");
369 kfree(urbtrack);
370 return -ENOMEM;
371 }
372 setup.bRequestType = (__u8)0x40;
373 setup.bRequest = (__u8)0x0e;
Mike Dunn63b91762010-04-15 17:02:09 -0400374 setup.wValue = get_reg_value(reg, dummy);
Mike Dunnb69578d2010-04-15 17:01:33 -0400375 setup.wIndex = get_reg_index(reg);
376 setup.wLength = 0;
377 usb_fill_control_urb(urbtrack->urb, usbdev,
378 usb_sndctrlpipe(usbdev, 0),
379 (unsigned char *)&setup,
380 NULL, 0, async_complete, urbtrack);
381 kref_init(&urbtrack->ref_count);
382 INIT_LIST_HEAD(&urbtrack->urblist_entry);
383
384 /*
385 * get the disconnect mutex, or add tracker to the deferred_urbs list
386 * and schedule a tasklet to try again later
387 */
388 if (!mutex_trylock(&serial->disc_mutex)) {
389 spin_lock_irqsave(&mos_parport->listlock, flags);
390 list_add_tail(&urbtrack->urblist_entry,
391 &mos_parport->deferred_urbs);
392 spin_unlock_irqrestore(&mos_parport->listlock, flags);
393 tasklet_schedule(&mos_parport->urb_tasklet);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700394 dev_dbg(&usbdev->dev, "tasklet scheduled");
Mike Dunnb69578d2010-04-15 17:01:33 -0400395 return 0;
396 }
397
398 /* bail if device disconnected */
399 if (serial->disconnected) {
400 kref_put(&urbtrack->ref_count, destroy_urbtracker);
401 mutex_unlock(&serial->disc_mutex);
402 return -ENODEV;
403 }
404
405 /* add the tracker to the active_urbs list and submit */
406 spin_lock_irqsave(&mos_parport->listlock, flags);
407 list_add_tail(&urbtrack->urblist_entry, &mos_parport->active_urbs);
408 spin_unlock_irqrestore(&mos_parport->listlock, flags);
409 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
410 mutex_unlock(&serial->disc_mutex);
411 if (ret_val) {
412 dev_err(&usbdev->dev,
413 "%s: submit_urb() failed: %d", __func__, ret_val);
414 spin_lock_irqsave(&mos_parport->listlock, flags);
415 list_del(&urbtrack->urblist_entry);
416 spin_unlock_irqrestore(&mos_parport->listlock, flags);
417 kref_put(&urbtrack->ref_count, destroy_urbtracker);
418 return ret_val;
419 }
420 return 0;
421}
422
423/*
424 * This is the the common top part of all parallel port callback operations that
425 * send synchronous messages to the device. This implements convoluted locking
426 * that avoids two scenarios: (1) a port operation is called after usbserial
427 * has called our release function, at which point struct mos7715_parport has
428 * been destroyed, and (2) the device has been disconnected, but usbserial has
429 * not called the release function yet because someone has a serial port open.
430 * The shared release_lock prevents the first, and the mutex and disconnected
431 * flag maintained by usbserial covers the second. We also use the msg_pending
432 * flag to ensure that all synchronous usb messgage calls have completed before
433 * our release function can return.
434 */
435static int parport_prologue(struct parport *pp)
436{
437 struct mos7715_parport *mos_parport;
438
439 spin_lock(&release_lock);
440 mos_parport = pp->private_data;
441 if (unlikely(mos_parport == NULL)) {
442 /* release fn called, port struct destroyed */
443 spin_unlock(&release_lock);
444 return -1;
445 }
446 mos_parport->msg_pending = true; /* synch usb call pending */
447 INIT_COMPLETION(mos_parport->syncmsg_compl);
448 spin_unlock(&release_lock);
449
450 mutex_lock(&mos_parport->serial->disc_mutex);
451 if (mos_parport->serial->disconnected) {
452 /* device disconnected */
453 mutex_unlock(&mos_parport->serial->disc_mutex);
454 mos_parport->msg_pending = false;
455 complete(&mos_parport->syncmsg_compl);
456 return -1;
457 }
458
459 return 0;
460}
461
462/*
463 * This is the the common bottom part of all parallel port functions that send
464 * synchronous messages to the device.
465 */
466static inline void parport_epilogue(struct parport *pp)
467{
468 struct mos7715_parport *mos_parport = pp->private_data;
469 mutex_unlock(&mos_parport->serial->disc_mutex);
470 mos_parport->msg_pending = false;
471 complete(&mos_parport->syncmsg_compl);
472}
473
474static void parport_mos7715_write_data(struct parport *pp, unsigned char d)
475{
476 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700477
Mike Dunnb69578d2010-04-15 17:01:33 -0400478 if (parport_prologue(pp) < 0)
479 return;
480 mos7715_change_mode(mos_parport, SPP);
Mike Dunn63b91762010-04-15 17:02:09 -0400481 write_mos_reg(mos_parport->serial, dummy, DPR, (__u8)d);
Mike Dunnb69578d2010-04-15 17:01:33 -0400482 parport_epilogue(pp);
483}
484
485static unsigned char parport_mos7715_read_data(struct parport *pp)
486{
487 struct mos7715_parport *mos_parport = pp->private_data;
488 unsigned char d;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700489
Mike Dunnb69578d2010-04-15 17:01:33 -0400490 if (parport_prologue(pp) < 0)
491 return 0;
Mike Dunn63b91762010-04-15 17:02:09 -0400492 read_mos_reg(mos_parport->serial, dummy, DPR, &d);
Mike Dunnb69578d2010-04-15 17:01:33 -0400493 parport_epilogue(pp);
494 return d;
495}
496
497static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
498{
499 struct mos7715_parport *mos_parport = pp->private_data;
500 __u8 data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700501
Mike Dunnb69578d2010-04-15 17:01:33 -0400502 if (parport_prologue(pp) < 0)
503 return;
504 data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
Mike Dunn63b91762010-04-15 17:02:09 -0400505 write_mos_reg(mos_parport->serial, dummy, DCR, data);
Mike Dunnb69578d2010-04-15 17:01:33 -0400506 mos_parport->shadowDCR = data;
507 parport_epilogue(pp);
508}
509
510static unsigned char parport_mos7715_read_control(struct parport *pp)
511{
512 struct mos7715_parport *mos_parport = pp->private_data;
513 __u8 dcr;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700514
Mike Dunnb69578d2010-04-15 17:01:33 -0400515 spin_lock(&release_lock);
516 mos_parport = pp->private_data;
517 if (unlikely(mos_parport == NULL)) {
518 spin_unlock(&release_lock);
519 return 0;
520 }
521 dcr = mos_parport->shadowDCR & 0x0f;
522 spin_unlock(&release_lock);
523 return dcr;
524}
525
526static unsigned char parport_mos7715_frob_control(struct parport *pp,
527 unsigned char mask,
528 unsigned char val)
529{
530 struct mos7715_parport *mos_parport = pp->private_data;
531 __u8 dcr;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700532
Mike Dunnb69578d2010-04-15 17:01:33 -0400533 mask &= 0x0f;
534 val &= 0x0f;
535 if (parport_prologue(pp) < 0)
536 return 0;
537 mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
Mike Dunn63b91762010-04-15 17:02:09 -0400538 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400539 dcr = mos_parport->shadowDCR & 0x0f;
540 parport_epilogue(pp);
541 return dcr;
542}
543
544static unsigned char parport_mos7715_read_status(struct parport *pp)
545{
546 unsigned char status;
547 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700548
Mike Dunnb69578d2010-04-15 17:01:33 -0400549 spin_lock(&release_lock);
550 mos_parport = pp->private_data;
551 if (unlikely(mos_parport == NULL)) { /* release called */
552 spin_unlock(&release_lock);
553 return 0;
554 }
555 status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
556 spin_unlock(&release_lock);
557 return status;
558}
559
560static void parport_mos7715_enable_irq(struct parport *pp)
561{
Mike Dunnb69578d2010-04-15 17:01:33 -0400562}
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700563
Mike Dunnb69578d2010-04-15 17:01:33 -0400564static void parport_mos7715_disable_irq(struct parport *pp)
565{
Mike Dunnb69578d2010-04-15 17:01:33 -0400566}
567
568static void parport_mos7715_data_forward(struct parport *pp)
569{
570 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700571
Mike Dunnb69578d2010-04-15 17:01:33 -0400572 if (parport_prologue(pp) < 0)
573 return;
574 mos7715_change_mode(mos_parport, PS2);
575 mos_parport->shadowDCR &= ~0x20;
Mike Dunn63b91762010-04-15 17:02:09 -0400576 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400577 parport_epilogue(pp);
578}
579
580static void parport_mos7715_data_reverse(struct parport *pp)
581{
582 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700583
Mike Dunnb69578d2010-04-15 17:01:33 -0400584 if (parport_prologue(pp) < 0)
585 return;
586 mos7715_change_mode(mos_parport, PS2);
587 mos_parport->shadowDCR |= 0x20;
Mike Dunn63b91762010-04-15 17:02:09 -0400588 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400589 parport_epilogue(pp);
590}
591
592static void parport_mos7715_init_state(struct pardevice *dev,
593 struct parport_state *s)
594{
Mike Dunnb69578d2010-04-15 17:01:33 -0400595 s->u.pc.ctr = DCR_INIT_VAL;
596 s->u.pc.ecr = ECR_INIT_VAL;
597}
598
599/* N.B. Parport core code requires that this function not block */
600static void parport_mos7715_save_state(struct parport *pp,
601 struct parport_state *s)
602{
603 struct mos7715_parport *mos_parport;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700604
Mike Dunnb69578d2010-04-15 17:01:33 -0400605 spin_lock(&release_lock);
606 mos_parport = pp->private_data;
607 if (unlikely(mos_parport == NULL)) { /* release called */
608 spin_unlock(&release_lock);
609 return;
610 }
611 s->u.pc.ctr = mos_parport->shadowDCR;
612 s->u.pc.ecr = mos_parport->shadowECR;
613 spin_unlock(&release_lock);
614}
615
616/* N.B. Parport core code requires that this function not block */
617static void parport_mos7715_restore_state(struct parport *pp,
618 struct parport_state *s)
619{
620 struct mos7715_parport *mos_parport;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700621
Mike Dunnb69578d2010-04-15 17:01:33 -0400622 spin_lock(&release_lock);
623 mos_parport = pp->private_data;
624 if (unlikely(mos_parport == NULL)) { /* release called */
625 spin_unlock(&release_lock);
626 return;
627 }
628 write_parport_reg_nonblock(mos_parport, DCR, mos_parport->shadowDCR);
629 write_parport_reg_nonblock(mos_parport, ECR, mos_parport->shadowECR);
630 spin_unlock(&release_lock);
631}
632
633static size_t parport_mos7715_write_compat(struct parport *pp,
634 const void *buffer,
635 size_t len, int flags)
636{
637 int retval;
638 struct mos7715_parport *mos_parport = pp->private_data;
639 int actual_len;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700640
Mike Dunnb69578d2010-04-15 17:01:33 -0400641 if (parport_prologue(pp) < 0)
642 return 0;
643 mos7715_change_mode(mos_parport, PPF);
644 retval = usb_bulk_msg(mos_parport->serial->dev,
645 usb_sndbulkpipe(mos_parport->serial->dev, 2),
646 (void *)buffer, len, &actual_len,
647 MOS_WDR_TIMEOUT);
648 parport_epilogue(pp);
649 if (retval) {
650 dev_err(&mos_parport->serial->dev->dev,
651 "mos7720: usb_bulk_msg() failed: %d", retval);
652 return 0;
653 }
654 return actual_len;
655}
656
657static struct parport_operations parport_mos7715_ops = {
658 .owner = THIS_MODULE,
659 .write_data = parport_mos7715_write_data,
660 .read_data = parport_mos7715_read_data,
661
662 .write_control = parport_mos7715_write_control,
663 .read_control = parport_mos7715_read_control,
664 .frob_control = parport_mos7715_frob_control,
665
666 .read_status = parport_mos7715_read_status,
667
668 .enable_irq = parport_mos7715_enable_irq,
669 .disable_irq = parport_mos7715_disable_irq,
670
671 .data_forward = parport_mos7715_data_forward,
672 .data_reverse = parport_mos7715_data_reverse,
673
674 .init_state = parport_mos7715_init_state,
675 .save_state = parport_mos7715_save_state,
676 .restore_state = parport_mos7715_restore_state,
677
678 .compat_write_data = parport_mos7715_write_compat,
679
680 .nibble_read_data = parport_ieee1284_read_nibble,
681 .byte_read_data = parport_ieee1284_read_byte,
682};
683
684/*
685 * Allocate and initialize parallel port control struct, initialize
686 * the parallel port hardware device, and register with the parport subsystem.
687 */
688static int mos7715_parport_init(struct usb_serial *serial)
689{
690 struct mos7715_parport *mos_parport;
691
692 /* allocate and initialize parallel port control struct */
693 mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
694 if (mos_parport == NULL) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700695 dev_dbg(&serial->dev->dev, "%s: kzalloc failed\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400696 return -ENOMEM;
697 }
698 mos_parport->msg_pending = false;
699 kref_init(&mos_parport->ref_count);
700 spin_lock_init(&mos_parport->listlock);
701 INIT_LIST_HEAD(&mos_parport->active_urbs);
702 INIT_LIST_HEAD(&mos_parport->deferred_urbs);
703 usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
704 mos_parport->serial = serial;
705 tasklet_init(&mos_parport->urb_tasklet, send_deferred_urbs,
706 (unsigned long) mos_parport);
707 init_completion(&mos_parport->syncmsg_compl);
708
709 /* cycle parallel port reset bit */
Mike Dunn63b91762010-04-15 17:02:09 -0400710 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x80);
711 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x00);
Mike Dunnb69578d2010-04-15 17:01:33 -0400712
713 /* initialize device registers */
714 mos_parport->shadowDCR = DCR_INIT_VAL;
Mike Dunn63b91762010-04-15 17:02:09 -0400715 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400716 mos_parport->shadowECR = ECR_INIT_VAL;
Mike Dunn63b91762010-04-15 17:02:09 -0400717 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400718
719 /* register with parport core */
720 mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE,
721 PARPORT_DMA_NONE,
722 &parport_mos7715_ops);
723 if (mos_parport->pp == NULL) {
724 dev_err(&serial->interface->dev,
725 "Could not register parport\n");
726 kref_put(&mos_parport->ref_count, destroy_mos_parport);
727 return -EIO;
728 }
729 mos_parport->pp->private_data = mos_parport;
730 mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP;
731 mos_parport->pp->dev = &serial->interface->dev;
732 parport_announce_port(mos_parport->pp);
733
734 return 0;
735}
736#endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700737
738/*
739 * mos7720_interrupt_callback
740 * this is the callback function for when we have received data on the
741 * interrupt endpoint.
742 */
743static void mos7720_interrupt_callback(struct urb *urb)
744{
745 int result;
746 int length;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700747 int status = urb->status;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700748 struct device *dev = &urb->dev->dev;
Oliver Neukum325b70c2007-03-19 13:58:29 +0100749 __u8 *data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700750 __u8 sp1;
751 __u8 sp2;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700752
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700753 switch (status) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700754 case 0:
755 /* success */
756 break;
757 case -ECONNRESET:
758 case -ENOENT:
759 case -ESHUTDOWN:
760 /* this urb is terminated, clean up */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700761 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700762 return;
763 default:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700764 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700765 goto exit;
766 }
767
768 length = urb->actual_length;
769 data = urb->transfer_buffer;
770
771 /* Moschip get 4 bytes
772 * Byte 1 IIR Port 1 (port.number is 0)
773 * Byte 2 IIR Port 2 (port.number is 1)
774 * Byte 3 --------------
775 * Byte 4 FIFO status for both */
Oliver Neukum325b70c2007-03-19 13:58:29 +0100776
777 /* the above description is inverted
778 * oneukum 2007-03-14 */
779
780 if (unlikely(length != 4)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700781 dev_dbg(dev, "Wrong data !!!\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700782 return;
783 }
784
Oliver Neukum325b70c2007-03-19 13:58:29 +0100785 sp1 = data[3];
786 sp2 = data[2];
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700787
Oliver Neukum325b70c2007-03-19 13:58:29 +0100788 if ((sp1 | sp2) & 0x01) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700789 /* No Interrupt Pending in both the ports */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700790 dev_dbg(dev, "No Interrupt !!!\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700791 } else {
792 switch (sp1 & 0x0f) {
793 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700794 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 -0700795 break;
796 case SERIAL_IIR_CTI:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700797 dev_dbg(dev, "Serial Port 1: Receiver time out\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700798 break;
799 case SERIAL_IIR_MS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700800 /* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700801 break;
802 }
803
804 switch (sp2 & 0x0f) {
805 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700806 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 -0700807 break;
808 case SERIAL_IIR_CTI:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700809 dev_dbg(dev, "Serial Port 2: Receiver time out\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700810 break;
811 case SERIAL_IIR_MS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700812 /* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700813 break;
814 }
815 }
816
817exit:
818 result = usb_submit_urb(urb, GFP_ATOMIC);
819 if (result)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700820 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700821}
822
823/*
Mike Dunnfb088e32010-01-26 12:12:12 -0500824 * mos7715_interrupt_callback
825 * this is the 7715's callback function for when we have received data on
826 * the interrupt endpoint.
827 */
828static void mos7715_interrupt_callback(struct urb *urb)
829{
830 int result;
831 int length;
832 int status = urb->status;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700833 struct device *dev = &urb->dev->dev;
Mike Dunnfb088e32010-01-26 12:12:12 -0500834 __u8 *data;
835 __u8 iir;
836
837 switch (status) {
838 case 0:
839 /* success */
840 break;
841 case -ECONNRESET:
842 case -ENOENT:
843 case -ESHUTDOWN:
Mike Dunnb69578d2010-04-15 17:01:33 -0400844 case -ENODEV:
Mike Dunnfb088e32010-01-26 12:12:12 -0500845 /* this urb is terminated, clean up */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700846 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
Mike Dunnfb088e32010-01-26 12:12:12 -0500847 return;
848 default:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700849 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
Mike Dunnfb088e32010-01-26 12:12:12 -0500850 goto exit;
851 }
852
853 length = urb->actual_length;
854 data = urb->transfer_buffer;
855
856 /* Structure of data from 7715 device:
857 * Byte 1: IIR serial Port
858 * Byte 2: unused
859 * Byte 2: DSR parallel port
860 * Byte 4: FIFO status for both */
861
862 if (unlikely(length != 4)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700863 dev_dbg(dev, "Wrong data !!!\n");
Mike Dunnfb088e32010-01-26 12:12:12 -0500864 return;
865 }
866
867 iir = data[0];
868 if (!(iir & 0x01)) { /* serial port interrupt pending */
869 switch (iir & 0x0f) {
870 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700871 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 -0500872 break;
873 case SERIAL_IIR_CTI:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700874 dev_dbg(dev, "Serial Port: Receiver time out\n");
Mike Dunnfb088e32010-01-26 12:12:12 -0500875 break;
876 case SERIAL_IIR_MS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700877 /* dev_dbg(dev, "Serial Port: Modem status change\n"); */
Mike Dunnfb088e32010-01-26 12:12:12 -0500878 break;
879 }
880 }
881
Mike Dunnb69578d2010-04-15 17:01:33 -0400882#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
883 { /* update local copy of DSR reg */
884 struct usb_serial_port *port = urb->context;
885 struct mos7715_parport *mos_parport = port->serial->private;
886 if (unlikely(mos_parport == NULL))
887 return;
888 atomic_set(&mos_parport->shadowDSR, data[2]);
889 }
890#endif
891
Mike Dunnfb088e32010-01-26 12:12:12 -0500892exit:
893 result = usb_submit_urb(urb, GFP_ATOMIC);
894 if (result)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700895 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
Mike Dunnfb088e32010-01-26 12:12:12 -0500896}
897
898/*
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700899 * mos7720_bulk_in_callback
900 * this is the callback function for when we have received data on the
901 * bulk in endpoint.
902 */
903static void mos7720_bulk_in_callback(struct urb *urb)
904{
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700905 int retval;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700906 unsigned char *data ;
907 struct usb_serial_port *port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700908 struct tty_struct *tty;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700909 int status = urb->status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700910
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700911 if (status) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700912 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700913 return;
914 }
915
Mike Dunnb69578d2010-04-15 17:01:33 -0400916 port = urb->context;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700917
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700918 dev_dbg(&port->dev, "Entering...%s\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700919
920 data = urb->transfer_buffer;
921
Alan Cox4a90f092008-10-13 10:39:46 +0100922 tty = tty_port_tty_get(&port->port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700923 if (tty && urb->actual_length) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700924 tty_insert_flip_string(tty, data, urb->actual_length);
925 tty_flip_buffer_push(tty);
926 }
Alan Cox4a90f092008-10-13 10:39:46 +0100927 tty_kref_put(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700928
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700929 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700930 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
931 if (retval)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700932 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700933 }
934}
935
936/*
937 * mos7720_bulk_out_data_callback
938 * this is the callback function for when we have finished sending serial
939 * data on the bulk out endpoint.
940 */
941static void mos7720_bulk_out_data_callback(struct urb *urb)
942{
943 struct moschip_port *mos7720_port;
944 struct tty_struct *tty;
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
Alan Cox4a90f092008-10-13 10:39:46 +0100958 tty = tty_port_tty_get(&mos7720_port->port->port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700959
Jiri Slabyb963a842007-02-10 01:44:55 -0800960 if (tty && mos7720_port->open)
961 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100962 tty_kref_put(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700963}
964
965/*
Mike Dunnfb088e32010-01-26 12:12:12 -0500966 * mos77xx_probe
967 * this function installs the appropriate read interrupt endpoint callback
968 * depending on whether the device is a 7720 or 7715, thus avoiding costly
969 * run-time checks in the high-frequency callback routine itself.
970 */
971static int mos77xx_probe(struct usb_serial *serial,
972 const struct usb_device_id *id)
973{
974 if (id->idProduct == MOSCHIP_DEVICE_ID_7715)
975 moschip7720_2port_driver.read_int_callback =
976 mos7715_interrupt_callback;
977 else
978 moschip7720_2port_driver.read_int_callback =
979 mos7720_interrupt_callback;
980
981 return 0;
982}
983
984static int mos77xx_calc_num_ports(struct usb_serial *serial)
985{
986 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
987 if (product == MOSCHIP_DEVICE_ID_7715)
988 return 1;
989
990 return 2;
991}
992
Alan Coxa509a7e2009-09-19 13:13:26 -0700993static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700994{
995 struct usb_serial *serial;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700996 struct urb *urb;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700997 struct moschip_port *mos7720_port;
998 int response;
999 int port_number;
Mike Dunn63b91762010-04-15 17:02:09 -04001000 __u8 data;
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001001 int allocated_urbs = 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001002 int j;
1003
1004 serial = port->serial;
1005
1006 mos7720_port = usb_get_serial_port_data(port);
1007 if (mos7720_port == NULL)
1008 return -ENODEV;
1009
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001010 usb_clear_halt(serial->dev, port->write_urb->pipe);
1011 usb_clear_halt(serial->dev, port->read_urb->pipe);
1012
1013 /* Initialising the write urb pool */
1014 for (j = 0; j < NUM_URBS; ++j) {
Alan Cox4da1a172008-07-22 11:16:21 +01001015 urb = usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001016 mos7720_port->write_urb_pool[j] = urb;
1017
1018 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001019 dev_err(&port->dev, "No more urbs???\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001020 continue;
1021 }
1022
1023 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1024 GFP_KERNEL);
1025 if (!urb->transfer_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001026 dev_err(&port->dev,
1027 "%s-out of memory for urb buffers.\n",
1028 __func__);
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001029 usb_free_urb(mos7720_port->write_urb_pool[j]);
1030 mos7720_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001031 continue;
1032 }
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001033 allocated_urbs++;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001034 }
1035
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001036 if (!allocated_urbs)
1037 return -ENOMEM;
1038
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001039 /* Initialize MCS7720 -- Write Init values to corresponding Registers
1040 *
1041 * Register Index
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001042 * 0 : THR/RHR
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001043 * 1 : IER
1044 * 2 : FCR
1045 * 3 : LCR
1046 * 4 : MCR
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001047 * 5 : LSR
1048 * 6 : MSR
1049 * 7 : SPR
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001050 *
1051 * 0x08 : SP1/2 Control Reg
1052 */
1053 port_number = port->number - port->serial->minor;
Mike Dunn63b91762010-04-15 17:02:09 -04001054 read_mos_reg(serial, port_number, LSR, &data);
1055
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001056 dev_dbg(&port->dev, "SS::%p LSR:%x\n", mos7720_port, data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001057
Mike Dunn63b91762010-04-15 17:02:09 -04001058 write_mos_reg(serial, dummy, SP1_REG, 0x02);
1059 write_mos_reg(serial, dummy, SP2_REG, 0x02);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001060
Mike Dunn63b91762010-04-15 17:02:09 -04001061 write_mos_reg(serial, port_number, IER, 0x00);
1062 write_mos_reg(serial, port_number, FCR, 0x00);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001063
Mike Dunn63b91762010-04-15 17:02:09 -04001064 write_mos_reg(serial, port_number, FCR, 0xcf);
1065 mos7720_port->shadowLCR = 0x03;
1066 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1067 mos7720_port->shadowMCR = 0x0b;
1068 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001069
Mike Dunn63b91762010-04-15 17:02:09 -04001070 write_mos_reg(serial, port_number, SP_CONTROL_REG, 0x00);
1071 read_mos_reg(serial, dummy, SP_CONTROL_REG, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001072 data = data | (port->number - port->serial->minor + 1);
Mike Dunn63b91762010-04-15 17:02:09 -04001073 write_mos_reg(serial, dummy, SP_CONTROL_REG, data);
1074 mos7720_port->shadowLCR = 0x83;
1075 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1076 write_mos_reg(serial, port_number, THR, 0x0c);
1077 write_mos_reg(serial, port_number, IER, 0x00);
1078 mos7720_port->shadowLCR = 0x03;
1079 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1080 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001081
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001082 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
1083 if (response)
Alan Cox4da1a172008-07-22 11:16:21 +01001084 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
1085 __func__, response);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001086
1087 /* initialize our icount structure */
1088 memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
1089
1090 /* initialize our port settings */
1091 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
1092
1093 /* send a open port command */
1094 mos7720_port->open = 1;
1095
1096 return 0;
1097}
1098
1099/*
1100 * mos7720_chars_in_buffer
1101 * this function is called by the tty driver when it wants to know how many
1102 * bytes of data we currently have outstanding in the port (data that has
1103 * been written, but hasn't made it out the port yet)
1104 * If successful, we return the number of bytes left to be written in the
1105 * system,
1106 * Otherwise we return a negative error number.
1107 */
Alan Cox95da3102008-07-22 11:09:07 +01001108static int mos7720_chars_in_buffer(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001109{
Alan Cox95da3102008-07-22 11:09:07 +01001110 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001111 int i;
1112 int chars = 0;
1113 struct moschip_port *mos7720_port;
1114
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001115 mos7720_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001116 if (mos7720_port == NULL)
Alan Cox23198fd2009-07-20 16:05:27 +01001117 return 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001118
1119 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001120 if (mos7720_port->write_urb_pool[i] &&
1121 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001122 chars += URB_TRANSFER_BUFFER_SIZE;
1123 }
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001124 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001125 return chars;
1126}
1127
Alan Cox335f8512009-06-11 12:26:29 +01001128static void mos7720_close(struct usb_serial_port *port)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001129{
1130 struct usb_serial *serial;
1131 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001132 int j;
1133
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001134 serial = port->serial;
1135
1136 mos7720_port = usb_get_serial_port_data(port);
1137 if (mos7720_port == NULL)
1138 return;
1139
1140 for (j = 0; j < NUM_URBS; ++j)
1141 usb_kill_urb(mos7720_port->write_urb_pool[j]);
1142
1143 /* Freeing Write URBs */
1144 for (j = 0; j < NUM_URBS; ++j) {
1145 if (mos7720_port->write_urb_pool[j]) {
1146 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
1147 usb_free_urb(mos7720_port->write_urb_pool[j]);
1148 }
1149 }
1150
1151 /* While closing port, shutdown all bulk read, write *
Oliver Neukuma1cd7e92008-01-16 17:18:52 +01001152 * and interrupt read if they exists, otherwise nop */
Oliver Neukuma1cd7e92008-01-16 17:18:52 +01001153 usb_kill_urb(port->write_urb);
Oliver Neukuma1cd7e92008-01-16 17:18:52 +01001154 usb_kill_urb(port->read_urb);
1155
1156 mutex_lock(&serial->disc_mutex);
1157 /* these commands must not be issued if the device has
1158 * been disconnected */
1159 if (!serial->disconnected) {
Mike Dunn63b91762010-04-15 17:02:09 -04001160 write_mos_reg(serial, port->number - port->serial->minor,
1161 MCR, 0x00);
1162 write_mos_reg(serial, port->number - port->serial->minor,
1163 IER, 0x00);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001164 }
Oliver Neukuma1cd7e92008-01-16 17:18:52 +01001165 mutex_unlock(&serial->disc_mutex);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001166 mos7720_port->open = 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001167}
1168
Alan Cox95da3102008-07-22 11:09:07 +01001169static void mos7720_break(struct tty_struct *tty, int break_state)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001170{
Alan Cox95da3102008-07-22 11:09:07 +01001171 struct usb_serial_port *port = tty->driver_data;
Alan Cox4da1a172008-07-22 11:16:21 +01001172 unsigned char data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001173 struct usb_serial *serial;
1174 struct moschip_port *mos7720_port;
1175
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001176 serial = port->serial;
1177
1178 mos7720_port = usb_get_serial_port_data(port);
1179 if (mos7720_port == NULL)
1180 return;
1181
1182 if (break_state == -1)
1183 data = mos7720_port->shadowLCR | UART_LCR_SBC;
1184 else
1185 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
1186
1187 mos7720_port->shadowLCR = data;
Mike Dunn63b91762010-04-15 17:02:09 -04001188 write_mos_reg(serial, port->number - port->serial->minor,
1189 LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001190}
1191
1192/*
1193 * mos7720_write_room
1194 * this function is called by the tty driver when it wants to know how many
1195 * bytes of data we can accept for a specific port.
1196 * If successful, we return the amount of room that we have for this port
1197 * Otherwise we return a negative error number.
1198 */
Alan Cox95da3102008-07-22 11:09:07 +01001199static int mos7720_write_room(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001200{
Alan Cox95da3102008-07-22 11:09:07 +01001201 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001202 struct moschip_port *mos7720_port;
1203 int room = 0;
1204 int i;
1205
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001206 mos7720_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001207 if (mos7720_port == NULL)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001208 return -ENODEV;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001209
Alan Coxa5b6f602008-04-08 17:16:06 +01001210 /* FIXME: Locking */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001211 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001212 if (mos7720_port->write_urb_pool[i] &&
1213 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001214 room += URB_TRANSFER_BUFFER_SIZE;
1215 }
1216
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001217 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001218 return room;
1219}
1220
Alan Cox95da3102008-07-22 11:09:07 +01001221static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
1222 const unsigned char *data, int count)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001223{
1224 int status;
1225 int i;
1226 int bytes_sent = 0;
1227 int transfer_size;
1228
1229 struct moschip_port *mos7720_port;
1230 struct usb_serial *serial;
1231 struct urb *urb;
1232 const unsigned char *current_position = data;
1233
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001234 serial = port->serial;
1235
1236 mos7720_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001237 if (mos7720_port == NULL)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001238 return -ENODEV;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001239
1240 /* try to find a free urb in the list */
1241 urb = NULL;
1242
1243 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001244 if (mos7720_port->write_urb_pool[i] &&
1245 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001246 urb = mos7720_port->write_urb_pool[i];
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001247 dev_dbg(&port->dev, "URB:%d\n", i);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001248 break;
1249 }
1250 }
1251
1252 if (urb == NULL) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001253 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001254 goto exit;
1255 }
1256
1257 if (urb->transfer_buffer == NULL) {
1258 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1259 GFP_KERNEL);
1260 if (urb->transfer_buffer == NULL) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001261 dev_err_console(port, "%s no more kernel memory...\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001262 __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001263 goto exit;
1264 }
1265 }
Alan Cox4da1a172008-07-22 11:16:21 +01001266 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001267
1268 memcpy(urb->transfer_buffer, current_position, transfer_size);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001269 usb_serial_debug_data(&port->dev, __func__, transfer_size,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001270 urb->transfer_buffer);
1271
1272 /* fill urb with data and submit */
1273 usb_fill_bulk_urb(urb, serial->dev,
1274 usb_sndbulkpipe(serial->dev,
Alan Cox4da1a172008-07-22 11:16:21 +01001275 port->bulk_out_endpointAddress),
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001276 urb->transfer_buffer, transfer_size,
1277 mos7720_bulk_out_data_callback, mos7720_port);
1278
1279 /* send it down the pipe */
Alan Cox4da1a172008-07-22 11:16:21 +01001280 status = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001281 if (status) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001282 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001283 "with status = %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001284 bytes_sent = status;
1285 goto exit;
1286 }
1287 bytes_sent = transfer_size;
1288
1289exit:
1290 return bytes_sent;
1291}
1292
Alan Cox95da3102008-07-22 11:09:07 +01001293static void mos7720_throttle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001294{
Alan Cox95da3102008-07-22 11:09:07 +01001295 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001296 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001297 int status;
1298
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001299 mos7720_port = usb_get_serial_port_data(port);
1300
1301 if (mos7720_port == NULL)
1302 return;
1303
1304 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001305 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001306 return;
1307 }
1308
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001309 /* if we are implementing XON/XOFF, send the stop character */
1310 if (I_IXOFF(tty)) {
1311 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001312 status = mos7720_write(tty, port, &stop_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001313 if (status <= 0)
1314 return;
1315 }
1316
1317 /* if we are implementing RTS/CTS, toggle that line */
1318 if (tty->termios->c_cflag & CRTSCTS) {
1319 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
Mike Dunn63b91762010-04-15 17:02:09 -04001320 write_mos_reg(port->serial, port->number - port->serial->minor,
1321 MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001322 if (status != 0)
1323 return;
1324 }
1325}
1326
Alan Cox95da3102008-07-22 11:09:07 +01001327static void mos7720_unthrottle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001328{
Alan Cox95da3102008-07-22 11:09:07 +01001329 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001330 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +01001331 int status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001332
1333 if (mos7720_port == NULL)
1334 return;
1335
1336 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001337 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001338 return;
1339 }
1340
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001341 /* if we are implementing XON/XOFF, send the start character */
1342 if (I_IXOFF(tty)) {
1343 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001344 status = mos7720_write(tty, port, &start_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001345 if (status <= 0)
1346 return;
1347 }
1348
1349 /* if we are implementing RTS/CTS, toggle that line */
1350 if (tty->termios->c_cflag & CRTSCTS) {
1351 mos7720_port->shadowMCR |= UART_MCR_RTS;
Mike Dunn63b91762010-04-15 17:02:09 -04001352 write_mos_reg(port->serial, port->number - port->serial->minor,
1353 MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001354 if (status != 0)
1355 return;
1356 }
1357}
1358
Mike Dunnb69578d2010-04-15 17:01:33 -04001359/* FIXME: this function does not work */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001360static int set_higher_rates(struct moschip_port *mos7720_port,
1361 unsigned int baud)
1362{
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001363 struct usb_serial_port *port;
1364 struct usb_serial *serial;
1365 int port_number;
Mike Dunn63b91762010-04-15 17:02:09 -04001366 enum mos_regs sp_reg;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001367 if (mos7720_port == NULL)
1368 return -EINVAL;
1369
1370 port = mos7720_port->port;
1371 serial = port->serial;
1372
Alan Cox4da1a172008-07-22 11:16:21 +01001373 /***********************************************
1374 * Init Sequence for higher rates
1375 ***********************************************/
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001376 dev_dbg(&port->dev, "Sending Setting Commands ..........\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001377 port_number = port->number - port->serial->minor;
1378
Mike Dunn63b91762010-04-15 17:02:09 -04001379 write_mos_reg(serial, port_number, IER, 0x00);
1380 write_mos_reg(serial, port_number, FCR, 0x00);
1381 write_mos_reg(serial, port_number, FCR, 0xcf);
1382 mos7720_port->shadowMCR = 0x0b;
1383 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1384 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x00);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001385
Alan Cox4da1a172008-07-22 11:16:21 +01001386 /***********************************************
1387 * Set for higher rates *
1388 ***********************************************/
Mike Dunnb69578d2010-04-15 17:01:33 -04001389 /* writing baud rate verbatum into uart clock field clearly not right */
Mike Dunn63b91762010-04-15 17:02:09 -04001390 if (port_number == 0)
1391 sp_reg = SP1_REG;
1392 else
1393 sp_reg = SP2_REG;
1394 write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
1395 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x03);
1396 mos7720_port->shadowMCR = 0x2b;
1397 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001398
Alan Cox4da1a172008-07-22 11:16:21 +01001399 /***********************************************
1400 * Set DLL/DLM
1401 ***********************************************/
Mike Dunn63b91762010-04-15 17:02:09 -04001402 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1403 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1404 write_mos_reg(serial, port_number, DLL, 0x01);
1405 write_mos_reg(serial, port_number, DLM, 0x00);
1406 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1407 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001408
1409 return 0;
1410}
1411
1412/* baud rate information */
Alan Cox4da1a172008-07-22 11:16:21 +01001413struct divisor_table_entry {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001414 __u32 baudrate;
1415 __u16 divisor;
1416};
1417
1418/* Define table of divisors for moschip 7720 hardware *
1419 * These assume a 3.6864MHz crystal, the standard /16, and *
1420 * MCR.7 = 0. */
1421static struct divisor_table_entry divisor_table[] = {
1422 { 50, 2304},
1423 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
1424 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
1425 { 150, 768},
1426 { 300, 384},
1427 { 600, 192},
1428 { 1200, 96},
1429 { 1800, 64},
1430 { 2400, 48},
1431 { 4800, 24},
1432 { 7200, 16},
1433 { 9600, 12},
1434 { 19200, 6},
1435 { 38400, 3},
1436 { 57600, 2},
1437 { 115200, 1},
1438};
1439
1440/*****************************************************************************
1441 * calc_baud_rate_divisor
1442 * this function calculates the proper baud rate divisor for the specified
1443 * baud rate.
1444 *****************************************************************************/
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001445static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, int *divisor)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001446{
1447 int i;
1448 __u16 custom;
1449 __u16 round1;
1450 __u16 round;
1451
1452
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001453 dev_dbg(&port->dev, "%s - %d\n", __func__, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001454
1455 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
1456 if (divisor_table[i].baudrate == baudrate) {
1457 *divisor = divisor_table[i].divisor;
1458 return 0;
1459 }
1460 }
1461
Alan Cox4da1a172008-07-22 11:16:21 +01001462 /* After trying for all the standard baud rates *
1463 * Try calculating the divisor for this baud rate */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001464 if (baudrate > 75 && baudrate < 230400) {
1465 /* get the divisor */
1466 custom = (__u16)(230400L / baudrate);
1467
1468 /* Check for round off */
1469 round1 = (__u16)(2304000L / baudrate);
1470 round = (__u16)(round1 - (custom * 10));
1471 if (round > 4)
1472 custom++;
1473 *divisor = custom;
1474
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001475 dev_dbg(&port->dev, "Baud %d = %d\n", baudrate, custom);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001476 return 0;
1477 }
1478
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001479 dev_dbg(&port->dev, "Baud calculation Failed...\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001480 return -EINVAL;
1481}
1482
1483/*
1484 * send_cmd_write_baud_rate
1485 * this function sends the proper command to change the baud rate of the
1486 * specified port.
1487 */
1488static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1489 int baudrate)
1490{
1491 struct usb_serial_port *port;
1492 struct usb_serial *serial;
1493 int divisor;
1494 int status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001495 unsigned char number;
1496
1497 if (mos7720_port == NULL)
1498 return -1;
1499
1500 port = mos7720_port->port;
1501 serial = port->serial;
1502
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001503 number = port->number - port->serial->minor;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001504 dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001505
Alan Cox4da1a172008-07-22 11:16:21 +01001506 /* Calculate the Divisor */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001507 status = calc_baud_rate_divisor(port, baudrate, &divisor);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001508 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001509 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001510 return status;
1511 }
1512
Alan Cox4da1a172008-07-22 11:16:21 +01001513 /* Enable access to divisor latch */
Mike Dunn63b91762010-04-15 17:02:09 -04001514 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1515 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001516
1517 /* Write the divisor */
Mike Dunn63b91762010-04-15 17:02:09 -04001518 write_mos_reg(serial, number, DLL, (__u8)(divisor & 0xff));
1519 write_mos_reg(serial, number, DLM, (__u8)((divisor & 0xff00) >> 8));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001520
Alan Cox4da1a172008-07-22 11:16:21 +01001521 /* Disable access to divisor latch */
Mike Dunn63b91762010-04-15 17:02:09 -04001522 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1523 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001524
1525 return status;
1526}
1527
1528/*
1529 * change_port_settings
1530 * This routine is called to set the UART on the device to match
1531 * the specified new settings.
1532 */
Alan Cox95da3102008-07-22 11:09:07 +01001533static void change_port_settings(struct tty_struct *tty,
1534 struct moschip_port *mos7720_port,
Alan Cox606d0992006-12-08 02:38:45 -08001535 struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001536{
1537 struct usb_serial_port *port;
1538 struct usb_serial *serial;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001539 int baud;
1540 unsigned cflag;
1541 unsigned iflag;
1542 __u8 mask = 0xff;
1543 __u8 lData;
1544 __u8 lParity;
1545 __u8 lStop;
1546 int status;
1547 int port_number;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001548
1549 if (mos7720_port == NULL)
1550 return ;
1551
1552 port = mos7720_port->port;
1553 serial = port->serial;
1554 port_number = port->number - port->serial->minor;
1555
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001556 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001557 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001558 return;
1559 }
1560
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001561 lData = UART_LCR_WLEN8;
1562 lStop = 0x00; /* 1 stop bit */
1563 lParity = 0x00; /* No parity */
1564
1565 cflag = tty->termios->c_cflag;
1566 iflag = tty->termios->c_iflag;
1567
1568 /* Change the number of bits */
1569 switch (cflag & CSIZE) {
1570 case CS5:
1571 lData = UART_LCR_WLEN5;
1572 mask = 0x1f;
1573 break;
1574
1575 case CS6:
1576 lData = UART_LCR_WLEN6;
1577 mask = 0x3f;
1578 break;
1579
1580 case CS7:
1581 lData = UART_LCR_WLEN7;
1582 mask = 0x7f;
1583 break;
1584 default:
1585 case CS8:
1586 lData = UART_LCR_WLEN8;
1587 break;
1588 }
1589
1590 /* Change the Parity bit */
1591 if (cflag & PARENB) {
1592 if (cflag & PARODD) {
1593 lParity = UART_LCR_PARITY;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001594 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001595 } else {
1596 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001597 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001598 }
1599
1600 } else {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001601 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001602 }
1603
1604 if (cflag & CMSPAR)
1605 lParity = lParity | 0x20;
1606
1607 /* Change the Stop bit */
1608 if (cflag & CSTOPB) {
1609 lStop = UART_LCR_STOP;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001610 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001611 } else {
1612 lStop = 0x00;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001613 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001614 }
1615
1616#define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1617#define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1618#define LCR_PAR_MASK 0x38 /* Mask for parity field */
1619
1620 /* Update the LCR with the correct value */
Alan Cox4da1a172008-07-22 11:16:21 +01001621 mos7720_port->shadowLCR &=
Mike Dunn63b91762010-04-15 17:02:09 -04001622 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001623 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1624
1625
1626 /* Disable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001627 write_mos_reg(serial, port_number, IER, 0x00);
1628 write_mos_reg(serial, port_number, FCR, 0x00);
1629 write_mos_reg(serial, port_number, FCR, 0xcf);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001630
1631 /* Send the updated LCR value to the mos7720 */
Mike Dunn63b91762010-04-15 17:02:09 -04001632 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1633 mos7720_port->shadowMCR = 0x0b;
1634 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001635
1636 /* set up the MCR register and send it to the mos7720 */
1637 mos7720_port->shadowMCR = UART_MCR_OUT2;
1638 if (cflag & CBAUD)
1639 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1640
1641 if (cflag & CRTSCTS) {
1642 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
Alan Cox4da1a172008-07-22 11:16:21 +01001643 /* To set hardware flow control to the specified *
1644 * serial port, in SP1/2_CONTROL_REG */
Mike Dunn63b91762010-04-15 17:02:09 -04001645 if (port->number)
1646 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x01);
1647 else
1648 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x02);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001649
Mike Dunn63b91762010-04-15 17:02:09 -04001650 } else
1651 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1652
1653 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001654
1655 /* Determine divisor based on baud rate */
1656 baud = tty_get_baud_rate(tty);
1657 if (!baud) {
1658 /* pick a default, any default... */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001659 dev_dbg(&port->dev, "Picked default baud...\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001660 baud = 9600;
1661 }
1662
1663 if (baud >= 230400) {
1664 set_higher_rates(mos7720_port, baud);
1665 /* Enable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001666 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001667 return;
1668 }
1669
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001670 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001671 status = send_cmd_write_baud_rate(mos7720_port, baud);
Alan Cox65d063ab2008-01-03 17:01:18 +00001672 /* FIXME: needs to write actual resulting baud back not just
1673 blindly do so */
1674 if (cflag & CBAUD)
1675 tty_encode_baud_rate(tty, baud, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001676 /* Enable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001677 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001678
1679 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001680 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1681 if (status)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001682 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001683 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001684}
1685
1686/*
1687 * mos7720_set_termios
1688 * this function is called by the tty driver when it wants to change the
1689 * termios structure.
1690 */
Alan Cox95da3102008-07-22 11:09:07 +01001691static void mos7720_set_termios(struct tty_struct *tty,
1692 struct usb_serial_port *port, struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001693{
1694 int status;
1695 unsigned int cflag;
1696 struct usb_serial *serial;
1697 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001698
1699 serial = port->serial;
1700
1701 mos7720_port = usb_get_serial_port_data(port);
1702
1703 if (mos7720_port == NULL)
1704 return;
1705
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001706 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001707 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001708 return;
1709 }
1710
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001711 dev_dbg(&port->dev, "setting termios - ASPIRE\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001712
1713 cflag = tty->termios->c_cflag;
1714
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001715 dev_dbg(&port->dev, "%s - cflag %08x iflag %08x\n", __func__,
1716 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001717
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001718 dev_dbg(&port->dev, "%s - old cflag %08x old iflag %08x\n", __func__,
1719 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001720
1721 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001722 change_port_settings(tty, mos7720_port, old_termios);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001723
Alan Cox4da1a172008-07-22 11:16:21 +01001724 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001725 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1726 if (status)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001727 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001728 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001729}
1730
1731/*
1732 * get_lsr_info - get line status register info
1733 *
1734 * Purpose: Let user call ioctl() to get info when the UART physically
1735 * is emptied. On bus types like RS485, the transmitter must
1736 * release the bus after transmitting. This must be done when
1737 * the transmit shift register is empty, not be done when the
1738 * transmit holding register is empty. This functionality
1739 * allows an RS485 driver to be written in user space.
1740 */
Alan Cox4da1a172008-07-22 11:16:21 +01001741static int get_lsr_info(struct tty_struct *tty,
1742 struct moschip_port *mos7720_port, unsigned int __user *value)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001743{
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001744 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001745 unsigned int result = 0;
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001746 unsigned char data = 0;
1747 int port_number = port->number - port->serial->minor;
1748 int count;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001749
Alan Cox95da3102008-07-22 11:09:07 +01001750 count = mos7720_chars_in_buffer(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001751 if (count == 0) {
Mike Dunn63b91762010-04-15 17:02:09 -04001752 read_mos_reg(port->serial, port_number, LSR, &data);
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001753 if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1754 == (UART_LSR_TEMT | UART_LSR_THRE)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001755 dev_dbg(&port->dev, "%s -- Empty\n", __func__);
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001756 result = TIOCSER_TEMT;
1757 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001758 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001759 if (copy_to_user(value, &result, sizeof(int)))
1760 return -EFAULT;
1761 return 0;
1762}
1763
Alan Cox60b33c12011-02-14 16:26:14 +00001764static int mos7720_tiocmget(struct tty_struct *tty)
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001765{
1766 struct usb_serial_port *port = tty->driver_data;
1767 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1768 unsigned int result = 0;
1769 unsigned int mcr ;
1770 unsigned int msr ;
1771
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001772 mcr = mos7720_port->shadowMCR;
1773 msr = mos7720_port->shadowMSR;
1774
1775 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1776 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1777 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1778 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
1779 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1780 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
1781
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001782 return result;
1783}
1784
Alan Cox20b9d172011-02-14 16:26:50 +00001785static int mos7720_tiocmset(struct tty_struct *tty,
Mike Dunn63b91762010-04-15 17:02:09 -04001786 unsigned int set, unsigned int clear)
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001787{
1788 struct usb_serial_port *port = tty->driver_data;
1789 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1790 unsigned int mcr ;
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001791
1792 mcr = mos7720_port->shadowMCR;
1793
1794 if (set & TIOCM_RTS)
1795 mcr |= UART_MCR_RTS;
1796 if (set & TIOCM_DTR)
1797 mcr |= UART_MCR_DTR;
1798 if (set & TIOCM_LOOP)
1799 mcr |= UART_MCR_LOOP;
1800
1801 if (clear & TIOCM_RTS)
1802 mcr &= ~UART_MCR_RTS;
1803 if (clear & TIOCM_DTR)
1804 mcr &= ~UART_MCR_DTR;
1805 if (clear & TIOCM_LOOP)
1806 mcr &= ~UART_MCR_LOOP;
1807
1808 mos7720_port->shadowMCR = mcr;
Mike Dunn63b91762010-04-15 17:02:09 -04001809 write_mos_reg(port->serial, port->number - port->serial->minor,
1810 MCR, mos7720_port->shadowMCR);
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001811
1812 return 0;
1813}
1814
Alan Cox0bca1b92010-09-16 18:21:40 +01001815static int mos7720_get_icount(struct tty_struct *tty,
1816 struct serial_icounter_struct *icount)
1817{
1818 struct usb_serial_port *port = tty->driver_data;
1819 struct moschip_port *mos7720_port;
1820 struct async_icount cnow;
1821
1822 mos7720_port = usb_get_serial_port_data(port);
1823 cnow = mos7720_port->icount;
1824
1825 icount->cts = cnow.cts;
1826 icount->dsr = cnow.dsr;
1827 icount->rng = cnow.rng;
1828 icount->dcd = cnow.dcd;
1829 icount->rx = cnow.rx;
1830 icount->tx = cnow.tx;
1831 icount->frame = cnow.frame;
1832 icount->overrun = cnow.overrun;
1833 icount->parity = cnow.parity;
1834 icount->brk = cnow.brk;
1835 icount->buf_overrun = cnow.buf_overrun;
1836
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001837 dev_dbg(&port->dev, "%s TIOCGICOUNT RX=%d, TX=%d\n", __func__,
1838 icount->rx, icount->tx);
Alan Cox0bca1b92010-09-16 18:21:40 +01001839 return 0;
1840}
1841
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001842static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1843 unsigned int __user *value)
1844{
Alan Cox0bca1b92010-09-16 18:21:40 +01001845 unsigned int mcr;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001846 unsigned int arg;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001847
1848 struct usb_serial_port *port;
1849
1850 if (mos7720_port == NULL)
1851 return -1;
1852
Alan Cox4da1a172008-07-22 11:16:21 +01001853 port = (struct usb_serial_port *)mos7720_port->port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001854 mcr = mos7720_port->shadowMCR;
1855
1856 if (copy_from_user(&arg, value, sizeof(int)))
1857 return -EFAULT;
1858
1859 switch (cmd) {
1860 case TIOCMBIS:
1861 if (arg & TIOCM_RTS)
1862 mcr |= UART_MCR_RTS;
1863 if (arg & TIOCM_DTR)
1864 mcr |= UART_MCR_RTS;
1865 if (arg & TIOCM_LOOP)
1866 mcr |= UART_MCR_LOOP;
1867 break;
1868
1869 case TIOCMBIC:
1870 if (arg & TIOCM_RTS)
1871 mcr &= ~UART_MCR_RTS;
1872 if (arg & TIOCM_DTR)
1873 mcr &= ~UART_MCR_RTS;
1874 if (arg & TIOCM_LOOP)
1875 mcr &= ~UART_MCR_LOOP;
1876 break;
1877
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001878 }
1879
1880 mos7720_port->shadowMCR = mcr;
Mike Dunn63b91762010-04-15 17:02:09 -04001881 write_mos_reg(port->serial, port->number - port->serial->minor,
1882 MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001883
1884 return 0;
1885}
1886
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001887static int get_serial_info(struct moschip_port *mos7720_port,
1888 struct serial_struct __user *retinfo)
1889{
1890 struct serial_struct tmp;
1891
1892 if (!retinfo)
1893 return -EFAULT;
1894
1895 memset(&tmp, 0, sizeof(tmp));
1896
1897 tmp.type = PORT_16550A;
1898 tmp.line = mos7720_port->port->serial->minor;
1899 tmp.port = mos7720_port->port->number;
1900 tmp.irq = 0;
1901 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
Alan Cox4da1a172008-07-22 11:16:21 +01001902 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001903 tmp.baud_base = 9600;
1904 tmp.close_delay = 5*HZ;
1905 tmp.closing_wait = 30*HZ;
1906
1907 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1908 return -EFAULT;
1909 return 0;
1910}
1911
Alan Cox00a0d0d2011-02-14 16:27:06 +00001912static int mos7720_ioctl(struct tty_struct *tty,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001913 unsigned int cmd, unsigned long arg)
1914{
Alan Cox95da3102008-07-22 11:09:07 +01001915 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001916 struct moschip_port *mos7720_port;
1917 struct async_icount cnow;
1918 struct async_icount cprev;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001919
1920 mos7720_port = usb_get_serial_port_data(port);
1921 if (mos7720_port == NULL)
1922 return -ENODEV;
1923
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001924 dev_dbg(&port->dev, "%s - cmd = 0x%x", __func__, cmd);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001925
1926 switch (cmd) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001927 case TIOCSERGETLSR:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001928 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
Alan Cox4da1a172008-07-22 11:16:21 +01001929 return get_lsr_info(tty, mos7720_port,
1930 (unsigned int __user *)arg);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001931
Alan Cox95da3102008-07-22 11:09:07 +01001932 /* FIXME: These should be using the mode methods */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001933 case TIOCMBIS:
1934 case TIOCMBIC:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001935 dev_dbg(&port->dev, "%s TIOCMSET/TIOCMBIC/TIOCMSET\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001936 return set_modem_info(mos7720_port, cmd,
1937 (unsigned int __user *)arg);
1938
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001939 case TIOCGSERIAL:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001940 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001941 return get_serial_info(mos7720_port,
1942 (struct serial_struct __user *)arg);
1943
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001944 case TIOCMIWAIT:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001945 dev_dbg(&port->dev, "%s TIOCMIWAIT\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001946 cprev = mos7720_port->icount;
1947 while (1) {
1948 if (signal_pending(current))
1949 return -ERESTARTSYS;
1950 cnow = mos7720_port->icount;
1951 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1952 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1953 return -EIO; /* no change => error */
1954 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1955 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1956 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
Alan Cox4da1a172008-07-22 11:16:21 +01001957 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001958 return 0;
1959 }
1960 cprev = cnow;
1961 }
1962 /* NOTREACHED */
1963 break;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001964 }
1965
1966 return -ENOIOCTLCMD;
1967}
1968
1969static int mos7720_startup(struct usb_serial *serial)
1970{
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001971 struct moschip_port *mos7720_port;
1972 struct usb_device *dev;
1973 int i;
1974 char data;
Huzaifa Sidhpurwala91f58ae2011-02-21 12:58:45 +05301975 u16 product;
Mike Dunnb69578d2010-04-15 17:01:33 -04001976 int ret_val;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001977
Huzaifa Sidhpurwala91f58ae2011-02-21 12:58:45 +05301978 product = le16_to_cpu(serial->dev->descriptor.idProduct);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001979 dev = serial->dev;
1980
Mike Dunnfb088e32010-01-26 12:12:12 -05001981 /*
1982 * The 7715 uses the first bulk in/out endpoint pair for the parallel
1983 * port, and the second for the serial port. Because the usbserial core
1984 * assumes both pairs are serial ports, we must engage in a bit of
1985 * subterfuge and swap the pointers for ports 0 and 1 in order to make
1986 * port 0 point to the serial port. However, both moschip devices use a
1987 * single interrupt-in endpoint for both ports (as mentioned a little
1988 * further down), and this endpoint was assigned to port 0. So after
1989 * the swap, we must copy the interrupt endpoint elements from port 1
1990 * (as newly assigned) to port 0, and null out port 1 pointers.
1991 */
1992 if (product == MOSCHIP_DEVICE_ID_7715) {
1993 struct usb_serial_port *tmp = serial->port[0];
1994 serial->port[0] = serial->port[1];
1995 serial->port[1] = tmp;
1996 serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb;
1997 serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer;
1998 serial->port[0]->interrupt_in_endpointAddress =
1999 tmp->interrupt_in_endpointAddress;
2000 serial->port[1]->interrupt_in_urb = NULL;
2001 serial->port[1]->interrupt_in_buffer = NULL;
2002 }
2003
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002004
Mike Dunnb69578d2010-04-15 17:01:33 -04002005 /* set up serial port private structures */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002006 for (i = 0; i < serial->num_ports; ++i) {
2007 mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2008 if (mos7720_port == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07002009 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002010 return -ENOMEM;
2011 }
2012
2013 /* Initialize all port interrupt end point to port 0 int
2014 * endpoint. Our device has only one interrupt endpoint
Mike Dunnfb088e32010-01-26 12:12:12 -05002015 * common to all ports */
Alan Cox4da1a172008-07-22 11:16:21 +01002016 serial->port[i]->interrupt_in_endpointAddress =
2017 serial->port[0]->interrupt_in_endpointAddress;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002018
2019 mos7720_port->port = serial->port[i];
2020 usb_set_serial_port_data(serial->port[i], mos7720_port);
2021
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07002022 dev_dbg(&dev->dev, "port number is %d\n", serial->port[i]->number);
2023 dev_dbg(&dev->dev, "serial number is %d\n", serial->minor);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002024 }
2025
2026
2027 /* setting configuration feature to one */
2028 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Alan Cox4da1a172008-07-22 11:16:21 +01002029 (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5*HZ);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002030
Mike Dunnb69578d2010-04-15 17:01:33 -04002031 /* start the interrupt urb */
2032 ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
2033 if (ret_val)
2034 dev_err(&dev->dev,
2035 "%s - Error %d submitting control urb\n",
2036 __func__, ret_val);
2037
2038#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
2039 if (product == MOSCHIP_DEVICE_ID_7715) {
2040 ret_val = mos7715_parport_init(serial);
2041 if (ret_val < 0)
2042 return ret_val;
2043 }
2044#endif
Alan Cox4da1a172008-07-22 11:16:21 +01002045 /* LSR For Port 1 */
Mike Dunn63b91762010-04-15 17:02:09 -04002046 read_mos_reg(serial, 0, LSR, &data);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07002047 dev_dbg(&dev->dev, "LSR:%x\n", data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002048
2049 return 0;
2050}
2051
Alan Sternf9c99bb2009-06-02 11:53:55 -04002052static void mos7720_release(struct usb_serial *serial)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002053{
2054 int i;
2055
Mike Dunnb69578d2010-04-15 17:01:33 -04002056#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
2057 /* close the parallel port */
2058
2059 if (le16_to_cpu(serial->dev->descriptor.idProduct)
2060 == MOSCHIP_DEVICE_ID_7715) {
2061 struct urbtracker *urbtrack;
2062 unsigned long flags;
2063 struct mos7715_parport *mos_parport =
2064 usb_get_serial_data(serial);
2065
2066 /* prevent NULL ptr dereference in port callbacks */
2067 spin_lock(&release_lock);
2068 mos_parport->pp->private_data = NULL;
2069 spin_unlock(&release_lock);
2070
2071 /* wait for synchronous usb calls to return */
2072 if (mos_parport->msg_pending)
2073 wait_for_completion_timeout(&mos_parport->syncmsg_compl,
2074 MOS_WDR_TIMEOUT);
2075
2076 parport_remove_port(mos_parport->pp);
2077 usb_set_serial_data(serial, NULL);
2078 mos_parport->serial = NULL;
2079
2080 /* if tasklet currently scheduled, wait for it to complete */
2081 tasklet_kill(&mos_parport->urb_tasklet);
2082
2083 /* unlink any urbs sent by the tasklet */
2084 spin_lock_irqsave(&mos_parport->listlock, flags);
2085 list_for_each_entry(urbtrack,
2086 &mos_parport->active_urbs,
2087 urblist_entry)
2088 usb_unlink_urb(urbtrack->urb);
2089 spin_unlock_irqrestore(&mos_parport->listlock, flags);
2090
2091 kref_put(&mos_parport->ref_count, destroy_mos_parport);
2092 }
2093#endif
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002094 /* free private structure allocated for serial port */
Alan Sternf9c99bb2009-06-02 11:53:55 -04002095 for (i = 0; i < serial->num_ports; ++i)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002096 kfree(usb_get_serial_port_data(serial->port[i]));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002097}
2098
2099static struct usb_serial_driver moschip7720_2port_driver = {
2100 .driver = {
2101 .owner = THIS_MODULE,
2102 .name = "moschip7720",
2103 },
2104 .description = "Moschip 2 port adapter",
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002105 .id_table = id_table,
Mike Dunnfb088e32010-01-26 12:12:12 -05002106 .calc_num_ports = mos77xx_calc_num_ports,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002107 .open = mos7720_open,
2108 .close = mos7720_close,
2109 .throttle = mos7720_throttle,
2110 .unthrottle = mos7720_unthrottle,
Mike Dunnfb088e32010-01-26 12:12:12 -05002111 .probe = mos77xx_probe,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002112 .attach = mos7720_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002113 .release = mos7720_release,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002114 .ioctl = mos7720_ioctl,
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07002115 .tiocmget = mos7720_tiocmget,
2116 .tiocmset = mos7720_tiocmset,
Alan Cox0bca1b92010-09-16 18:21:40 +01002117 .get_icount = mos7720_get_icount,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002118 .set_termios = mos7720_set_termios,
2119 .write = mos7720_write,
2120 .write_room = mos7720_write_room,
2121 .chars_in_buffer = mos7720_chars_in_buffer,
2122 .break_ctl = mos7720_break,
2123 .read_bulk_callback = mos7720_bulk_in_callback,
Mike Dunnfb088e32010-01-26 12:12:12 -05002124 .read_int_callback = NULL /* dynamically assigned in probe() */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002125};
2126
Alan Stern4d2a7af2012-02-23 14:57:09 -05002127static struct usb_serial_driver * const serial_drivers[] = {
2128 &moschip7720_2port_driver, NULL
2129};
2130
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002131module_usb_serial_driver(serial_drivers, id_table);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002132
Alan Cox4da1a172008-07-22 11:16:21 +01002133MODULE_AUTHOR(DRIVER_AUTHOR);
2134MODULE_DESCRIPTION(DRIVER_DESC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002135MODULE_LICENSE("GPL");
2136
2137module_param(debug, bool, S_IRUGO | S_IWUSR);
2138MODULE_PARM_DESC(debug, "Debug enabled or not");