blob: 31a00f97d9649f803cff226e0de6e141440df927 [file] [log] [blame]
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001/*
2 * mos7720.c
3 * Controls the Moschip 7720 usb to dual port serial convertor
4 *
5 * Copyright 2006 Moschip Semiconductor Tech. Ltd.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, version 2 of the License.
10 *
11 * Developed by:
Greg Kroah-Hartman50d2dc72007-06-25 01:08:01 -070012 * Vijaya Kumar <vijaykumar.gn@gmail.com>
13 * Ajay Kumar <naanuajay@yahoo.com>
14 * Gurudeva <ngurudeva@yahoo.com>
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070015 *
16 * Cleaned up from the original by:
17 * Greg Kroah-Hartman <gregkh@suse.de>
18 *
19 * Originally based on drivers/usb/serial/io_edgeport.c which is:
20 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
21 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
22 */
23#include <linux/kernel.h>
24#include <linux/errno.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/tty.h>
28#include <linux/tty_driver.h>
29#include <linux/tty_flip.h>
30#include <linux/module.h>
31#include <linux/spinlock.h>
32#include <linux/serial.h>
33#include <linux/serial_reg.h>
34#include <linux/usb.h>
35#include <linux/usb/serial.h>
Alan Cox4da1a172008-07-22 11:16:21 +010036#include <linux/uaccess.h>
Mike Dunnb69578d2010-04-15 17:01:33 -040037#include <linux/parport.h>
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070038
39/*
40 * Version Information
41 */
Mike Dunn63b91762010-04-15 17:02:09 -040042#define DRIVER_VERSION "2.1"
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070043#define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
44#define DRIVER_DESC "Moschip USB Serial Driver"
45
46/* default urb timeout */
47#define MOS_WDR_TIMEOUT (HZ * 5)
48
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070049#define MOS_MAX_PORT 0x02
50#define MOS_WRITE 0x0E
51#define MOS_READ 0x0D
52
53/* Interrupt Rotinue Defines */
54#define SERIAL_IIR_RLS 0x06
55#define SERIAL_IIR_RDA 0x04
56#define SERIAL_IIR_CTI 0x0c
57#define SERIAL_IIR_THR 0x02
58#define SERIAL_IIR_MS 0x00
59
60#define NUM_URBS 16 /* URB Count */
61#define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
62
Mike Dunnb69578d2010-04-15 17:01:33 -040063/* This structure holds all of the local serial port information */
Alan Cox4da1a172008-07-22 11:16:21 +010064struct moschip_port {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070065 __u8 shadowLCR; /* last LCR value received */
66 __u8 shadowMCR; /* last MCR value received */
67 __u8 shadowMSR; /* last MSR value received */
68 char open;
69 struct async_icount icount;
70 struct usb_serial_port *port; /* loop back to the owner */
71 struct urb *write_urb_pool[NUM_URBS];
72};
73
Rusty Russell90ab5ee2012-01-13 09:32:20 +103074static bool debug;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070075
Mike Dunnfb088e32010-01-26 12:12:12 -050076static struct usb_serial_driver moschip7720_2port_driver;
77
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070078#define USB_VENDOR_ID_MOSCHIP 0x9710
79#define MOSCHIP_DEVICE_ID_7720 0x7720
80#define MOSCHIP_DEVICE_ID_7715 0x7715
81
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;
100};
101
102enum mos7715_pp_modes {
103 SPP = 0<<5,
104 PS2 = 1<<5, /* moschip calls this 'NIBBLE' mode */
105 PPF = 2<<5, /* moschip calls this 'CB-FIFO mode */
106};
107
108struct mos7715_parport {
109 struct parport *pp; /* back to containing struct */
110 struct kref ref_count; /* to instance of this struct */
111 struct list_head deferred_urbs; /* list deferred async urbs */
112 struct list_head active_urbs; /* list async urbs in flight */
113 spinlock_t listlock; /* protects list access */
114 bool msg_pending; /* usb sync call pending */
115 struct completion syncmsg_compl; /* usb sync call completed */
116 struct tasklet_struct urb_tasklet; /* for sending deferred urbs */
117 struct usb_serial *serial; /* back to containing struct */
118 __u8 shadowECR; /* parallel port regs... */
119 __u8 shadowDCR;
120 atomic_t shadowDSR; /* updated in int-in callback */
121};
122
123/* lock guards against dereferencing NULL ptr in parport ops callbacks */
124static DEFINE_SPINLOCK(release_lock);
125
Mike Dunn63b91762010-04-15 17:02:09 -0400126#endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
127
128static const unsigned int dummy; /* for clarity in register access fns */
129
Mike Dunnb69578d2010-04-15 17:01:33 -0400130enum mos_regs {
131 THR, /* serial port regs */
132 RHR,
133 IER,
134 FCR,
135 ISR,
136 LCR,
137 MCR,
138 LSR,
139 MSR,
140 SPR,
141 DLL,
142 DLM,
143 DPR, /* parallel port regs */
144 DSR,
145 DCR,
146 ECR,
147 SP1_REG, /* device control regs */
148 SP2_REG, /* serial port 2 (7720 only) */
149 PP_REG,
150 SP_CONTROL_REG,
151};
152
153/*
154 * Return the correct value for the Windex field of the setup packet
155 * for a control endpoint message. See the 7715 datasheet.
156 */
157static inline __u16 get_reg_index(enum mos_regs reg)
158{
159 static const __u16 mos7715_index_lookup_table[] = {
160 0x00, /* THR */
161 0x00, /* RHR */
162 0x01, /* IER */
163 0x02, /* FCR */
164 0x02, /* ISR */
165 0x03, /* LCR */
166 0x04, /* MCR */
167 0x05, /* LSR */
168 0x06, /* MSR */
169 0x07, /* SPR */
170 0x00, /* DLL */
171 0x01, /* DLM */
172 0x00, /* DPR */
173 0x01, /* DSR */
174 0x02, /* DCR */
175 0x0a, /* ECR */
176 0x01, /* SP1_REG */
177 0x02, /* SP2_REG (7720 only) */
178 0x04, /* PP_REG (7715 only) */
179 0x08, /* SP_CONTROL_REG */
180 };
181 return mos7715_index_lookup_table[reg];
182}
183
184/*
185 * Return the correct value for the upper byte of the Wvalue field of
186 * the setup packet for a control endpoint message.
187 */
Mike Dunn63b91762010-04-15 17:02:09 -0400188static inline __u16 get_reg_value(enum mos_regs reg,
189 unsigned int serial_portnum)
Mike Dunnb69578d2010-04-15 17:01:33 -0400190{
191 if (reg >= SP1_REG) /* control reg */
192 return 0x0000;
Mike Dunn63b91762010-04-15 17:02:09 -0400193
194 else if (reg >= DPR) /* parallel port reg (7715 only) */
Mike Dunnb69578d2010-04-15 17:01:33 -0400195 return 0x0100;
Mike Dunn63b91762010-04-15 17:02:09 -0400196
197 else /* serial port reg */
198 return (serial_portnum + 2) << 8;
Mike Dunnb69578d2010-04-15 17:01:33 -0400199}
200
201/*
202 * Write data byte to the specified device register. The data is embedded in
Mike Dunn63b91762010-04-15 17:02:09 -0400203 * the value field of the setup packet. serial_portnum is ignored for registers
204 * not specific to a particular serial port.
Mike Dunnb69578d2010-04-15 17:01:33 -0400205 */
Mike Dunn63b91762010-04-15 17:02:09 -0400206static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
207 enum mos_regs reg, __u8 data)
Mike Dunnb69578d2010-04-15 17:01:33 -0400208{
Mike Dunnb69578d2010-04-15 17:01:33 -0400209 struct usb_device *usbdev = serial->dev;
210 unsigned int pipe = usb_sndctrlpipe(usbdev, 0);
211 __u8 request = (__u8)0x0e;
212 __u8 requesttype = (__u8)0x40;
Mike Dunnb69578d2010-04-15 17:01:33 -0400213 __u16 index = get_reg_index(reg);
Mike Dunn63b91762010-04-15 17:02:09 -0400214 __u16 value = get_reg_value(reg, serial_portnum) + data;
215 int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
216 index, NULL, 0, MOS_WDR_TIMEOUT);
Mike Dunnb69578d2010-04-15 17:01:33 -0400217 if (status < 0)
218 dev_err(&usbdev->dev,
219 "mos7720: usb_control_msg() failed: %d", status);
220 return status;
221}
222
223/*
224 * Read data byte from the specified device register. The data returned by the
Mike Dunn63b91762010-04-15 17:02:09 -0400225 * device is embedded in the value field of the setup packet. serial_portnum is
226 * ignored for registers that are not specific to a particular serial port.
Mike Dunnb69578d2010-04-15 17:01:33 -0400227 */
Mike Dunn63b91762010-04-15 17:02:09 -0400228static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
229 enum mos_regs reg, __u8 *data)
Mike Dunnb69578d2010-04-15 17:01:33 -0400230{
Mike Dunn63b91762010-04-15 17:02:09 -0400231 struct usb_device *usbdev = serial->dev;
Mike Dunnb69578d2010-04-15 17:01:33 -0400232 unsigned int pipe = usb_rcvctrlpipe(usbdev, 0);
233 __u8 request = (__u8)0x0d;
234 __u8 requesttype = (__u8)0xc0;
Mike Dunnb69578d2010-04-15 17:01:33 -0400235 __u16 index = get_reg_index(reg);
Mike Dunn63b91762010-04-15 17:02:09 -0400236 __u16 value = get_reg_value(reg, serial_portnum);
Johan Hovoldf856f082013-05-27 14:44:39 +0200237 u8 *buf;
238 int status;
239
240 buf = kmalloc(1, GFP_KERNEL);
241 if (!buf)
242 return -ENOMEM;
243
244 status = usb_control_msg(usbdev, pipe, request, requesttype, value,
245 index, buf, 1, MOS_WDR_TIMEOUT);
246 if (status == 1)
247 *data = *buf;
248 else if (status < 0)
Mike Dunnb69578d2010-04-15 17:01:33 -0400249 dev_err(&usbdev->dev,
250 "mos7720: usb_control_msg() failed: %d", status);
Johan Hovoldf856f082013-05-27 14:44:39 +0200251 kfree(buf);
252
Mike Dunnb69578d2010-04-15 17:01:33 -0400253 return status;
254}
255
Mike Dunn63b91762010-04-15 17:02:09 -0400256#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
257
Mike Dunnb69578d2010-04-15 17:01:33 -0400258static inline int mos7715_change_mode(struct mos7715_parport *mos_parport,
259 enum mos7715_pp_modes mode)
260{
261 mos_parport->shadowECR = mode;
Mike Dunn63b91762010-04-15 17:02:09 -0400262 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400263 return 0;
264}
265
266static void destroy_mos_parport(struct kref *kref)
267{
268 struct mos7715_parport *mos_parport =
269 container_of(kref, struct mos7715_parport, ref_count);
270
271 dbg("%s called", __func__);
272 kfree(mos_parport);
273}
274
275static void destroy_urbtracker(struct kref *kref)
276{
277 struct urbtracker *urbtrack =
278 container_of(kref, struct urbtracker, ref_count);
279 struct mos7715_parport *mos_parport = urbtrack->mos_parport;
280 dbg("%s called", __func__);
281 usb_free_urb(urbtrack->urb);
282 kfree(urbtrack);
283 kref_put(&mos_parport->ref_count, destroy_mos_parport);
284}
285
286/*
287 * This runs as a tasklet when sending an urb in a non-blocking parallel
288 * port callback had to be deferred because the disconnect mutex could not be
289 * obtained at the time.
290 */
291static void send_deferred_urbs(unsigned long _mos_parport)
292{
293 int ret_val;
294 unsigned long flags;
295 struct mos7715_parport *mos_parport = (void *)_mos_parport;
296 struct urbtracker *urbtrack;
297 struct list_head *cursor, *next;
298
299 dbg("%s called", __func__);
300
301 /* if release function ran, game over */
302 if (unlikely(mos_parport->serial == NULL))
303 return;
304
305 /* try again to get the mutex */
306 if (!mutex_trylock(&mos_parport->serial->disc_mutex)) {
307 dbg("%s: rescheduling tasklet", __func__);
308 tasklet_schedule(&mos_parport->urb_tasklet);
309 return;
310 }
311
312 /* if device disconnected, game over */
313 if (unlikely(mos_parport->serial->disconnected)) {
314 mutex_unlock(&mos_parport->serial->disc_mutex);
315 return;
316 }
317
318 spin_lock_irqsave(&mos_parport->listlock, flags);
319 if (list_empty(&mos_parport->deferred_urbs)) {
320 spin_unlock_irqrestore(&mos_parport->listlock, flags);
321 mutex_unlock(&mos_parport->serial->disc_mutex);
322 dbg("%s: deferred_urbs list empty", __func__);
323 return;
324 }
325
326 /* move contents of deferred_urbs list to active_urbs list and submit */
327 list_for_each_safe(cursor, next, &mos_parport->deferred_urbs)
328 list_move_tail(cursor, &mos_parport->active_urbs);
329 list_for_each_entry(urbtrack, &mos_parport->active_urbs,
330 urblist_entry) {
331 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
332 dbg("%s: urb submitted", __func__);
333 if (ret_val) {
334 dev_err(&mos_parport->serial->dev->dev,
335 "usb_submit_urb() failed: %d", ret_val);
336 list_del(&urbtrack->urblist_entry);
337 kref_put(&urbtrack->ref_count, destroy_urbtracker);
338 }
339 }
340 spin_unlock_irqrestore(&mos_parport->listlock, flags);
341 mutex_unlock(&mos_parport->serial->disc_mutex);
342}
343
344/* callback for parallel port control urbs submitted asynchronously */
345static void async_complete(struct urb *urb)
346{
347 struct urbtracker *urbtrack = urb->context;
348 int status = urb->status;
349 dbg("%s called", __func__);
350 if (unlikely(status))
351 dbg("%s - nonzero urb status received: %d", __func__, status);
352
353 /* remove the urbtracker from the active_urbs list */
354 spin_lock(&urbtrack->mos_parport->listlock);
355 list_del(&urbtrack->urblist_entry);
356 spin_unlock(&urbtrack->mos_parport->listlock);
357 kref_put(&urbtrack->ref_count, destroy_urbtracker);
358}
359
360static int write_parport_reg_nonblock(struct mos7715_parport *mos_parport,
361 enum mos_regs reg, __u8 data)
362{
363 struct urbtracker *urbtrack;
364 int ret_val;
365 unsigned long flags;
366 struct usb_ctrlrequest setup;
367 struct usb_serial *serial = mos_parport->serial;
368 struct usb_device *usbdev = serial->dev;
369 dbg("%s called", __func__);
370
371 /* create and initialize the control urb and containing urbtracker */
372 urbtrack = kmalloc(sizeof(struct urbtracker), GFP_ATOMIC);
373 if (urbtrack == NULL) {
374 dev_err(&usbdev->dev, "out of memory");
375 return -ENOMEM;
376 }
377 kref_get(&mos_parport->ref_count);
378 urbtrack->mos_parport = mos_parport;
379 urbtrack->urb = usb_alloc_urb(0, GFP_ATOMIC);
380 if (urbtrack->urb == NULL) {
381 dev_err(&usbdev->dev, "out of urbs");
382 kfree(urbtrack);
383 return -ENOMEM;
384 }
385 setup.bRequestType = (__u8)0x40;
386 setup.bRequest = (__u8)0x0e;
Mike Dunn63b91762010-04-15 17:02:09 -0400387 setup.wValue = get_reg_value(reg, dummy);
Mike Dunnb69578d2010-04-15 17:01:33 -0400388 setup.wIndex = get_reg_index(reg);
389 setup.wLength = 0;
390 usb_fill_control_urb(urbtrack->urb, usbdev,
391 usb_sndctrlpipe(usbdev, 0),
392 (unsigned char *)&setup,
393 NULL, 0, async_complete, urbtrack);
394 kref_init(&urbtrack->ref_count);
395 INIT_LIST_HEAD(&urbtrack->urblist_entry);
396
397 /*
398 * get the disconnect mutex, or add tracker to the deferred_urbs list
399 * and schedule a tasklet to try again later
400 */
401 if (!mutex_trylock(&serial->disc_mutex)) {
402 spin_lock_irqsave(&mos_parport->listlock, flags);
403 list_add_tail(&urbtrack->urblist_entry,
404 &mos_parport->deferred_urbs);
405 spin_unlock_irqrestore(&mos_parport->listlock, flags);
406 tasklet_schedule(&mos_parport->urb_tasklet);
407 dbg("tasklet scheduled");
408 return 0;
409 }
410
411 /* bail if device disconnected */
412 if (serial->disconnected) {
413 kref_put(&urbtrack->ref_count, destroy_urbtracker);
414 mutex_unlock(&serial->disc_mutex);
415 return -ENODEV;
416 }
417
418 /* add the tracker to the active_urbs list and submit */
419 spin_lock_irqsave(&mos_parport->listlock, flags);
420 list_add_tail(&urbtrack->urblist_entry, &mos_parport->active_urbs);
421 spin_unlock_irqrestore(&mos_parport->listlock, flags);
422 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
423 mutex_unlock(&serial->disc_mutex);
424 if (ret_val) {
425 dev_err(&usbdev->dev,
426 "%s: submit_urb() failed: %d", __func__, ret_val);
427 spin_lock_irqsave(&mos_parport->listlock, flags);
428 list_del(&urbtrack->urblist_entry);
429 spin_unlock_irqrestore(&mos_parport->listlock, flags);
430 kref_put(&urbtrack->ref_count, destroy_urbtracker);
431 return ret_val;
432 }
433 return 0;
434}
435
436/*
437 * This is the the common top part of all parallel port callback operations that
438 * send synchronous messages to the device. This implements convoluted locking
439 * that avoids two scenarios: (1) a port operation is called after usbserial
440 * has called our release function, at which point struct mos7715_parport has
441 * been destroyed, and (2) the device has been disconnected, but usbserial has
442 * not called the release function yet because someone has a serial port open.
443 * The shared release_lock prevents the first, and the mutex and disconnected
444 * flag maintained by usbserial covers the second. We also use the msg_pending
445 * flag to ensure that all synchronous usb messgage calls have completed before
446 * our release function can return.
447 */
448static int parport_prologue(struct parport *pp)
449{
450 struct mos7715_parport *mos_parport;
451
452 spin_lock(&release_lock);
453 mos_parport = pp->private_data;
454 if (unlikely(mos_parport == NULL)) {
455 /* release fn called, port struct destroyed */
456 spin_unlock(&release_lock);
457 return -1;
458 }
459 mos_parport->msg_pending = true; /* synch usb call pending */
460 INIT_COMPLETION(mos_parport->syncmsg_compl);
461 spin_unlock(&release_lock);
462
463 mutex_lock(&mos_parport->serial->disc_mutex);
464 if (mos_parport->serial->disconnected) {
465 /* device disconnected */
466 mutex_unlock(&mos_parport->serial->disc_mutex);
467 mos_parport->msg_pending = false;
468 complete(&mos_parport->syncmsg_compl);
469 return -1;
470 }
471
472 return 0;
473}
474
475/*
476 * This is the the common bottom part of all parallel port functions that send
477 * synchronous messages to the device.
478 */
479static inline void parport_epilogue(struct parport *pp)
480{
481 struct mos7715_parport *mos_parport = pp->private_data;
482 mutex_unlock(&mos_parport->serial->disc_mutex);
483 mos_parport->msg_pending = false;
484 complete(&mos_parport->syncmsg_compl);
485}
486
487static void parport_mos7715_write_data(struct parport *pp, unsigned char d)
488{
489 struct mos7715_parport *mos_parport = pp->private_data;
490 dbg("%s called: %2.2x", __func__, d);
491 if (parport_prologue(pp) < 0)
492 return;
493 mos7715_change_mode(mos_parport, SPP);
Mike Dunn63b91762010-04-15 17:02:09 -0400494 write_mos_reg(mos_parport->serial, dummy, DPR, (__u8)d);
Mike Dunnb69578d2010-04-15 17:01:33 -0400495 parport_epilogue(pp);
496}
497
498static unsigned char parport_mos7715_read_data(struct parport *pp)
499{
500 struct mos7715_parport *mos_parport = pp->private_data;
501 unsigned char d;
502 dbg("%s called", __func__);
503 if (parport_prologue(pp) < 0)
504 return 0;
Mike Dunn63b91762010-04-15 17:02:09 -0400505 read_mos_reg(mos_parport->serial, dummy, DPR, &d);
Mike Dunnb69578d2010-04-15 17:01:33 -0400506 parport_epilogue(pp);
507 return d;
508}
509
510static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
511{
512 struct mos7715_parport *mos_parport = pp->private_data;
513 __u8 data;
514 dbg("%s called: %2.2x", __func__, d);
515 if (parport_prologue(pp) < 0)
516 return;
517 data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
Mike Dunn63b91762010-04-15 17:02:09 -0400518 write_mos_reg(mos_parport->serial, dummy, DCR, data);
Mike Dunnb69578d2010-04-15 17:01:33 -0400519 mos_parport->shadowDCR = data;
520 parport_epilogue(pp);
521}
522
523static unsigned char parport_mos7715_read_control(struct parport *pp)
524{
525 struct mos7715_parport *mos_parport = pp->private_data;
526 __u8 dcr;
527 dbg("%s called", __func__);
528 spin_lock(&release_lock);
529 mos_parport = pp->private_data;
530 if (unlikely(mos_parport == NULL)) {
531 spin_unlock(&release_lock);
532 return 0;
533 }
534 dcr = mos_parport->shadowDCR & 0x0f;
535 spin_unlock(&release_lock);
536 return dcr;
537}
538
539static unsigned char parport_mos7715_frob_control(struct parport *pp,
540 unsigned char mask,
541 unsigned char val)
542{
543 struct mos7715_parport *mos_parport = pp->private_data;
544 __u8 dcr;
545 dbg("%s called", __func__);
546 mask &= 0x0f;
547 val &= 0x0f;
548 if (parport_prologue(pp) < 0)
549 return 0;
550 mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
Mike Dunn63b91762010-04-15 17:02:09 -0400551 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400552 dcr = mos_parport->shadowDCR & 0x0f;
553 parport_epilogue(pp);
554 return dcr;
555}
556
557static unsigned char parport_mos7715_read_status(struct parport *pp)
558{
559 unsigned char status;
560 struct mos7715_parport *mos_parport = pp->private_data;
561 dbg("%s called", __func__);
562 spin_lock(&release_lock);
563 mos_parport = pp->private_data;
564 if (unlikely(mos_parport == NULL)) { /* release called */
565 spin_unlock(&release_lock);
566 return 0;
567 }
568 status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
569 spin_unlock(&release_lock);
570 return status;
571}
572
573static void parport_mos7715_enable_irq(struct parport *pp)
574{
575 dbg("%s called", __func__);
576}
577static void parport_mos7715_disable_irq(struct parport *pp)
578{
579 dbg("%s called", __func__);
580}
581
582static void parport_mos7715_data_forward(struct parport *pp)
583{
584 struct mos7715_parport *mos_parport = pp->private_data;
585 dbg("%s called", __func__);
586 if (parport_prologue(pp) < 0)
587 return;
588 mos7715_change_mode(mos_parport, PS2);
589 mos_parport->shadowDCR &= ~0x20;
Mike Dunn63b91762010-04-15 17:02:09 -0400590 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400591 parport_epilogue(pp);
592}
593
594static void parport_mos7715_data_reverse(struct parport *pp)
595{
596 struct mos7715_parport *mos_parport = pp->private_data;
597 dbg("%s called", __func__);
598 if (parport_prologue(pp) < 0)
599 return;
600 mos7715_change_mode(mos_parport, PS2);
601 mos_parport->shadowDCR |= 0x20;
Mike Dunn63b91762010-04-15 17:02:09 -0400602 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400603 parport_epilogue(pp);
604}
605
606static void parport_mos7715_init_state(struct pardevice *dev,
607 struct parport_state *s)
608{
609 dbg("%s called", __func__);
610 s->u.pc.ctr = DCR_INIT_VAL;
611 s->u.pc.ecr = ECR_INIT_VAL;
612}
613
614/* N.B. Parport core code requires that this function not block */
615static void parport_mos7715_save_state(struct parport *pp,
616 struct parport_state *s)
617{
618 struct mos7715_parport *mos_parport;
619 dbg("%s called", __func__);
620 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 s->u.pc.ctr = mos_parport->shadowDCR;
627 s->u.pc.ecr = mos_parport->shadowECR;
628 spin_unlock(&release_lock);
629}
630
631/* N.B. Parport core code requires that this function not block */
632static void parport_mos7715_restore_state(struct parport *pp,
633 struct parport_state *s)
634{
635 struct mos7715_parport *mos_parport;
636 dbg("%s called", __func__);
637 spin_lock(&release_lock);
638 mos_parport = pp->private_data;
639 if (unlikely(mos_parport == NULL)) { /* release called */
640 spin_unlock(&release_lock);
641 return;
642 }
643 write_parport_reg_nonblock(mos_parport, DCR, mos_parport->shadowDCR);
644 write_parport_reg_nonblock(mos_parport, ECR, mos_parport->shadowECR);
645 spin_unlock(&release_lock);
646}
647
648static size_t parport_mos7715_write_compat(struct parport *pp,
649 const void *buffer,
650 size_t len, int flags)
651{
652 int retval;
653 struct mos7715_parport *mos_parport = pp->private_data;
654 int actual_len;
655 dbg("%s called: %u chars", __func__, (unsigned int)len);
656 if (parport_prologue(pp) < 0)
657 return 0;
658 mos7715_change_mode(mos_parport, PPF);
659 retval = usb_bulk_msg(mos_parport->serial->dev,
660 usb_sndbulkpipe(mos_parport->serial->dev, 2),
661 (void *)buffer, len, &actual_len,
662 MOS_WDR_TIMEOUT);
663 parport_epilogue(pp);
664 if (retval) {
665 dev_err(&mos_parport->serial->dev->dev,
666 "mos7720: usb_bulk_msg() failed: %d", retval);
667 return 0;
668 }
669 return actual_len;
670}
671
672static struct parport_operations parport_mos7715_ops = {
673 .owner = THIS_MODULE,
674 .write_data = parport_mos7715_write_data,
675 .read_data = parport_mos7715_read_data,
676
677 .write_control = parport_mos7715_write_control,
678 .read_control = parport_mos7715_read_control,
679 .frob_control = parport_mos7715_frob_control,
680
681 .read_status = parport_mos7715_read_status,
682
683 .enable_irq = parport_mos7715_enable_irq,
684 .disable_irq = parport_mos7715_disable_irq,
685
686 .data_forward = parport_mos7715_data_forward,
687 .data_reverse = parport_mos7715_data_reverse,
688
689 .init_state = parport_mos7715_init_state,
690 .save_state = parport_mos7715_save_state,
691 .restore_state = parport_mos7715_restore_state,
692
693 .compat_write_data = parport_mos7715_write_compat,
694
695 .nibble_read_data = parport_ieee1284_read_nibble,
696 .byte_read_data = parport_ieee1284_read_byte,
697};
698
699/*
700 * Allocate and initialize parallel port control struct, initialize
701 * the parallel port hardware device, and register with the parport subsystem.
702 */
703static int mos7715_parport_init(struct usb_serial *serial)
704{
705 struct mos7715_parport *mos_parport;
706
707 /* allocate and initialize parallel port control struct */
708 mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
709 if (mos_parport == NULL) {
710 dbg("mos7715_parport_init: kzalloc failed");
711 return -ENOMEM;
712 }
713 mos_parport->msg_pending = false;
714 kref_init(&mos_parport->ref_count);
715 spin_lock_init(&mos_parport->listlock);
716 INIT_LIST_HEAD(&mos_parport->active_urbs);
717 INIT_LIST_HEAD(&mos_parport->deferred_urbs);
718 usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
719 mos_parport->serial = serial;
720 tasklet_init(&mos_parport->urb_tasklet, send_deferred_urbs,
721 (unsigned long) mos_parport);
722 init_completion(&mos_parport->syncmsg_compl);
723
724 /* cycle parallel port reset bit */
Mike Dunn63b91762010-04-15 17:02:09 -0400725 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x80);
726 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x00);
Mike Dunnb69578d2010-04-15 17:01:33 -0400727
728 /* initialize device registers */
729 mos_parport->shadowDCR = DCR_INIT_VAL;
Mike Dunn63b91762010-04-15 17:02:09 -0400730 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400731 mos_parport->shadowECR = ECR_INIT_VAL;
Mike Dunn63b91762010-04-15 17:02:09 -0400732 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
Mike Dunnb69578d2010-04-15 17:01:33 -0400733
734 /* register with parport core */
735 mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE,
736 PARPORT_DMA_NONE,
737 &parport_mos7715_ops);
738 if (mos_parport->pp == NULL) {
739 dev_err(&serial->interface->dev,
740 "Could not register parport\n");
741 kref_put(&mos_parport->ref_count, destroy_mos_parport);
742 return -EIO;
743 }
744 mos_parport->pp->private_data = mos_parport;
745 mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP;
746 mos_parport->pp->dev = &serial->interface->dev;
747 parport_announce_port(mos_parport->pp);
748
749 return 0;
750}
751#endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700752
753/*
754 * mos7720_interrupt_callback
755 * this is the callback function for when we have received data on the
756 * interrupt endpoint.
757 */
758static void mos7720_interrupt_callback(struct urb *urb)
759{
760 int result;
761 int length;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700762 int status = urb->status;
Oliver Neukum325b70c2007-03-19 13:58:29 +0100763 __u8 *data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700764 __u8 sp1;
765 __u8 sp2;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700766
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700767 switch (status) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700768 case 0:
769 /* success */
770 break;
771 case -ECONNRESET:
772 case -ENOENT:
773 case -ESHUTDOWN:
774 /* this urb is terminated, clean up */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800775 dbg("%s - urb shutting down with status: %d", __func__,
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700776 status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700777 return;
778 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800779 dbg("%s - nonzero urb status received: %d", __func__,
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700780 status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700781 goto exit;
782 }
783
784 length = urb->actual_length;
785 data = urb->transfer_buffer;
786
787 /* Moschip get 4 bytes
788 * Byte 1 IIR Port 1 (port.number is 0)
789 * Byte 2 IIR Port 2 (port.number is 1)
790 * Byte 3 --------------
791 * Byte 4 FIFO status for both */
Oliver Neukum325b70c2007-03-19 13:58:29 +0100792
793 /* the above description is inverted
794 * oneukum 2007-03-14 */
795
796 if (unlikely(length != 4)) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700797 dbg("Wrong data !!!");
798 return;
799 }
800
Oliver Neukum325b70c2007-03-19 13:58:29 +0100801 sp1 = data[3];
802 sp2 = data[2];
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700803
Oliver Neukum325b70c2007-03-19 13:58:29 +0100804 if ((sp1 | sp2) & 0x01) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700805 /* No Interrupt Pending in both the ports */
806 dbg("No Interrupt !!!");
807 } else {
808 switch (sp1 & 0x0f) {
809 case SERIAL_IIR_RLS:
810 dbg("Serial Port 1: Receiver status error or address "
811 "bit detected in 9-bit mode\n");
812 break;
813 case SERIAL_IIR_CTI:
814 dbg("Serial Port 1: Receiver time out");
815 break;
816 case SERIAL_IIR_MS:
Mike Dunnb69578d2010-04-15 17:01:33 -0400817 /* dbg("Serial Port 1: Modem status change"); */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700818 break;
819 }
820
821 switch (sp2 & 0x0f) {
822 case SERIAL_IIR_RLS:
823 dbg("Serial Port 2: Receiver status error or address "
824 "bit detected in 9-bit mode");
825 break;
826 case SERIAL_IIR_CTI:
827 dbg("Serial Port 2: Receiver time out");
828 break;
829 case SERIAL_IIR_MS:
Mike Dunnb69578d2010-04-15 17:01:33 -0400830 /* dbg("Serial Port 2: Modem status change"); */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700831 break;
832 }
833 }
834
835exit:
836 result = usb_submit_urb(urb, GFP_ATOMIC);
837 if (result)
838 dev_err(&urb->dev->dev,
839 "%s - Error %d submitting control urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800840 __func__, result);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700841}
842
843/*
Mike Dunnfb088e32010-01-26 12:12:12 -0500844 * mos7715_interrupt_callback
845 * this is the 7715's callback function for when we have received data on
846 * the interrupt endpoint.
847 */
848static void mos7715_interrupt_callback(struct urb *urb)
849{
850 int result;
851 int length;
852 int status = urb->status;
853 __u8 *data;
854 __u8 iir;
855
856 switch (status) {
857 case 0:
858 /* success */
859 break;
860 case -ECONNRESET:
861 case -ENOENT:
862 case -ESHUTDOWN:
Mike Dunnb69578d2010-04-15 17:01:33 -0400863 case -ENODEV:
Mike Dunnfb088e32010-01-26 12:12:12 -0500864 /* this urb is terminated, clean up */
865 dbg("%s - urb shutting down with status: %d", __func__,
866 status);
867 return;
868 default:
869 dbg("%s - nonzero urb status received: %d", __func__,
870 status);
871 goto exit;
872 }
873
874 length = urb->actual_length;
875 data = urb->transfer_buffer;
876
877 /* Structure of data from 7715 device:
878 * Byte 1: IIR serial Port
879 * Byte 2: unused
880 * Byte 2: DSR parallel port
881 * Byte 4: FIFO status for both */
882
883 if (unlikely(length != 4)) {
884 dbg("Wrong data !!!");
885 return;
886 }
887
888 iir = data[0];
889 if (!(iir & 0x01)) { /* serial port interrupt pending */
890 switch (iir & 0x0f) {
891 case SERIAL_IIR_RLS:
892 dbg("Serial Port: Receiver status error or address "
893 "bit detected in 9-bit mode\n");
894 break;
895 case SERIAL_IIR_CTI:
896 dbg("Serial Port: Receiver time out");
897 break;
898 case SERIAL_IIR_MS:
Mike Dunnb69578d2010-04-15 17:01:33 -0400899 /* dbg("Serial Port: Modem status change"); */
Mike Dunnfb088e32010-01-26 12:12:12 -0500900 break;
901 }
902 }
903
Mike Dunnb69578d2010-04-15 17:01:33 -0400904#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
905 { /* update local copy of DSR reg */
906 struct usb_serial_port *port = urb->context;
907 struct mos7715_parport *mos_parport = port->serial->private;
908 if (unlikely(mos_parport == NULL))
909 return;
910 atomic_set(&mos_parport->shadowDSR, data[2]);
911 }
912#endif
913
Mike Dunnfb088e32010-01-26 12:12:12 -0500914exit:
915 result = usb_submit_urb(urb, GFP_ATOMIC);
916 if (result)
917 dev_err(&urb->dev->dev,
918 "%s - Error %d submitting control urb\n",
919 __func__, result);
Mike Dunnfb088e32010-01-26 12:12:12 -0500920}
921
922/*
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700923 * mos7720_bulk_in_callback
924 * this is the callback function for when we have received data on the
925 * bulk in endpoint.
926 */
927static void mos7720_bulk_in_callback(struct urb *urb)
928{
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700929 int retval;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700930 unsigned char *data ;
931 struct usb_serial_port *port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700932 struct tty_struct *tty;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700933 int status = urb->status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700934
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700935 if (status) {
936 dbg("nonzero read bulk status received: %d", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700937 return;
938 }
939
Mike Dunnb69578d2010-04-15 17:01:33 -0400940 port = urb->context;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700941
Harvey Harrison441b62c2008-03-03 16:08:34 -0800942 dbg("Entering...%s", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700943
944 data = urb->transfer_buffer;
945
Alan Cox4a90f092008-10-13 10:39:46 +0100946 tty = tty_port_tty_get(&port->port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700947 if (tty && urb->actual_length) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700948 tty_insert_flip_string(tty, data, urb->actual_length);
949 tty_flip_buffer_push(tty);
950 }
Alan Cox4a90f092008-10-13 10:39:46 +0100951 tty_kref_put(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700952
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700953 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700954 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
955 if (retval)
956 dbg("usb_submit_urb(read bulk) failed, retval = %d",
957 retval);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700958 }
959}
960
961/*
962 * mos7720_bulk_out_data_callback
963 * this is the callback function for when we have finished sending serial
964 * data on the bulk out endpoint.
965 */
966static void mos7720_bulk_out_data_callback(struct urb *urb)
967{
968 struct moschip_port *mos7720_port;
969 struct tty_struct *tty;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700970 int status = urb->status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700971
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700972 if (status) {
973 dbg("nonzero write bulk status received:%d", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700974 return;
975 }
976
977 mos7720_port = urb->context;
978 if (!mos7720_port) {
979 dbg("NULL mos7720_port pointer");
980 return ;
981 }
982
Alan Cox4a90f092008-10-13 10:39:46 +0100983 tty = tty_port_tty_get(&mos7720_port->port->port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700984
Jiri Slabyb963a842007-02-10 01:44:55 -0800985 if (tty && mos7720_port->open)
986 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100987 tty_kref_put(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700988}
989
990/*
Mike Dunnfb088e32010-01-26 12:12:12 -0500991 * mos77xx_probe
992 * this function installs the appropriate read interrupt endpoint callback
993 * depending on whether the device is a 7720 or 7715, thus avoiding costly
994 * run-time checks in the high-frequency callback routine itself.
995 */
996static int mos77xx_probe(struct usb_serial *serial,
997 const struct usb_device_id *id)
998{
999 if (id->idProduct == MOSCHIP_DEVICE_ID_7715)
1000 moschip7720_2port_driver.read_int_callback =
1001 mos7715_interrupt_callback;
1002 else
1003 moschip7720_2port_driver.read_int_callback =
1004 mos7720_interrupt_callback;
1005
1006 return 0;
1007}
1008
1009static int mos77xx_calc_num_ports(struct usb_serial *serial)
1010{
1011 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
1012 if (product == MOSCHIP_DEVICE_ID_7715)
1013 return 1;
1014
1015 return 2;
1016}
1017
Alan Coxa509a7e2009-09-19 13:13:26 -07001018static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001019{
1020 struct usb_serial *serial;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001021 struct urb *urb;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001022 struct moschip_port *mos7720_port;
1023 int response;
1024 int port_number;
Mike Dunn63b91762010-04-15 17:02:09 -04001025 __u8 data;
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001026 int allocated_urbs = 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001027 int j;
1028
1029 serial = port->serial;
1030
1031 mos7720_port = usb_get_serial_port_data(port);
1032 if (mos7720_port == NULL)
1033 return -ENODEV;
1034
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001035 usb_clear_halt(serial->dev, port->write_urb->pipe);
1036 usb_clear_halt(serial->dev, port->read_urb->pipe);
1037
1038 /* Initialising the write urb pool */
1039 for (j = 0; j < NUM_URBS; ++j) {
Alan Cox4da1a172008-07-22 11:16:21 +01001040 urb = usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001041 mos7720_port->write_urb_pool[j] = urb;
1042
1043 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001044 dev_err(&port->dev, "No more urbs???\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001045 continue;
1046 }
1047
1048 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1049 GFP_KERNEL);
1050 if (!urb->transfer_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001051 dev_err(&port->dev,
1052 "%s-out of memory for urb buffers.\n",
1053 __func__);
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001054 usb_free_urb(mos7720_port->write_urb_pool[j]);
1055 mos7720_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001056 continue;
1057 }
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001058 allocated_urbs++;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001059 }
1060
Oliver Neukumfe4b65e2007-03-14 15:22:25 +01001061 if (!allocated_urbs)
1062 return -ENOMEM;
1063
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001064 /* Initialize MCS7720 -- Write Init values to corresponding Registers
1065 *
1066 * Register Index
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001067 * 0 : THR/RHR
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001068 * 1 : IER
1069 * 2 : FCR
1070 * 3 : LCR
1071 * 4 : MCR
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001072 * 5 : LSR
1073 * 6 : MSR
1074 * 7 : SPR
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001075 *
1076 * 0x08 : SP1/2 Control Reg
1077 */
1078 port_number = port->number - port->serial->minor;
Mike Dunn63b91762010-04-15 17:02:09 -04001079 read_mos_reg(serial, port_number, LSR, &data);
1080
Joe Perches759f3632010-02-05 16:50:08 -08001081 dbg("SS::%p LSR:%x", mos7720_port, data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001082
1083 dbg("Check:Sending Command ..........");
1084
Mike Dunn63b91762010-04-15 17:02:09 -04001085 write_mos_reg(serial, dummy, SP1_REG, 0x02);
1086 write_mos_reg(serial, dummy, SP2_REG, 0x02);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001087
Mike Dunn63b91762010-04-15 17:02:09 -04001088 write_mos_reg(serial, port_number, IER, 0x00);
1089 write_mos_reg(serial, port_number, FCR, 0x00);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001090
Mike Dunn63b91762010-04-15 17:02:09 -04001091 write_mos_reg(serial, port_number, FCR, 0xcf);
1092 mos7720_port->shadowLCR = 0x03;
1093 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1094 mos7720_port->shadowMCR = 0x0b;
1095 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001096
Mike Dunn63b91762010-04-15 17:02:09 -04001097 write_mos_reg(serial, port_number, SP_CONTROL_REG, 0x00);
1098 read_mos_reg(serial, dummy, SP_CONTROL_REG, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001099 data = data | (port->number - port->serial->minor + 1);
Mike Dunn63b91762010-04-15 17:02:09 -04001100 write_mos_reg(serial, dummy, SP_CONTROL_REG, data);
1101 mos7720_port->shadowLCR = 0x83;
1102 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1103 write_mos_reg(serial, port_number, THR, 0x0c);
1104 write_mos_reg(serial, port_number, IER, 0x00);
1105 mos7720_port->shadowLCR = 0x03;
1106 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1107 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001108
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001109 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
1110 if (response)
Alan Cox4da1a172008-07-22 11:16:21 +01001111 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
1112 __func__, response);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001113
1114 /* initialize our icount structure */
1115 memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
1116
1117 /* initialize our port settings */
1118 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
1119
1120 /* send a open port command */
1121 mos7720_port->open = 1;
1122
1123 return 0;
1124}
1125
1126/*
1127 * mos7720_chars_in_buffer
1128 * this function is called by the tty driver when it wants to know how many
1129 * bytes of data we currently have outstanding in the port (data that has
1130 * been written, but hasn't made it out the port yet)
1131 * If successful, we return the number of bytes left to be written in the
1132 * system,
1133 * Otherwise we return a negative error number.
1134 */
Alan Cox95da3102008-07-22 11:09:07 +01001135static int mos7720_chars_in_buffer(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001136{
Alan Cox95da3102008-07-22 11:09:07 +01001137 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001138 int i;
1139 int chars = 0;
1140 struct moschip_port *mos7720_port;
1141
Harvey Harrison441b62c2008-03-03 16:08:34 -08001142 dbg("%s:entering ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001143
1144 mos7720_port = usb_get_serial_port_data(port);
1145 if (mos7720_port == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001146 dbg("%s:leaving ...........", __func__);
Alan Cox23198fd2009-07-20 16:05:27 +01001147 return 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001148 }
1149
1150 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001151 if (mos7720_port->write_urb_pool[i] &&
1152 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001153 chars += URB_TRANSFER_BUFFER_SIZE;
1154 }
Harvey Harrison441b62c2008-03-03 16:08:34 -08001155 dbg("%s - returns %d", __func__, chars);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001156 return chars;
1157}
1158
Alan Cox335f8512009-06-11 12:26:29 +01001159static void mos7720_close(struct usb_serial_port *port)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001160{
1161 struct usb_serial *serial;
1162 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001163 int j;
1164
1165 dbg("mos7720_close:entering...");
1166
1167 serial = port->serial;
1168
1169 mos7720_port = usb_get_serial_port_data(port);
1170 if (mos7720_port == NULL)
1171 return;
1172
1173 for (j = 0; j < NUM_URBS; ++j)
1174 usb_kill_urb(mos7720_port->write_urb_pool[j]);
1175
1176 /* Freeing Write URBs */
1177 for (j = 0; j < NUM_URBS; ++j) {
1178 if (mos7720_port->write_urb_pool[j]) {
1179 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
1180 usb_free_urb(mos7720_port->write_urb_pool[j]);
1181 }
1182 }
1183
1184 /* While closing port, shutdown all bulk read, write *
Oliver Neukuma1cd7e92008-01-16 17:18:52 +01001185 * and interrupt read if they exists, otherwise nop */
1186 dbg("Shutdown bulk write");
1187 usb_kill_urb(port->write_urb);
1188 dbg("Shutdown bulk read");
1189 usb_kill_urb(port->read_urb);
1190
1191 mutex_lock(&serial->disc_mutex);
1192 /* these commands must not be issued if the device has
1193 * been disconnected */
1194 if (!serial->disconnected) {
Mike Dunn63b91762010-04-15 17:02:09 -04001195 write_mos_reg(serial, port->number - port->serial->minor,
1196 MCR, 0x00);
1197 write_mos_reg(serial, port->number - port->serial->minor,
1198 IER, 0x00);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001199 }
Oliver Neukuma1cd7e92008-01-16 17:18:52 +01001200 mutex_unlock(&serial->disc_mutex);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001201 mos7720_port->open = 0;
1202
Harvey Harrison441b62c2008-03-03 16:08:34 -08001203 dbg("Leaving %s", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001204}
1205
Alan Cox95da3102008-07-22 11:09:07 +01001206static void mos7720_break(struct tty_struct *tty, int break_state)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001207{
Alan Cox95da3102008-07-22 11:09:07 +01001208 struct usb_serial_port *port = tty->driver_data;
Alan Cox4da1a172008-07-22 11:16:21 +01001209 unsigned char data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001210 struct usb_serial *serial;
1211 struct moschip_port *mos7720_port;
1212
Harvey Harrison441b62c2008-03-03 16:08:34 -08001213 dbg("Entering %s", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001214
1215 serial = port->serial;
1216
1217 mos7720_port = usb_get_serial_port_data(port);
1218 if (mos7720_port == NULL)
1219 return;
1220
1221 if (break_state == -1)
1222 data = mos7720_port->shadowLCR | UART_LCR_SBC;
1223 else
1224 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
1225
1226 mos7720_port->shadowLCR = data;
Mike Dunn63b91762010-04-15 17:02:09 -04001227 write_mos_reg(serial, port->number - port->serial->minor,
1228 LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001229}
1230
1231/*
1232 * mos7720_write_room
1233 * this function is called by the tty driver when it wants to know how many
1234 * bytes of data we can accept for a specific port.
1235 * If successful, we return the amount of room that we have for this port
1236 * Otherwise we return a negative error number.
1237 */
Alan Cox95da3102008-07-22 11:09:07 +01001238static int mos7720_write_room(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001239{
Alan Cox95da3102008-07-22 11:09:07 +01001240 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001241 struct moschip_port *mos7720_port;
1242 int room = 0;
1243 int i;
1244
Harvey Harrison441b62c2008-03-03 16:08:34 -08001245 dbg("%s:entering ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001246
1247 mos7720_port = usb_get_serial_port_data(port);
1248 if (mos7720_port == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001249 dbg("%s:leaving ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001250 return -ENODEV;
1251 }
1252
Alan Coxa5b6f602008-04-08 17:16:06 +01001253 /* FIXME: Locking */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001254 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001255 if (mos7720_port->write_urb_pool[i] &&
1256 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001257 room += URB_TRANSFER_BUFFER_SIZE;
1258 }
1259
Harvey Harrison441b62c2008-03-03 16:08:34 -08001260 dbg("%s - returns %d", __func__, room);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001261 return room;
1262}
1263
Alan Cox95da3102008-07-22 11:09:07 +01001264static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
1265 const unsigned char *data, int count)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001266{
1267 int status;
1268 int i;
1269 int bytes_sent = 0;
1270 int transfer_size;
1271
1272 struct moschip_port *mos7720_port;
1273 struct usb_serial *serial;
1274 struct urb *urb;
1275 const unsigned char *current_position = data;
1276
Harvey Harrison441b62c2008-03-03 16:08:34 -08001277 dbg("%s:entering ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001278
1279 serial = port->serial;
1280
1281 mos7720_port = usb_get_serial_port_data(port);
1282 if (mos7720_port == NULL) {
1283 dbg("mos7720_port is NULL");
1284 return -ENODEV;
1285 }
1286
1287 /* try to find a free urb in the list */
1288 urb = NULL;
1289
1290 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +01001291 if (mos7720_port->write_urb_pool[i] &&
1292 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001293 urb = mos7720_port->write_urb_pool[i];
Alan Cox4da1a172008-07-22 11:16:21 +01001294 dbg("URB:%d", i);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001295 break;
1296 }
1297 }
1298
1299 if (urb == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001300 dbg("%s - no more free urbs", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001301 goto exit;
1302 }
1303
1304 if (urb->transfer_buffer == NULL) {
1305 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1306 GFP_KERNEL);
1307 if (urb->transfer_buffer == NULL) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001308 dev_err_console(port, "%s no more kernel memory...\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001309 __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001310 goto exit;
1311 }
1312 }
Alan Cox4da1a172008-07-22 11:16:21 +01001313 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001314
1315 memcpy(urb->transfer_buffer, current_position, transfer_size);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001316 usb_serial_debug_data(debug, &port->dev, __func__, transfer_size,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001317 urb->transfer_buffer);
1318
1319 /* fill urb with data and submit */
1320 usb_fill_bulk_urb(urb, serial->dev,
1321 usb_sndbulkpipe(serial->dev,
Alan Cox4da1a172008-07-22 11:16:21 +01001322 port->bulk_out_endpointAddress),
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001323 urb->transfer_buffer, transfer_size,
1324 mos7720_bulk_out_data_callback, mos7720_port);
1325
1326 /* send it down the pipe */
Alan Cox4da1a172008-07-22 11:16:21 +01001327 status = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001328 if (status) {
Johan Hovold22a416c2012-02-10 13:20:51 +01001329 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001330 "with status = %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001331 bytes_sent = status;
1332 goto exit;
1333 }
1334 bytes_sent = transfer_size;
1335
1336exit:
1337 return bytes_sent;
1338}
1339
Alan Cox95da3102008-07-22 11:09:07 +01001340static void mos7720_throttle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001341{
Alan Cox95da3102008-07-22 11:09:07 +01001342 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001343 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001344 int status;
1345
Joe Perches759f3632010-02-05 16:50:08 -08001346 dbg("%s- port %d", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001347
1348 mos7720_port = usb_get_serial_port_data(port);
1349
1350 if (mos7720_port == NULL)
1351 return;
1352
1353 if (!mos7720_port->open) {
1354 dbg("port not opened");
1355 return;
1356 }
1357
Harvey Harrison441b62c2008-03-03 16:08:34 -08001358 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001359
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001360 /* if we are implementing XON/XOFF, send the stop character */
1361 if (I_IXOFF(tty)) {
1362 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001363 status = mos7720_write(tty, port, &stop_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001364 if (status <= 0)
1365 return;
1366 }
1367
1368 /* if we are implementing RTS/CTS, toggle that line */
1369 if (tty->termios->c_cflag & CRTSCTS) {
1370 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
Mike Dunn63b91762010-04-15 17:02:09 -04001371 write_mos_reg(port->serial, port->number - port->serial->minor,
1372 MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001373 if (status != 0)
1374 return;
1375 }
1376}
1377
Alan Cox95da3102008-07-22 11:09:07 +01001378static void mos7720_unthrottle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001379{
Alan Cox95da3102008-07-22 11:09:07 +01001380 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001381 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +01001382 int status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001383
1384 if (mos7720_port == NULL)
1385 return;
1386
1387 if (!mos7720_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001388 dbg("%s - port not opened", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001389 return;
1390 }
1391
Harvey Harrison441b62c2008-03-03 16:08:34 -08001392 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001393
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001394 /* if we are implementing XON/XOFF, send the start character */
1395 if (I_IXOFF(tty)) {
1396 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +01001397 status = mos7720_write(tty, port, &start_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001398 if (status <= 0)
1399 return;
1400 }
1401
1402 /* if we are implementing RTS/CTS, toggle that line */
1403 if (tty->termios->c_cflag & CRTSCTS) {
1404 mos7720_port->shadowMCR |= UART_MCR_RTS;
Mike Dunn63b91762010-04-15 17:02:09 -04001405 write_mos_reg(port->serial, port->number - port->serial->minor,
1406 MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001407 if (status != 0)
1408 return;
1409 }
1410}
1411
Mike Dunnb69578d2010-04-15 17:01:33 -04001412/* FIXME: this function does not work */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001413static int set_higher_rates(struct moschip_port *mos7720_port,
1414 unsigned int baud)
1415{
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001416 struct usb_serial_port *port;
1417 struct usb_serial *serial;
1418 int port_number;
Mike Dunn63b91762010-04-15 17:02:09 -04001419 enum mos_regs sp_reg;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001420 if (mos7720_port == NULL)
1421 return -EINVAL;
1422
1423 port = mos7720_port->port;
1424 serial = port->serial;
1425
Alan Cox4da1a172008-07-22 11:16:21 +01001426 /***********************************************
1427 * Init Sequence for higher rates
1428 ***********************************************/
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001429 dbg("Sending Setting Commands ..........");
1430 port_number = port->number - port->serial->minor;
1431
Mike Dunn63b91762010-04-15 17:02:09 -04001432 write_mos_reg(serial, port_number, IER, 0x00);
1433 write_mos_reg(serial, port_number, FCR, 0x00);
1434 write_mos_reg(serial, port_number, FCR, 0xcf);
1435 mos7720_port->shadowMCR = 0x0b;
1436 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1437 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x00);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001438
Alan Cox4da1a172008-07-22 11:16:21 +01001439 /***********************************************
1440 * Set for higher rates *
1441 ***********************************************/
Mike Dunnb69578d2010-04-15 17:01:33 -04001442 /* writing baud rate verbatum into uart clock field clearly not right */
Mike Dunn63b91762010-04-15 17:02:09 -04001443 if (port_number == 0)
1444 sp_reg = SP1_REG;
1445 else
1446 sp_reg = SP2_REG;
1447 write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
1448 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x03);
1449 mos7720_port->shadowMCR = 0x2b;
1450 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001451
Alan Cox4da1a172008-07-22 11:16:21 +01001452 /***********************************************
1453 * Set DLL/DLM
1454 ***********************************************/
Mike Dunn63b91762010-04-15 17:02:09 -04001455 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1456 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1457 write_mos_reg(serial, port_number, DLL, 0x01);
1458 write_mos_reg(serial, port_number, DLM, 0x00);
1459 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1460 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001461
1462 return 0;
1463}
1464
1465/* baud rate information */
Alan Cox4da1a172008-07-22 11:16:21 +01001466struct divisor_table_entry {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001467 __u32 baudrate;
1468 __u16 divisor;
1469};
1470
1471/* Define table of divisors for moschip 7720 hardware *
1472 * These assume a 3.6864MHz crystal, the standard /16, and *
1473 * MCR.7 = 0. */
1474static struct divisor_table_entry divisor_table[] = {
1475 { 50, 2304},
1476 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
1477 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
1478 { 150, 768},
1479 { 300, 384},
1480 { 600, 192},
1481 { 1200, 96},
1482 { 1800, 64},
1483 { 2400, 48},
1484 { 4800, 24},
1485 { 7200, 16},
1486 { 9600, 12},
1487 { 19200, 6},
1488 { 38400, 3},
1489 { 57600, 2},
1490 { 115200, 1},
1491};
1492
1493/*****************************************************************************
1494 * calc_baud_rate_divisor
1495 * this function calculates the proper baud rate divisor for the specified
1496 * baud rate.
1497 *****************************************************************************/
1498static int calc_baud_rate_divisor(int baudrate, int *divisor)
1499{
1500 int i;
1501 __u16 custom;
1502 __u16 round1;
1503 __u16 round;
1504
1505
Harvey Harrison441b62c2008-03-03 16:08:34 -08001506 dbg("%s - %d", __func__, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001507
1508 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
1509 if (divisor_table[i].baudrate == baudrate) {
1510 *divisor = divisor_table[i].divisor;
1511 return 0;
1512 }
1513 }
1514
Alan Cox4da1a172008-07-22 11:16:21 +01001515 /* After trying for all the standard baud rates *
1516 * Try calculating the divisor for this baud rate */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001517 if (baudrate > 75 && baudrate < 230400) {
1518 /* get the divisor */
1519 custom = (__u16)(230400L / baudrate);
1520
1521 /* Check for round off */
1522 round1 = (__u16)(2304000L / baudrate);
1523 round = (__u16)(round1 - (custom * 10));
1524 if (round > 4)
1525 custom++;
1526 *divisor = custom;
1527
Alan Cox4da1a172008-07-22 11:16:21 +01001528 dbg("Baud %d = %d", baudrate, custom);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001529 return 0;
1530 }
1531
1532 dbg("Baud calculation Failed...");
1533 return -EINVAL;
1534}
1535
1536/*
1537 * send_cmd_write_baud_rate
1538 * this function sends the proper command to change the baud rate of the
1539 * specified port.
1540 */
1541static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1542 int baudrate)
1543{
1544 struct usb_serial_port *port;
1545 struct usb_serial *serial;
1546 int divisor;
1547 int status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001548 unsigned char number;
1549
1550 if (mos7720_port == NULL)
1551 return -1;
1552
1553 port = mos7720_port->port;
1554 serial = port->serial;
1555
Harvey Harrison441b62c2008-03-03 16:08:34 -08001556 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001557
1558 number = port->number - port->serial->minor;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001559 dbg("%s - port = %d, baud = %d", __func__, port->number, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001560
Alan Cox4da1a172008-07-22 11:16:21 +01001561 /* Calculate the Divisor */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001562 status = calc_baud_rate_divisor(baudrate, &divisor);
1563 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001564 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001565 return status;
1566 }
1567
Alan Cox4da1a172008-07-22 11:16:21 +01001568 /* Enable access to divisor latch */
Mike Dunn63b91762010-04-15 17:02:09 -04001569 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1570 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001571
1572 /* Write the divisor */
Mike Dunn63b91762010-04-15 17:02:09 -04001573 write_mos_reg(serial, number, DLL, (__u8)(divisor & 0xff));
1574 write_mos_reg(serial, number, DLM, (__u8)((divisor & 0xff00) >> 8));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001575
Alan Cox4da1a172008-07-22 11:16:21 +01001576 /* Disable access to divisor latch */
Mike Dunn63b91762010-04-15 17:02:09 -04001577 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1578 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001579
1580 return status;
1581}
1582
1583/*
1584 * change_port_settings
1585 * This routine is called to set the UART on the device to match
1586 * the specified new settings.
1587 */
Alan Cox95da3102008-07-22 11:09:07 +01001588static void change_port_settings(struct tty_struct *tty,
1589 struct moschip_port *mos7720_port,
Alan Cox606d0992006-12-08 02:38:45 -08001590 struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001591{
1592 struct usb_serial_port *port;
1593 struct usb_serial *serial;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001594 int baud;
1595 unsigned cflag;
1596 unsigned iflag;
1597 __u8 mask = 0xff;
1598 __u8 lData;
1599 __u8 lParity;
1600 __u8 lStop;
1601 int status;
1602 int port_number;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001603
1604 if (mos7720_port == NULL)
1605 return ;
1606
1607 port = mos7720_port->port;
1608 serial = port->serial;
1609 port_number = port->number - port->serial->minor;
1610
Harvey Harrison441b62c2008-03-03 16:08:34 -08001611 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001612
1613 if (!mos7720_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001614 dbg("%s - port not opened", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001615 return;
1616 }
1617
Harvey Harrison441b62c2008-03-03 16:08:34 -08001618 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001619
1620 lData = UART_LCR_WLEN8;
1621 lStop = 0x00; /* 1 stop bit */
1622 lParity = 0x00; /* No parity */
1623
1624 cflag = tty->termios->c_cflag;
1625 iflag = tty->termios->c_iflag;
1626
1627 /* Change the number of bits */
1628 switch (cflag & CSIZE) {
1629 case CS5:
1630 lData = UART_LCR_WLEN5;
1631 mask = 0x1f;
1632 break;
1633
1634 case CS6:
1635 lData = UART_LCR_WLEN6;
1636 mask = 0x3f;
1637 break;
1638
1639 case CS7:
1640 lData = UART_LCR_WLEN7;
1641 mask = 0x7f;
1642 break;
1643 default:
1644 case CS8:
1645 lData = UART_LCR_WLEN8;
1646 break;
1647 }
1648
1649 /* Change the Parity bit */
1650 if (cflag & PARENB) {
1651 if (cflag & PARODD) {
1652 lParity = UART_LCR_PARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001653 dbg("%s - parity = odd", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001654 } else {
1655 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001656 dbg("%s - parity = even", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001657 }
1658
1659 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001660 dbg("%s - parity = none", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001661 }
1662
1663 if (cflag & CMSPAR)
1664 lParity = lParity | 0x20;
1665
1666 /* Change the Stop bit */
1667 if (cflag & CSTOPB) {
1668 lStop = UART_LCR_STOP;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001669 dbg("%s - stop bits = 2", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001670 } else {
1671 lStop = 0x00;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001672 dbg("%s - stop bits = 1", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001673 }
1674
1675#define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1676#define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1677#define LCR_PAR_MASK 0x38 /* Mask for parity field */
1678
1679 /* Update the LCR with the correct value */
Alan Cox4da1a172008-07-22 11:16:21 +01001680 mos7720_port->shadowLCR &=
Mike Dunn63b91762010-04-15 17:02:09 -04001681 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001682 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1683
1684
1685 /* Disable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001686 write_mos_reg(serial, port_number, IER, 0x00);
1687 write_mos_reg(serial, port_number, FCR, 0x00);
1688 write_mos_reg(serial, port_number, FCR, 0xcf);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001689
1690 /* Send the updated LCR value to the mos7720 */
Mike Dunn63b91762010-04-15 17:02:09 -04001691 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1692 mos7720_port->shadowMCR = 0x0b;
1693 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001694
1695 /* set up the MCR register and send it to the mos7720 */
1696 mos7720_port->shadowMCR = UART_MCR_OUT2;
1697 if (cflag & CBAUD)
1698 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1699
1700 if (cflag & CRTSCTS) {
1701 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
Alan Cox4da1a172008-07-22 11:16:21 +01001702 /* To set hardware flow control to the specified *
1703 * serial port, in SP1/2_CONTROL_REG */
Mike Dunn63b91762010-04-15 17:02:09 -04001704 if (port->number)
1705 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x01);
1706 else
1707 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x02);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001708
Mike Dunn63b91762010-04-15 17:02:09 -04001709 } else
1710 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1711
1712 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001713
1714 /* Determine divisor based on baud rate */
1715 baud = tty_get_baud_rate(tty);
1716 if (!baud) {
1717 /* pick a default, any default... */
1718 dbg("Picked default baud...");
1719 baud = 9600;
1720 }
1721
1722 if (baud >= 230400) {
1723 set_higher_rates(mos7720_port, baud);
1724 /* Enable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001725 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001726 return;
1727 }
1728
Harvey Harrison441b62c2008-03-03 16:08:34 -08001729 dbg("%s - baud rate = %d", __func__, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001730 status = send_cmd_write_baud_rate(mos7720_port, baud);
Alan Cox65d063a2008-01-03 17:01:18 +00001731 /* FIXME: needs to write actual resulting baud back not just
1732 blindly do so */
1733 if (cflag & CBAUD)
1734 tty_encode_baud_rate(tty, baud, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001735 /* Enable Interrupts */
Mike Dunn63b91762010-04-15 17:02:09 -04001736 write_mos_reg(serial, port_number, IER, 0x0c);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001737
1738 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001739 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1740 if (status)
1741 dbg("usb_submit_urb(read bulk) failed, status = %d",
1742 status);
1743 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001744}
1745
1746/*
1747 * mos7720_set_termios
1748 * this function is called by the tty driver when it wants to change the
1749 * termios structure.
1750 */
Alan Cox95da3102008-07-22 11:09:07 +01001751static void mos7720_set_termios(struct tty_struct *tty,
1752 struct usb_serial_port *port, struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001753{
1754 int status;
1755 unsigned int cflag;
1756 struct usb_serial *serial;
1757 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001758
1759 serial = port->serial;
1760
1761 mos7720_port = usb_get_serial_port_data(port);
1762
1763 if (mos7720_port == NULL)
1764 return;
1765
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001766 if (!mos7720_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001767 dbg("%s - port not opened", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001768 return;
1769 }
1770
Mike Dunnb69578d2010-04-15 17:01:33 -04001771 dbg("%s\n", "setting termios - ASPIRE");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001772
1773 cflag = tty->termios->c_cflag;
1774
Harvey Harrison441b62c2008-03-03 16:08:34 -08001775 dbg("%s - cflag %08x iflag %08x", __func__,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001776 tty->termios->c_cflag,
1777 RELEVANT_IFLAG(tty->termios->c_iflag));
1778
Harvey Harrison441b62c2008-03-03 16:08:34 -08001779 dbg("%s - old cflag %08x old iflag %08x", __func__,
Alan Cox65d063a2008-01-03 17:01:18 +00001780 old_termios->c_cflag,
1781 RELEVANT_IFLAG(old_termios->c_iflag));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001782
Harvey Harrison441b62c2008-03-03 16:08:34 -08001783 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001784
1785 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001786 change_port_settings(tty, mos7720_port, old_termios);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001787
Alan Cox4da1a172008-07-22 11:16:21 +01001788 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001789 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1790 if (status)
1791 dbg("usb_submit_urb(read bulk) failed, status = %d",
1792 status);
1793 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001794}
1795
1796/*
1797 * get_lsr_info - get line status register info
1798 *
1799 * Purpose: Let user call ioctl() to get info when the UART physically
1800 * is emptied. On bus types like RS485, the transmitter must
1801 * release the bus after transmitting. This must be done when
1802 * the transmit shift register is empty, not be done when the
1803 * transmit holding register is empty. This functionality
1804 * allows an RS485 driver to be written in user space.
1805 */
Alan Cox4da1a172008-07-22 11:16:21 +01001806static int get_lsr_info(struct tty_struct *tty,
1807 struct moschip_port *mos7720_port, unsigned int __user *value)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001808{
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001809 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001810 unsigned int result = 0;
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001811 unsigned char data = 0;
1812 int port_number = port->number - port->serial->minor;
1813 int count;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001814
Alan Cox95da3102008-07-22 11:09:07 +01001815 count = mos7720_chars_in_buffer(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001816 if (count == 0) {
Mike Dunn63b91762010-04-15 17:02:09 -04001817 read_mos_reg(port->serial, port_number, LSR, &data);
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001818 if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1819 == (UART_LSR_TEMT | UART_LSR_THRE)) {
1820 dbg("%s -- Empty", __func__);
1821 result = TIOCSER_TEMT;
1822 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001823 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001824 if (copy_to_user(value, &result, sizeof(int)))
1825 return -EFAULT;
1826 return 0;
1827}
1828
Alan Cox60b33c12011-02-14 16:26:14 +00001829static int mos7720_tiocmget(struct tty_struct *tty)
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001830{
1831 struct usb_serial_port *port = tty->driver_data;
1832 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1833 unsigned int result = 0;
1834 unsigned int mcr ;
1835 unsigned int msr ;
1836
1837 dbg("%s - port %d", __func__, port->number);
1838
1839 mcr = mos7720_port->shadowMCR;
1840 msr = mos7720_port->shadowMSR;
1841
1842 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1843 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1844 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1845 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
1846 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1847 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
1848
1849 dbg("%s -- %x", __func__, result);
1850
1851 return result;
1852}
1853
Alan Cox20b9d172011-02-14 16:26:50 +00001854static int mos7720_tiocmset(struct tty_struct *tty,
Mike Dunn63b91762010-04-15 17:02:09 -04001855 unsigned int set, unsigned int clear)
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001856{
1857 struct usb_serial_port *port = tty->driver_data;
1858 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1859 unsigned int mcr ;
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001860 dbg("%s - port %d", __func__, port->number);
Alan Cox60b33c12011-02-14 16:26:14 +00001861 dbg("he was at tiocmset");
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001862
1863 mcr = mos7720_port->shadowMCR;
1864
1865 if (set & TIOCM_RTS)
1866 mcr |= UART_MCR_RTS;
1867 if (set & TIOCM_DTR)
1868 mcr |= UART_MCR_DTR;
1869 if (set & TIOCM_LOOP)
1870 mcr |= UART_MCR_LOOP;
1871
1872 if (clear & TIOCM_RTS)
1873 mcr &= ~UART_MCR_RTS;
1874 if (clear & TIOCM_DTR)
1875 mcr &= ~UART_MCR_DTR;
1876 if (clear & TIOCM_LOOP)
1877 mcr &= ~UART_MCR_LOOP;
1878
1879 mos7720_port->shadowMCR = mcr;
Mike Dunn63b91762010-04-15 17:02:09 -04001880 write_mos_reg(port->serial, port->number - port->serial->minor,
1881 MCR, mos7720_port->shadowMCR);
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001882
1883 return 0;
1884}
1885
Alan Cox0bca1b92010-09-16 18:21:40 +01001886static int mos7720_get_icount(struct tty_struct *tty,
1887 struct serial_icounter_struct *icount)
1888{
1889 struct usb_serial_port *port = tty->driver_data;
1890 struct moschip_port *mos7720_port;
1891 struct async_icount cnow;
1892
1893 mos7720_port = usb_get_serial_port_data(port);
1894 cnow = mos7720_port->icount;
1895
1896 icount->cts = cnow.cts;
1897 icount->dsr = cnow.dsr;
1898 icount->rng = cnow.rng;
1899 icount->dcd = cnow.dcd;
1900 icount->rx = cnow.rx;
1901 icount->tx = cnow.tx;
1902 icount->frame = cnow.frame;
1903 icount->overrun = cnow.overrun;
1904 icount->parity = cnow.parity;
1905 icount->brk = cnow.brk;
1906 icount->buf_overrun = cnow.buf_overrun;
1907
1908 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
1909 port->number, icount->rx, icount->tx);
1910 return 0;
1911}
1912
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001913static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1914 unsigned int __user *value)
1915{
Alan Cox0bca1b92010-09-16 18:21:40 +01001916 unsigned int mcr;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001917 unsigned int arg;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001918
1919 struct usb_serial_port *port;
1920
1921 if (mos7720_port == NULL)
1922 return -1;
1923
Alan Cox4da1a172008-07-22 11:16:21 +01001924 port = (struct usb_serial_port *)mos7720_port->port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001925 mcr = mos7720_port->shadowMCR;
1926
1927 if (copy_from_user(&arg, value, sizeof(int)))
1928 return -EFAULT;
1929
1930 switch (cmd) {
1931 case TIOCMBIS:
1932 if (arg & TIOCM_RTS)
1933 mcr |= UART_MCR_RTS;
1934 if (arg & TIOCM_DTR)
1935 mcr |= UART_MCR_RTS;
1936 if (arg & TIOCM_LOOP)
1937 mcr |= UART_MCR_LOOP;
1938 break;
1939
1940 case TIOCMBIC:
1941 if (arg & TIOCM_RTS)
1942 mcr &= ~UART_MCR_RTS;
1943 if (arg & TIOCM_DTR)
1944 mcr &= ~UART_MCR_RTS;
1945 if (arg & TIOCM_LOOP)
1946 mcr &= ~UART_MCR_LOOP;
1947 break;
1948
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001949 }
1950
1951 mos7720_port->shadowMCR = mcr;
Mike Dunn63b91762010-04-15 17:02:09 -04001952 write_mos_reg(port->serial, port->number - port->serial->minor,
1953 MCR, mos7720_port->shadowMCR);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001954
1955 return 0;
1956}
1957
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001958static int get_serial_info(struct moschip_port *mos7720_port,
1959 struct serial_struct __user *retinfo)
1960{
1961 struct serial_struct tmp;
1962
1963 if (!retinfo)
1964 return -EFAULT;
1965
1966 memset(&tmp, 0, sizeof(tmp));
1967
1968 tmp.type = PORT_16550A;
1969 tmp.line = mos7720_port->port->serial->minor;
1970 tmp.port = mos7720_port->port->number;
1971 tmp.irq = 0;
1972 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
Alan Cox4da1a172008-07-22 11:16:21 +01001973 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001974 tmp.baud_base = 9600;
1975 tmp.close_delay = 5*HZ;
1976 tmp.closing_wait = 30*HZ;
1977
1978 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1979 return -EFAULT;
1980 return 0;
1981}
1982
Alan Cox00a0d0d2011-02-14 16:27:06 +00001983static int mos7720_ioctl(struct tty_struct *tty,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001984 unsigned int cmd, unsigned long arg)
1985{
Alan Cox95da3102008-07-22 11:09:07 +01001986 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001987 struct moschip_port *mos7720_port;
1988 struct async_icount cnow;
1989 struct async_icount cprev;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001990
1991 mos7720_port = usb_get_serial_port_data(port);
1992 if (mos7720_port == NULL)
1993 return -ENODEV;
1994
Harvey Harrison441b62c2008-03-03 16:08:34 -08001995 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001996
1997 switch (cmd) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001998 case TIOCSERGETLSR:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001999 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
Alan Cox4da1a172008-07-22 11:16:21 +01002000 return get_lsr_info(tty, mos7720_port,
2001 (unsigned int __user *)arg);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002002
Alan Cox95da3102008-07-22 11:09:07 +01002003 /* FIXME: These should be using the mode methods */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002004 case TIOCMBIS:
2005 case TIOCMBIC:
Alan Cox4da1a172008-07-22 11:16:21 +01002006 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET",
2007 __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002008 return set_modem_info(mos7720_port, cmd,
2009 (unsigned int __user *)arg);
2010
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002011 case TIOCGSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002012 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002013 return get_serial_info(mos7720_port,
2014 (struct serial_struct __user *)arg);
2015
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002016 case TIOCMIWAIT:
Harvey Harrison441b62c2008-03-03 16:08:34 -08002017 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002018 cprev = mos7720_port->icount;
2019 while (1) {
2020 if (signal_pending(current))
2021 return -ERESTARTSYS;
2022 cnow = mos7720_port->icount;
2023 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2024 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2025 return -EIO; /* no change => error */
2026 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2027 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2028 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
Alan Cox4da1a172008-07-22 11:16:21 +01002029 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002030 return 0;
2031 }
2032 cprev = cnow;
2033 }
2034 /* NOTREACHED */
2035 break;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002036 }
2037
2038 return -ENOIOCTLCMD;
2039}
2040
2041static int mos7720_startup(struct usb_serial *serial)
2042{
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002043 struct moschip_port *mos7720_port;
2044 struct usb_device *dev;
2045 int i;
2046 char data;
Huzaifa Sidhpurwala91f58ae2011-02-21 12:58:45 +05302047 u16 product;
Mike Dunnb69578d2010-04-15 17:01:33 -04002048 int ret_val;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002049
Harvey Harrison441b62c2008-03-03 16:08:34 -08002050 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002051
2052 if (!serial) {
2053 dbg("Invalid Handler");
2054 return -ENODEV;
2055 }
2056
Huzaifa Sidhpurwala91f58ae2011-02-21 12:58:45 +05302057 product = le16_to_cpu(serial->dev->descriptor.idProduct);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002058 dev = serial->dev;
2059
Mike Dunnfb088e32010-01-26 12:12:12 -05002060 /*
2061 * The 7715 uses the first bulk in/out endpoint pair for the parallel
2062 * port, and the second for the serial port. Because the usbserial core
2063 * assumes both pairs are serial ports, we must engage in a bit of
2064 * subterfuge and swap the pointers for ports 0 and 1 in order to make
2065 * port 0 point to the serial port. However, both moschip devices use a
2066 * single interrupt-in endpoint for both ports (as mentioned a little
2067 * further down), and this endpoint was assigned to port 0. So after
2068 * the swap, we must copy the interrupt endpoint elements from port 1
2069 * (as newly assigned) to port 0, and null out port 1 pointers.
2070 */
2071 if (product == MOSCHIP_DEVICE_ID_7715) {
2072 struct usb_serial_port *tmp = serial->port[0];
2073 serial->port[0] = serial->port[1];
2074 serial->port[1] = tmp;
2075 serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb;
2076 serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer;
2077 serial->port[0]->interrupt_in_endpointAddress =
2078 tmp->interrupt_in_endpointAddress;
2079 serial->port[1]->interrupt_in_urb = NULL;
2080 serial->port[1]->interrupt_in_buffer = NULL;
2081 }
2082
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002083
Mike Dunnb69578d2010-04-15 17:01:33 -04002084 /* set up serial port private structures */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002085 for (i = 0; i < serial->num_ports; ++i) {
2086 mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2087 if (mos7720_port == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07002088 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002089 return -ENOMEM;
2090 }
2091
2092 /* Initialize all port interrupt end point to port 0 int
2093 * endpoint. Our device has only one interrupt endpoint
Mike Dunnfb088e32010-01-26 12:12:12 -05002094 * common to all ports */
Alan Cox4da1a172008-07-22 11:16:21 +01002095 serial->port[i]->interrupt_in_endpointAddress =
2096 serial->port[0]->interrupt_in_endpointAddress;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002097
2098 mos7720_port->port = serial->port[i];
2099 usb_set_serial_port_data(serial->port[i], mos7720_port);
2100
2101 dbg("port number is %d", serial->port[i]->number);
2102 dbg("serial number is %d", serial->minor);
2103 }
2104
2105
2106 /* setting configuration feature to one */
2107 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Alan Cox4da1a172008-07-22 11:16:21 +01002108 (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5*HZ);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002109
Mike Dunnb69578d2010-04-15 17:01:33 -04002110 /* start the interrupt urb */
2111 ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
2112 if (ret_val)
2113 dev_err(&dev->dev,
2114 "%s - Error %d submitting control urb\n",
2115 __func__, ret_val);
2116
2117#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
2118 if (product == MOSCHIP_DEVICE_ID_7715) {
2119 ret_val = mos7715_parport_init(serial);
2120 if (ret_val < 0)
2121 return ret_val;
2122 }
2123#endif
Alan Cox4da1a172008-07-22 11:16:21 +01002124 /* LSR For Port 1 */
Mike Dunn63b91762010-04-15 17:02:09 -04002125 read_mos_reg(serial, 0, LSR, &data);
Alan Cox4da1a172008-07-22 11:16:21 +01002126 dbg("LSR:%x", data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002127
2128 return 0;
2129}
2130
Alan Sternf9c99bb2009-06-02 11:53:55 -04002131static void mos7720_release(struct usb_serial *serial)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002132{
2133 int i;
2134
Mike Dunnb69578d2010-04-15 17:01:33 -04002135#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
2136 /* close the parallel port */
2137
2138 if (le16_to_cpu(serial->dev->descriptor.idProduct)
2139 == MOSCHIP_DEVICE_ID_7715) {
2140 struct urbtracker *urbtrack;
2141 unsigned long flags;
2142 struct mos7715_parport *mos_parport =
2143 usb_get_serial_data(serial);
2144
2145 /* prevent NULL ptr dereference in port callbacks */
2146 spin_lock(&release_lock);
2147 mos_parport->pp->private_data = NULL;
2148 spin_unlock(&release_lock);
2149
2150 /* wait for synchronous usb calls to return */
2151 if (mos_parport->msg_pending)
2152 wait_for_completion_timeout(&mos_parport->syncmsg_compl,
2153 MOS_WDR_TIMEOUT);
2154
2155 parport_remove_port(mos_parport->pp);
2156 usb_set_serial_data(serial, NULL);
2157 mos_parport->serial = NULL;
2158
2159 /* if tasklet currently scheduled, wait for it to complete */
2160 tasklet_kill(&mos_parport->urb_tasklet);
2161
2162 /* unlink any urbs sent by the tasklet */
2163 spin_lock_irqsave(&mos_parport->listlock, flags);
2164 list_for_each_entry(urbtrack,
2165 &mos_parport->active_urbs,
2166 urblist_entry)
2167 usb_unlink_urb(urbtrack->urb);
2168 spin_unlock_irqrestore(&mos_parport->listlock, flags);
2169
2170 kref_put(&mos_parport->ref_count, destroy_mos_parport);
2171 }
2172#endif
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002173 /* free private structure allocated for serial port */
Alan Sternf9c99bb2009-06-02 11:53:55 -04002174 for (i = 0; i < serial->num_ports; ++i)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002175 kfree(usb_get_serial_port_data(serial->port[i]));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002176}
2177
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002178static struct usb_driver usb_driver = {
2179 .name = "moschip7720",
2180 .probe = usb_serial_probe,
2181 .disconnect = usb_serial_disconnect,
2182 .id_table = moschip_port_id_table,
Johannes Hölzld9b1b782006-12-17 21:50:24 +01002183};
2184
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002185static struct usb_serial_driver moschip7720_2port_driver = {
2186 .driver = {
2187 .owner = THIS_MODULE,
2188 .name = "moschip7720",
2189 },
2190 .description = "Moschip 2 port adapter",
2191 .id_table = moschip_port_id_table,
Mike Dunnfb088e32010-01-26 12:12:12 -05002192 .calc_num_ports = mos77xx_calc_num_ports,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002193 .open = mos7720_open,
2194 .close = mos7720_close,
2195 .throttle = mos7720_throttle,
2196 .unthrottle = mos7720_unthrottle,
Mike Dunnfb088e32010-01-26 12:12:12 -05002197 .probe = mos77xx_probe,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002198 .attach = mos7720_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04002199 .release = mos7720_release,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002200 .ioctl = mos7720_ioctl,
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07002201 .tiocmget = mos7720_tiocmget,
2202 .tiocmset = mos7720_tiocmset,
Alan Cox0bca1b92010-09-16 18:21:40 +01002203 .get_icount = mos7720_get_icount,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002204 .set_termios = mos7720_set_termios,
2205 .write = mos7720_write,
2206 .write_room = mos7720_write_room,
2207 .chars_in_buffer = mos7720_chars_in_buffer,
2208 .break_ctl = mos7720_break,
2209 .read_bulk_callback = mos7720_bulk_in_callback,
Mike Dunnfb088e32010-01-26 12:12:12 -05002210 .read_int_callback = NULL /* dynamically assigned in probe() */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002211};
2212
Alan Stern4d2a7af2012-02-23 14:57:09 -05002213static struct usb_serial_driver * const serial_drivers[] = {
2214 &moschip7720_2port_driver, NULL
2215};
2216
Greg Kroah-Hartman964e2b82012-02-28 13:12:07 -08002217module_usb_serial_driver(usb_driver, serial_drivers);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002218
Alan Cox4da1a172008-07-22 11:16:21 +01002219MODULE_AUTHOR(DRIVER_AUTHOR);
2220MODULE_DESCRIPTION(DRIVER_DESC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07002221MODULE_LICENSE("GPL");
2222
2223module_param(debug, bool, S_IRUGO | S_IWUSR);
2224MODULE_PARM_DESC(debug, "Debug enabled or not");