blob: 75267421aad85636d480b732a507a176e874814f [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
Mike Dunnfb088e32010-01-26 12:12:12 -050074static struct usb_serial_driver moschip7720_2port_driver;
75
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070076#define USB_VENDOR_ID_MOSCHIP 0x9710
77#define MOSCHIP_DEVICE_ID_7720 0x7720
78#define MOSCHIP_DEVICE_ID_7715 0x7715
79
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -070080static const struct usb_device_id id_table[] = {
Alan Cox4da1a172008-07-22 11:16:21 +010081 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
Mike Dunnfb088e32010-01-26 12:12:12 -050082 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) },
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070083 { } /* terminating entry */
84};
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -070085MODULE_DEVICE_TABLE(usb, id_table);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070086
Mike Dunnb69578d2010-04-15 17:01:33 -040087#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
88
89/* initial values for parport regs */
90#define DCR_INIT_VAL 0x0c /* SLCTIN, nINIT */
91#define ECR_INIT_VAL 0x00 /* SPP mode */
92
93struct urbtracker {
94 struct mos7715_parport *mos_parport;
95 struct list_head urblist_entry;
96 struct kref ref_count;
97 struct urb *urb;
98};
99
100enum mos7715_pp_modes {
101 SPP = 0<<5,
102 PS2 = 1<<5, /* moschip calls this 'NIBBLE' mode */
103 PPF = 2<<5, /* moschip calls this 'CB-FIFO mode */
104};
105
106struct mos7715_parport {
107 struct parport *pp; /* back to containing struct */
108 struct kref ref_count; /* to instance of this struct */
109 struct list_head deferred_urbs; /* list deferred async urbs */
110 struct list_head active_urbs; /* list async urbs in flight */
111 spinlock_t listlock; /* protects list access */
112 bool msg_pending; /* usb sync call pending */
113 struct completion syncmsg_compl; /* usb sync call completed */
114 struct tasklet_struct urb_tasklet; /* for sending deferred urbs */
115 struct usb_serial *serial; /* back to containing struct */
116 __u8 shadowECR; /* parallel port regs... */
117 __u8 shadowDCR;
118 atomic_t shadowDSR; /* updated in int-in callback */
119};
120
121/* lock guards against dereferencing NULL ptr in parport ops callbacks */
122static DEFINE_SPINLOCK(release_lock);
123
Mike Dunn63b91762010-04-15 17:02:09 -0400124#endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
125
126static const unsigned int dummy; /* for clarity in register access fns */
127
Mike Dunnb69578d2010-04-15 17:01:33 -0400128enum mos_regs {
129 THR, /* serial port regs */
130 RHR,
131 IER,
132 FCR,
133 ISR,
134 LCR,
135 MCR,
136 LSR,
137 MSR,
138 SPR,
139 DLL,
140 DLM,
141 DPR, /* parallel port regs */
142 DSR,
143 DCR,
144 ECR,
145 SP1_REG, /* device control regs */
146 SP2_REG, /* serial port 2 (7720 only) */
147 PP_REG,
148 SP_CONTROL_REG,
149};
150
151/*
152 * Return the correct value for the Windex field of the setup packet
153 * for a control endpoint message. See the 7715 datasheet.
154 */
155static inline __u16 get_reg_index(enum mos_regs reg)
156{
157 static const __u16 mos7715_index_lookup_table[] = {
158 0x00, /* THR */
159 0x00, /* RHR */
160 0x01, /* IER */
161 0x02, /* FCR */
162 0x02, /* ISR */
163 0x03, /* LCR */
164 0x04, /* MCR */
165 0x05, /* LSR */
166 0x06, /* MSR */
167 0x07, /* SPR */
168 0x00, /* DLL */
169 0x01, /* DLM */
170 0x00, /* DPR */
171 0x01, /* DSR */
172 0x02, /* DCR */
173 0x0a, /* ECR */
174 0x01, /* SP1_REG */
175 0x02, /* SP2_REG (7720 only) */
176 0x04, /* PP_REG (7715 only) */
177 0x08, /* SP_CONTROL_REG */
178 };
179 return mos7715_index_lookup_table[reg];
180}
181
182/*
183 * Return the correct value for the upper byte of the Wvalue field of
184 * the setup packet for a control endpoint message.
185 */
Mike Dunn63b91762010-04-15 17:02:09 -0400186static inline __u16 get_reg_value(enum mos_regs reg,
187 unsigned int serial_portnum)
Mike Dunnb69578d2010-04-15 17:01:33 -0400188{
189 if (reg >= SP1_REG) /* control reg */
190 return 0x0000;
Mike Dunn63b91762010-04-15 17:02:09 -0400191
192 else if (reg >= DPR) /* parallel port reg (7715 only) */
Mike Dunnb69578d2010-04-15 17:01:33 -0400193 return 0x0100;
Mike Dunn63b91762010-04-15 17:02:09 -0400194
195 else /* serial port reg */
196 return (serial_portnum + 2) << 8;
Mike Dunnb69578d2010-04-15 17:01:33 -0400197}
198
199/*
200 * Write data byte to the specified device register. The data is embedded in
Mike Dunn63b91762010-04-15 17:02:09 -0400201 * the value field of the setup packet. serial_portnum is ignored for registers
202 * not specific to a particular serial port.
Mike Dunnb69578d2010-04-15 17:01:33 -0400203 */
Mike Dunn63b91762010-04-15 17:02:09 -0400204static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
205 enum mos_regs reg, __u8 data)
Mike Dunnb69578d2010-04-15 17:01:33 -0400206{
Mike Dunnb69578d2010-04-15 17:01:33 -0400207 struct usb_device *usbdev = serial->dev;
208 unsigned int pipe = usb_sndctrlpipe(usbdev, 0);
209 __u8 request = (__u8)0x0e;
210 __u8 requesttype = (__u8)0x40;
Mike Dunnb69578d2010-04-15 17:01:33 -0400211 __u16 index = get_reg_index(reg);
Mike Dunn63b91762010-04-15 17:02:09 -0400212 __u16 value = get_reg_value(reg, serial_portnum) + data;
213 int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
214 index, NULL, 0, MOS_WDR_TIMEOUT);
Mike Dunnb69578d2010-04-15 17:01:33 -0400215 if (status < 0)
216 dev_err(&usbdev->dev,
217 "mos7720: usb_control_msg() failed: %d", status);
218 return status;
219}
220
221/*
222 * Read data byte from the specified device register. The data returned by the
Mike Dunn63b91762010-04-15 17:02:09 -0400223 * device is embedded in the value field of the setup packet. serial_portnum is
224 * ignored for registers that are not specific to a particular serial port.
Mike Dunnb69578d2010-04-15 17:01:33 -0400225 */
Mike Dunn63b91762010-04-15 17:02:09 -0400226static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
227 enum mos_regs reg, __u8 *data)
Mike Dunnb69578d2010-04-15 17:01:33 -0400228{
Mike Dunn63b91762010-04-15 17:02:09 -0400229 struct usb_device *usbdev = serial->dev;
Mike Dunnb69578d2010-04-15 17:01:33 -0400230 unsigned int pipe = usb_rcvctrlpipe(usbdev, 0);
231 __u8 request = (__u8)0x0d;
232 __u8 requesttype = (__u8)0xc0;
Mike Dunnb69578d2010-04-15 17:01:33 -0400233 __u16 index = get_reg_index(reg);
Mike Dunn63b91762010-04-15 17:02:09 -0400234 __u16 value = get_reg_value(reg, serial_portnum);
Mike Dunnb69578d2010-04-15 17:01:33 -0400235 int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
Mike Dunn63b91762010-04-15 17:02:09 -0400236 index, data, 1, MOS_WDR_TIMEOUT);
Mike Dunnb69578d2010-04-15 17:01:33 -0400237 if (status < 0)
238 dev_err(&usbdev->dev,
239 "mos7720: usb_control_msg() failed: %d", status);
240 return status;
241}
242
Mike Dunn63b91762010-04-15 17:02:09 -0400243#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
244
Mike Dunnb69578d2010-04-15 17:01:33 -0400245static inline int mos7715_change_mode(struct mos7715_parport *mos_parport,
246 enum mos7715_pp_modes mode)
247{
248 mos_parport->shadowECR = mode;
Mike Dunn63b91762010-04-15 17:02:09 -0400249 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400250 return 0;
251}
252
253static void destroy_mos_parport(struct kref *kref)
254{
255 struct mos7715_parport *mos_parport =
256 container_of(kref, struct mos7715_parport, ref_count);
257
Mike Dunnb69578d2010-04-15 17:01:33 -0400258 kfree(mos_parport);
259}
260
261static void destroy_urbtracker(struct kref *kref)
262{
263 struct urbtracker *urbtrack =
264 container_of(kref, struct urbtracker, ref_count);
265 struct mos7715_parport *mos_parport = urbtrack->mos_parport;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700266
Mike Dunnb69578d2010-04-15 17:01:33 -0400267 usb_free_urb(urbtrack->urb);
268 kfree(urbtrack);
269 kref_put(&mos_parport->ref_count, destroy_mos_parport);
270}
271
272/*
273 * This runs as a tasklet when sending an urb in a non-blocking parallel
274 * port callback had to be deferred because the disconnect mutex could not be
275 * obtained at the time.
276 */
277static void send_deferred_urbs(unsigned long _mos_parport)
278{
279 int ret_val;
280 unsigned long flags;
281 struct mos7715_parport *mos_parport = (void *)_mos_parport;
Wei Yongjun67990472012-08-21 11:28:45 +0800282 struct urbtracker *urbtrack, *tmp;
Mike Dunnb69578d2010-04-15 17:01:33 -0400283 struct list_head *cursor, *next;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700284 struct device *dev;
Mike Dunnb69578d2010-04-15 17:01:33 -0400285
Mike Dunnb69578d2010-04-15 17:01:33 -0400286 /* if release function ran, game over */
287 if (unlikely(mos_parport->serial == NULL))
288 return;
289
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700290 dev = &mos_parport->serial->dev->dev;
291
Mike Dunnb69578d2010-04-15 17:01:33 -0400292 /* try again to get the mutex */
293 if (!mutex_trylock(&mos_parport->serial->disc_mutex)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700294 dev_dbg(dev, "%s: rescheduling tasklet\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400295 tasklet_schedule(&mos_parport->urb_tasklet);
296 return;
297 }
298
299 /* if device disconnected, game over */
300 if (unlikely(mos_parport->serial->disconnected)) {
301 mutex_unlock(&mos_parport->serial->disc_mutex);
302 return;
303 }
304
305 spin_lock_irqsave(&mos_parport->listlock, flags);
306 if (list_empty(&mos_parport->deferred_urbs)) {
307 spin_unlock_irqrestore(&mos_parport->listlock, flags);
308 mutex_unlock(&mos_parport->serial->disc_mutex);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700309 dev_dbg(dev, "%s: deferred_urbs list empty\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400310 return;
311 }
312
313 /* move contents of deferred_urbs list to active_urbs list and submit */
314 list_for_each_safe(cursor, next, &mos_parport->deferred_urbs)
315 list_move_tail(cursor, &mos_parport->active_urbs);
Wei Yongjun67990472012-08-21 11:28:45 +0800316 list_for_each_entry_safe(urbtrack, tmp, &mos_parport->active_urbs,
Mike Dunnb69578d2010-04-15 17:01:33 -0400317 urblist_entry) {
318 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700319 dev_dbg(dev, "%s: urb submitted\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400320 if (ret_val) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700321 dev_err(dev, "usb_submit_urb() failed: %d\n", ret_val);
Mike Dunnb69578d2010-04-15 17:01:33 -0400322 list_del(&urbtrack->urblist_entry);
323 kref_put(&urbtrack->ref_count, destroy_urbtracker);
324 }
325 }
326 spin_unlock_irqrestore(&mos_parport->listlock, flags);
327 mutex_unlock(&mos_parport->serial->disc_mutex);
328}
329
330/* callback for parallel port control urbs submitted asynchronously */
331static void async_complete(struct urb *urb)
332{
333 struct urbtracker *urbtrack = urb->context;
334 int status = urb->status;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700335
Mike Dunnb69578d2010-04-15 17:01:33 -0400336 if (unlikely(status))
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700337 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
Mike Dunnb69578d2010-04-15 17:01:33 -0400338
339 /* remove the urbtracker from the active_urbs list */
340 spin_lock(&urbtrack->mos_parport->listlock);
341 list_del(&urbtrack->urblist_entry);
342 spin_unlock(&urbtrack->mos_parport->listlock);
343 kref_put(&urbtrack->ref_count, destroy_urbtracker);
344}
345
346static int write_parport_reg_nonblock(struct mos7715_parport *mos_parport,
347 enum mos_regs reg, __u8 data)
348{
349 struct urbtracker *urbtrack;
350 int ret_val;
351 unsigned long flags;
352 struct usb_ctrlrequest setup;
353 struct usb_serial *serial = mos_parport->serial;
354 struct usb_device *usbdev = serial->dev;
Mike Dunnb69578d2010-04-15 17:01:33 -0400355
356 /* create and initialize the control urb and containing urbtracker */
357 urbtrack = kmalloc(sizeof(struct urbtracker), GFP_ATOMIC);
358 if (urbtrack == NULL) {
359 dev_err(&usbdev->dev, "out of memory");
360 return -ENOMEM;
361 }
362 kref_get(&mos_parport->ref_count);
363 urbtrack->mos_parport = mos_parport;
364 urbtrack->urb = usb_alloc_urb(0, GFP_ATOMIC);
365 if (urbtrack->urb == NULL) {
366 dev_err(&usbdev->dev, "out of urbs");
367 kfree(urbtrack);
368 return -ENOMEM;
369 }
370 setup.bRequestType = (__u8)0x40;
371 setup.bRequest = (__u8)0x0e;
Mike Dunn63b91762010-04-15 17:02:09 -0400372 setup.wValue = get_reg_value(reg, dummy);
Mike Dunnb69578d2010-04-15 17:01:33 -0400373 setup.wIndex = get_reg_index(reg);
374 setup.wLength = 0;
375 usb_fill_control_urb(urbtrack->urb, usbdev,
376 usb_sndctrlpipe(usbdev, 0),
377 (unsigned char *)&setup,
378 NULL, 0, async_complete, urbtrack);
379 kref_init(&urbtrack->ref_count);
380 INIT_LIST_HEAD(&urbtrack->urblist_entry);
381
382 /*
383 * get the disconnect mutex, or add tracker to the deferred_urbs list
384 * and schedule a tasklet to try again later
385 */
386 if (!mutex_trylock(&serial->disc_mutex)) {
387 spin_lock_irqsave(&mos_parport->listlock, flags);
388 list_add_tail(&urbtrack->urblist_entry,
389 &mos_parport->deferred_urbs);
390 spin_unlock_irqrestore(&mos_parport->listlock, flags);
391 tasklet_schedule(&mos_parport->urb_tasklet);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700392 dev_dbg(&usbdev->dev, "tasklet scheduled");
Mike Dunnb69578d2010-04-15 17:01:33 -0400393 return 0;
394 }
395
396 /* bail if device disconnected */
397 if (serial->disconnected) {
398 kref_put(&urbtrack->ref_count, destroy_urbtracker);
399 mutex_unlock(&serial->disc_mutex);
400 return -ENODEV;
401 }
402
403 /* add the tracker to the active_urbs list and submit */
404 spin_lock_irqsave(&mos_parport->listlock, flags);
405 list_add_tail(&urbtrack->urblist_entry, &mos_parport->active_urbs);
406 spin_unlock_irqrestore(&mos_parport->listlock, flags);
407 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
408 mutex_unlock(&serial->disc_mutex);
409 if (ret_val) {
410 dev_err(&usbdev->dev,
411 "%s: submit_urb() failed: %d", __func__, ret_val);
412 spin_lock_irqsave(&mos_parport->listlock, flags);
413 list_del(&urbtrack->urblist_entry);
414 spin_unlock_irqrestore(&mos_parport->listlock, flags);
415 kref_put(&urbtrack->ref_count, destroy_urbtracker);
416 return ret_val;
417 }
418 return 0;
419}
420
421/*
422 * This is the the common top part of all parallel port callback operations that
423 * send synchronous messages to the device. This implements convoluted locking
424 * that avoids two scenarios: (1) a port operation is called after usbserial
425 * has called our release function, at which point struct mos7715_parport has
426 * been destroyed, and (2) the device has been disconnected, but usbserial has
427 * not called the release function yet because someone has a serial port open.
428 * The shared release_lock prevents the first, and the mutex and disconnected
429 * flag maintained by usbserial covers the second. We also use the msg_pending
430 * flag to ensure that all synchronous usb messgage calls have completed before
431 * our release function can return.
432 */
433static int parport_prologue(struct parport *pp)
434{
435 struct mos7715_parport *mos_parport;
436
437 spin_lock(&release_lock);
438 mos_parport = pp->private_data;
439 if (unlikely(mos_parport == NULL)) {
440 /* release fn called, port struct destroyed */
441 spin_unlock(&release_lock);
442 return -1;
443 }
444 mos_parport->msg_pending = true; /* synch usb call pending */
445 INIT_COMPLETION(mos_parport->syncmsg_compl);
446 spin_unlock(&release_lock);
447
448 mutex_lock(&mos_parport->serial->disc_mutex);
449 if (mos_parport->serial->disconnected) {
450 /* device disconnected */
451 mutex_unlock(&mos_parport->serial->disc_mutex);
452 mos_parport->msg_pending = false;
453 complete(&mos_parport->syncmsg_compl);
454 return -1;
455 }
456
457 return 0;
458}
459
460/*
461 * This is the the common bottom part of all parallel port functions that send
462 * synchronous messages to the device.
463 */
464static inline void parport_epilogue(struct parport *pp)
465{
466 struct mos7715_parport *mos_parport = pp->private_data;
467 mutex_unlock(&mos_parport->serial->disc_mutex);
468 mos_parport->msg_pending = false;
469 complete(&mos_parport->syncmsg_compl);
470}
471
472static void parport_mos7715_write_data(struct parport *pp, unsigned char d)
473{
474 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700475
Mike Dunnb69578d2010-04-15 17:01:33 -0400476 if (parport_prologue(pp) < 0)
477 return;
478 mos7715_change_mode(mos_parport, SPP);
Mike Dunn63b91762010-04-15 17:02:09 -0400479 write_mos_reg(mos_parport->serial, dummy, DPR, (__u8)d);
Mike Dunnb69578d2010-04-15 17:01:33 -0400480 parport_epilogue(pp);
481}
482
483static unsigned char parport_mos7715_read_data(struct parport *pp)
484{
485 struct mos7715_parport *mos_parport = pp->private_data;
486 unsigned char d;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700487
Mike Dunnb69578d2010-04-15 17:01:33 -0400488 if (parport_prologue(pp) < 0)
489 return 0;
Mike Dunn63b91762010-04-15 17:02:09 -0400490 read_mos_reg(mos_parport->serial, dummy, DPR, &d);
Mike Dunnb69578d2010-04-15 17:01:33 -0400491 parport_epilogue(pp);
492 return d;
493}
494
495static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
496{
497 struct mos7715_parport *mos_parport = pp->private_data;
498 __u8 data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700499
Mike Dunnb69578d2010-04-15 17:01:33 -0400500 if (parport_prologue(pp) < 0)
501 return;
502 data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
Mike Dunn63b91762010-04-15 17:02:09 -0400503 write_mos_reg(mos_parport->serial, dummy, DCR, data);
Mike Dunnb69578d2010-04-15 17:01:33 -0400504 mos_parport->shadowDCR = data;
505 parport_epilogue(pp);
506}
507
508static unsigned char parport_mos7715_read_control(struct parport *pp)
509{
510 struct mos7715_parport *mos_parport = pp->private_data;
511 __u8 dcr;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700512
Mike Dunnb69578d2010-04-15 17:01:33 -0400513 spin_lock(&release_lock);
514 mos_parport = pp->private_data;
515 if (unlikely(mos_parport == NULL)) {
516 spin_unlock(&release_lock);
517 return 0;
518 }
519 dcr = mos_parport->shadowDCR & 0x0f;
520 spin_unlock(&release_lock);
521 return dcr;
522}
523
524static unsigned char parport_mos7715_frob_control(struct parport *pp,
525 unsigned char mask,
526 unsigned char val)
527{
528 struct mos7715_parport *mos_parport = pp->private_data;
529 __u8 dcr;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700530
Mike Dunnb69578d2010-04-15 17:01:33 -0400531 mask &= 0x0f;
532 val &= 0x0f;
533 if (parport_prologue(pp) < 0)
534 return 0;
535 mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
Mike Dunn63b91762010-04-15 17:02:09 -0400536 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400537 dcr = mos_parport->shadowDCR & 0x0f;
538 parport_epilogue(pp);
539 return dcr;
540}
541
542static unsigned char parport_mos7715_read_status(struct parport *pp)
543{
544 unsigned char status;
545 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700546
Mike Dunnb69578d2010-04-15 17:01:33 -0400547 spin_lock(&release_lock);
548 mos_parport = pp->private_data;
549 if (unlikely(mos_parport == NULL)) { /* release called */
550 spin_unlock(&release_lock);
551 return 0;
552 }
553 status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
554 spin_unlock(&release_lock);
555 return status;
556}
557
558static void parport_mos7715_enable_irq(struct parport *pp)
559{
Mike Dunnb69578d2010-04-15 17:01:33 -0400560}
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700561
Mike Dunnb69578d2010-04-15 17:01:33 -0400562static void parport_mos7715_disable_irq(struct parport *pp)
563{
Mike Dunnb69578d2010-04-15 17:01:33 -0400564}
565
566static void parport_mos7715_data_forward(struct parport *pp)
567{
568 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700569
Mike Dunnb69578d2010-04-15 17:01:33 -0400570 if (parport_prologue(pp) < 0)
571 return;
572 mos7715_change_mode(mos_parport, PS2);
573 mos_parport->shadowDCR &= ~0x20;
Mike Dunn63b91762010-04-15 17:02:09 -0400574 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400575 parport_epilogue(pp);
576}
577
578static void parport_mos7715_data_reverse(struct parport *pp)
579{
580 struct mos7715_parport *mos_parport = pp->private_data;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700581
Mike Dunnb69578d2010-04-15 17:01:33 -0400582 if (parport_prologue(pp) < 0)
583 return;
584 mos7715_change_mode(mos_parport, PS2);
585 mos_parport->shadowDCR |= 0x20;
Mike Dunn63b91762010-04-15 17:02:09 -0400586 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400587 parport_epilogue(pp);
588}
589
590static void parport_mos7715_init_state(struct pardevice *dev,
591 struct parport_state *s)
592{
Mike Dunnb69578d2010-04-15 17:01:33 -0400593 s->u.pc.ctr = DCR_INIT_VAL;
594 s->u.pc.ecr = ECR_INIT_VAL;
595}
596
597/* N.B. Parport core code requires that this function not block */
598static void parport_mos7715_save_state(struct parport *pp,
599 struct parport_state *s)
600{
601 struct mos7715_parport *mos_parport;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700602
Mike Dunnb69578d2010-04-15 17:01:33 -0400603 spin_lock(&release_lock);
604 mos_parport = pp->private_data;
605 if (unlikely(mos_parport == NULL)) { /* release called */
606 spin_unlock(&release_lock);
607 return;
608 }
609 s->u.pc.ctr = mos_parport->shadowDCR;
610 s->u.pc.ecr = mos_parport->shadowECR;
611 spin_unlock(&release_lock);
612}
613
614/* N.B. Parport core code requires that this function not block */
615static void parport_mos7715_restore_state(struct parport *pp,
616 struct parport_state *s)
617{
618 struct mos7715_parport *mos_parport;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700619
Mike Dunnb69578d2010-04-15 17:01:33 -0400620 spin_lock(&release_lock);
621 mos_parport = pp->private_data;
622 if (unlikely(mos_parport == NULL)) { /* release called */
623 spin_unlock(&release_lock);
624 return;
625 }
626 write_parport_reg_nonblock(mos_parport, DCR, mos_parport->shadowDCR);
627 write_parport_reg_nonblock(mos_parport, ECR, mos_parport->shadowECR);
628 spin_unlock(&release_lock);
629}
630
631static size_t parport_mos7715_write_compat(struct parport *pp,
632 const void *buffer,
633 size_t len, int flags)
634{
635 int retval;
636 struct mos7715_parport *mos_parport = pp->private_data;
637 int actual_len;
Greg Kroah-Hartmanca099072012-05-03 16:44:31 -0700638
Mike Dunnb69578d2010-04-15 17:01:33 -0400639 if (parport_prologue(pp) < 0)
640 return 0;
641 mos7715_change_mode(mos_parport, PPF);
642 retval = usb_bulk_msg(mos_parport->serial->dev,
643 usb_sndbulkpipe(mos_parport->serial->dev, 2),
644 (void *)buffer, len, &actual_len,
645 MOS_WDR_TIMEOUT);
646 parport_epilogue(pp);
647 if (retval) {
648 dev_err(&mos_parport->serial->dev->dev,
649 "mos7720: usb_bulk_msg() failed: %d", retval);
650 return 0;
651 }
652 return actual_len;
653}
654
655static struct parport_operations parport_mos7715_ops = {
656 .owner = THIS_MODULE,
657 .write_data = parport_mos7715_write_data,
658 .read_data = parport_mos7715_read_data,
659
660 .write_control = parport_mos7715_write_control,
661 .read_control = parport_mos7715_read_control,
662 .frob_control = parport_mos7715_frob_control,
663
664 .read_status = parport_mos7715_read_status,
665
666 .enable_irq = parport_mos7715_enable_irq,
667 .disable_irq = parport_mos7715_disable_irq,
668
669 .data_forward = parport_mos7715_data_forward,
670 .data_reverse = parport_mos7715_data_reverse,
671
672 .init_state = parport_mos7715_init_state,
673 .save_state = parport_mos7715_save_state,
674 .restore_state = parport_mos7715_restore_state,
675
676 .compat_write_data = parport_mos7715_write_compat,
677
678 .nibble_read_data = parport_ieee1284_read_nibble,
679 .byte_read_data = parport_ieee1284_read_byte,
680};
681
682/*
683 * Allocate and initialize parallel port control struct, initialize
684 * the parallel port hardware device, and register with the parport subsystem.
685 */
686static int mos7715_parport_init(struct usb_serial *serial)
687{
688 struct mos7715_parport *mos_parport;
689
690 /* allocate and initialize parallel port control struct */
691 mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
692 if (mos_parport == NULL) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700693 dev_dbg(&serial->dev->dev, "%s: kzalloc failed\n", __func__);
Mike Dunnb69578d2010-04-15 17:01:33 -0400694 return -ENOMEM;
695 }
696 mos_parport->msg_pending = false;
697 kref_init(&mos_parport->ref_count);
698 spin_lock_init(&mos_parport->listlock);
699 INIT_LIST_HEAD(&mos_parport->active_urbs);
700 INIT_LIST_HEAD(&mos_parport->deferred_urbs);
701 usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
702 mos_parport->serial = serial;
703 tasklet_init(&mos_parport->urb_tasklet, send_deferred_urbs,
704 (unsigned long) mos_parport);
705 init_completion(&mos_parport->syncmsg_compl);
706
707 /* cycle parallel port reset bit */
Mike Dunn63b91762010-04-15 17:02:09 -0400708 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x80);
709 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x00);
Mike Dunnb69578d2010-04-15 17:01:33 -0400710
711 /* initialize device registers */
712 mos_parport->shadowDCR = DCR_INIT_VAL;
Mike Dunn63b91762010-04-15 17:02:09 -0400713 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400714 mos_parport->shadowECR = ECR_INIT_VAL;
Mike Dunn63b91762010-04-15 17:02:09 -0400715 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400716
717 /* register with parport core */
718 mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE,
719 PARPORT_DMA_NONE,
720 &parport_mos7715_ops);
721 if (mos_parport->pp == NULL) {
722 dev_err(&serial->interface->dev,
723 "Could not register parport\n");
724 kref_put(&mos_parport->ref_count, destroy_mos_parport);
725 return -EIO;
726 }
727 mos_parport->pp->private_data = mos_parport;
728 mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP;
729 mos_parport->pp->dev = &serial->interface->dev;
730 parport_announce_port(mos_parport->pp);
731
732 return 0;
733}
734#endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700735
736/*
737 * mos7720_interrupt_callback
738 * this is the callback function for when we have received data on the
739 * interrupt endpoint.
740 */
741static void mos7720_interrupt_callback(struct urb *urb)
742{
743 int result;
744 int length;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700745 int status = urb->status;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700746 struct device *dev = &urb->dev->dev;
Oliver Neukum325b70c2007-03-19 13:58:29 +0100747 __u8 *data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700748 __u8 sp1;
749 __u8 sp2;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700750
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700751 switch (status) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700752 case 0:
753 /* success */
754 break;
755 case -ECONNRESET:
756 case -ENOENT:
757 case -ESHUTDOWN:
758 /* this urb is terminated, clean up */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700759 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700760 return;
761 default:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700762 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700763 goto exit;
764 }
765
766 length = urb->actual_length;
767 data = urb->transfer_buffer;
768
769 /* Moschip get 4 bytes
770 * Byte 1 IIR Port 1 (port.number is 0)
771 * Byte 2 IIR Port 2 (port.number is 1)
772 * Byte 3 --------------
773 * Byte 4 FIFO status for both */
Oliver Neukum325b70c2007-03-19 13:58:29 +0100774
775 /* the above description is inverted
776 * oneukum 2007-03-14 */
777
778 if (unlikely(length != 4)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700779 dev_dbg(dev, "Wrong data !!!\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700780 return;
781 }
782
Oliver Neukum325b70c2007-03-19 13:58:29 +0100783 sp1 = data[3];
784 sp2 = data[2];
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700785
Oliver Neukum325b70c2007-03-19 13:58:29 +0100786 if ((sp1 | sp2) & 0x01) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700787 /* No Interrupt Pending in both the ports */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700788 dev_dbg(dev, "No Interrupt !!!\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700789 } else {
790 switch (sp1 & 0x0f) {
791 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700792 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 -0700793 break;
794 case SERIAL_IIR_CTI:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700795 dev_dbg(dev, "Serial Port 1: Receiver time out\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700796 break;
797 case SERIAL_IIR_MS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700798 /* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700799 break;
800 }
801
802 switch (sp2 & 0x0f) {
803 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700804 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 -0700805 break;
806 case SERIAL_IIR_CTI:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700807 dev_dbg(dev, "Serial Port 2: Receiver time out\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700808 break;
809 case SERIAL_IIR_MS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700810 /* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700811 break;
812 }
813 }
814
815exit:
816 result = usb_submit_urb(urb, GFP_ATOMIC);
817 if (result)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700818 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700819}
820
821/*
Mike Dunnfb088e32010-01-26 12:12:12 -0500822 * mos7715_interrupt_callback
823 * this is the 7715's callback function for when we have received data on
824 * the interrupt endpoint.
825 */
826static void mos7715_interrupt_callback(struct urb *urb)
827{
828 int result;
829 int length;
830 int status = urb->status;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700831 struct device *dev = &urb->dev->dev;
Mike Dunnfb088e32010-01-26 12:12:12 -0500832 __u8 *data;
833 __u8 iir;
834
835 switch (status) {
836 case 0:
837 /* success */
838 break;
839 case -ECONNRESET:
840 case -ENOENT:
841 case -ESHUTDOWN:
Mike Dunnb69578d2010-04-15 17:01:33 -0400842 case -ENODEV:
Mike Dunnfb088e32010-01-26 12:12:12 -0500843 /* this urb is terminated, clean up */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700844 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
Mike Dunnfb088e32010-01-26 12:12:12 -0500845 return;
846 default:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700847 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
Mike Dunnfb088e32010-01-26 12:12:12 -0500848 goto exit;
849 }
850
851 length = urb->actual_length;
852 data = urb->transfer_buffer;
853
854 /* Structure of data from 7715 device:
855 * Byte 1: IIR serial Port
856 * Byte 2: unused
857 * Byte 2: DSR parallel port
858 * Byte 4: FIFO status for both */
859
860 if (unlikely(length != 4)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700861 dev_dbg(dev, "Wrong data !!!\n");
Mike Dunnfb088e32010-01-26 12:12:12 -0500862 return;
863 }
864
865 iir = data[0];
866 if (!(iir & 0x01)) { /* serial port interrupt pending */
867 switch (iir & 0x0f) {
868 case SERIAL_IIR_RLS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700869 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 -0500870 break;
871 case SERIAL_IIR_CTI:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700872 dev_dbg(dev, "Serial Port: Receiver time out\n");
Mike Dunnfb088e32010-01-26 12:12:12 -0500873 break;
874 case SERIAL_IIR_MS:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700875 /* dev_dbg(dev, "Serial Port: Modem status change\n"); */
Mike Dunnfb088e32010-01-26 12:12:12 -0500876 break;
877 }
878 }
879
Mike Dunnb69578d2010-04-15 17:01:33 -0400880#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
881 { /* update local copy of DSR reg */
882 struct usb_serial_port *port = urb->context;
883 struct mos7715_parport *mos_parport = port->serial->private;
884 if (unlikely(mos_parport == NULL))
885 return;
886 atomic_set(&mos_parport->shadowDSR, data[2]);
887 }
888#endif
889
Mike Dunnfb088e32010-01-26 12:12:12 -0500890exit:
891 result = usb_submit_urb(urb, GFP_ATOMIC);
892 if (result)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700893 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
Mike Dunnfb088e32010-01-26 12:12:12 -0500894}
895
896/*
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700897 * mos7720_bulk_in_callback
898 * this is the callback function for when we have received data on the
899 * bulk in endpoint.
900 */
901static void mos7720_bulk_in_callback(struct urb *urb)
902{
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700903 int retval;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700904 unsigned char *data ;
905 struct usb_serial_port *port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700906 struct tty_struct *tty;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700907 int status = urb->status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700908
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700909 if (status) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700910 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700911 return;
912 }
913
Mike Dunnb69578d2010-04-15 17:01:33 -0400914 port = urb->context;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700915
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700916 dev_dbg(&port->dev, "Entering...%s\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700917
918 data = urb->transfer_buffer;
919
Alan Cox4a90f092008-10-13 10:39:46 +0100920 tty = tty_port_tty_get(&port->port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700921 if (tty && urb->actual_length) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700922 tty_insert_flip_string(tty, data, urb->actual_length);
923 tty_flip_buffer_push(tty);
924 }
Alan Cox4a90f092008-10-13 10:39:46 +0100925 tty_kref_put(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700926
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700927 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700928 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
929 if (retval)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700930 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700931 }
932}
933
934/*
935 * mos7720_bulk_out_data_callback
936 * this is the callback function for when we have finished sending serial
937 * data on the bulk out endpoint.
938 */
939static void mos7720_bulk_out_data_callback(struct urb *urb)
940{
941 struct moschip_port *mos7720_port;
942 struct tty_struct *tty;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700943 int status = urb->status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700944
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700945 if (status) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700946 dev_dbg(&urb->dev->dev, "nonzero write bulk status received:%d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700947 return;
948 }
949
950 mos7720_port = urb->context;
951 if (!mos7720_port) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -0700952 dev_dbg(&urb->dev->dev, "NULL mos7720_port pointer\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700953 return ;
954 }
955
Alan Cox4a90f092008-10-13 10:39:46 +0100956 tty = tty_port_tty_get(&mos7720_port->port->port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700957
Jiri Slabyb963a842007-02-10 01:44:55 -0800958 if (tty && mos7720_port->open)
959 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100960 tty_kref_put(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700961}
962
963/*
Mike Dunnfb088e32010-01-26 12:12:12 -0500964 * mos77xx_probe
965 * this function installs the appropriate read interrupt endpoint callback
966 * depending on whether the device is a 7720 or 7715, thus avoiding costly
967 * run-time checks in the high-frequency callback routine itself.
968 */
969static int mos77xx_probe(struct usb_serial *serial,
970 const struct usb_device_id *id)
971{
972 if (id->idProduct == MOSCHIP_DEVICE_ID_7715)
973 moschip7720_2port_driver.read_int_callback =
974 mos7715_interrupt_callback;
975 else
976 moschip7720_2port_driver.read_int_callback =
977 mos7720_interrupt_callback;
978
979 return 0;
980}
981
982static int mos77xx_calc_num_ports(struct usb_serial *serial)
983{
984 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
985 if (product == MOSCHIP_DEVICE_ID_7715)
986 return 1;
987
988 return 2;
989}
990
Alan Coxa509a7e2009-09-19 13:13:26 -0700991static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700992{
993 struct usb_serial *serial;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700994 struct urb *urb;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700995 struct moschip_port *mos7720_port;
996 int response;
997 int port_number;
Mike Dunn63b91762010-04-15 17:02:09 -0400998 __u8 data;
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100999 int allocated_urbs = 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001000 int j;
1001
1002 serial = port->serial;
1003
1004 mos7720_port = usb_get_serial_port_data(port);
1005 if (mos7720_port == NULL)
1006 return -ENODEV;
1007
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001008 usb_clear_halt(serial->dev, port->write_urb->pipe);
1009 usb_clear_halt(serial->dev, port->read_urb->pipe);
1010
1011 /* Initialising the write urb pool */
1012 for (j = 0; j < NUM_URBS; ++j) {
Alan Cox4da1a172008-07-22 11:16:21 +01001013 urb = usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001014 mos7720_port->write_urb_pool[j] = urb;
1015
1016 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001017 dev_err(&port->dev, "No more urbs???\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001018 continue;
1019 }
1020
1021 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1022 GFP_KERNEL);
1023 if (!urb->transfer_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001024 dev_err(&port->dev,
1025 "%s-out of memory for urb buffers.\n",
1026 __func__);
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001027 usb_free_urb(mos7720_port->write_urb_pool[j]);
1028 mos7720_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001029 continue;
1030 }
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001031 allocated_urbs++;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001032 }
1033
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001034 if (!allocated_urbs)
1035 return -ENOMEM;
1036
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001037 /* Initialize MCS7720 -- Write Init values to corresponding Registers
1038 *
1039 * Register Index
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001040 * 0 : THR/RHR
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001041 * 1 : IER
1042 * 2 : FCR
1043 * 3 : LCR
1044 * 4 : MCR
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001045 * 5 : LSR
1046 * 6 : MSR
1047 * 7 : SPR
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001048 *
1049 * 0x08 : SP1/2 Control Reg
1050 */
1051 port_number = port->number - port->serial->minor;
Mike Dunn63b91762010-04-15 17:02:09 -04001052 read_mos_reg(serial, port_number, LSR, &data);
1053
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001054 dev_dbg(&port->dev, "SS::%p LSR:%x\n", mos7720_port, data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001055
Mike Dunn63b91762010-04-15 17:02:09 -04001056 write_mos_reg(serial, dummy, SP1_REG, 0x02);
1057 write_mos_reg(serial, dummy, SP2_REG, 0x02);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001058
Mike Dunn63b91762010-04-15 17:02:09 -04001059 write_mos_reg(serial, port_number, IER, 0x00);
1060 write_mos_reg(serial, port_number, FCR, 0x00);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001061
Mike Dunn63b91762010-04-15 17:02:09 -04001062 write_mos_reg(serial, port_number, FCR, 0xcf);
1063 mos7720_port->shadowLCR = 0x03;
1064 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1065 mos7720_port->shadowMCR = 0x0b;
1066 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001067
Mike Dunn63b91762010-04-15 17:02:09 -04001068 write_mos_reg(serial, port_number, SP_CONTROL_REG, 0x00);
1069 read_mos_reg(serial, dummy, SP_CONTROL_REG, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001070 data = data | (port->number - port->serial->minor + 1);
Mike Dunn63b91762010-04-15 17:02:09 -04001071 write_mos_reg(serial, dummy, SP_CONTROL_REG, data);
1072 mos7720_port->shadowLCR = 0x83;
1073 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1074 write_mos_reg(serial, port_number, THR, 0x0c);
1075 write_mos_reg(serial, port_number, IER, 0x00);
1076 mos7720_port->shadowLCR = 0x03;
1077 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1078 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001079
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001080 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
1081 if (response)
Alan Cox4da1a172008-07-22 11:16:21 +01001082 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
1083 __func__, response);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001084
1085 /* initialize our icount structure */
1086 memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
1087
1088 /* initialize our port settings */
1089 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
1090
1091 /* send a open port command */
1092 mos7720_port->open = 1;
1093
1094 return 0;
1095}
1096
1097/*
1098 * mos7720_chars_in_buffer
1099 * this function is called by the tty driver when it wants to know how many
1100 * bytes of data we currently have outstanding in the port (data that has
1101 * been written, but hasn't made it out the port yet)
1102 * If successful, we return the number of bytes left to be written in the
1103 * system,
1104 * Otherwise we return a negative error number.
1105 */
Alan Cox95da3102008-07-22 11:09:07 +01001106static int mos7720_chars_in_buffer(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001107{
Alan Cox95da3102008-07-22 11:09:07 +01001108 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001109 int i;
1110 int chars = 0;
1111 struct moschip_port *mos7720_port;
1112
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001113 mos7720_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001114 if (mos7720_port == NULL)
Alan Cox23198fd2009-07-20 16:05:27 +01001115 return 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001116
1117 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001118 if (mos7720_port->write_urb_pool[i] &&
1119 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001120 chars += URB_TRANSFER_BUFFER_SIZE;
1121 }
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001122 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001123 return chars;
1124}
1125
Alan Cox335f8512009-06-11 12:26:29 +01001126static void mos7720_close(struct usb_serial_port *port)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001127{
1128 struct usb_serial *serial;
1129 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001130 int j;
1131
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001132 serial = port->serial;
1133
1134 mos7720_port = usb_get_serial_port_data(port);
1135 if (mos7720_port == NULL)
1136 return;
1137
1138 for (j = 0; j < NUM_URBS; ++j)
1139 usb_kill_urb(mos7720_port->write_urb_pool[j]);
1140
1141 /* Freeing Write URBs */
1142 for (j = 0; j < NUM_URBS; ++j) {
1143 if (mos7720_port->write_urb_pool[j]) {
1144 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
1145 usb_free_urb(mos7720_port->write_urb_pool[j]);
1146 }
1147 }
1148
1149 /* While closing port, shutdown all bulk read, write *
Oliver Neukuma1cd7e92008-01-16 17:18:52 +01001150 * and interrupt read if they exists, otherwise nop */
Oliver Neukuma1cd7e92008-01-16 17:18:52 +01001151 usb_kill_urb(port->write_urb);
Oliver Neukuma1cd7e92008-01-16 17:18:52 +01001152 usb_kill_urb(port->read_urb);
1153
1154 mutex_lock(&serial->disc_mutex);
1155 /* these commands must not be issued if the device has
1156 * been disconnected */
1157 if (!serial->disconnected) {
Mike Dunn63b91762010-04-15 17:02:09 -04001158 write_mos_reg(serial, port->number - port->serial->minor,
1159 MCR, 0x00);
1160 write_mos_reg(serial, port->number - port->serial->minor,
1161 IER, 0x00);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001162 }
Oliver Neukuma1cd7e92008-01-16 17:18:52 +01001163 mutex_unlock(&serial->disc_mutex);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001164 mos7720_port->open = 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001165}
1166
Alan Cox95da3102008-07-22 11:09:07 +01001167static void mos7720_break(struct tty_struct *tty, int break_state)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001168{
Alan Cox95da3102008-07-22 11:09:07 +01001169 struct usb_serial_port *port = tty->driver_data;
Alan Cox4da1a172008-07-22 11:16:21 +01001170 unsigned char data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001171 struct usb_serial *serial;
1172 struct moschip_port *mos7720_port;
1173
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001174 serial = port->serial;
1175
1176 mos7720_port = usb_get_serial_port_data(port);
1177 if (mos7720_port == NULL)
1178 return;
1179
1180 if (break_state == -1)
1181 data = mos7720_port->shadowLCR | UART_LCR_SBC;
1182 else
1183 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
1184
1185 mos7720_port->shadowLCR = data;
Mike Dunn63b91762010-04-15 17:02:09 -04001186 write_mos_reg(serial, port->number - port->serial->minor,
1187 LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001188}
1189
1190/*
1191 * mos7720_write_room
1192 * this function is called by the tty driver when it wants to know how many
1193 * bytes of data we can accept for a specific port.
1194 * If successful, we return the amount of room that we have for this port
1195 * Otherwise we return a negative error number.
1196 */
Alan Cox95da3102008-07-22 11:09:07 +01001197static int mos7720_write_room(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001198{
Alan Cox95da3102008-07-22 11:09:07 +01001199 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001200 struct moschip_port *mos7720_port;
1201 int room = 0;
1202 int i;
1203
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001204 mos7720_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001205 if (mos7720_port == NULL)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001206 return -ENODEV;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001207
Alan Coxa5b6f602008-04-08 17:16:06 +01001208 /* FIXME: Locking */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001209 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001210 if (mos7720_port->write_urb_pool[i] &&
1211 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001212 room += URB_TRANSFER_BUFFER_SIZE;
1213 }
1214
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001215 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001216 return room;
1217}
1218
Alan Cox95da3102008-07-22 11:09:07 +01001219static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
1220 const unsigned char *data, int count)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001221{
1222 int status;
1223 int i;
1224 int bytes_sent = 0;
1225 int transfer_size;
1226
1227 struct moschip_port *mos7720_port;
1228 struct usb_serial *serial;
1229 struct urb *urb;
1230 const unsigned char *current_position = data;
1231
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001232 serial = port->serial;
1233
1234 mos7720_port = usb_get_serial_port_data(port);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001235 if (mos7720_port == NULL)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001236 return -ENODEV;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001237
1238 /* try to find a free urb in the list */
1239 urb = NULL;
1240
1241 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001242 if (mos7720_port->write_urb_pool[i] &&
1243 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001244 urb = mos7720_port->write_urb_pool[i];
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001245 dev_dbg(&port->dev, "URB:%d\n", i);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001246 break;
1247 }
1248 }
1249
1250 if (urb == NULL) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001251 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001252 goto exit;
1253 }
1254
1255 if (urb->transfer_buffer == NULL) {
1256 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1257 GFP_KERNEL);
1258 if (urb->transfer_buffer == NULL) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001259 dev_err_console(port, "%s no more kernel memory...\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001260 __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001261 goto exit;
1262 }
1263 }
Alan Cox4da1a172008-07-22 11:16:21 +01001264 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001265
1266 memcpy(urb->transfer_buffer, current_position, transfer_size);
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +01001267 usb_serial_debug_data(&port->dev, __func__, transfer_size,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001268 urb->transfer_buffer);
1269
1270 /* fill urb with data and submit */
1271 usb_fill_bulk_urb(urb, serial->dev,
1272 usb_sndbulkpipe(serial->dev,
Alan Cox4da1a172008-07-22 11:16:21 +01001273 port->bulk_out_endpointAddress),
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001274 urb->transfer_buffer, transfer_size,
1275 mos7720_bulk_out_data_callback, mos7720_port);
1276
1277 /* send it down the pipe */
Alan Cox4da1a172008-07-22 11:16:21 +01001278 status = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001279 if (status) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001280 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001281 "with status = %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001282 bytes_sent = status;
1283 goto exit;
1284 }
1285 bytes_sent = transfer_size;
1286
1287exit:
1288 return bytes_sent;
1289}
1290
Alan Cox95da3102008-07-22 11:09:07 +01001291static void mos7720_throttle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001292{
Alan Cox95da3102008-07-22 11:09:07 +01001293 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001294 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001295 int status;
1296
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001297 mos7720_port = usb_get_serial_port_data(port);
1298
1299 if (mos7720_port == NULL)
1300 return;
1301
1302 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001303 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001304 return;
1305 }
1306
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001307 /* if we are implementing XON/XOFF, send the stop character */
1308 if (I_IXOFF(tty)) {
1309 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001310 status = mos7720_write(tty, port, &stop_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001311 if (status <= 0)
1312 return;
1313 }
1314
1315 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001316 if (tty->termios.c_cflag & CRTSCTS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001317 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
Mike Dunn63b91762010-04-15 17:02:09 -04001318 write_mos_reg(port->serial, port->number - port->serial->minor,
1319 MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001320 if (status != 0)
1321 return;
1322 }
1323}
1324
Alan Cox95da3102008-07-22 11:09:07 +01001325static void mos7720_unthrottle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001326{
Alan Cox95da3102008-07-22 11:09:07 +01001327 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001328 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +01001329 int status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001330
1331 if (mos7720_port == NULL)
1332 return;
1333
1334 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001335 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001336 return;
1337 }
1338
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001339 /* if we are implementing XON/XOFF, send the start character */
1340 if (I_IXOFF(tty)) {
1341 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001342 status = mos7720_write(tty, port, &start_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001343 if (status <= 0)
1344 return;
1345 }
1346
1347 /* if we are implementing RTS/CTS, toggle that line */
Alan Coxadc8d742012-07-14 15:31:47 +01001348 if (tty->termios.c_cflag & CRTSCTS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001349 mos7720_port->shadowMCR |= UART_MCR_RTS;
Mike Dunn63b91762010-04-15 17:02:09 -04001350 write_mos_reg(port->serial, port->number - port->serial->minor,
1351 MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001352 if (status != 0)
1353 return;
1354 }
1355}
1356
Mike Dunnb69578d2010-04-15 17:01:33 -04001357/* FIXME: this function does not work */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001358static int set_higher_rates(struct moschip_port *mos7720_port,
1359 unsigned int baud)
1360{
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001361 struct usb_serial_port *port;
1362 struct usb_serial *serial;
1363 int port_number;
Mike Dunn63b91762010-04-15 17:02:09 -04001364 enum mos_regs sp_reg;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001365 if (mos7720_port == NULL)
1366 return -EINVAL;
1367
1368 port = mos7720_port->port;
1369 serial = port->serial;
1370
Alan Cox4da1a172008-07-22 11:16:21 +01001371 /***********************************************
1372 * Init Sequence for higher rates
1373 ***********************************************/
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001374 dev_dbg(&port->dev, "Sending Setting Commands ..........\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001375 port_number = port->number - port->serial->minor;
1376
Mike Dunn63b91762010-04-15 17:02:09 -04001377 write_mos_reg(serial, port_number, IER, 0x00);
1378 write_mos_reg(serial, port_number, FCR, 0x00);
1379 write_mos_reg(serial, port_number, FCR, 0xcf);
1380 mos7720_port->shadowMCR = 0x0b;
1381 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1382 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x00);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001383
Alan Cox4da1a172008-07-22 11:16:21 +01001384 /***********************************************
1385 * Set for higher rates *
1386 ***********************************************/
Mike Dunnb69578d2010-04-15 17:01:33 -04001387 /* writing baud rate verbatum into uart clock field clearly not right */
Mike Dunn63b91762010-04-15 17:02:09 -04001388 if (port_number == 0)
1389 sp_reg = SP1_REG;
1390 else
1391 sp_reg = SP2_REG;
1392 write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
1393 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x03);
1394 mos7720_port->shadowMCR = 0x2b;
1395 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001396
Alan Cox4da1a172008-07-22 11:16:21 +01001397 /***********************************************
1398 * Set DLL/DLM
1399 ***********************************************/
Mike Dunn63b91762010-04-15 17:02:09 -04001400 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1401 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1402 write_mos_reg(serial, port_number, DLL, 0x01);
1403 write_mos_reg(serial, port_number, DLM, 0x00);
1404 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1405 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001406
1407 return 0;
1408}
1409
1410/* baud rate information */
Alan Cox4da1a172008-07-22 11:16:21 +01001411struct divisor_table_entry {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001412 __u32 baudrate;
1413 __u16 divisor;
1414};
1415
1416/* Define table of divisors for moschip 7720 hardware *
1417 * These assume a 3.6864MHz crystal, the standard /16, and *
1418 * MCR.7 = 0. */
1419static struct divisor_table_entry divisor_table[] = {
1420 { 50, 2304},
1421 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
1422 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
1423 { 150, 768},
1424 { 300, 384},
1425 { 600, 192},
1426 { 1200, 96},
1427 { 1800, 64},
1428 { 2400, 48},
1429 { 4800, 24},
1430 { 7200, 16},
1431 { 9600, 12},
1432 { 19200, 6},
1433 { 38400, 3},
1434 { 57600, 2},
1435 { 115200, 1},
1436};
1437
1438/*****************************************************************************
1439 * calc_baud_rate_divisor
1440 * this function calculates the proper baud rate divisor for the specified
1441 * baud rate.
1442 *****************************************************************************/
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001443static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, int *divisor)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001444{
1445 int i;
1446 __u16 custom;
1447 __u16 round1;
1448 __u16 round;
1449
1450
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001451 dev_dbg(&port->dev, "%s - %d\n", __func__, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001452
1453 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
1454 if (divisor_table[i].baudrate == baudrate) {
1455 *divisor = divisor_table[i].divisor;
1456 return 0;
1457 }
1458 }
1459
Alan Cox4da1a172008-07-22 11:16:21 +01001460 /* After trying for all the standard baud rates *
1461 * Try calculating the divisor for this baud rate */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001462 if (baudrate > 75 && baudrate < 230400) {
1463 /* get the divisor */
1464 custom = (__u16)(230400L / baudrate);
1465
1466 /* Check for round off */
1467 round1 = (__u16)(2304000L / baudrate);
1468 round = (__u16)(round1 - (custom * 10));
1469 if (round > 4)
1470 custom++;
1471 *divisor = custom;
1472
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001473 dev_dbg(&port->dev, "Baud %d = %d\n", baudrate, custom);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001474 return 0;
1475 }
1476
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001477 dev_dbg(&port->dev, "Baud calculation Failed...\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001478 return -EINVAL;
1479}
1480
1481/*
1482 * send_cmd_write_baud_rate
1483 * this function sends the proper command to change the baud rate of the
1484 * specified port.
1485 */
1486static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1487 int baudrate)
1488{
1489 struct usb_serial_port *port;
1490 struct usb_serial *serial;
1491 int divisor;
1492 int status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001493 unsigned char number;
1494
1495 if (mos7720_port == NULL)
1496 return -1;
1497
1498 port = mos7720_port->port;
1499 serial = port->serial;
1500
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001501 number = port->number - port->serial->minor;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001502 dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001503
Alan Cox4da1a172008-07-22 11:16:21 +01001504 /* Calculate the Divisor */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001505 status = calc_baud_rate_divisor(port, baudrate, &divisor);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001506 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001507 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001508 return status;
1509 }
1510
Alan Cox4da1a172008-07-22 11:16:21 +01001511 /* Enable access to divisor latch */
Mike Dunn63b91762010-04-15 17:02:09 -04001512 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1513 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001514
1515 /* Write the divisor */
Mike Dunn63b91762010-04-15 17:02:09 -04001516 write_mos_reg(serial, number, DLL, (__u8)(divisor & 0xff));
1517 write_mos_reg(serial, number, DLM, (__u8)((divisor & 0xff00) >> 8));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001518
Alan Cox4da1a172008-07-22 11:16:21 +01001519 /* Disable access to divisor latch */
Mike Dunn63b91762010-04-15 17:02:09 -04001520 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1521 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001522
1523 return status;
1524}
1525
1526/*
1527 * change_port_settings
1528 * This routine is called to set the UART on the device to match
1529 * the specified new settings.
1530 */
Alan Cox95da3102008-07-22 11:09:07 +01001531static void change_port_settings(struct tty_struct *tty,
1532 struct moschip_port *mos7720_port,
Alan Cox606d0992006-12-08 02:38:45 -08001533 struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001534{
1535 struct usb_serial_port *port;
1536 struct usb_serial *serial;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001537 int baud;
1538 unsigned cflag;
1539 unsigned iflag;
1540 __u8 mask = 0xff;
1541 __u8 lData;
1542 __u8 lParity;
1543 __u8 lStop;
1544 int status;
1545 int port_number;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001546
1547 if (mos7720_port == NULL)
1548 return ;
1549
1550 port = mos7720_port->port;
1551 serial = port->serial;
1552 port_number = port->number - port->serial->minor;
1553
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001554 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001555 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001556 return;
1557 }
1558
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001559 lData = UART_LCR_WLEN8;
1560 lStop = 0x00; /* 1 stop bit */
1561 lParity = 0x00; /* No parity */
1562
Alan Coxadc8d742012-07-14 15:31:47 +01001563 cflag = tty->termios.c_cflag;
1564 iflag = tty->termios.c_iflag;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001565
1566 /* Change the number of bits */
1567 switch (cflag & CSIZE) {
1568 case CS5:
1569 lData = UART_LCR_WLEN5;
1570 mask = 0x1f;
1571 break;
1572
1573 case CS6:
1574 lData = UART_LCR_WLEN6;
1575 mask = 0x3f;
1576 break;
1577
1578 case CS7:
1579 lData = UART_LCR_WLEN7;
1580 mask = 0x7f;
1581 break;
1582 default:
1583 case CS8:
1584 lData = UART_LCR_WLEN8;
1585 break;
1586 }
1587
1588 /* Change the Parity bit */
1589 if (cflag & PARENB) {
1590 if (cflag & PARODD) {
1591 lParity = UART_LCR_PARITY;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001592 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001593 } else {
1594 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001595 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001596 }
1597
1598 } else {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001599 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001600 }
1601
1602 if (cflag & CMSPAR)
1603 lParity = lParity | 0x20;
1604
1605 /* Change the Stop bit */
1606 if (cflag & CSTOPB) {
1607 lStop = UART_LCR_STOP;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001608 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001609 } else {
1610 lStop = 0x00;
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001611 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001612 }
1613
1614#define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1615#define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1616#define LCR_PAR_MASK 0x38 /* Mask for parity field */
1617
1618 /* Update the LCR with the correct value */
Alan Cox4da1a172008-07-22 11:16:21 +01001619 mos7720_port->shadowLCR &=
Mike Dunn63b91762010-04-15 17:02:09 -04001620 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001621 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1622
1623
1624 /* Disable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001625 write_mos_reg(serial, port_number, IER, 0x00);
1626 write_mos_reg(serial, port_number, FCR, 0x00);
1627 write_mos_reg(serial, port_number, FCR, 0xcf);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001628
1629 /* Send the updated LCR value to the mos7720 */
Mike Dunn63b91762010-04-15 17:02:09 -04001630 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1631 mos7720_port->shadowMCR = 0x0b;
1632 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001633
1634 /* set up the MCR register and send it to the mos7720 */
1635 mos7720_port->shadowMCR = UART_MCR_OUT2;
1636 if (cflag & CBAUD)
1637 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1638
1639 if (cflag & CRTSCTS) {
1640 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
Alan Cox4da1a172008-07-22 11:16:21 +01001641 /* To set hardware flow control to the specified *
1642 * serial port, in SP1/2_CONTROL_REG */
Mike Dunn63b91762010-04-15 17:02:09 -04001643 if (port->number)
1644 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x01);
1645 else
1646 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x02);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001647
Mike Dunn63b91762010-04-15 17:02:09 -04001648 } else
1649 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1650
1651 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001652
1653 /* Determine divisor based on baud rate */
1654 baud = tty_get_baud_rate(tty);
1655 if (!baud) {
1656 /* pick a default, any default... */
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001657 dev_dbg(&port->dev, "Picked default baud...\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001658 baud = 9600;
1659 }
1660
1661 if (baud >= 230400) {
1662 set_higher_rates(mos7720_port, baud);
1663 /* Enable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001664 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001665 return;
1666 }
1667
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001668 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001669 status = send_cmd_write_baud_rate(mos7720_port, baud);
Alan Cox65d063ab2008-01-03 17:01:18 +00001670 /* FIXME: needs to write actual resulting baud back not just
1671 blindly do so */
1672 if (cflag & CBAUD)
1673 tty_encode_baud_rate(tty, baud, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001674 /* Enable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001675 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001676
1677 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001678 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1679 if (status)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001680 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001681 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001682}
1683
1684/*
1685 * mos7720_set_termios
1686 * this function is called by the tty driver when it wants to change the
1687 * termios structure.
1688 */
Alan Cox95da3102008-07-22 11:09:07 +01001689static void mos7720_set_termios(struct tty_struct *tty,
1690 struct usb_serial_port *port, struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001691{
1692 int status;
1693 unsigned int cflag;
1694 struct usb_serial *serial;
1695 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001696
1697 serial = port->serial;
1698
1699 mos7720_port = usb_get_serial_port_data(port);
1700
1701 if (mos7720_port == NULL)
1702 return;
1703
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001704 if (!mos7720_port->open) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001705 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001706 return;
1707 }
1708
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001709 dev_dbg(&port->dev, "setting termios - ASPIRE\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001710
Alan Coxadc8d742012-07-14 15:31:47 +01001711 cflag = tty->termios.c_cflag;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001712
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001713 dev_dbg(&port->dev, "%s - cflag %08x iflag %08x\n", __func__,
Linus Torvaldsd9a80742012-10-01 13:23:01 -07001714 tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001715
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001716 dev_dbg(&port->dev, "%s - old cflag %08x old iflag %08x\n", __func__,
1717 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001718
1719 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001720 change_port_settings(tty, mos7720_port, old_termios);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001721
Alan Cox4da1a172008-07-22 11:16:21 +01001722 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001723 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1724 if (status)
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001725 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001726 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001727}
1728
1729/*
1730 * get_lsr_info - get line status register info
1731 *
1732 * Purpose: Let user call ioctl() to get info when the UART physically
1733 * is emptied. On bus types like RS485, the transmitter must
1734 * release the bus after transmitting. This must be done when
1735 * the transmit shift register is empty, not be done when the
1736 * transmit holding register is empty. This functionality
1737 * allows an RS485 driver to be written in user space.
1738 */
Alan Cox4da1a172008-07-22 11:16:21 +01001739static int get_lsr_info(struct tty_struct *tty,
1740 struct moschip_port *mos7720_port, unsigned int __user *value)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001741{
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001742 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001743 unsigned int result = 0;
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001744 unsigned char data = 0;
1745 int port_number = port->number - port->serial->minor;
1746 int count;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001747
Alan Cox95da3102008-07-22 11:09:07 +01001748 count = mos7720_chars_in_buffer(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001749 if (count == 0) {
Mike Dunn63b91762010-04-15 17:02:09 -04001750 read_mos_reg(port->serial, port_number, LSR, &data);
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001751 if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1752 == (UART_LSR_TEMT | UART_LSR_THRE)) {
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001753 dev_dbg(&port->dev, "%s -- Empty\n", __func__);
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001754 result = TIOCSER_TEMT;
1755 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001756 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001757 if (copy_to_user(value, &result, sizeof(int)))
1758 return -EFAULT;
1759 return 0;
1760}
1761
Alan Cox60b33c12011-02-14 16:26:14 +00001762static int mos7720_tiocmget(struct tty_struct *tty)
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001763{
1764 struct usb_serial_port *port = tty->driver_data;
1765 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1766 unsigned int result = 0;
1767 unsigned int mcr ;
1768 unsigned int msr ;
1769
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001770 mcr = mos7720_port->shadowMCR;
1771 msr = mos7720_port->shadowMSR;
1772
1773 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1774 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1775 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1776 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
1777 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1778 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
1779
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001780 return result;
1781}
1782
Alan Cox20b9d172011-02-14 16:26:50 +00001783static int mos7720_tiocmset(struct tty_struct *tty,
Mike Dunn63b91762010-04-15 17:02:09 -04001784 unsigned int set, unsigned int clear)
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001785{
1786 struct usb_serial_port *port = tty->driver_data;
1787 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1788 unsigned int mcr ;
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001789
1790 mcr = mos7720_port->shadowMCR;
1791
1792 if (set & TIOCM_RTS)
1793 mcr |= UART_MCR_RTS;
1794 if (set & TIOCM_DTR)
1795 mcr |= UART_MCR_DTR;
1796 if (set & TIOCM_LOOP)
1797 mcr |= UART_MCR_LOOP;
1798
1799 if (clear & TIOCM_RTS)
1800 mcr &= ~UART_MCR_RTS;
1801 if (clear & TIOCM_DTR)
1802 mcr &= ~UART_MCR_DTR;
1803 if (clear & TIOCM_LOOP)
1804 mcr &= ~UART_MCR_LOOP;
1805
1806 mos7720_port->shadowMCR = mcr;
Mike Dunn63b91762010-04-15 17:02:09 -04001807 write_mos_reg(port->serial, port->number - port->serial->minor,
1808 MCR, mos7720_port->shadowMCR);
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001809
1810 return 0;
1811}
1812
Alan Cox0bca1b92010-09-16 18:21:40 +01001813static int mos7720_get_icount(struct tty_struct *tty,
1814 struct serial_icounter_struct *icount)
1815{
1816 struct usb_serial_port *port = tty->driver_data;
1817 struct moschip_port *mos7720_port;
1818 struct async_icount cnow;
1819
1820 mos7720_port = usb_get_serial_port_data(port);
1821 cnow = mos7720_port->icount;
1822
1823 icount->cts = cnow.cts;
1824 icount->dsr = cnow.dsr;
1825 icount->rng = cnow.rng;
1826 icount->dcd = cnow.dcd;
1827 icount->rx = cnow.rx;
1828 icount->tx = cnow.tx;
1829 icount->frame = cnow.frame;
1830 icount->overrun = cnow.overrun;
1831 icount->parity = cnow.parity;
1832 icount->brk = cnow.brk;
1833 icount->buf_overrun = cnow.buf_overrun;
1834
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001835 dev_dbg(&port->dev, "%s TIOCGICOUNT RX=%d, TX=%d\n", __func__,
1836 icount->rx, icount->tx);
Alan Cox0bca1b92010-09-16 18:21:40 +01001837 return 0;
1838}
1839
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001840static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1841 unsigned int __user *value)
1842{
Alan Cox0bca1b92010-09-16 18:21:40 +01001843 unsigned int mcr;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001844 unsigned int arg;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001845
1846 struct usb_serial_port *port;
1847
1848 if (mos7720_port == NULL)
1849 return -1;
1850
Alan Cox4da1a172008-07-22 11:16:21 +01001851 port = (struct usb_serial_port *)mos7720_port->port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001852 mcr = mos7720_port->shadowMCR;
1853
1854 if (copy_from_user(&arg, value, sizeof(int)))
1855 return -EFAULT;
1856
1857 switch (cmd) {
1858 case TIOCMBIS:
1859 if (arg & TIOCM_RTS)
1860 mcr |= UART_MCR_RTS;
1861 if (arg & TIOCM_DTR)
1862 mcr |= UART_MCR_RTS;
1863 if (arg & TIOCM_LOOP)
1864 mcr |= UART_MCR_LOOP;
1865 break;
1866
1867 case TIOCMBIC:
1868 if (arg & TIOCM_RTS)
1869 mcr &= ~UART_MCR_RTS;
1870 if (arg & TIOCM_DTR)
1871 mcr &= ~UART_MCR_RTS;
1872 if (arg & TIOCM_LOOP)
1873 mcr &= ~UART_MCR_LOOP;
1874 break;
1875
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001876 }
1877
1878 mos7720_port->shadowMCR = mcr;
Mike Dunn63b91762010-04-15 17:02:09 -04001879 write_mos_reg(port->serial, port->number - port->serial->minor,
1880 MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001881
1882 return 0;
1883}
1884
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001885static int get_serial_info(struct moschip_port *mos7720_port,
1886 struct serial_struct __user *retinfo)
1887{
1888 struct serial_struct tmp;
1889
1890 if (!retinfo)
1891 return -EFAULT;
1892
1893 memset(&tmp, 0, sizeof(tmp));
1894
1895 tmp.type = PORT_16550A;
1896 tmp.line = mos7720_port->port->serial->minor;
1897 tmp.port = mos7720_port->port->number;
1898 tmp.irq = 0;
1899 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
Alan Cox4da1a172008-07-22 11:16:21 +01001900 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001901 tmp.baud_base = 9600;
1902 tmp.close_delay = 5*HZ;
1903 tmp.closing_wait = 30*HZ;
1904
1905 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1906 return -EFAULT;
1907 return 0;
1908}
1909
Alan Cox00a0d0d2011-02-14 16:27:06 +00001910static int mos7720_ioctl(struct tty_struct *tty,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001911 unsigned int cmd, unsigned long arg)
1912{
Alan Cox95da3102008-07-22 11:09:07 +01001913 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001914 struct moschip_port *mos7720_port;
1915 struct async_icount cnow;
1916 struct async_icount cprev;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001917
1918 mos7720_port = usb_get_serial_port_data(port);
1919 if (mos7720_port == NULL)
1920 return -ENODEV;
1921
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001922 dev_dbg(&port->dev, "%s - cmd = 0x%x", __func__, cmd);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001923
1924 switch (cmd) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001925 case TIOCSERGETLSR:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001926 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
Alan Cox4da1a172008-07-22 11:16:21 +01001927 return get_lsr_info(tty, mos7720_port,
1928 (unsigned int __user *)arg);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001929
Alan Cox95da3102008-07-22 11:09:07 +01001930 /* FIXME: These should be using the mode methods */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001931 case TIOCMBIS:
1932 case TIOCMBIC:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001933 dev_dbg(&port->dev, "%s TIOCMSET/TIOCMBIC/TIOCMSET\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001934 return set_modem_info(mos7720_port, cmd,
1935 (unsigned int __user *)arg);
1936
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001937 case TIOCGSERIAL:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001938 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001939 return get_serial_info(mos7720_port,
1940 (struct serial_struct __user *)arg);
1941
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001942 case TIOCMIWAIT:
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07001943 dev_dbg(&port->dev, "%s TIOCMIWAIT\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001944 cprev = mos7720_port->icount;
1945 while (1) {
1946 if (signal_pending(current))
1947 return -ERESTARTSYS;
1948 cnow = mos7720_port->icount;
1949 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1950 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1951 return -EIO; /* no change => error */
1952 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1953 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1954 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
Alan Cox4da1a172008-07-22 11:16:21 +01001955 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001956 return 0;
1957 }
1958 cprev = cnow;
1959 }
1960 /* NOTREACHED */
1961 break;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001962 }
1963
1964 return -ENOIOCTLCMD;
1965}
1966
1967static int mos7720_startup(struct usb_serial *serial)
1968{
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001969 struct usb_device *dev;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001970 char data;
Huzaifa Sidhpurwala91f58ae2011-02-21 12:58:45 +05301971 u16 product;
Mike Dunnb69578d2010-04-15 17:01:33 -04001972 int ret_val;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001973
Huzaifa Sidhpurwala91f58ae2011-02-21 12:58:45 +05301974 product = le16_to_cpu(serial->dev->descriptor.idProduct);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001975 dev = serial->dev;
1976
Mike Dunnfb088e32010-01-26 12:12:12 -05001977 /*
1978 * The 7715 uses the first bulk in/out endpoint pair for the parallel
1979 * port, and the second for the serial port. Because the usbserial core
1980 * assumes both pairs are serial ports, we must engage in a bit of
1981 * subterfuge and swap the pointers for ports 0 and 1 in order to make
1982 * port 0 point to the serial port. However, both moschip devices use a
1983 * single interrupt-in endpoint for both ports (as mentioned a little
1984 * further down), and this endpoint was assigned to port 0. So after
1985 * the swap, we must copy the interrupt endpoint elements from port 1
1986 * (as newly assigned) to port 0, and null out port 1 pointers.
1987 */
1988 if (product == MOSCHIP_DEVICE_ID_7715) {
1989 struct usb_serial_port *tmp = serial->port[0];
1990 serial->port[0] = serial->port[1];
1991 serial->port[1] = tmp;
1992 serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb;
1993 serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer;
1994 serial->port[0]->interrupt_in_endpointAddress =
1995 tmp->interrupt_in_endpointAddress;
1996 serial->port[1]->interrupt_in_urb = NULL;
1997 serial->port[1]->interrupt_in_buffer = NULL;
1998 }
1999
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002000 /* setting configuration feature to one */
2001 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Alan Cox4da1a172008-07-22 11:16:21 +01002002 (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5*HZ);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002003
Mike Dunnb69578d2010-04-15 17:01:33 -04002004 /* start the interrupt urb */
2005 ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
2006 if (ret_val)
2007 dev_err(&dev->dev,
2008 "%s - Error %d submitting control urb\n",
2009 __func__, ret_val);
2010
2011#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
2012 if (product == MOSCHIP_DEVICE_ID_7715) {
2013 ret_val = mos7715_parport_init(serial);
2014 if (ret_val < 0)
2015 return ret_val;
2016 }
2017#endif
Alan Cox4da1a172008-07-22 11:16:21 +01002018 /* LSR For Port 1 */
Mike Dunn63b91762010-04-15 17:02:09 -04002019 read_mos_reg(serial, 0, LSR, &data);
Greg Kroah-Hartman9eecf802012-09-14 15:08:33 -07002020 dev_dbg(&dev->dev, "LSR:%x\n", data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002021
2022 return 0;
2023}
2024
Alan Sternf9c99bb2009-06-02 11:53:55 -04002025static void mos7720_release(struct usb_serial *serial)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002026{
Mike Dunnb69578d2010-04-15 17:01:33 -04002027#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
2028 /* close the parallel port */
2029
2030 if (le16_to_cpu(serial->dev->descriptor.idProduct)
2031 == MOSCHIP_DEVICE_ID_7715) {
2032 struct urbtracker *urbtrack;
2033 unsigned long flags;
2034 struct mos7715_parport *mos_parport =
2035 usb_get_serial_data(serial);
2036
2037 /* prevent NULL ptr dereference in port callbacks */
2038 spin_lock(&release_lock);
2039 mos_parport->pp->private_data = NULL;
2040 spin_unlock(&release_lock);
2041
2042 /* wait for synchronous usb calls to return */
2043 if (mos_parport->msg_pending)
2044 wait_for_completion_timeout(&mos_parport->syncmsg_compl,
2045 MOS_WDR_TIMEOUT);
2046
2047 parport_remove_port(mos_parport->pp);
2048 usb_set_serial_data(serial, NULL);
2049 mos_parport->serial = NULL;
2050
2051 /* if tasklet currently scheduled, wait for it to complete */
2052 tasklet_kill(&mos_parport->urb_tasklet);
2053
2054 /* unlink any urbs sent by the tasklet */
2055 spin_lock_irqsave(&mos_parport->listlock, flags);
2056 list_for_each_entry(urbtrack,
2057 &mos_parport->active_urbs,
2058 urblist_entry)
2059 usb_unlink_urb(urbtrack->urb);
2060 spin_unlock_irqrestore(&mos_parport->listlock, flags);
2061
2062 kref_put(&mos_parport->ref_count, destroy_mos_parport);
2063 }
2064#endif
Johan Hovold4230af52012-10-25 10:29:05 +02002065}
2066
2067static int mos7720_port_probe(struct usb_serial_port *port)
2068{
2069 struct moschip_port *mos7720_port;
2070
2071 mos7720_port = kzalloc(sizeof(*mos7720_port), GFP_KERNEL);
2072 if (!mos7720_port)
2073 return -ENOMEM;
2074
2075 /* Initialize all port interrupt end point to port 0 int endpoint.
2076 * Our device has only one interrupt endpoint common to all ports.
2077 */
2078 port->interrupt_in_endpointAddress =
2079 port->serial->port[0]->interrupt_in_endpointAddress;
2080 mos7720_port->port = port;
2081
2082 usb_set_serial_port_data(port, mos7720_port);
2083
2084 return 0;
2085}
2086
2087static int mos7720_port_remove(struct usb_serial_port *port)
2088{
2089 struct moschip_port *mos7720_port;
2090
2091 mos7720_port = usb_get_serial_port_data(port);
2092 kfree(mos7720_port);
2093
2094 return 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002095}
2096
2097static struct usb_serial_driver moschip7720_2port_driver = {
2098 .driver = {
2099 .owner = THIS_MODULE,
2100 .name = "moschip7720",
2101 },
2102 .description = "Moschip 2 port adapter",
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002103 .id_table = id_table,
Mike Dunnfb088e32010-01-26 12:12:12 -05002104 .calc_num_ports = mos77xx_calc_num_ports,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002105 .open = mos7720_open,
2106 .close = mos7720_close,
2107 .throttle = mos7720_throttle,
2108 .unthrottle = mos7720_unthrottle,
Mike Dunnfb088e32010-01-26 12:12:12 -05002109 .probe = mos77xx_probe,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002110 .attach = mos7720_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002111 .release = mos7720_release,
Johan Hovold4230af52012-10-25 10:29:05 +02002112 .port_probe = mos7720_port_probe,
2113 .port_remove = mos7720_port_remove,
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");