blob: cdde45dee67d42ffee619100cabd963aa6423bfc [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 */
Johan Hovold2591cec2013-05-27 14:44:43 +020047#define MOS_WDR_TIMEOUT 5000
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070048
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070049#define MOS_MAX_PORT 0x02
50#define MOS_WRITE 0x0E
51#define MOS_READ 0x0D
52
53/* Interrupt Rotinue Defines */
54#define SERIAL_IIR_RLS 0x06
55#define SERIAL_IIR_RDA 0x04
56#define SERIAL_IIR_CTI 0x0c
57#define SERIAL_IIR_THR 0x02
58#define SERIAL_IIR_MS 0x00
59
60#define NUM_URBS 16 /* URB Count */
61#define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
62
Mike Dunnb69578d2010-04-15 17:01:33 -040063/* This structure holds all of the local serial port information */
Alan Cox4da1a172008-07-22 11:16:21 +010064struct moschip_port {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070065 __u8 shadowLCR; /* last LCR value received */
66 __u8 shadowMCR; /* last MCR value received */
67 __u8 shadowMSR; /* last MSR value received */
68 char open;
69 struct async_icount icount;
70 struct usb_serial_port *port; /* loop back to the owner */
71 struct urb *write_urb_pool[NUM_URBS];
72};
73
Rusty Russell90ab5ee2012-01-13 09:32:20 +103074static bool debug;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070075
Mike Dunnfb088e32010-01-26 12:12:12 -050076static struct usb_serial_driver moschip7720_2port_driver;
77
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070078#define USB_VENDOR_ID_MOSCHIP 0x9710
79#define MOSCHIP_DEVICE_ID_7720 0x7720
80#define MOSCHIP_DEVICE_ID_7715 0x7715
81
Németh Márton7d40d7e2010-01-10 15:34:24 +010082static const struct usb_device_id moschip_port_id_table[] = {
Alan Cox4da1a172008-07-22 11:16:21 +010083 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
Mike Dunnfb088e32010-01-26 12:12:12 -050084 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) },
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070085 { } /* terminating entry */
86};
87MODULE_DEVICE_TABLE(usb, moschip_port_id_table);
88
Mike Dunnb69578d2010-04-15 17:01:33 -040089#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
90
91/* initial values for parport regs */
92#define DCR_INIT_VAL 0x0c /* SLCTIN, nINIT */
93#define ECR_INIT_VAL 0x00 /* SPP mode */
94
95struct urbtracker {
96 struct mos7715_parport *mos_parport;
97 struct list_head urblist_entry;
98 struct kref ref_count;
99 struct urb *urb;
Johan Hovold419628b2013-08-13 13:27:34 +0200100 struct usb_ctrlrequest *setup;
Mike Dunnb69578d2010-04-15 17:01:33 -0400101};
102
103enum mos7715_pp_modes {
104 SPP = 0<<5,
105 PS2 = 1<<5, /* moschip calls this 'NIBBLE' mode */
106 PPF = 2<<5, /* moschip calls this 'CB-FIFO mode */
107};
108
109struct mos7715_parport {
110 struct parport *pp; /* back to containing struct */
111 struct kref ref_count; /* to instance of this struct */
112 struct list_head deferred_urbs; /* list deferred async urbs */
113 struct list_head active_urbs; /* list async urbs in flight */
114 spinlock_t listlock; /* protects list access */
115 bool msg_pending; /* usb sync call pending */
116 struct completion syncmsg_compl; /* usb sync call completed */
117 struct tasklet_struct urb_tasklet; /* for sending deferred urbs */
118 struct usb_serial *serial; /* back to containing struct */
119 __u8 shadowECR; /* parallel port regs... */
120 __u8 shadowDCR;
121 atomic_t shadowDSR; /* updated in int-in callback */
122};
123
124/* lock guards against dereferencing NULL ptr in parport ops callbacks */
125static DEFINE_SPINLOCK(release_lock);
126
Mike Dunn63b91762010-04-15 17:02:09 -0400127#endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
128
129static const unsigned int dummy; /* for clarity in register access fns */
130
Mike Dunnb69578d2010-04-15 17:01:33 -0400131enum mos_regs {
132 THR, /* serial port regs */
133 RHR,
134 IER,
135 FCR,
136 ISR,
137 LCR,
138 MCR,
139 LSR,
140 MSR,
141 SPR,
142 DLL,
143 DLM,
144 DPR, /* parallel port regs */
145 DSR,
146 DCR,
147 ECR,
148 SP1_REG, /* device control regs */
149 SP2_REG, /* serial port 2 (7720 only) */
150 PP_REG,
151 SP_CONTROL_REG,
152};
153
154/*
155 * Return the correct value for the Windex field of the setup packet
156 * for a control endpoint message. See the 7715 datasheet.
157 */
158static inline __u16 get_reg_index(enum mos_regs reg)
159{
160 static const __u16 mos7715_index_lookup_table[] = {
161 0x00, /* THR */
162 0x00, /* RHR */
163 0x01, /* IER */
164 0x02, /* FCR */
165 0x02, /* ISR */
166 0x03, /* LCR */
167 0x04, /* MCR */
168 0x05, /* LSR */
169 0x06, /* MSR */
170 0x07, /* SPR */
171 0x00, /* DLL */
172 0x01, /* DLM */
173 0x00, /* DPR */
174 0x01, /* DSR */
175 0x02, /* DCR */
176 0x0a, /* ECR */
177 0x01, /* SP1_REG */
178 0x02, /* SP2_REG (7720 only) */
179 0x04, /* PP_REG (7715 only) */
180 0x08, /* SP_CONTROL_REG */
181 };
182 return mos7715_index_lookup_table[reg];
183}
184
185/*
186 * Return the correct value for the upper byte of the Wvalue field of
187 * the setup packet for a control endpoint message.
188 */
Mike Dunn63b91762010-04-15 17:02:09 -0400189static inline __u16 get_reg_value(enum mos_regs reg,
190 unsigned int serial_portnum)
Mike Dunnb69578d2010-04-15 17:01:33 -0400191{
192 if (reg >= SP1_REG) /* control reg */
193 return 0x0000;
Mike Dunn63b91762010-04-15 17:02:09 -0400194
195 else if (reg >= DPR) /* parallel port reg (7715 only) */
Mike Dunnb69578d2010-04-15 17:01:33 -0400196 return 0x0100;
Mike Dunn63b91762010-04-15 17:02:09 -0400197
198 else /* serial port reg */
199 return (serial_portnum + 2) << 8;
Mike Dunnb69578d2010-04-15 17:01:33 -0400200}
201
202/*
203 * Write data byte to the specified device register. The data is embedded in
Mike Dunn63b91762010-04-15 17:02:09 -0400204 * the value field of the setup packet. serial_portnum is ignored for registers
205 * not specific to a particular serial port.
Mike Dunnb69578d2010-04-15 17:01:33 -0400206 */
Mike Dunn63b91762010-04-15 17:02:09 -0400207static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
208 enum mos_regs reg, __u8 data)
Mike Dunnb69578d2010-04-15 17:01:33 -0400209{
Mike Dunnb69578d2010-04-15 17:01:33 -0400210 struct usb_device *usbdev = serial->dev;
211 unsigned int pipe = usb_sndctrlpipe(usbdev, 0);
212 __u8 request = (__u8)0x0e;
213 __u8 requesttype = (__u8)0x40;
Mike Dunnb69578d2010-04-15 17:01:33 -0400214 __u16 index = get_reg_index(reg);
Mike Dunn63b91762010-04-15 17:02:09 -0400215 __u16 value = get_reg_value(reg, serial_portnum) + data;
216 int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
217 index, NULL, 0, MOS_WDR_TIMEOUT);
Mike Dunnb69578d2010-04-15 17:01:33 -0400218 if (status < 0)
219 dev_err(&usbdev->dev,
220 "mos7720: usb_control_msg() failed: %d", status);
221 return status;
222}
223
224/*
225 * Read data byte from the specified device register. The data returned by the
Mike Dunn63b91762010-04-15 17:02:09 -0400226 * device is embedded in the value field of the setup packet. serial_portnum is
227 * ignored for registers that are not specific to a particular serial port.
Mike Dunnb69578d2010-04-15 17:01:33 -0400228 */
Mike Dunn63b91762010-04-15 17:02:09 -0400229static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
230 enum mos_regs reg, __u8 *data)
Mike Dunnb69578d2010-04-15 17:01:33 -0400231{
Mike Dunn63b91762010-04-15 17:02:09 -0400232 struct usb_device *usbdev = serial->dev;
Mike Dunnb69578d2010-04-15 17:01:33 -0400233 unsigned int pipe = usb_rcvctrlpipe(usbdev, 0);
234 __u8 request = (__u8)0x0d;
235 __u8 requesttype = (__u8)0xc0;
Mike Dunnb69578d2010-04-15 17:01:33 -0400236 __u16 index = get_reg_index(reg);
Mike Dunn63b91762010-04-15 17:02:09 -0400237 __u16 value = get_reg_value(reg, serial_portnum);
Johan Hovold2fbc6322013-05-27 14:44:39 +0200238 u8 *buf;
239 int status;
240
241 buf = kmalloc(1, GFP_KERNEL);
242 if (!buf)
243 return -ENOMEM;
244
245 status = usb_control_msg(usbdev, pipe, request, requesttype, value,
246 index, buf, 1, MOS_WDR_TIMEOUT);
247 if (status == 1)
248 *data = *buf;
249 else if (status < 0)
Mike Dunnb69578d2010-04-15 17:01:33 -0400250 dev_err(&usbdev->dev,
251 "mos7720: usb_control_msg() failed: %d", status);
Johan Hovold2fbc6322013-05-27 14:44:39 +0200252 kfree(buf);
253
Mike Dunnb69578d2010-04-15 17:01:33 -0400254 return status;
255}
256
Mike Dunn63b91762010-04-15 17:02:09 -0400257#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
258
Mike Dunnb69578d2010-04-15 17:01:33 -0400259static inline int mos7715_change_mode(struct mos7715_parport *mos_parport,
260 enum mos7715_pp_modes mode)
261{
262 mos_parport->shadowECR = mode;
Mike Dunn63b91762010-04-15 17:02:09 -0400263 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400264 return 0;
265}
266
267static void destroy_mos_parport(struct kref *kref)
268{
269 struct mos7715_parport *mos_parport =
270 container_of(kref, struct mos7715_parport, ref_count);
271
272 dbg("%s called", __func__);
273 kfree(mos_parport);
274}
275
276static void destroy_urbtracker(struct kref *kref)
277{
278 struct urbtracker *urbtrack =
279 container_of(kref, struct urbtracker, ref_count);
280 struct mos7715_parport *mos_parport = urbtrack->mos_parport;
281 dbg("%s called", __func__);
282 usb_free_urb(urbtrack->urb);
Johan Hovold419628b2013-08-13 13:27:34 +0200283 kfree(urbtrack->setup);
Mike Dunnb69578d2010-04-15 17:01:33 -0400284 kfree(urbtrack);
285 kref_put(&mos_parport->ref_count, destroy_mos_parport);
286}
287
288/*
289 * This runs as a tasklet when sending an urb in a non-blocking parallel
290 * port callback had to be deferred because the disconnect mutex could not be
291 * obtained at the time.
292 */
293static void send_deferred_urbs(unsigned long _mos_parport)
294{
295 int ret_val;
296 unsigned long flags;
297 struct mos7715_parport *mos_parport = (void *)_mos_parport;
298 struct urbtracker *urbtrack;
299 struct list_head *cursor, *next;
300
301 dbg("%s called", __func__);
302
303 /* if release function ran, game over */
304 if (unlikely(mos_parport->serial == NULL))
305 return;
306
307 /* try again to get the mutex */
308 if (!mutex_trylock(&mos_parport->serial->disc_mutex)) {
309 dbg("%s: rescheduling tasklet", __func__);
310 tasklet_schedule(&mos_parport->urb_tasklet);
311 return;
312 }
313
314 /* if device disconnected, game over */
315 if (unlikely(mos_parport->serial->disconnected)) {
316 mutex_unlock(&mos_parport->serial->disc_mutex);
317 return;
318 }
319
320 spin_lock_irqsave(&mos_parport->listlock, flags);
321 if (list_empty(&mos_parport->deferred_urbs)) {
322 spin_unlock_irqrestore(&mos_parport->listlock, flags);
323 mutex_unlock(&mos_parport->serial->disc_mutex);
324 dbg("%s: deferred_urbs list empty", __func__);
325 return;
326 }
327
328 /* move contents of deferred_urbs list to active_urbs list and submit */
329 list_for_each_safe(cursor, next, &mos_parport->deferred_urbs)
330 list_move_tail(cursor, &mos_parport->active_urbs);
331 list_for_each_entry(urbtrack, &mos_parport->active_urbs,
332 urblist_entry) {
333 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
334 dbg("%s: urb submitted", __func__);
335 if (ret_val) {
336 dev_err(&mos_parport->serial->dev->dev,
337 "usb_submit_urb() failed: %d", ret_val);
338 list_del(&urbtrack->urblist_entry);
339 kref_put(&urbtrack->ref_count, destroy_urbtracker);
340 }
341 }
342 spin_unlock_irqrestore(&mos_parport->listlock, flags);
343 mutex_unlock(&mos_parport->serial->disc_mutex);
344}
345
346/* callback for parallel port control urbs submitted asynchronously */
347static void async_complete(struct urb *urb)
348{
349 struct urbtracker *urbtrack = urb->context;
350 int status = urb->status;
351 dbg("%s called", __func__);
352 if (unlikely(status))
353 dbg("%s - nonzero urb status received: %d", __func__, status);
354
355 /* remove the urbtracker from the active_urbs list */
356 spin_lock(&urbtrack->mos_parport->listlock);
357 list_del(&urbtrack->urblist_entry);
358 spin_unlock(&urbtrack->mos_parport->listlock);
359 kref_put(&urbtrack->ref_count, destroy_urbtracker);
360}
361
362static int write_parport_reg_nonblock(struct mos7715_parport *mos_parport,
363 enum mos_regs reg, __u8 data)
364{
365 struct urbtracker *urbtrack;
366 int ret_val;
367 unsigned long flags;
Mike Dunnb69578d2010-04-15 17:01:33 -0400368 struct usb_serial *serial = mos_parport->serial;
369 struct usb_device *usbdev = serial->dev;
370 dbg("%s called", __func__);
371
372 /* create and initialize the control urb and containing urbtracker */
373 urbtrack = kmalloc(sizeof(struct urbtracker), GFP_ATOMIC);
374 if (urbtrack == NULL) {
375 dev_err(&usbdev->dev, "out of memory");
376 return -ENOMEM;
377 }
378 kref_get(&mos_parport->ref_count);
379 urbtrack->mos_parport = mos_parport;
380 urbtrack->urb = usb_alloc_urb(0, GFP_ATOMIC);
381 if (urbtrack->urb == NULL) {
382 dev_err(&usbdev->dev, "out of urbs");
383 kfree(urbtrack);
384 return -ENOMEM;
385 }
Johan Hovold419628b2013-08-13 13:27:34 +0200386 urbtrack->setup = kmalloc(sizeof(*urbtrack->setup), GFP_KERNEL);
387 if (!urbtrack->setup) {
388 usb_free_urb(urbtrack->urb);
389 kfree(urbtrack);
390 return -ENOMEM;
391 }
392 urbtrack->setup->bRequestType = (__u8)0x40;
393 urbtrack->setup->bRequest = (__u8)0x0e;
394 urbtrack->setup->wValue = get_reg_value(reg, dummy);
395 urbtrack->setup->wIndex = get_reg_index(reg);
396 urbtrack->setup->wLength = 0;
Mike Dunnb69578d2010-04-15 17:01:33 -0400397 usb_fill_control_urb(urbtrack->urb, usbdev,
398 usb_sndctrlpipe(usbdev, 0),
Johan Hovold419628b2013-08-13 13:27:34 +0200399 (unsigned char *)urbtrack->setup,
Mike Dunnb69578d2010-04-15 17:01:33 -0400400 NULL, 0, async_complete, urbtrack);
401 kref_init(&urbtrack->ref_count);
402 INIT_LIST_HEAD(&urbtrack->urblist_entry);
403
404 /*
405 * get the disconnect mutex, or add tracker to the deferred_urbs list
406 * and schedule a tasklet to try again later
407 */
408 if (!mutex_trylock(&serial->disc_mutex)) {
409 spin_lock_irqsave(&mos_parport->listlock, flags);
410 list_add_tail(&urbtrack->urblist_entry,
411 &mos_parport->deferred_urbs);
412 spin_unlock_irqrestore(&mos_parport->listlock, flags);
413 tasklet_schedule(&mos_parport->urb_tasklet);
414 dbg("tasklet scheduled");
415 return 0;
416 }
417
418 /* bail if device disconnected */
419 if (serial->disconnected) {
420 kref_put(&urbtrack->ref_count, destroy_urbtracker);
421 mutex_unlock(&serial->disc_mutex);
422 return -ENODEV;
423 }
424
425 /* add the tracker to the active_urbs list and submit */
426 spin_lock_irqsave(&mos_parport->listlock, flags);
427 list_add_tail(&urbtrack->urblist_entry, &mos_parport->active_urbs);
428 spin_unlock_irqrestore(&mos_parport->listlock, flags);
429 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
430 mutex_unlock(&serial->disc_mutex);
431 if (ret_val) {
432 dev_err(&usbdev->dev,
433 "%s: submit_urb() failed: %d", __func__, ret_val);
434 spin_lock_irqsave(&mos_parport->listlock, flags);
435 list_del(&urbtrack->urblist_entry);
436 spin_unlock_irqrestore(&mos_parport->listlock, flags);
437 kref_put(&urbtrack->ref_count, destroy_urbtracker);
438 return ret_val;
439 }
440 return 0;
441}
442
443/*
444 * This is the the common top part of all parallel port callback operations that
445 * send synchronous messages to the device. This implements convoluted locking
446 * that avoids two scenarios: (1) a port operation is called after usbserial
447 * has called our release function, at which point struct mos7715_parport has
448 * been destroyed, and (2) the device has been disconnected, but usbserial has
449 * not called the release function yet because someone has a serial port open.
450 * The shared release_lock prevents the first, and the mutex and disconnected
451 * flag maintained by usbserial covers the second. We also use the msg_pending
452 * flag to ensure that all synchronous usb messgage calls have completed before
453 * our release function can return.
454 */
455static int parport_prologue(struct parport *pp)
456{
457 struct mos7715_parport *mos_parport;
458
459 spin_lock(&release_lock);
460 mos_parport = pp->private_data;
461 if (unlikely(mos_parport == NULL)) {
462 /* release fn called, port struct destroyed */
463 spin_unlock(&release_lock);
464 return -1;
465 }
466 mos_parport->msg_pending = true; /* synch usb call pending */
467 INIT_COMPLETION(mos_parport->syncmsg_compl);
468 spin_unlock(&release_lock);
469
470 mutex_lock(&mos_parport->serial->disc_mutex);
471 if (mos_parport->serial->disconnected) {
472 /* device disconnected */
473 mutex_unlock(&mos_parport->serial->disc_mutex);
474 mos_parport->msg_pending = false;
475 complete(&mos_parport->syncmsg_compl);
476 return -1;
477 }
478
479 return 0;
480}
481
482/*
483 * This is the the common bottom part of all parallel port functions that send
484 * synchronous messages to the device.
485 */
486static inline void parport_epilogue(struct parport *pp)
487{
488 struct mos7715_parport *mos_parport = pp->private_data;
489 mutex_unlock(&mos_parport->serial->disc_mutex);
490 mos_parport->msg_pending = false;
491 complete(&mos_parport->syncmsg_compl);
492}
493
494static void parport_mos7715_write_data(struct parport *pp, unsigned char d)
495{
496 struct mos7715_parport *mos_parport = pp->private_data;
497 dbg("%s called: %2.2x", __func__, d);
498 if (parport_prologue(pp) < 0)
499 return;
500 mos7715_change_mode(mos_parport, SPP);
Mike Dunn63b91762010-04-15 17:02:09 -0400501 write_mos_reg(mos_parport->serial, dummy, DPR, (__u8)d);
Mike Dunnb69578d2010-04-15 17:01:33 -0400502 parport_epilogue(pp);
503}
504
505static unsigned char parport_mos7715_read_data(struct parport *pp)
506{
507 struct mos7715_parport *mos_parport = pp->private_data;
508 unsigned char d;
509 dbg("%s called", __func__);
510 if (parport_prologue(pp) < 0)
511 return 0;
Mike Dunn63b91762010-04-15 17:02:09 -0400512 read_mos_reg(mos_parport->serial, dummy, DPR, &d);
Mike Dunnb69578d2010-04-15 17:01:33 -0400513 parport_epilogue(pp);
514 return d;
515}
516
517static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
518{
519 struct mos7715_parport *mos_parport = pp->private_data;
520 __u8 data;
521 dbg("%s called: %2.2x", __func__, d);
522 if (parport_prologue(pp) < 0)
523 return;
524 data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
Mike Dunn63b91762010-04-15 17:02:09 -0400525 write_mos_reg(mos_parport->serial, dummy, DCR, data);
Mike Dunnb69578d2010-04-15 17:01:33 -0400526 mos_parport->shadowDCR = data;
527 parport_epilogue(pp);
528}
529
530static unsigned char parport_mos7715_read_control(struct parport *pp)
531{
532 struct mos7715_parport *mos_parport = pp->private_data;
533 __u8 dcr;
534 dbg("%s called", __func__);
535 spin_lock(&release_lock);
536 mos_parport = pp->private_data;
537 if (unlikely(mos_parport == NULL)) {
538 spin_unlock(&release_lock);
539 return 0;
540 }
541 dcr = mos_parport->shadowDCR & 0x0f;
542 spin_unlock(&release_lock);
543 return dcr;
544}
545
546static unsigned char parport_mos7715_frob_control(struct parport *pp,
547 unsigned char mask,
548 unsigned char val)
549{
550 struct mos7715_parport *mos_parport = pp->private_data;
551 __u8 dcr;
552 dbg("%s called", __func__);
553 mask &= 0x0f;
554 val &= 0x0f;
555 if (parport_prologue(pp) < 0)
556 return 0;
557 mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
Mike Dunn63b91762010-04-15 17:02:09 -0400558 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400559 dcr = mos_parport->shadowDCR & 0x0f;
560 parport_epilogue(pp);
561 return dcr;
562}
563
564static unsigned char parport_mos7715_read_status(struct parport *pp)
565{
566 unsigned char status;
567 struct mos7715_parport *mos_parport = pp->private_data;
568 dbg("%s called", __func__);
569 spin_lock(&release_lock);
570 mos_parport = pp->private_data;
571 if (unlikely(mos_parport == NULL)) { /* release called */
572 spin_unlock(&release_lock);
573 return 0;
574 }
575 status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
576 spin_unlock(&release_lock);
577 return status;
578}
579
580static void parport_mos7715_enable_irq(struct parport *pp)
581{
582 dbg("%s called", __func__);
583}
584static void parport_mos7715_disable_irq(struct parport *pp)
585{
586 dbg("%s called", __func__);
587}
588
589static void parport_mos7715_data_forward(struct parport *pp)
590{
591 struct mos7715_parport *mos_parport = pp->private_data;
592 dbg("%s called", __func__);
593 if (parport_prologue(pp) < 0)
594 return;
595 mos7715_change_mode(mos_parport, PS2);
596 mos_parport->shadowDCR &= ~0x20;
Mike Dunn63b91762010-04-15 17:02:09 -0400597 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400598 parport_epilogue(pp);
599}
600
601static void parport_mos7715_data_reverse(struct parport *pp)
602{
603 struct mos7715_parport *mos_parport = pp->private_data;
604 dbg("%s called", __func__);
605 if (parport_prologue(pp) < 0)
606 return;
607 mos7715_change_mode(mos_parport, PS2);
608 mos_parport->shadowDCR |= 0x20;
Mike Dunn63b91762010-04-15 17:02:09 -0400609 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400610 parport_epilogue(pp);
611}
612
613static void parport_mos7715_init_state(struct pardevice *dev,
614 struct parport_state *s)
615{
616 dbg("%s called", __func__);
617 s->u.pc.ctr = DCR_INIT_VAL;
618 s->u.pc.ecr = ECR_INIT_VAL;
619}
620
621/* N.B. Parport core code requires that this function not block */
622static void parport_mos7715_save_state(struct parport *pp,
623 struct parport_state *s)
624{
625 struct mos7715_parport *mos_parport;
626 dbg("%s called", __func__);
627 spin_lock(&release_lock);
628 mos_parport = pp->private_data;
629 if (unlikely(mos_parport == NULL)) { /* release called */
630 spin_unlock(&release_lock);
631 return;
632 }
633 s->u.pc.ctr = mos_parport->shadowDCR;
634 s->u.pc.ecr = mos_parport->shadowECR;
635 spin_unlock(&release_lock);
636}
637
638/* N.B. Parport core code requires that this function not block */
639static void parport_mos7715_restore_state(struct parport *pp,
640 struct parport_state *s)
641{
642 struct mos7715_parport *mos_parport;
643 dbg("%s called", __func__);
644 spin_lock(&release_lock);
645 mos_parport = pp->private_data;
646 if (unlikely(mos_parport == NULL)) { /* release called */
647 spin_unlock(&release_lock);
648 return;
649 }
650 write_parport_reg_nonblock(mos_parport, DCR, mos_parport->shadowDCR);
651 write_parport_reg_nonblock(mos_parport, ECR, mos_parport->shadowECR);
652 spin_unlock(&release_lock);
653}
654
655static size_t parport_mos7715_write_compat(struct parport *pp,
656 const void *buffer,
657 size_t len, int flags)
658{
659 int retval;
660 struct mos7715_parport *mos_parport = pp->private_data;
661 int actual_len;
662 dbg("%s called: %u chars", __func__, (unsigned int)len);
663 if (parport_prologue(pp) < 0)
664 return 0;
665 mos7715_change_mode(mos_parport, PPF);
666 retval = usb_bulk_msg(mos_parport->serial->dev,
667 usb_sndbulkpipe(mos_parport->serial->dev, 2),
668 (void *)buffer, len, &actual_len,
669 MOS_WDR_TIMEOUT);
670 parport_epilogue(pp);
671 if (retval) {
672 dev_err(&mos_parport->serial->dev->dev,
673 "mos7720: usb_bulk_msg() failed: %d", retval);
674 return 0;
675 }
676 return actual_len;
677}
678
679static struct parport_operations parport_mos7715_ops = {
680 .owner = THIS_MODULE,
681 .write_data = parport_mos7715_write_data,
682 .read_data = parport_mos7715_read_data,
683
684 .write_control = parport_mos7715_write_control,
685 .read_control = parport_mos7715_read_control,
686 .frob_control = parport_mos7715_frob_control,
687
688 .read_status = parport_mos7715_read_status,
689
690 .enable_irq = parport_mos7715_enable_irq,
691 .disable_irq = parport_mos7715_disable_irq,
692
693 .data_forward = parport_mos7715_data_forward,
694 .data_reverse = parport_mos7715_data_reverse,
695
696 .init_state = parport_mos7715_init_state,
697 .save_state = parport_mos7715_save_state,
698 .restore_state = parport_mos7715_restore_state,
699
700 .compat_write_data = parport_mos7715_write_compat,
701
702 .nibble_read_data = parport_ieee1284_read_nibble,
703 .byte_read_data = parport_ieee1284_read_byte,
704};
705
706/*
707 * Allocate and initialize parallel port control struct, initialize
708 * the parallel port hardware device, and register with the parport subsystem.
709 */
710static int mos7715_parport_init(struct usb_serial *serial)
711{
712 struct mos7715_parport *mos_parport;
713
714 /* allocate and initialize parallel port control struct */
715 mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
716 if (mos_parport == NULL) {
717 dbg("mos7715_parport_init: kzalloc failed");
718 return -ENOMEM;
719 }
720 mos_parport->msg_pending = false;
721 kref_init(&mos_parport->ref_count);
722 spin_lock_init(&mos_parport->listlock);
723 INIT_LIST_HEAD(&mos_parport->active_urbs);
724 INIT_LIST_HEAD(&mos_parport->deferred_urbs);
725 usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
726 mos_parport->serial = serial;
727 tasklet_init(&mos_parport->urb_tasklet, send_deferred_urbs,
728 (unsigned long) mos_parport);
729 init_completion(&mos_parport->syncmsg_compl);
730
731 /* cycle parallel port reset bit */
Mike Dunn63b91762010-04-15 17:02:09 -0400732 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x80);
733 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x00);
Mike Dunnb69578d2010-04-15 17:01:33 -0400734
735 /* initialize device registers */
736 mos_parport->shadowDCR = DCR_INIT_VAL;
Mike Dunn63b91762010-04-15 17:02:09 -0400737 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400738 mos_parport->shadowECR = ECR_INIT_VAL;
Mike Dunn63b91762010-04-15 17:02:09 -0400739 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400740
741 /* register with parport core */
742 mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE,
743 PARPORT_DMA_NONE,
744 &parport_mos7715_ops);
745 if (mos_parport->pp == NULL) {
746 dev_err(&serial->interface->dev,
747 "Could not register parport\n");
748 kref_put(&mos_parport->ref_count, destroy_mos_parport);
749 return -EIO;
750 }
751 mos_parport->pp->private_data = mos_parport;
752 mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP;
753 mos_parport->pp->dev = &serial->interface->dev;
754 parport_announce_port(mos_parport->pp);
755
756 return 0;
757}
758#endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700759
760/*
761 * mos7720_interrupt_callback
762 * this is the callback function for when we have received data on the
763 * interrupt endpoint.
764 */
765static void mos7720_interrupt_callback(struct urb *urb)
766{
767 int result;
768 int length;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700769 int status = urb->status;
Oliver Neukum325b70c2007-03-19 13:58:29 +0100770 __u8 *data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700771 __u8 sp1;
772 __u8 sp2;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700773
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700774 switch (status) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700775 case 0:
776 /* success */
777 break;
778 case -ECONNRESET:
779 case -ENOENT:
780 case -ESHUTDOWN:
781 /* this urb is terminated, clean up */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800782 dbg("%s - urb shutting down with status: %d", __func__,
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700783 status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700784 return;
785 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800786 dbg("%s - nonzero urb status received: %d", __func__,
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700787 status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700788 goto exit;
789 }
790
791 length = urb->actual_length;
792 data = urb->transfer_buffer;
793
794 /* Moschip get 4 bytes
795 * Byte 1 IIR Port 1 (port.number is 0)
796 * Byte 2 IIR Port 2 (port.number is 1)
797 * Byte 3 --------------
798 * Byte 4 FIFO status for both */
Oliver Neukum325b70c2007-03-19 13:58:29 +0100799
800 /* the above description is inverted
801 * oneukum 2007-03-14 */
802
803 if (unlikely(length != 4)) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700804 dbg("Wrong data !!!");
805 return;
806 }
807
Oliver Neukum325b70c2007-03-19 13:58:29 +0100808 sp1 = data[3];
809 sp2 = data[2];
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700810
Oliver Neukum325b70c2007-03-19 13:58:29 +0100811 if ((sp1 | sp2) & 0x01) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700812 /* No Interrupt Pending in both the ports */
813 dbg("No Interrupt !!!");
814 } else {
815 switch (sp1 & 0x0f) {
816 case SERIAL_IIR_RLS:
817 dbg("Serial Port 1: Receiver status error or address "
818 "bit detected in 9-bit mode\n");
819 break;
820 case SERIAL_IIR_CTI:
821 dbg("Serial Port 1: Receiver time out");
822 break;
823 case SERIAL_IIR_MS:
Mike Dunnb69578d2010-04-15 17:01:33 -0400824 /* dbg("Serial Port 1: Modem status change"); */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700825 break;
826 }
827
828 switch (sp2 & 0x0f) {
829 case SERIAL_IIR_RLS:
830 dbg("Serial Port 2: Receiver status error or address "
831 "bit detected in 9-bit mode");
832 break;
833 case SERIAL_IIR_CTI:
834 dbg("Serial Port 2: Receiver time out");
835 break;
836 case SERIAL_IIR_MS:
Mike Dunnb69578d2010-04-15 17:01:33 -0400837 /* dbg("Serial Port 2: Modem status change"); */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700838 break;
839 }
840 }
841
842exit:
843 result = usb_submit_urb(urb, GFP_ATOMIC);
844 if (result)
845 dev_err(&urb->dev->dev,
846 "%s - Error %d submitting control urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800847 __func__, result);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700848}
849
850/*
Mike Dunnfb088e32010-01-26 12:12:12 -0500851 * mos7715_interrupt_callback
852 * this is the 7715's callback function for when we have received data on
853 * the interrupt endpoint.
854 */
855static void mos7715_interrupt_callback(struct urb *urb)
856{
857 int result;
858 int length;
859 int status = urb->status;
860 __u8 *data;
861 __u8 iir;
862
863 switch (status) {
864 case 0:
865 /* success */
866 break;
867 case -ECONNRESET:
868 case -ENOENT:
869 case -ESHUTDOWN:
Mike Dunnb69578d2010-04-15 17:01:33 -0400870 case -ENODEV:
Mike Dunnfb088e32010-01-26 12:12:12 -0500871 /* this urb is terminated, clean up */
872 dbg("%s - urb shutting down with status: %d", __func__,
873 status);
874 return;
875 default:
876 dbg("%s - nonzero urb status received: %d", __func__,
877 status);
878 goto exit;
879 }
880
881 length = urb->actual_length;
882 data = urb->transfer_buffer;
883
884 /* Structure of data from 7715 device:
885 * Byte 1: IIR serial Port
886 * Byte 2: unused
887 * Byte 2: DSR parallel port
888 * Byte 4: FIFO status for both */
889
890 if (unlikely(length != 4)) {
891 dbg("Wrong data !!!");
892 return;
893 }
894
895 iir = data[0];
896 if (!(iir & 0x01)) { /* serial port interrupt pending */
897 switch (iir & 0x0f) {
898 case SERIAL_IIR_RLS:
899 dbg("Serial Port: Receiver status error or address "
900 "bit detected in 9-bit mode\n");
901 break;
902 case SERIAL_IIR_CTI:
903 dbg("Serial Port: Receiver time out");
904 break;
905 case SERIAL_IIR_MS:
Mike Dunnb69578d2010-04-15 17:01:33 -0400906 /* dbg("Serial Port: Modem status change"); */
Mike Dunnfb088e32010-01-26 12:12:12 -0500907 break;
908 }
909 }
910
Mike Dunnb69578d2010-04-15 17:01:33 -0400911#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
912 { /* update local copy of DSR reg */
913 struct usb_serial_port *port = urb->context;
914 struct mos7715_parport *mos_parport = port->serial->private;
915 if (unlikely(mos_parport == NULL))
916 return;
917 atomic_set(&mos_parport->shadowDSR, data[2]);
918 }
919#endif
920
Mike Dunnfb088e32010-01-26 12:12:12 -0500921exit:
922 result = usb_submit_urb(urb, GFP_ATOMIC);
923 if (result)
924 dev_err(&urb->dev->dev,
925 "%s - Error %d submitting control urb\n",
926 __func__, result);
Mike Dunnfb088e32010-01-26 12:12:12 -0500927}
928
929/*
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700930 * mos7720_bulk_in_callback
931 * this is the callback function for when we have received data on the
932 * bulk in endpoint.
933 */
934static void mos7720_bulk_in_callback(struct urb *urb)
935{
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700936 int retval;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700937 unsigned char *data ;
938 struct usb_serial_port *port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700939 struct tty_struct *tty;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700940 int status = urb->status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700941
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700942 if (status) {
943 dbg("nonzero read bulk status received: %d", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700944 return;
945 }
946
Mike Dunnb69578d2010-04-15 17:01:33 -0400947 port = urb->context;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700948
Harvey Harrison441b62c2008-03-03 16:08:34 -0800949 dbg("Entering...%s", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700950
951 data = urb->transfer_buffer;
952
Alan Cox4a90f092008-10-13 10:39:46 +0100953 tty = tty_port_tty_get(&port->port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700954 if (tty && urb->actual_length) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700955 tty_insert_flip_string(tty, data, urb->actual_length);
956 tty_flip_buffer_push(tty);
957 }
Alan Cox4a90f092008-10-13 10:39:46 +0100958 tty_kref_put(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700959
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700960 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700961 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
962 if (retval)
963 dbg("usb_submit_urb(read bulk) failed, retval = %d",
964 retval);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700965 }
966}
967
968/*
969 * mos7720_bulk_out_data_callback
970 * this is the callback function for when we have finished sending serial
971 * data on the bulk out endpoint.
972 */
973static void mos7720_bulk_out_data_callback(struct urb *urb)
974{
975 struct moschip_port *mos7720_port;
976 struct tty_struct *tty;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700977 int status = urb->status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700978
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700979 if (status) {
980 dbg("nonzero write bulk status received:%d", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700981 return;
982 }
983
984 mos7720_port = urb->context;
985 if (!mos7720_port) {
986 dbg("NULL mos7720_port pointer");
987 return ;
988 }
989
Alan Cox4a90f092008-10-13 10:39:46 +0100990 tty = tty_port_tty_get(&mos7720_port->port->port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700991
Jiri Slabyb963a842007-02-10 01:44:55 -0800992 if (tty && mos7720_port->open)
993 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100994 tty_kref_put(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700995}
996
997/*
Mike Dunnfb088e32010-01-26 12:12:12 -0500998 * mos77xx_probe
999 * this function installs the appropriate read interrupt endpoint callback
1000 * depending on whether the device is a 7720 or 7715, thus avoiding costly
1001 * run-time checks in the high-frequency callback routine itself.
1002 */
1003static int mos77xx_probe(struct usb_serial *serial,
1004 const struct usb_device_id *id)
1005{
1006 if (id->idProduct == MOSCHIP_DEVICE_ID_7715)
1007 moschip7720_2port_driver.read_int_callback =
1008 mos7715_interrupt_callback;
1009 else
1010 moschip7720_2port_driver.read_int_callback =
1011 mos7720_interrupt_callback;
1012
1013 return 0;
1014}
1015
1016static int mos77xx_calc_num_ports(struct usb_serial *serial)
1017{
1018 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
1019 if (product == MOSCHIP_DEVICE_ID_7715)
1020 return 1;
1021
1022 return 2;
1023}
1024
Alan Coxa509a7e2009-09-19 13:13:26 -07001025static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001026{
1027 struct usb_serial *serial;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001028 struct urb *urb;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001029 struct moschip_port *mos7720_port;
1030 int response;
1031 int port_number;
Mike Dunn63b91762010-04-15 17:02:09 -04001032 __u8 data;
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001033 int allocated_urbs = 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001034 int j;
1035
1036 serial = port->serial;
1037
1038 mos7720_port = usb_get_serial_port_data(port);
1039 if (mos7720_port == NULL)
1040 return -ENODEV;
1041
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001042 usb_clear_halt(serial->dev, port->write_urb->pipe);
1043 usb_clear_halt(serial->dev, port->read_urb->pipe);
1044
1045 /* Initialising the write urb pool */
1046 for (j = 0; j < NUM_URBS; ++j) {
Alan Cox4da1a172008-07-22 11:16:21 +01001047 urb = usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001048 mos7720_port->write_urb_pool[j] = urb;
1049
1050 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001051 dev_err(&port->dev, "No more urbs???\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001052 continue;
1053 }
1054
1055 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1056 GFP_KERNEL);
1057 if (!urb->transfer_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001058 dev_err(&port->dev,
1059 "%s-out of memory for urb buffers.\n",
1060 __func__);
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001061 usb_free_urb(mos7720_port->write_urb_pool[j]);
1062 mos7720_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001063 continue;
1064 }
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001065 allocated_urbs++;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001066 }
1067
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001068 if (!allocated_urbs)
1069 return -ENOMEM;
1070
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001071 /* Initialize MCS7720 -- Write Init values to corresponding Registers
1072 *
1073 * Register Index
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001074 * 0 : THR/RHR
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001075 * 1 : IER
1076 * 2 : FCR
1077 * 3 : LCR
1078 * 4 : MCR
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001079 * 5 : LSR
1080 * 6 : MSR
1081 * 7 : SPR
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001082 *
1083 * 0x08 : SP1/2 Control Reg
1084 */
1085 port_number = port->number - port->serial->minor;
Mike Dunn63b91762010-04-15 17:02:09 -04001086 read_mos_reg(serial, port_number, LSR, &data);
1087
Joe Perches759f3632010-02-05 16:50:08 -08001088 dbg("SS::%p LSR:%x", mos7720_port, data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001089
1090 dbg("Check:Sending Command ..........");
1091
Mike Dunn63b91762010-04-15 17:02:09 -04001092 write_mos_reg(serial, dummy, SP1_REG, 0x02);
1093 write_mos_reg(serial, dummy, SP2_REG, 0x02);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001094
Mike Dunn63b91762010-04-15 17:02:09 -04001095 write_mos_reg(serial, port_number, IER, 0x00);
1096 write_mos_reg(serial, port_number, FCR, 0x00);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001097
Mike Dunn63b91762010-04-15 17:02:09 -04001098 write_mos_reg(serial, port_number, FCR, 0xcf);
1099 mos7720_port->shadowLCR = 0x03;
1100 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1101 mos7720_port->shadowMCR = 0x0b;
1102 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001103
Mike Dunn63b91762010-04-15 17:02:09 -04001104 write_mos_reg(serial, port_number, SP_CONTROL_REG, 0x00);
1105 read_mos_reg(serial, dummy, SP_CONTROL_REG, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001106 data = data | (port->number - port->serial->minor + 1);
Mike Dunn63b91762010-04-15 17:02:09 -04001107 write_mos_reg(serial, dummy, SP_CONTROL_REG, data);
1108 mos7720_port->shadowLCR = 0x83;
1109 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1110 write_mos_reg(serial, port_number, THR, 0x0c);
1111 write_mos_reg(serial, port_number, IER, 0x00);
1112 mos7720_port->shadowLCR = 0x03;
1113 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1114 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001115
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001116 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
1117 if (response)
Alan Cox4da1a172008-07-22 11:16:21 +01001118 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
1119 __func__, response);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001120
1121 /* initialize our icount structure */
1122 memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
1123
1124 /* initialize our port settings */
1125 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
1126
1127 /* send a open port command */
1128 mos7720_port->open = 1;
1129
1130 return 0;
1131}
1132
1133/*
1134 * mos7720_chars_in_buffer
1135 * this function is called by the tty driver when it wants to know how many
1136 * bytes of data we currently have outstanding in the port (data that has
1137 * been written, but hasn't made it out the port yet)
1138 * If successful, we return the number of bytes left to be written in the
1139 * system,
1140 * Otherwise we return a negative error number.
1141 */
Alan Cox95da3102008-07-22 11:09:07 +01001142static int mos7720_chars_in_buffer(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001143{
Alan Cox95da3102008-07-22 11:09:07 +01001144 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001145 int i;
1146 int chars = 0;
1147 struct moschip_port *mos7720_port;
1148
Harvey Harrison441b62c2008-03-03 16:08:34 -08001149 dbg("%s:entering ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001150
1151 mos7720_port = usb_get_serial_port_data(port);
1152 if (mos7720_port == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001153 dbg("%s:leaving ...........", __func__);
Alan Cox23198fd2009-07-20 16:05:27 +01001154 return 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001155 }
1156
1157 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001158 if (mos7720_port->write_urb_pool[i] &&
1159 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001160 chars += URB_TRANSFER_BUFFER_SIZE;
1161 }
Harvey Harrison441b62c2008-03-03 16:08:34 -08001162 dbg("%s - returns %d", __func__, chars);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001163 return chars;
1164}
1165
Alan Cox335f8512009-06-11 12:26:29 +01001166static void mos7720_close(struct usb_serial_port *port)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001167{
1168 struct usb_serial *serial;
1169 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001170 int j;
1171
1172 dbg("mos7720_close:entering...");
1173
1174 serial = port->serial;
1175
1176 mos7720_port = usb_get_serial_port_data(port);
1177 if (mos7720_port == NULL)
1178 return;
1179
1180 for (j = 0; j < NUM_URBS; ++j)
1181 usb_kill_urb(mos7720_port->write_urb_pool[j]);
1182
1183 /* Freeing Write URBs */
1184 for (j = 0; j < NUM_URBS; ++j) {
1185 if (mos7720_port->write_urb_pool[j]) {
1186 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
1187 usb_free_urb(mos7720_port->write_urb_pool[j]);
1188 }
1189 }
1190
1191 /* While closing port, shutdown all bulk read, write *
Oliver Neukuma1cd7e92008-01-16 17:18:52 +01001192 * and interrupt read if they exists, otherwise nop */
1193 dbg("Shutdown bulk write");
1194 usb_kill_urb(port->write_urb);
1195 dbg("Shutdown bulk read");
1196 usb_kill_urb(port->read_urb);
1197
1198 mutex_lock(&serial->disc_mutex);
1199 /* these commands must not be issued if the device has
1200 * been disconnected */
1201 if (!serial->disconnected) {
Mike Dunn63b91762010-04-15 17:02:09 -04001202 write_mos_reg(serial, port->number - port->serial->minor,
1203 MCR, 0x00);
1204 write_mos_reg(serial, port->number - port->serial->minor,
1205 IER, 0x00);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001206 }
Oliver Neukuma1cd7e92008-01-16 17:18:52 +01001207 mutex_unlock(&serial->disc_mutex);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001208 mos7720_port->open = 0;
1209
Harvey Harrison441b62c2008-03-03 16:08:34 -08001210 dbg("Leaving %s", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001211}
1212
Alan Cox95da3102008-07-22 11:09:07 +01001213static void mos7720_break(struct tty_struct *tty, int break_state)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001214{
Alan Cox95da3102008-07-22 11:09:07 +01001215 struct usb_serial_port *port = tty->driver_data;
Alan Cox4da1a172008-07-22 11:16:21 +01001216 unsigned char data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001217 struct usb_serial *serial;
1218 struct moschip_port *mos7720_port;
1219
Harvey Harrison441b62c2008-03-03 16:08:34 -08001220 dbg("Entering %s", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001221
1222 serial = port->serial;
1223
1224 mos7720_port = usb_get_serial_port_data(port);
1225 if (mos7720_port == NULL)
1226 return;
1227
1228 if (break_state == -1)
1229 data = mos7720_port->shadowLCR | UART_LCR_SBC;
1230 else
1231 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
1232
1233 mos7720_port->shadowLCR = data;
Mike Dunn63b91762010-04-15 17:02:09 -04001234 write_mos_reg(serial, port->number - port->serial->minor,
1235 LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001236}
1237
1238/*
1239 * mos7720_write_room
1240 * this function is called by the tty driver when it wants to know how many
1241 * bytes of data we can accept for a specific port.
1242 * If successful, we return the amount of room that we have for this port
1243 * Otherwise we return a negative error number.
1244 */
Alan Cox95da3102008-07-22 11:09:07 +01001245static int mos7720_write_room(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001246{
Alan Cox95da3102008-07-22 11:09:07 +01001247 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001248 struct moschip_port *mos7720_port;
1249 int room = 0;
1250 int i;
1251
Harvey Harrison441b62c2008-03-03 16:08:34 -08001252 dbg("%s:entering ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001253
1254 mos7720_port = usb_get_serial_port_data(port);
1255 if (mos7720_port == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001256 dbg("%s:leaving ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001257 return -ENODEV;
1258 }
1259
Alan Coxa5b6f602008-04-08 17:16:06 +01001260 /* FIXME: Locking */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001261 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001262 if (mos7720_port->write_urb_pool[i] &&
1263 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001264 room += URB_TRANSFER_BUFFER_SIZE;
1265 }
1266
Harvey Harrison441b62c2008-03-03 16:08:34 -08001267 dbg("%s - returns %d", __func__, room);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001268 return room;
1269}
1270
Alan Cox95da3102008-07-22 11:09:07 +01001271static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
1272 const unsigned char *data, int count)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001273{
1274 int status;
1275 int i;
1276 int bytes_sent = 0;
1277 int transfer_size;
1278
1279 struct moschip_port *mos7720_port;
1280 struct usb_serial *serial;
1281 struct urb *urb;
1282 const unsigned char *current_position = data;
1283
Harvey Harrison441b62c2008-03-03 16:08:34 -08001284 dbg("%s:entering ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001285
1286 serial = port->serial;
1287
1288 mos7720_port = usb_get_serial_port_data(port);
1289 if (mos7720_port == NULL) {
1290 dbg("mos7720_port is NULL");
1291 return -ENODEV;
1292 }
1293
1294 /* try to find a free urb in the list */
1295 urb = NULL;
1296
1297 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001298 if (mos7720_port->write_urb_pool[i] &&
1299 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001300 urb = mos7720_port->write_urb_pool[i];
Alan Cox4da1a172008-07-22 11:16:21 +01001301 dbg("URB:%d", i);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001302 break;
1303 }
1304 }
1305
1306 if (urb == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001307 dbg("%s - no more free urbs", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001308 goto exit;
1309 }
1310
1311 if (urb->transfer_buffer == NULL) {
1312 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1313 GFP_KERNEL);
1314 if (urb->transfer_buffer == NULL) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001315 dev_err_console(port, "%s no more kernel memory...\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001316 __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001317 goto exit;
1318 }
1319 }
Alan Cox4da1a172008-07-22 11:16:21 +01001320 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001321
1322 memcpy(urb->transfer_buffer, current_position, transfer_size);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001323 usb_serial_debug_data(debug, &port->dev, __func__, transfer_size,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001324 urb->transfer_buffer);
1325
1326 /* fill urb with data and submit */
1327 usb_fill_bulk_urb(urb, serial->dev,
1328 usb_sndbulkpipe(serial->dev,
Alan Cox4da1a172008-07-22 11:16:21 +01001329 port->bulk_out_endpointAddress),
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001330 urb->transfer_buffer, transfer_size,
1331 mos7720_bulk_out_data_callback, mos7720_port);
1332
1333 /* send it down the pipe */
Alan Cox4da1a172008-07-22 11:16:21 +01001334 status = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001335 if (status) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001336 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001337 "with status = %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001338 bytes_sent = status;
1339 goto exit;
1340 }
1341 bytes_sent = transfer_size;
1342
1343exit:
1344 return bytes_sent;
1345}
1346
Alan Cox95da3102008-07-22 11:09:07 +01001347static void mos7720_throttle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001348{
Alan Cox95da3102008-07-22 11:09:07 +01001349 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001350 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001351 int status;
1352
Joe Perches759f3632010-02-05 16:50:08 -08001353 dbg("%s- port %d", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001354
1355 mos7720_port = usb_get_serial_port_data(port);
1356
1357 if (mos7720_port == NULL)
1358 return;
1359
1360 if (!mos7720_port->open) {
1361 dbg("port not opened");
1362 return;
1363 }
1364
Harvey Harrison441b62c2008-03-03 16:08:34 -08001365 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001366
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001367 /* if we are implementing XON/XOFF, send the stop character */
1368 if (I_IXOFF(tty)) {
1369 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001370 status = mos7720_write(tty, port, &stop_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001371 if (status <= 0)
1372 return;
1373 }
1374
1375 /* if we are implementing RTS/CTS, toggle that line */
1376 if (tty->termios->c_cflag & CRTSCTS) {
1377 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
Mike Dunn63b91762010-04-15 17:02:09 -04001378 write_mos_reg(port->serial, port->number - port->serial->minor,
1379 MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001380 if (status != 0)
1381 return;
1382 }
1383}
1384
Alan Cox95da3102008-07-22 11:09:07 +01001385static void mos7720_unthrottle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001386{
Alan Cox95da3102008-07-22 11:09:07 +01001387 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001388 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +01001389 int status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001390
1391 if (mos7720_port == NULL)
1392 return;
1393
1394 if (!mos7720_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001395 dbg("%s - port not opened", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001396 return;
1397 }
1398
Harvey Harrison441b62c2008-03-03 16:08:34 -08001399 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001400
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001401 /* if we are implementing XON/XOFF, send the start character */
1402 if (I_IXOFF(tty)) {
1403 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001404 status = mos7720_write(tty, port, &start_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001405 if (status <= 0)
1406 return;
1407 }
1408
1409 /* if we are implementing RTS/CTS, toggle that line */
1410 if (tty->termios->c_cflag & CRTSCTS) {
1411 mos7720_port->shadowMCR |= UART_MCR_RTS;
Mike Dunn63b91762010-04-15 17:02:09 -04001412 write_mos_reg(port->serial, port->number - port->serial->minor,
1413 MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001414 if (status != 0)
1415 return;
1416 }
1417}
1418
Mike Dunnb69578d2010-04-15 17:01:33 -04001419/* FIXME: this function does not work */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001420static int set_higher_rates(struct moschip_port *mos7720_port,
1421 unsigned int baud)
1422{
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001423 struct usb_serial_port *port;
1424 struct usb_serial *serial;
1425 int port_number;
Mike Dunn63b91762010-04-15 17:02:09 -04001426 enum mos_regs sp_reg;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001427 if (mos7720_port == NULL)
1428 return -EINVAL;
1429
1430 port = mos7720_port->port;
1431 serial = port->serial;
1432
Alan Cox4da1a172008-07-22 11:16:21 +01001433 /***********************************************
1434 * Init Sequence for higher rates
1435 ***********************************************/
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001436 dbg("Sending Setting Commands ..........");
1437 port_number = port->number - port->serial->minor;
1438
Mike Dunn63b91762010-04-15 17:02:09 -04001439 write_mos_reg(serial, port_number, IER, 0x00);
1440 write_mos_reg(serial, port_number, FCR, 0x00);
1441 write_mos_reg(serial, port_number, FCR, 0xcf);
1442 mos7720_port->shadowMCR = 0x0b;
1443 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1444 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x00);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001445
Alan Cox4da1a172008-07-22 11:16:21 +01001446 /***********************************************
1447 * Set for higher rates *
1448 ***********************************************/
Mike Dunnb69578d2010-04-15 17:01:33 -04001449 /* writing baud rate verbatum into uart clock field clearly not right */
Mike Dunn63b91762010-04-15 17:02:09 -04001450 if (port_number == 0)
1451 sp_reg = SP1_REG;
1452 else
1453 sp_reg = SP2_REG;
1454 write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
1455 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x03);
1456 mos7720_port->shadowMCR = 0x2b;
1457 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001458
Alan Cox4da1a172008-07-22 11:16:21 +01001459 /***********************************************
1460 * Set DLL/DLM
1461 ***********************************************/
Mike Dunn63b91762010-04-15 17:02:09 -04001462 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1463 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1464 write_mos_reg(serial, port_number, DLL, 0x01);
1465 write_mos_reg(serial, port_number, DLM, 0x00);
1466 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1467 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001468
1469 return 0;
1470}
1471
1472/* baud rate information */
Alan Cox4da1a172008-07-22 11:16:21 +01001473struct divisor_table_entry {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001474 __u32 baudrate;
1475 __u16 divisor;
1476};
1477
1478/* Define table of divisors for moschip 7720 hardware *
1479 * These assume a 3.6864MHz crystal, the standard /16, and *
1480 * MCR.7 = 0. */
1481static struct divisor_table_entry divisor_table[] = {
1482 { 50, 2304},
1483 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
1484 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
1485 { 150, 768},
1486 { 300, 384},
1487 { 600, 192},
1488 { 1200, 96},
1489 { 1800, 64},
1490 { 2400, 48},
1491 { 4800, 24},
1492 { 7200, 16},
1493 { 9600, 12},
1494 { 19200, 6},
1495 { 38400, 3},
1496 { 57600, 2},
1497 { 115200, 1},
1498};
1499
1500/*****************************************************************************
1501 * calc_baud_rate_divisor
1502 * this function calculates the proper baud rate divisor for the specified
1503 * baud rate.
1504 *****************************************************************************/
1505static int calc_baud_rate_divisor(int baudrate, int *divisor)
1506{
1507 int i;
1508 __u16 custom;
1509 __u16 round1;
1510 __u16 round;
1511
1512
Harvey Harrison441b62c2008-03-03 16:08:34 -08001513 dbg("%s - %d", __func__, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001514
1515 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
1516 if (divisor_table[i].baudrate == baudrate) {
1517 *divisor = divisor_table[i].divisor;
1518 return 0;
1519 }
1520 }
1521
Alan Cox4da1a172008-07-22 11:16:21 +01001522 /* After trying for all the standard baud rates *
1523 * Try calculating the divisor for this baud rate */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001524 if (baudrate > 75 && baudrate < 230400) {
1525 /* get the divisor */
1526 custom = (__u16)(230400L / baudrate);
1527
1528 /* Check for round off */
1529 round1 = (__u16)(2304000L / baudrate);
1530 round = (__u16)(round1 - (custom * 10));
1531 if (round > 4)
1532 custom++;
1533 *divisor = custom;
1534
Alan Cox4da1a172008-07-22 11:16:21 +01001535 dbg("Baud %d = %d", baudrate, custom);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001536 return 0;
1537 }
1538
1539 dbg("Baud calculation Failed...");
1540 return -EINVAL;
1541}
1542
1543/*
1544 * send_cmd_write_baud_rate
1545 * this function sends the proper command to change the baud rate of the
1546 * specified port.
1547 */
1548static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1549 int baudrate)
1550{
1551 struct usb_serial_port *port;
1552 struct usb_serial *serial;
1553 int divisor;
1554 int status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001555 unsigned char number;
1556
1557 if (mos7720_port == NULL)
1558 return -1;
1559
1560 port = mos7720_port->port;
1561 serial = port->serial;
1562
Harvey Harrison441b62c2008-03-03 16:08:34 -08001563 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001564
1565 number = port->number - port->serial->minor;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001566 dbg("%s - port = %d, baud = %d", __func__, port->number, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001567
Alan Cox4da1a172008-07-22 11:16:21 +01001568 /* Calculate the Divisor */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001569 status = calc_baud_rate_divisor(baudrate, &divisor);
1570 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001571 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001572 return status;
1573 }
1574
Alan Cox4da1a172008-07-22 11:16:21 +01001575 /* Enable access to divisor latch */
Mike Dunn63b91762010-04-15 17:02:09 -04001576 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1577 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001578
1579 /* Write the divisor */
Mike Dunn63b91762010-04-15 17:02:09 -04001580 write_mos_reg(serial, number, DLL, (__u8)(divisor & 0xff));
1581 write_mos_reg(serial, number, DLM, (__u8)((divisor & 0xff00) >> 8));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001582
Alan Cox4da1a172008-07-22 11:16:21 +01001583 /* Disable access to divisor latch */
Mike Dunn63b91762010-04-15 17:02:09 -04001584 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1585 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001586
1587 return status;
1588}
1589
1590/*
1591 * change_port_settings
1592 * This routine is called to set the UART on the device to match
1593 * the specified new settings.
1594 */
Alan Cox95da3102008-07-22 11:09:07 +01001595static void change_port_settings(struct tty_struct *tty,
1596 struct moschip_port *mos7720_port,
Alan Cox606d0992006-12-08 02:38:45 -08001597 struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001598{
1599 struct usb_serial_port *port;
1600 struct usb_serial *serial;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001601 int baud;
1602 unsigned cflag;
1603 unsigned iflag;
1604 __u8 mask = 0xff;
1605 __u8 lData;
1606 __u8 lParity;
1607 __u8 lStop;
1608 int status;
1609 int port_number;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001610
1611 if (mos7720_port == NULL)
1612 return ;
1613
1614 port = mos7720_port->port;
1615 serial = port->serial;
1616 port_number = port->number - port->serial->minor;
1617
Harvey Harrison441b62c2008-03-03 16:08:34 -08001618 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001619
1620 if (!mos7720_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001621 dbg("%s - port not opened", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001622 return;
1623 }
1624
Harvey Harrison441b62c2008-03-03 16:08:34 -08001625 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001626
1627 lData = UART_LCR_WLEN8;
1628 lStop = 0x00; /* 1 stop bit */
1629 lParity = 0x00; /* No parity */
1630
1631 cflag = tty->termios->c_cflag;
1632 iflag = tty->termios->c_iflag;
1633
1634 /* Change the number of bits */
1635 switch (cflag & CSIZE) {
1636 case CS5:
1637 lData = UART_LCR_WLEN5;
1638 mask = 0x1f;
1639 break;
1640
1641 case CS6:
1642 lData = UART_LCR_WLEN6;
1643 mask = 0x3f;
1644 break;
1645
1646 case CS7:
1647 lData = UART_LCR_WLEN7;
1648 mask = 0x7f;
1649 break;
1650 default:
1651 case CS8:
1652 lData = UART_LCR_WLEN8;
1653 break;
1654 }
1655
1656 /* Change the Parity bit */
1657 if (cflag & PARENB) {
1658 if (cflag & PARODD) {
1659 lParity = UART_LCR_PARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001660 dbg("%s - parity = odd", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001661 } else {
1662 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001663 dbg("%s - parity = even", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001664 }
1665
1666 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001667 dbg("%s - parity = none", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001668 }
1669
1670 if (cflag & CMSPAR)
1671 lParity = lParity | 0x20;
1672
1673 /* Change the Stop bit */
1674 if (cflag & CSTOPB) {
1675 lStop = UART_LCR_STOP;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001676 dbg("%s - stop bits = 2", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001677 } else {
1678 lStop = 0x00;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001679 dbg("%s - stop bits = 1", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001680 }
1681
1682#define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1683#define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1684#define LCR_PAR_MASK 0x38 /* Mask for parity field */
1685
1686 /* Update the LCR with the correct value */
Alan Cox4da1a172008-07-22 11:16:21 +01001687 mos7720_port->shadowLCR &=
Mike Dunn63b91762010-04-15 17:02:09 -04001688 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001689 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1690
1691
1692 /* Disable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001693 write_mos_reg(serial, port_number, IER, 0x00);
1694 write_mos_reg(serial, port_number, FCR, 0x00);
1695 write_mos_reg(serial, port_number, FCR, 0xcf);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001696
1697 /* Send the updated LCR value to the mos7720 */
Mike Dunn63b91762010-04-15 17:02:09 -04001698 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1699 mos7720_port->shadowMCR = 0x0b;
1700 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001701
1702 /* set up the MCR register and send it to the mos7720 */
1703 mos7720_port->shadowMCR = UART_MCR_OUT2;
1704 if (cflag & CBAUD)
1705 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1706
1707 if (cflag & CRTSCTS) {
1708 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
Alan Cox4da1a172008-07-22 11:16:21 +01001709 /* To set hardware flow control to the specified *
1710 * serial port, in SP1/2_CONTROL_REG */
Johan Hovold119ae902013-06-04 18:50:31 +02001711 if (port_number)
Mike Dunn63b91762010-04-15 17:02:09 -04001712 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x01);
1713 else
1714 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x02);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001715
Mike Dunn63b91762010-04-15 17:02:09 -04001716 } else
1717 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1718
1719 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001720
1721 /* Determine divisor based on baud rate */
1722 baud = tty_get_baud_rate(tty);
1723 if (!baud) {
1724 /* pick a default, any default... */
1725 dbg("Picked default baud...");
1726 baud = 9600;
1727 }
1728
1729 if (baud >= 230400) {
1730 set_higher_rates(mos7720_port, baud);
1731 /* Enable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001732 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001733 return;
1734 }
1735
Harvey Harrison441b62c2008-03-03 16:08:34 -08001736 dbg("%s - baud rate = %d", __func__, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001737 status = send_cmd_write_baud_rate(mos7720_port, baud);
Alan Cox65d063a2008-01-03 17:01:18 +00001738 /* FIXME: needs to write actual resulting baud back not just
1739 blindly do so */
1740 if (cflag & CBAUD)
1741 tty_encode_baud_rate(tty, baud, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001742 /* Enable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001743 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001744
1745 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001746 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1747 if (status)
1748 dbg("usb_submit_urb(read bulk) failed, status = %d",
1749 status);
1750 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001751}
1752
1753/*
1754 * mos7720_set_termios
1755 * this function is called by the tty driver when it wants to change the
1756 * termios structure.
1757 */
Alan Cox95da3102008-07-22 11:09:07 +01001758static void mos7720_set_termios(struct tty_struct *tty,
1759 struct usb_serial_port *port, struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001760{
1761 int status;
1762 unsigned int cflag;
1763 struct usb_serial *serial;
1764 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001765
1766 serial = port->serial;
1767
1768 mos7720_port = usb_get_serial_port_data(port);
1769
1770 if (mos7720_port == NULL)
1771 return;
1772
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001773 if (!mos7720_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001774 dbg("%s - port not opened", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001775 return;
1776 }
1777
Mike Dunnb69578d2010-04-15 17:01:33 -04001778 dbg("%s\n", "setting termios - ASPIRE");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001779
1780 cflag = tty->termios->c_cflag;
1781
Harvey Harrison441b62c2008-03-03 16:08:34 -08001782 dbg("%s - cflag %08x iflag %08x", __func__,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001783 tty->termios->c_cflag,
1784 RELEVANT_IFLAG(tty->termios->c_iflag));
1785
Harvey Harrison441b62c2008-03-03 16:08:34 -08001786 dbg("%s - old cflag %08x old iflag %08x", __func__,
Alan Cox65d063a2008-01-03 17:01:18 +00001787 old_termios->c_cflag,
1788 RELEVANT_IFLAG(old_termios->c_iflag));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001789
Harvey Harrison441b62c2008-03-03 16:08:34 -08001790 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001791
1792 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001793 change_port_settings(tty, mos7720_port, old_termios);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001794
Alan Cox4da1a172008-07-22 11:16:21 +01001795 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001796 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1797 if (status)
1798 dbg("usb_submit_urb(read bulk) failed, status = %d",
1799 status);
1800 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001801}
1802
1803/*
1804 * get_lsr_info - get line status register info
1805 *
1806 * Purpose: Let user call ioctl() to get info when the UART physically
1807 * is emptied. On bus types like RS485, the transmitter must
1808 * release the bus after transmitting. This must be done when
1809 * the transmit shift register is empty, not be done when the
1810 * transmit holding register is empty. This functionality
1811 * allows an RS485 driver to be written in user space.
1812 */
Alan Cox4da1a172008-07-22 11:16:21 +01001813static int get_lsr_info(struct tty_struct *tty,
1814 struct moschip_port *mos7720_port, unsigned int __user *value)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001815{
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001816 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001817 unsigned int result = 0;
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001818 unsigned char data = 0;
1819 int port_number = port->number - port->serial->minor;
1820 int count;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001821
Alan Cox95da3102008-07-22 11:09:07 +01001822 count = mos7720_chars_in_buffer(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001823 if (count == 0) {
Mike Dunn63b91762010-04-15 17:02:09 -04001824 read_mos_reg(port->serial, port_number, LSR, &data);
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001825 if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1826 == (UART_LSR_TEMT | UART_LSR_THRE)) {
1827 dbg("%s -- Empty", __func__);
1828 result = TIOCSER_TEMT;
1829 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001830 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001831 if (copy_to_user(value, &result, sizeof(int)))
1832 return -EFAULT;
1833 return 0;
1834}
1835
Alan Cox60b33c12011-02-14 16:26:14 +00001836static int mos7720_tiocmget(struct tty_struct *tty)
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001837{
1838 struct usb_serial_port *port = tty->driver_data;
1839 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1840 unsigned int result = 0;
1841 unsigned int mcr ;
1842 unsigned int msr ;
1843
1844 dbg("%s - port %d", __func__, port->number);
1845
1846 mcr = mos7720_port->shadowMCR;
1847 msr = mos7720_port->shadowMSR;
1848
1849 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1850 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1851 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1852 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
1853 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1854 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
1855
1856 dbg("%s -- %x", __func__, result);
1857
1858 return result;
1859}
1860
Alan Cox20b9d172011-02-14 16:26:50 +00001861static int mos7720_tiocmset(struct tty_struct *tty,
Mike Dunn63b91762010-04-15 17:02:09 -04001862 unsigned int set, unsigned int clear)
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001863{
1864 struct usb_serial_port *port = tty->driver_data;
1865 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1866 unsigned int mcr ;
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001867 dbg("%s - port %d", __func__, port->number);
Alan Cox60b33c12011-02-14 16:26:14 +00001868 dbg("he was at tiocmset");
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001869
1870 mcr = mos7720_port->shadowMCR;
1871
1872 if (set & TIOCM_RTS)
1873 mcr |= UART_MCR_RTS;
1874 if (set & TIOCM_DTR)
1875 mcr |= UART_MCR_DTR;
1876 if (set & TIOCM_LOOP)
1877 mcr |= UART_MCR_LOOP;
1878
1879 if (clear & TIOCM_RTS)
1880 mcr &= ~UART_MCR_RTS;
1881 if (clear & TIOCM_DTR)
1882 mcr &= ~UART_MCR_DTR;
1883 if (clear & TIOCM_LOOP)
1884 mcr &= ~UART_MCR_LOOP;
1885
1886 mos7720_port->shadowMCR = mcr;
Mike Dunn63b91762010-04-15 17:02:09 -04001887 write_mos_reg(port->serial, port->number - port->serial->minor,
1888 MCR, mos7720_port->shadowMCR);
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001889
1890 return 0;
1891}
1892
Alan Cox0bca1b92010-09-16 18:21:40 +01001893static int mos7720_get_icount(struct tty_struct *tty,
1894 struct serial_icounter_struct *icount)
1895{
1896 struct usb_serial_port *port = tty->driver_data;
1897 struct moschip_port *mos7720_port;
1898 struct async_icount cnow;
1899
1900 mos7720_port = usb_get_serial_port_data(port);
1901 cnow = mos7720_port->icount;
1902
1903 icount->cts = cnow.cts;
1904 icount->dsr = cnow.dsr;
1905 icount->rng = cnow.rng;
1906 icount->dcd = cnow.dcd;
1907 icount->rx = cnow.rx;
1908 icount->tx = cnow.tx;
1909 icount->frame = cnow.frame;
1910 icount->overrun = cnow.overrun;
1911 icount->parity = cnow.parity;
1912 icount->brk = cnow.brk;
1913 icount->buf_overrun = cnow.buf_overrun;
1914
1915 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
1916 port->number, icount->rx, icount->tx);
1917 return 0;
1918}
1919
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001920static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1921 unsigned int __user *value)
1922{
Alan Cox0bca1b92010-09-16 18:21:40 +01001923 unsigned int mcr;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001924 unsigned int arg;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001925
1926 struct usb_serial_port *port;
1927
1928 if (mos7720_port == NULL)
1929 return -1;
1930
Alan Cox4da1a172008-07-22 11:16:21 +01001931 port = (struct usb_serial_port *)mos7720_port->port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001932 mcr = mos7720_port->shadowMCR;
1933
1934 if (copy_from_user(&arg, value, sizeof(int)))
1935 return -EFAULT;
1936
1937 switch (cmd) {
1938 case TIOCMBIS:
1939 if (arg & TIOCM_RTS)
1940 mcr |= UART_MCR_RTS;
1941 if (arg & TIOCM_DTR)
1942 mcr |= UART_MCR_RTS;
1943 if (arg & TIOCM_LOOP)
1944 mcr |= UART_MCR_LOOP;
1945 break;
1946
1947 case TIOCMBIC:
1948 if (arg & TIOCM_RTS)
1949 mcr &= ~UART_MCR_RTS;
1950 if (arg & TIOCM_DTR)
1951 mcr &= ~UART_MCR_RTS;
1952 if (arg & TIOCM_LOOP)
1953 mcr &= ~UART_MCR_LOOP;
1954 break;
1955
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001956 }
1957
1958 mos7720_port->shadowMCR = mcr;
Mike Dunn63b91762010-04-15 17:02:09 -04001959 write_mos_reg(port->serial, port->number - port->serial->minor,
1960 MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001961
1962 return 0;
1963}
1964
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001965static int get_serial_info(struct moschip_port *mos7720_port,
1966 struct serial_struct __user *retinfo)
1967{
1968 struct serial_struct tmp;
1969
1970 if (!retinfo)
1971 return -EFAULT;
1972
1973 memset(&tmp, 0, sizeof(tmp));
1974
1975 tmp.type = PORT_16550A;
1976 tmp.line = mos7720_port->port->serial->minor;
1977 tmp.port = mos7720_port->port->number;
1978 tmp.irq = 0;
1979 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
Alan Cox4da1a172008-07-22 11:16:21 +01001980 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001981 tmp.baud_base = 9600;
1982 tmp.close_delay = 5*HZ;
1983 tmp.closing_wait = 30*HZ;
1984
1985 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1986 return -EFAULT;
1987 return 0;
1988}
1989
Alan Cox00a0d0d2011-02-14 16:27:06 +00001990static int mos7720_ioctl(struct tty_struct *tty,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001991 unsigned int cmd, unsigned long arg)
1992{
Alan Cox95da3102008-07-22 11:09:07 +01001993 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001994 struct moschip_port *mos7720_port;
1995 struct async_icount cnow;
1996 struct async_icount cprev;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001997
1998 mos7720_port = usb_get_serial_port_data(port);
1999 if (mos7720_port == NULL)
2000 return -ENODEV;
2001
Harvey Harrison441b62c2008-03-03 16:08:34 -08002002 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002003
2004 switch (cmd) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002005 case TIOCSERGETLSR:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002006 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
Alan Cox4da1a172008-07-22 11:16:21 +01002007 return get_lsr_info(tty, mos7720_port,
2008 (unsigned int __user *)arg);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002009
Alan Cox95da3102008-07-22 11:09:07 +01002010 /* FIXME: These should be using the mode methods */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002011 case TIOCMBIS:
2012 case TIOCMBIC:
Alan Cox4da1a172008-07-22 11:16:21 +01002013 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET",
2014 __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002015 return set_modem_info(mos7720_port, cmd,
2016 (unsigned int __user *)arg);
2017
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002018 case TIOCGSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002019 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002020 return get_serial_info(mos7720_port,
2021 (struct serial_struct __user *)arg);
2022
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002023 case TIOCMIWAIT:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002024 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002025 cprev = mos7720_port->icount;
2026 while (1) {
2027 if (signal_pending(current))
2028 return -ERESTARTSYS;
2029 cnow = mos7720_port->icount;
2030 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2031 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2032 return -EIO; /* no change => error */
2033 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2034 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2035 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
Alan Cox4da1a172008-07-22 11:16:21 +01002036 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002037 return 0;
2038 }
2039 cprev = cnow;
2040 }
2041 /* NOTREACHED */
2042 break;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002043 }
2044
2045 return -ENOIOCTLCMD;
2046}
2047
2048static int mos7720_startup(struct usb_serial *serial)
2049{
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002050 struct moschip_port *mos7720_port;
2051 struct usb_device *dev;
2052 int i;
2053 char data;
Huzaifa Sidhpurwala91f58ae2011-02-21 12:58:45 +05302054 u16 product;
Mike Dunnb69578d2010-04-15 17:01:33 -04002055 int ret_val;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002056
Harvey Harrison441b62c2008-03-03 16:08:34 -08002057 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002058
2059 if (!serial) {
2060 dbg("Invalid Handler");
2061 return -ENODEV;
2062 }
2063
Huzaifa Sidhpurwala91f58ae2011-02-21 12:58:45 +05302064 product = le16_to_cpu(serial->dev->descriptor.idProduct);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002065 dev = serial->dev;
2066
Mike Dunnfb088e32010-01-26 12:12:12 -05002067 /*
2068 * The 7715 uses the first bulk in/out endpoint pair for the parallel
2069 * port, and the second for the serial port. Because the usbserial core
2070 * assumes both pairs are serial ports, we must engage in a bit of
2071 * subterfuge and swap the pointers for ports 0 and 1 in order to make
2072 * port 0 point to the serial port. However, both moschip devices use a
2073 * single interrupt-in endpoint for both ports (as mentioned a little
2074 * further down), and this endpoint was assigned to port 0. So after
2075 * the swap, we must copy the interrupt endpoint elements from port 1
2076 * (as newly assigned) to port 0, and null out port 1 pointers.
2077 */
2078 if (product == MOSCHIP_DEVICE_ID_7715) {
2079 struct usb_serial_port *tmp = serial->port[0];
2080 serial->port[0] = serial->port[1];
2081 serial->port[1] = tmp;
2082 serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb;
2083 serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer;
2084 serial->port[0]->interrupt_in_endpointAddress =
2085 tmp->interrupt_in_endpointAddress;
2086 serial->port[1]->interrupt_in_urb = NULL;
2087 serial->port[1]->interrupt_in_buffer = NULL;
2088 }
2089
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002090
Mike Dunnb69578d2010-04-15 17:01:33 -04002091 /* set up serial port private structures */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002092 for (i = 0; i < serial->num_ports; ++i) {
2093 mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2094 if (mos7720_port == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07002095 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002096 return -ENOMEM;
2097 }
2098
2099 /* Initialize all port interrupt end point to port 0 int
2100 * endpoint. Our device has only one interrupt endpoint
Mike Dunnfb088e32010-01-26 12:12:12 -05002101 * common to all ports */
Alan Cox4da1a172008-07-22 11:16:21 +01002102 serial->port[i]->interrupt_in_endpointAddress =
2103 serial->port[0]->interrupt_in_endpointAddress;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002104
2105 mos7720_port->port = serial->port[i];
2106 usb_set_serial_port_data(serial->port[i], mos7720_port);
2107
2108 dbg("port number is %d", serial->port[i]->number);
2109 dbg("serial number is %d", serial->minor);
2110 }
2111
2112
2113 /* setting configuration feature to one */
2114 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Johan Hovold2591cec2013-05-27 14:44:43 +02002115 (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5000);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002116
Mike Dunnb69578d2010-04-15 17:01:33 -04002117 /* start the interrupt urb */
2118 ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
2119 if (ret_val)
2120 dev_err(&dev->dev,
2121 "%s - Error %d submitting control urb\n",
2122 __func__, ret_val);
2123
2124#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
2125 if (product == MOSCHIP_DEVICE_ID_7715) {
2126 ret_val = mos7715_parport_init(serial);
2127 if (ret_val < 0)
2128 return ret_val;
2129 }
2130#endif
Alan Cox4da1a172008-07-22 11:16:21 +01002131 /* LSR For Port 1 */
Mike Dunn63b91762010-04-15 17:02:09 -04002132 read_mos_reg(serial, 0, LSR, &data);
Alan Cox4da1a172008-07-22 11:16:21 +01002133 dbg("LSR:%x", data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002134
2135 return 0;
2136}
2137
Alan Sternf9c99bb2009-06-02 11:53:55 -04002138static void mos7720_release(struct usb_serial *serial)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002139{
2140 int i;
2141
Mike Dunnb69578d2010-04-15 17:01:33 -04002142#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
2143 /* close the parallel port */
2144
2145 if (le16_to_cpu(serial->dev->descriptor.idProduct)
2146 == MOSCHIP_DEVICE_ID_7715) {
2147 struct urbtracker *urbtrack;
2148 unsigned long flags;
2149 struct mos7715_parport *mos_parport =
2150 usb_get_serial_data(serial);
2151
2152 /* prevent NULL ptr dereference in port callbacks */
2153 spin_lock(&release_lock);
2154 mos_parport->pp->private_data = NULL;
2155 spin_unlock(&release_lock);
2156
2157 /* wait for synchronous usb calls to return */
2158 if (mos_parport->msg_pending)
2159 wait_for_completion_timeout(&mos_parport->syncmsg_compl,
Johan Hovold2591cec2013-05-27 14:44:43 +02002160 msecs_to_jiffies(MOS_WDR_TIMEOUT));
Mike Dunnb69578d2010-04-15 17:01:33 -04002161
2162 parport_remove_port(mos_parport->pp);
2163 usb_set_serial_data(serial, NULL);
2164 mos_parport->serial = NULL;
2165
2166 /* if tasklet currently scheduled, wait for it to complete */
2167 tasklet_kill(&mos_parport->urb_tasklet);
2168
2169 /* unlink any urbs sent by the tasklet */
2170 spin_lock_irqsave(&mos_parport->listlock, flags);
2171 list_for_each_entry(urbtrack,
2172 &mos_parport->active_urbs,
2173 urblist_entry)
2174 usb_unlink_urb(urbtrack->urb);
2175 spin_unlock_irqrestore(&mos_parport->listlock, flags);
2176
2177 kref_put(&mos_parport->ref_count, destroy_mos_parport);
2178 }
2179#endif
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002180 /* free private structure allocated for serial port */
Alan Sternf9c99bb2009-06-02 11:53:55 -04002181 for (i = 0; i < serial->num_ports; ++i)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002182 kfree(usb_get_serial_port_data(serial->port[i]));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002183}
2184
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002185static struct usb_driver usb_driver = {
2186 .name = "moschip7720",
2187 .probe = usb_serial_probe,
2188 .disconnect = usb_serial_disconnect,
2189 .id_table = moschip_port_id_table,
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002190};
2191
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002192static struct usb_serial_driver moschip7720_2port_driver = {
2193 .driver = {
2194 .owner = THIS_MODULE,
2195 .name = "moschip7720",
2196 },
2197 .description = "Moschip 2 port adapter",
2198 .id_table = moschip_port_id_table,
Mike Dunnfb088e32010-01-26 12:12:12 -05002199 .calc_num_ports = mos77xx_calc_num_ports,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002200 .open = mos7720_open,
2201 .close = mos7720_close,
2202 .throttle = mos7720_throttle,
2203 .unthrottle = mos7720_unthrottle,
Mike Dunnfb088e32010-01-26 12:12:12 -05002204 .probe = mos77xx_probe,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002205 .attach = mos7720_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002206 .release = mos7720_release,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002207 .ioctl = mos7720_ioctl,
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07002208 .tiocmget = mos7720_tiocmget,
2209 .tiocmset = mos7720_tiocmset,
Alan Cox0bca1b92010-09-16 18:21:40 +01002210 .get_icount = mos7720_get_icount,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002211 .set_termios = mos7720_set_termios,
2212 .write = mos7720_write,
2213 .write_room = mos7720_write_room,
2214 .chars_in_buffer = mos7720_chars_in_buffer,
2215 .break_ctl = mos7720_break,
2216 .read_bulk_callback = mos7720_bulk_in_callback,
Mike Dunnfb088e32010-01-26 12:12:12 -05002217 .read_int_callback = NULL /* dynamically assigned in probe() */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002218};
2219
Alan Stern4d2a7af2012-02-23 14:57:09 -05002220static struct usb_serial_driver * const serial_drivers[] = {
2221 &moschip7720_2port_driver, NULL
2222};
2223
Greg Kroah-Hartman964e2b82012-02-28 13:12:07 -08002224module_usb_serial_driver(usb_driver, serial_drivers);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002225
Alan Cox4da1a172008-07-22 11:16:21 +01002226MODULE_AUTHOR(DRIVER_AUTHOR);
2227MODULE_DESCRIPTION(DRIVER_DESC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002228MODULE_LICENSE("GPL");
2229
2230module_param(debug, bool, S_IRUGO | S_IWUSR);
2231MODULE_PARM_DESC(debug, "Debug enabled or not");