USB: serial: remove debug parameter from usb_serial_debug_data()
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / usb / serial / mos7720.c
CommitLineData
0f64478c
GKH
1/*
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:
50d2dc72
GKH
12 * Vijaya Kumar <vijaykumar.gn@gmail.com>
13 * Ajay Kumar <naanuajay@yahoo.com>
14 * Gurudeva <ngurudeva@yahoo.com>
0f64478c
GKH
15 *
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>
4da1a17d 36#include <linux/uaccess.h>
b69578df 37#include <linux/parport.h>
0f64478c
GKH
38
39/*
40 * Version Information
41 */
63b91767 42#define DRIVER_VERSION "2.1"
0f64478c
GKH
43#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
0f64478c
GKH
49#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
b69578df 63/* This structure holds all of the local serial port information */
4da1a17d 64struct moschip_port {
0f64478c
GKH
65 __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
90ab5ee9 74static bool debug;
0f64478c 75
fb088e33
MD
76static struct usb_serial_driver moschip7720_2port_driver;
77
0f64478c
GKH
78#define USB_VENDOR_ID_MOSCHIP 0x9710
79#define MOSCHIP_DEVICE_ID_7720 0x7720
80#define MOSCHIP_DEVICE_ID_7715 0x7715
81
68e24113 82static const struct usb_device_id id_table[] = {
4da1a17d 83 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
fb088e33 84 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) },
0f64478c
GKH
85 { } /* terminating entry */
86};
68e24113 87MODULE_DEVICE_TABLE(usb, id_table);
0f64478c 88
b69578df
MD
89#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
63b91767
MD
126#endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
127
128static const unsigned int dummy; /* for clarity in register access fns */
129
b69578df
MD
130enum 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 */
63b91767
MD
188static inline __u16 get_reg_value(enum mos_regs reg,
189 unsigned int serial_portnum)
b69578df
MD
190{
191 if (reg >= SP1_REG) /* control reg */
192 return 0x0000;
63b91767
MD
193
194 else if (reg >= DPR) /* parallel port reg (7715 only) */
b69578df 195 return 0x0100;
63b91767
MD
196
197 else /* serial port reg */
198 return (serial_portnum + 2) << 8;
b69578df
MD
199}
200
201/*
202 * Write data byte to the specified device register. The data is embedded in
63b91767
MD
203 * the value field of the setup packet. serial_portnum is ignored for registers
204 * not specific to a particular serial port.
b69578df 205 */
63b91767
MD
206static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
207 enum mos_regs reg, __u8 data)
b69578df 208{
b69578df
MD
209 struct usb_device *usbdev = serial->dev;
210 unsigned int pipe = usb_sndctrlpipe(usbdev, 0);
211 __u8 request = (__u8)0x0e;
212 __u8 requesttype = (__u8)0x40;
b69578df 213 __u16 index = get_reg_index(reg);
63b91767
MD
214 __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);
b69578df
MD
217 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
63b91767
MD
225 * 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.
b69578df 227 */
63b91767
MD
228static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
229 enum mos_regs reg, __u8 *data)
b69578df 230{
63b91767 231 struct usb_device *usbdev = serial->dev;
b69578df
MD
232 unsigned int pipe = usb_rcvctrlpipe(usbdev, 0);
233 __u8 request = (__u8)0x0d;
234 __u8 requesttype = (__u8)0xc0;
b69578df 235 __u16 index = get_reg_index(reg);
63b91767 236 __u16 value = get_reg_value(reg, serial_portnum);
b69578df 237 int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
63b91767 238 index, data, 1, MOS_WDR_TIMEOUT);
b69578df
MD
239 if (status < 0)
240 dev_err(&usbdev->dev,
241 "mos7720: usb_control_msg() failed: %d", status);
242 return status;
243}
244
63b91767
MD
245#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
246
b69578df
MD
247static inline int mos7715_change_mode(struct mos7715_parport *mos_parport,
248 enum mos7715_pp_modes mode)
249{
250 mos_parport->shadowECR = mode;
63b91767 251 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
b69578df
MD
252 return 0;
253}
254
255static void destroy_mos_parport(struct kref *kref)
256{
257 struct mos7715_parport *mos_parport =
258 container_of(kref, struct mos7715_parport, ref_count);
259
b69578df
MD
260 kfree(mos_parport);
261}
262
263static void destroy_urbtracker(struct kref *kref)
264{
265 struct urbtracker *urbtrack =
266 container_of(kref, struct urbtracker, ref_count);
267 struct mos7715_parport *mos_parport = urbtrack->mos_parport;
ca09907d 268
b69578df
MD
269 usb_free_urb(urbtrack->urb);
270 kfree(urbtrack);
271 kref_put(&mos_parport->ref_count, destroy_mos_parport);
272}
273
274/*
275 * This runs as a tasklet when sending an urb in a non-blocking parallel
276 * port callback had to be deferred because the disconnect mutex could not be
277 * obtained at the time.
278 */
279static void send_deferred_urbs(unsigned long _mos_parport)
280{
281 int ret_val;
282 unsigned long flags;
283 struct mos7715_parport *mos_parport = (void *)_mos_parport;
67990472 284 struct urbtracker *urbtrack, *tmp;
b69578df 285 struct list_head *cursor, *next;
9eecf808 286 struct device *dev;
b69578df 287
b69578df
MD
288 /* if release function ran, game over */
289 if (unlikely(mos_parport->serial == NULL))
290 return;
291
9eecf808
GKH
292 dev = &mos_parport->serial->dev->dev;
293
b69578df
MD
294 /* try again to get the mutex */
295 if (!mutex_trylock(&mos_parport->serial->disc_mutex)) {
9eecf808 296 dev_dbg(dev, "%s: rescheduling tasklet\n", __func__);
b69578df
MD
297 tasklet_schedule(&mos_parport->urb_tasklet);
298 return;
299 }
300
301 /* if device disconnected, game over */
302 if (unlikely(mos_parport->serial->disconnected)) {
303 mutex_unlock(&mos_parport->serial->disc_mutex);
304 return;
305 }
306
307 spin_lock_irqsave(&mos_parport->listlock, flags);
308 if (list_empty(&mos_parport->deferred_urbs)) {
309 spin_unlock_irqrestore(&mos_parport->listlock, flags);
310 mutex_unlock(&mos_parport->serial->disc_mutex);
9eecf808 311 dev_dbg(dev, "%s: deferred_urbs list empty\n", __func__);
b69578df
MD
312 return;
313 }
314
315 /* move contents of deferred_urbs list to active_urbs list and submit */
316 list_for_each_safe(cursor, next, &mos_parport->deferred_urbs)
317 list_move_tail(cursor, &mos_parport->active_urbs);
67990472 318 list_for_each_entry_safe(urbtrack, tmp, &mos_parport->active_urbs,
b69578df
MD
319 urblist_entry) {
320 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
9eecf808 321 dev_dbg(dev, "%s: urb submitted\n", __func__);
b69578df 322 if (ret_val) {
9eecf808 323 dev_err(dev, "usb_submit_urb() failed: %d\n", ret_val);
b69578df
MD
324 list_del(&urbtrack->urblist_entry);
325 kref_put(&urbtrack->ref_count, destroy_urbtracker);
326 }
327 }
328 spin_unlock_irqrestore(&mos_parport->listlock, flags);
329 mutex_unlock(&mos_parport->serial->disc_mutex);
330}
331
332/* callback for parallel port control urbs submitted asynchronously */
333static void async_complete(struct urb *urb)
334{
335 struct urbtracker *urbtrack = urb->context;
336 int status = urb->status;
ca09907d 337
b69578df 338 if (unlikely(status))
9eecf808 339 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
b69578df
MD
340
341 /* remove the urbtracker from the active_urbs list */
342 spin_lock(&urbtrack->mos_parport->listlock);
343 list_del(&urbtrack->urblist_entry);
344 spin_unlock(&urbtrack->mos_parport->listlock);
345 kref_put(&urbtrack->ref_count, destroy_urbtracker);
346}
347
348static int write_parport_reg_nonblock(struct mos7715_parport *mos_parport,
349 enum mos_regs reg, __u8 data)
350{
351 struct urbtracker *urbtrack;
352 int ret_val;
353 unsigned long flags;
354 struct usb_ctrlrequest setup;
355 struct usb_serial *serial = mos_parport->serial;
356 struct usb_device *usbdev = serial->dev;
b69578df
MD
357
358 /* create and initialize the control urb and containing urbtracker */
359 urbtrack = kmalloc(sizeof(struct urbtracker), GFP_ATOMIC);
360 if (urbtrack == NULL) {
361 dev_err(&usbdev->dev, "out of memory");
362 return -ENOMEM;
363 }
364 kref_get(&mos_parport->ref_count);
365 urbtrack->mos_parport = mos_parport;
366 urbtrack->urb = usb_alloc_urb(0, GFP_ATOMIC);
367 if (urbtrack->urb == NULL) {
368 dev_err(&usbdev->dev, "out of urbs");
369 kfree(urbtrack);
370 return -ENOMEM;
371 }
372 setup.bRequestType = (__u8)0x40;
373 setup.bRequest = (__u8)0x0e;
63b91767 374 setup.wValue = get_reg_value(reg, dummy);
b69578df
MD
375 setup.wIndex = get_reg_index(reg);
376 setup.wLength = 0;
377 usb_fill_control_urb(urbtrack->urb, usbdev,
378 usb_sndctrlpipe(usbdev, 0),
379 (unsigned char *)&setup,
380 NULL, 0, async_complete, urbtrack);
381 kref_init(&urbtrack->ref_count);
382 INIT_LIST_HEAD(&urbtrack->urblist_entry);
383
384 /*
385 * get the disconnect mutex, or add tracker to the deferred_urbs list
386 * and schedule a tasklet to try again later
387 */
388 if (!mutex_trylock(&serial->disc_mutex)) {
389 spin_lock_irqsave(&mos_parport->listlock, flags);
390 list_add_tail(&urbtrack->urblist_entry,
391 &mos_parport->deferred_urbs);
392 spin_unlock_irqrestore(&mos_parport->listlock, flags);
393 tasklet_schedule(&mos_parport->urb_tasklet);
9eecf808 394 dev_dbg(&usbdev->dev, "tasklet scheduled");
b69578df
MD
395 return 0;
396 }
397
398 /* bail if device disconnected */
399 if (serial->disconnected) {
400 kref_put(&urbtrack->ref_count, destroy_urbtracker);
401 mutex_unlock(&serial->disc_mutex);
402 return -ENODEV;
403 }
404
405 /* add the tracker to the active_urbs list and submit */
406 spin_lock_irqsave(&mos_parport->listlock, flags);
407 list_add_tail(&urbtrack->urblist_entry, &mos_parport->active_urbs);
408 spin_unlock_irqrestore(&mos_parport->listlock, flags);
409 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
410 mutex_unlock(&serial->disc_mutex);
411 if (ret_val) {
412 dev_err(&usbdev->dev,
413 "%s: submit_urb() failed: %d", __func__, ret_val);
414 spin_lock_irqsave(&mos_parport->listlock, flags);
415 list_del(&urbtrack->urblist_entry);
416 spin_unlock_irqrestore(&mos_parport->listlock, flags);
417 kref_put(&urbtrack->ref_count, destroy_urbtracker);
418 return ret_val;
419 }
420 return 0;
421}
422
423/*
424 * This is the the common top part of all parallel port callback operations that
425 * send synchronous messages to the device. This implements convoluted locking
426 * that avoids two scenarios: (1) a port operation is called after usbserial
427 * has called our release function, at which point struct mos7715_parport has
428 * been destroyed, and (2) the device has been disconnected, but usbserial has
429 * not called the release function yet because someone has a serial port open.
430 * The shared release_lock prevents the first, and the mutex and disconnected
431 * flag maintained by usbserial covers the second. We also use the msg_pending
432 * flag to ensure that all synchronous usb messgage calls have completed before
433 * our release function can return.
434 */
435static int parport_prologue(struct parport *pp)
436{
437 struct mos7715_parport *mos_parport;
438
439 spin_lock(&release_lock);
440 mos_parport = pp->private_data;
441 if (unlikely(mos_parport == NULL)) {
442 /* release fn called, port struct destroyed */
443 spin_unlock(&release_lock);
444 return -1;
445 }
446 mos_parport->msg_pending = true; /* synch usb call pending */
447 INIT_COMPLETION(mos_parport->syncmsg_compl);
448 spin_unlock(&release_lock);
449
450 mutex_lock(&mos_parport->serial->disc_mutex);
451 if (mos_parport->serial->disconnected) {
452 /* device disconnected */
453 mutex_unlock(&mos_parport->serial->disc_mutex);
454 mos_parport->msg_pending = false;
455 complete(&mos_parport->syncmsg_compl);
456 return -1;
457 }
458
459 return 0;
460}
461
462/*
463 * This is the the common bottom part of all parallel port functions that send
464 * synchronous messages to the device.
465 */
466static inline void parport_epilogue(struct parport *pp)
467{
468 struct mos7715_parport *mos_parport = pp->private_data;
469 mutex_unlock(&mos_parport->serial->disc_mutex);
470 mos_parport->msg_pending = false;
471 complete(&mos_parport->syncmsg_compl);
472}
473
474static void parport_mos7715_write_data(struct parport *pp, unsigned char d)
475{
476 struct mos7715_parport *mos_parport = pp->private_data;
ca09907d 477
b69578df
MD
478 if (parport_prologue(pp) < 0)
479 return;
480 mos7715_change_mode(mos_parport, SPP);
63b91767 481 write_mos_reg(mos_parport->serial, dummy, DPR, (__u8)d);
b69578df
MD
482 parport_epilogue(pp);
483}
484
485static unsigned char parport_mos7715_read_data(struct parport *pp)
486{
487 struct mos7715_parport *mos_parport = pp->private_data;
488 unsigned char d;
ca09907d 489
b69578df
MD
490 if (parport_prologue(pp) < 0)
491 return 0;
63b91767 492 read_mos_reg(mos_parport->serial, dummy, DPR, &d);
b69578df
MD
493 parport_epilogue(pp);
494 return d;
495}
496
497static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
498{
499 struct mos7715_parport *mos_parport = pp->private_data;
500 __u8 data;
ca09907d 501
b69578df
MD
502 if (parport_prologue(pp) < 0)
503 return;
504 data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
63b91767 505 write_mos_reg(mos_parport->serial, dummy, DCR, data);
b69578df
MD
506 mos_parport->shadowDCR = data;
507 parport_epilogue(pp);
508}
509
510static unsigned char parport_mos7715_read_control(struct parport *pp)
511{
512 struct mos7715_parport *mos_parport = pp->private_data;
513 __u8 dcr;
ca09907d 514
b69578df
MD
515 spin_lock(&release_lock);
516 mos_parport = pp->private_data;
517 if (unlikely(mos_parport == NULL)) {
518 spin_unlock(&release_lock);
519 return 0;
520 }
521 dcr = mos_parport->shadowDCR & 0x0f;
522 spin_unlock(&release_lock);
523 return dcr;
524}
525
526static unsigned char parport_mos7715_frob_control(struct parport *pp,
527 unsigned char mask,
528 unsigned char val)
529{
530 struct mos7715_parport *mos_parport = pp->private_data;
531 __u8 dcr;
ca09907d 532
b69578df
MD
533 mask &= 0x0f;
534 val &= 0x0f;
535 if (parport_prologue(pp) < 0)
536 return 0;
537 mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
63b91767 538 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
b69578df
MD
539 dcr = mos_parport->shadowDCR & 0x0f;
540 parport_epilogue(pp);
541 return dcr;
542}
543
544static unsigned char parport_mos7715_read_status(struct parport *pp)
545{
546 unsigned char status;
547 struct mos7715_parport *mos_parport = pp->private_data;
ca09907d 548
b69578df
MD
549 spin_lock(&release_lock);
550 mos_parport = pp->private_data;
551 if (unlikely(mos_parport == NULL)) { /* release called */
552 spin_unlock(&release_lock);
553 return 0;
554 }
555 status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
556 spin_unlock(&release_lock);
557 return status;
558}
559
560static void parport_mos7715_enable_irq(struct parport *pp)
561{
b69578df 562}
ca09907d 563
b69578df
MD
564static void parport_mos7715_disable_irq(struct parport *pp)
565{
b69578df
MD
566}
567
568static void parport_mos7715_data_forward(struct parport *pp)
569{
570 struct mos7715_parport *mos_parport = pp->private_data;
ca09907d 571
b69578df
MD
572 if (parport_prologue(pp) < 0)
573 return;
574 mos7715_change_mode(mos_parport, PS2);
575 mos_parport->shadowDCR &= ~0x20;
63b91767 576 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
b69578df
MD
577 parport_epilogue(pp);
578}
579
580static void parport_mos7715_data_reverse(struct parport *pp)
581{
582 struct mos7715_parport *mos_parport = pp->private_data;
ca09907d 583
b69578df
MD
584 if (parport_prologue(pp) < 0)
585 return;
586 mos7715_change_mode(mos_parport, PS2);
587 mos_parport->shadowDCR |= 0x20;
63b91767 588 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
b69578df
MD
589 parport_epilogue(pp);
590}
591
592static void parport_mos7715_init_state(struct pardevice *dev,
593 struct parport_state *s)
594{
b69578df
MD
595 s->u.pc.ctr = DCR_INIT_VAL;
596 s->u.pc.ecr = ECR_INIT_VAL;
597}
598
599/* N.B. Parport core code requires that this function not block */
600static void parport_mos7715_save_state(struct parport *pp,
601 struct parport_state *s)
602{
603 struct mos7715_parport *mos_parport;
ca09907d 604
b69578df
MD
605 spin_lock(&release_lock);
606 mos_parport = pp->private_data;
607 if (unlikely(mos_parport == NULL)) { /* release called */
608 spin_unlock(&release_lock);
609 return;
610 }
611 s->u.pc.ctr = mos_parport->shadowDCR;
612 s->u.pc.ecr = mos_parport->shadowECR;
613 spin_unlock(&release_lock);
614}
615
616/* N.B. Parport core code requires that this function not block */
617static void parport_mos7715_restore_state(struct parport *pp,
618 struct parport_state *s)
619{
620 struct mos7715_parport *mos_parport;
ca09907d 621
b69578df
MD
622 spin_lock(&release_lock);
623 mos_parport = pp->private_data;
624 if (unlikely(mos_parport == NULL)) { /* release called */
625 spin_unlock(&release_lock);
626 return;
627 }
628 write_parport_reg_nonblock(mos_parport, DCR, mos_parport->shadowDCR);
629 write_parport_reg_nonblock(mos_parport, ECR, mos_parport->shadowECR);
630 spin_unlock(&release_lock);
631}
632
633static size_t parport_mos7715_write_compat(struct parport *pp,
634 const void *buffer,
635 size_t len, int flags)
636{
637 int retval;
638 struct mos7715_parport *mos_parport = pp->private_data;
639 int actual_len;
ca09907d 640
b69578df
MD
641 if (parport_prologue(pp) < 0)
642 return 0;
643 mos7715_change_mode(mos_parport, PPF);
644 retval = usb_bulk_msg(mos_parport->serial->dev,
645 usb_sndbulkpipe(mos_parport->serial->dev, 2),
646 (void *)buffer, len, &actual_len,
647 MOS_WDR_TIMEOUT);
648 parport_epilogue(pp);
649 if (retval) {
650 dev_err(&mos_parport->serial->dev->dev,
651 "mos7720: usb_bulk_msg() failed: %d", retval);
652 return 0;
653 }
654 return actual_len;
655}
656
657static struct parport_operations parport_mos7715_ops = {
658 .owner = THIS_MODULE,
659 .write_data = parport_mos7715_write_data,
660 .read_data = parport_mos7715_read_data,
661
662 .write_control = parport_mos7715_write_control,
663 .read_control = parport_mos7715_read_control,
664 .frob_control = parport_mos7715_frob_control,
665
666 .read_status = parport_mos7715_read_status,
667
668 .enable_irq = parport_mos7715_enable_irq,
669 .disable_irq = parport_mos7715_disable_irq,
670
671 .data_forward = parport_mos7715_data_forward,
672 .data_reverse = parport_mos7715_data_reverse,
673
674 .init_state = parport_mos7715_init_state,
675 .save_state = parport_mos7715_save_state,
676 .restore_state = parport_mos7715_restore_state,
677
678 .compat_write_data = parport_mos7715_write_compat,
679
680 .nibble_read_data = parport_ieee1284_read_nibble,
681 .byte_read_data = parport_ieee1284_read_byte,
682};
683
684/*
685 * Allocate and initialize parallel port control struct, initialize
686 * the parallel port hardware device, and register with the parport subsystem.
687 */
688static int mos7715_parport_init(struct usb_serial *serial)
689{
690 struct mos7715_parport *mos_parport;
691
692 /* allocate and initialize parallel port control struct */
693 mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
694 if (mos_parport == NULL) {
9eecf808 695 dev_dbg(&serial->dev->dev, "%s: kzalloc failed\n", __func__);
b69578df
MD
696 return -ENOMEM;
697 }
698 mos_parport->msg_pending = false;
699 kref_init(&mos_parport->ref_count);
700 spin_lock_init(&mos_parport->listlock);
701 INIT_LIST_HEAD(&mos_parport->active_urbs);
702 INIT_LIST_HEAD(&mos_parport->deferred_urbs);
703 usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
704 mos_parport->serial = serial;
705 tasklet_init(&mos_parport->urb_tasklet, send_deferred_urbs,
706 (unsigned long) mos_parport);
707 init_completion(&mos_parport->syncmsg_compl);
708
709 /* cycle parallel port reset bit */
63b91767
MD
710 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x80);
711 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x00);
b69578df
MD
712
713 /* initialize device registers */
714 mos_parport->shadowDCR = DCR_INIT_VAL;
63b91767 715 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
b69578df 716 mos_parport->shadowECR = ECR_INIT_VAL;
63b91767 717 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
b69578df
MD
718
719 /* register with parport core */
720 mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE,
721 PARPORT_DMA_NONE,
722 &parport_mos7715_ops);
723 if (mos_parport->pp == NULL) {
724 dev_err(&serial->interface->dev,
725 "Could not register parport\n");
726 kref_put(&mos_parport->ref_count, destroy_mos_parport);
727 return -EIO;
728 }
729 mos_parport->pp->private_data = mos_parport;
730 mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP;
731 mos_parport->pp->dev = &serial->interface->dev;
732 parport_announce_port(mos_parport->pp);
733
734 return 0;
735}
736#endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
0f64478c
GKH
737
738/*
739 * mos7720_interrupt_callback
740 * this is the callback function for when we have received data on the
741 * interrupt endpoint.
742 */
743static void mos7720_interrupt_callback(struct urb *urb)
744{
745 int result;
746 int length;
81105984 747 int status = urb->status;
9eecf808 748 struct device *dev = &urb->dev->dev;
325b70c2 749 __u8 *data;
0f64478c
GKH
750 __u8 sp1;
751 __u8 sp2;
0f64478c 752
81105984 753 switch (status) {
0f64478c
GKH
754 case 0:
755 /* success */
756 break;
757 case -ECONNRESET:
758 case -ENOENT:
759 case -ESHUTDOWN:
760 /* this urb is terminated, clean up */
9eecf808 761 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
0f64478c
GKH
762 return;
763 default:
9eecf808 764 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
0f64478c
GKH
765 goto exit;
766 }
767
768 length = urb->actual_length;
769 data = urb->transfer_buffer;
770
771 /* Moschip get 4 bytes
772 * Byte 1 IIR Port 1 (port.number is 0)
773 * Byte 2 IIR Port 2 (port.number is 1)
774 * Byte 3 --------------
775 * Byte 4 FIFO status for both */
325b70c2
ON
776
777 /* the above description is inverted
778 * oneukum 2007-03-14 */
779
780 if (unlikely(length != 4)) {
9eecf808 781 dev_dbg(dev, "Wrong data !!!\n");
0f64478c
GKH
782 return;
783 }
784
325b70c2
ON
785 sp1 = data[3];
786 sp2 = data[2];
0f64478c 787
325b70c2 788 if ((sp1 | sp2) & 0x01) {
0f64478c 789 /* No Interrupt Pending in both the ports */
9eecf808 790 dev_dbg(dev, "No Interrupt !!!\n");
0f64478c
GKH
791 } else {
792 switch (sp1 & 0x0f) {
793 case SERIAL_IIR_RLS:
9eecf808 794 dev_dbg(dev, "Serial Port 1: Receiver status error or address bit detected in 9-bit mode\n");
0f64478c
GKH
795 break;
796 case SERIAL_IIR_CTI:
9eecf808 797 dev_dbg(dev, "Serial Port 1: Receiver time out\n");
0f64478c
GKH
798 break;
799 case SERIAL_IIR_MS:
9eecf808 800 /* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */
0f64478c
GKH
801 break;
802 }
803
804 switch (sp2 & 0x0f) {
805 case SERIAL_IIR_RLS:
9eecf808 806 dev_dbg(dev, "Serial Port 2: Receiver status error or address bit detected in 9-bit mode\n");
0f64478c
GKH
807 break;
808 case SERIAL_IIR_CTI:
9eecf808 809 dev_dbg(dev, "Serial Port 2: Receiver time out\n");
0f64478c
GKH
810 break;
811 case SERIAL_IIR_MS:
9eecf808 812 /* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */
0f64478c
GKH
813 break;
814 }
815 }
816
817exit:
818 result = usb_submit_urb(urb, GFP_ATOMIC);
819 if (result)
9eecf808 820 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
0f64478c
GKH
821}
822
fb088e33
MD
823/*
824 * mos7715_interrupt_callback
825 * this is the 7715's callback function for when we have received data on
826 * the interrupt endpoint.
827 */
828static void mos7715_interrupt_callback(struct urb *urb)
829{
830 int result;
831 int length;
832 int status = urb->status;
9eecf808 833 struct device *dev = &urb->dev->dev;
fb088e33
MD
834 __u8 *data;
835 __u8 iir;
836
837 switch (status) {
838 case 0:
839 /* success */
840 break;
841 case -ECONNRESET:
842 case -ENOENT:
843 case -ESHUTDOWN:
b69578df 844 case -ENODEV:
fb088e33 845 /* this urb is terminated, clean up */
9eecf808 846 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
fb088e33
MD
847 return;
848 default:
9eecf808 849 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
fb088e33
MD
850 goto exit;
851 }
852
853 length = urb->actual_length;
854 data = urb->transfer_buffer;
855
856 /* Structure of data from 7715 device:
857 * Byte 1: IIR serial Port
858 * Byte 2: unused
859 * Byte 2: DSR parallel port
860 * Byte 4: FIFO status for both */
861
862 if (unlikely(length != 4)) {
9eecf808 863 dev_dbg(dev, "Wrong data !!!\n");
fb088e33
MD
864 return;
865 }
866
867 iir = data[0];
868 if (!(iir & 0x01)) { /* serial port interrupt pending */
869 switch (iir & 0x0f) {
870 case SERIAL_IIR_RLS:
9eecf808 871 dev_dbg(dev, "Serial Port: Receiver status error or address bit detected in 9-bit mode\n\n");
fb088e33
MD
872 break;
873 case SERIAL_IIR_CTI:
9eecf808 874 dev_dbg(dev, "Serial Port: Receiver time out\n");
fb088e33
MD
875 break;
876 case SERIAL_IIR_MS:
9eecf808 877 /* dev_dbg(dev, "Serial Port: Modem status change\n"); */
fb088e33
MD
878 break;
879 }
880 }
881
b69578df
MD
882#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
883 { /* update local copy of DSR reg */
884 struct usb_serial_port *port = urb->context;
885 struct mos7715_parport *mos_parport = port->serial->private;
886 if (unlikely(mos_parport == NULL))
887 return;
888 atomic_set(&mos_parport->shadowDSR, data[2]);
889 }
890#endif
891
fb088e33
MD
892exit:
893 result = usb_submit_urb(urb, GFP_ATOMIC);
894 if (result)
9eecf808 895 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
fb088e33
MD
896}
897
0f64478c
GKH
898/*
899 * mos7720_bulk_in_callback
900 * this is the callback function for when we have received data on the
901 * bulk in endpoint.
902 */
903static void mos7720_bulk_in_callback(struct urb *urb)
904{
81105984 905 int retval;
0f64478c
GKH
906 unsigned char *data ;
907 struct usb_serial_port *port;
0f64478c 908 struct tty_struct *tty;
81105984 909 int status = urb->status;
0f64478c 910
81105984 911 if (status) {
9eecf808 912 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
0f64478c
GKH
913 return;
914 }
915
b69578df 916 port = urb->context;
0f64478c 917
9eecf808 918 dev_dbg(&port->dev, "Entering...%s\n", __func__);
0f64478c
GKH
919
920 data = urb->transfer_buffer;
921
4a90f09b 922 tty = tty_port_tty_get(&port->port);
0f64478c 923 if (tty && urb->actual_length) {
0f64478c
GKH
924 tty_insert_flip_string(tty, data, urb->actual_length);
925 tty_flip_buffer_push(tty);
926 }
4a90f09b 927 tty_kref_put(tty);
0f64478c 928
0f64478c 929 if (port->read_urb->status != -EINPROGRESS) {
81105984
GKH
930 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
931 if (retval)
9eecf808 932 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
0f64478c
GKH
933 }
934}
935
936/*
937 * mos7720_bulk_out_data_callback
938 * this is the callback function for when we have finished sending serial
939 * data on the bulk out endpoint.
940 */
941static void mos7720_bulk_out_data_callback(struct urb *urb)
942{
943 struct moschip_port *mos7720_port;
944 struct tty_struct *tty;
81105984 945 int status = urb->status;
0f64478c 946
81105984 947 if (status) {
9eecf808 948 dev_dbg(&urb->dev->dev, "nonzero write bulk status received:%d\n", status);
0f64478c
GKH
949 return;
950 }
951
952 mos7720_port = urb->context;
953 if (!mos7720_port) {
9eecf808 954 dev_dbg(&urb->dev->dev, "NULL mos7720_port pointer\n");
0f64478c
GKH
955 return ;
956 }
957
4a90f09b 958 tty = tty_port_tty_get(&mos7720_port->port->port);
0f64478c 959
b963a844
JS
960 if (tty && mos7720_port->open)
961 tty_wakeup(tty);
4a90f09b 962 tty_kref_put(tty);
0f64478c
GKH
963}
964
fb088e33
MD
965/*
966 * mos77xx_probe
967 * this function installs the appropriate read interrupt endpoint callback
968 * depending on whether the device is a 7720 or 7715, thus avoiding costly
969 * run-time checks in the high-frequency callback routine itself.
970 */
971static int mos77xx_probe(struct usb_serial *serial,
972 const struct usb_device_id *id)
973{
974 if (id->idProduct == MOSCHIP_DEVICE_ID_7715)
975 moschip7720_2port_driver.read_int_callback =
976 mos7715_interrupt_callback;
977 else
978 moschip7720_2port_driver.read_int_callback =
979 mos7720_interrupt_callback;
980
981 return 0;
982}
983
984static int mos77xx_calc_num_ports(struct usb_serial *serial)
985{
986 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
987 if (product == MOSCHIP_DEVICE_ID_7715)
988 return 1;
989
990 return 2;
991}
992
a509a7e4 993static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
0f64478c
GKH
994{
995 struct usb_serial *serial;
0f64478c 996 struct urb *urb;
0f64478c
GKH
997 struct moschip_port *mos7720_port;
998 int response;
999 int port_number;
63b91767 1000 __u8 data;
fe4b65ec 1001 int allocated_urbs = 0;
0f64478c
GKH
1002 int j;
1003
1004 serial = port->serial;
1005
1006 mos7720_port = usb_get_serial_port_data(port);
1007 if (mos7720_port == NULL)
1008 return -ENODEV;
1009
0f64478c
GKH
1010 usb_clear_halt(serial->dev, port->write_urb->pipe);
1011 usb_clear_halt(serial->dev, port->read_urb->pipe);
1012
1013 /* Initialising the write urb pool */
1014 for (j = 0; j < NUM_URBS; ++j) {
4da1a17d 1015 urb = usb_alloc_urb(0, GFP_KERNEL);
0f64478c
GKH
1016 mos7720_port->write_urb_pool[j] = urb;
1017
1018 if (urb == NULL) {
194343d9 1019 dev_err(&port->dev, "No more urbs???\n");
0f64478c
GKH
1020 continue;
1021 }
1022
1023 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1024 GFP_KERNEL);
1025 if (!urb->transfer_buffer) {
194343d9
GKH
1026 dev_err(&port->dev,
1027 "%s-out of memory for urb buffers.\n",
1028 __func__);
fe4b65ec
ON
1029 usb_free_urb(mos7720_port->write_urb_pool[j]);
1030 mos7720_port->write_urb_pool[j] = NULL;
0f64478c
GKH
1031 continue;
1032 }
fe4b65ec 1033 allocated_urbs++;
0f64478c
GKH
1034 }
1035
fe4b65ec
ON
1036 if (!allocated_urbs)
1037 return -ENOMEM;
1038
0f64478c
GKH
1039 /* Initialize MCS7720 -- Write Init values to corresponding Registers
1040 *
1041 * Register Index
2f9ea55c 1042 * 0 : THR/RHR
0f64478c
GKH
1043 * 1 : IER
1044 * 2 : FCR
1045 * 3 : LCR
1046 * 4 : MCR
2f9ea55c
KS
1047 * 5 : LSR
1048 * 6 : MSR
1049 * 7 : SPR
0f64478c
GKH
1050 *
1051 * 0x08 : SP1/2 Control Reg
1052 */
1053 port_number = port->number - port->serial->minor;
63b91767
MD
1054 read_mos_reg(serial, port_number, LSR, &data);
1055
9eecf808 1056 dev_dbg(&port->dev, "SS::%p LSR:%x\n", mos7720_port, data);
0f64478c 1057
63b91767
MD
1058 write_mos_reg(serial, dummy, SP1_REG, 0x02);
1059 write_mos_reg(serial, dummy, SP2_REG, 0x02);
0f64478c 1060
63b91767
MD
1061 write_mos_reg(serial, port_number, IER, 0x00);
1062 write_mos_reg(serial, port_number, FCR, 0x00);
0f64478c 1063
63b91767
MD
1064 write_mos_reg(serial, port_number, FCR, 0xcf);
1065 mos7720_port->shadowLCR = 0x03;
1066 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1067 mos7720_port->shadowMCR = 0x0b;
1068 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
0f64478c 1069
63b91767
MD
1070 write_mos_reg(serial, port_number, SP_CONTROL_REG, 0x00);
1071 read_mos_reg(serial, dummy, SP_CONTROL_REG, &data);
0f64478c 1072 data = data | (port->number - port->serial->minor + 1);
63b91767
MD
1073 write_mos_reg(serial, dummy, SP_CONTROL_REG, data);
1074 mos7720_port->shadowLCR = 0x83;
1075 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1076 write_mos_reg(serial, port_number, THR, 0x0c);
1077 write_mos_reg(serial, port_number, IER, 0x00);
1078 mos7720_port->shadowLCR = 0x03;
1079 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1080 write_mos_reg(serial, port_number, IER, 0x0c);
0f64478c 1081
0f64478c
GKH
1082 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
1083 if (response)
4da1a17d
AC
1084 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
1085 __func__, response);
0f64478c
GKH
1086
1087 /* initialize our icount structure */
1088 memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
1089
1090 /* initialize our port settings */
1091 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
1092
1093 /* send a open port command */
1094 mos7720_port->open = 1;
1095
1096 return 0;
1097}
1098
1099/*
1100 * mos7720_chars_in_buffer
1101 * this function is called by the tty driver when it wants to know how many
1102 * bytes of data we currently have outstanding in the port (data that has
1103 * been written, but hasn't made it out the port yet)
1104 * If successful, we return the number of bytes left to be written in the
1105 * system,
1106 * Otherwise we return a negative error number.
1107 */
95da310e 1108static int mos7720_chars_in_buffer(struct tty_struct *tty)
0f64478c 1109{
95da310e 1110 struct usb_serial_port *port = tty->driver_data;
0f64478c
GKH
1111 int i;
1112 int chars = 0;
1113 struct moschip_port *mos7720_port;
1114
0f64478c 1115 mos7720_port = usb_get_serial_port_data(port);
9eecf808 1116 if (mos7720_port == NULL)
23198fda 1117 return 0;
0f64478c
GKH
1118
1119 for (i = 0; i < NUM_URBS; ++i) {
4da1a17d
AC
1120 if (mos7720_port->write_urb_pool[i] &&
1121 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
0f64478c
GKH
1122 chars += URB_TRANSFER_BUFFER_SIZE;
1123 }
9eecf808 1124 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
0f64478c
GKH
1125 return chars;
1126}
1127
335f8514 1128static void mos7720_close(struct usb_serial_port *port)
0f64478c
GKH
1129{
1130 struct usb_serial *serial;
1131 struct moschip_port *mos7720_port;
0f64478c
GKH
1132 int j;
1133
0f64478c
GKH
1134 serial = port->serial;
1135
1136 mos7720_port = usb_get_serial_port_data(port);
1137 if (mos7720_port == NULL)
1138 return;
1139
1140 for (j = 0; j < NUM_URBS; ++j)
1141 usb_kill_urb(mos7720_port->write_urb_pool[j]);
1142
1143 /* Freeing Write URBs */
1144 for (j = 0; j < NUM_URBS; ++j) {
1145 if (mos7720_port->write_urb_pool[j]) {
1146 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
1147 usb_free_urb(mos7720_port->write_urb_pool[j]);
1148 }
1149 }
1150
1151 /* While closing port, shutdown all bulk read, write *
a1cd7e99 1152 * and interrupt read if they exists, otherwise nop */
a1cd7e99 1153 usb_kill_urb(port->write_urb);
a1cd7e99
ON
1154 usb_kill_urb(port->read_urb);
1155
1156 mutex_lock(&serial->disc_mutex);
1157 /* these commands must not be issued if the device has
1158 * been disconnected */
1159 if (!serial->disconnected) {
63b91767
MD
1160 write_mos_reg(serial, port->number - port->serial->minor,
1161 MCR, 0x00);
1162 write_mos_reg(serial, port->number - port->serial->minor,
1163 IER, 0x00);
0f64478c 1164 }
a1cd7e99 1165 mutex_unlock(&serial->disc_mutex);
0f64478c 1166 mos7720_port->open = 0;
0f64478c
GKH
1167}
1168
95da310e 1169static void mos7720_break(struct tty_struct *tty, int break_state)
0f64478c 1170{
95da310e 1171 struct usb_serial_port *port = tty->driver_data;
4da1a17d 1172 unsigned char data;
0f64478c
GKH
1173 struct usb_serial *serial;
1174 struct moschip_port *mos7720_port;
1175
0f64478c
GKH
1176 serial = port->serial;
1177
1178 mos7720_port = usb_get_serial_port_data(port);
1179 if (mos7720_port == NULL)
1180 return;
1181
1182 if (break_state == -1)
1183 data = mos7720_port->shadowLCR | UART_LCR_SBC;
1184 else
1185 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
1186
1187 mos7720_port->shadowLCR = data;
63b91767
MD
1188 write_mos_reg(serial, port->number - port->serial->minor,
1189 LCR, mos7720_port->shadowLCR);
0f64478c
GKH
1190}
1191
1192/*
1193 * mos7720_write_room
1194 * this function is called by the tty driver when it wants to know how many
1195 * bytes of data we can accept for a specific port.
1196 * If successful, we return the amount of room that we have for this port
1197 * Otherwise we return a negative error number.
1198 */
95da310e 1199static int mos7720_write_room(struct tty_struct *tty)
0f64478c 1200{
95da310e 1201 struct usb_serial_port *port = tty->driver_data;
0f64478c
GKH
1202 struct moschip_port *mos7720_port;
1203 int room = 0;
1204 int i;
1205
0f64478c 1206 mos7720_port = usb_get_serial_port_data(port);
9eecf808 1207 if (mos7720_port == NULL)
0f64478c 1208 return -ENODEV;
0f64478c 1209
a5b6f60c 1210 /* FIXME: Locking */
0f64478c 1211 for (i = 0; i < NUM_URBS; ++i) {
4da1a17d
AC
1212 if (mos7720_port->write_urb_pool[i] &&
1213 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
0f64478c
GKH
1214 room += URB_TRANSFER_BUFFER_SIZE;
1215 }
1216
9eecf808 1217 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
0f64478c
GKH
1218 return room;
1219}
1220
95da310e
AC
1221static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
1222 const unsigned char *data, int count)
0f64478c
GKH
1223{
1224 int status;
1225 int i;
1226 int bytes_sent = 0;
1227 int transfer_size;
1228
1229 struct moschip_port *mos7720_port;
1230 struct usb_serial *serial;
1231 struct urb *urb;
1232 const unsigned char *current_position = data;
1233
0f64478c
GKH
1234 serial = port->serial;
1235
1236 mos7720_port = usb_get_serial_port_data(port);
9eecf808 1237 if (mos7720_port == NULL)
0f64478c 1238 return -ENODEV;
0f64478c
GKH
1239
1240 /* try to find a free urb in the list */
1241 urb = NULL;
1242
1243 for (i = 0; i < NUM_URBS; ++i) {
4da1a17d
AC
1244 if (mos7720_port->write_urb_pool[i] &&
1245 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
0f64478c 1246 urb = mos7720_port->write_urb_pool[i];
9eecf808 1247 dev_dbg(&port->dev, "URB:%d\n", i);
0f64478c
GKH
1248 break;
1249 }
1250 }
1251
1252 if (urb == NULL) {
9eecf808 1253 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
0f64478c
GKH
1254 goto exit;
1255 }
1256
1257 if (urb->transfer_buffer == NULL) {
1258 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1259 GFP_KERNEL);
1260 if (urb->transfer_buffer == NULL) {
22a416c4 1261 dev_err_console(port, "%s no more kernel memory...\n",
194343d9 1262 __func__);
0f64478c
GKH
1263 goto exit;
1264 }
1265 }
4da1a17d 1266 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
0f64478c
GKH
1267
1268 memcpy(urb->transfer_buffer, current_position, transfer_size);
59d33f2f 1269 usb_serial_debug_data(&port->dev, __func__, transfer_size,
0f64478c
GKH
1270 urb->transfer_buffer);
1271
1272 /* fill urb with data and submit */
1273 usb_fill_bulk_urb(urb, serial->dev,
1274 usb_sndbulkpipe(serial->dev,
4da1a17d 1275 port->bulk_out_endpointAddress),
0f64478c
GKH
1276 urb->transfer_buffer, transfer_size,
1277 mos7720_bulk_out_data_callback, mos7720_port);
1278
1279 /* send it down the pipe */
4da1a17d 1280 status = usb_submit_urb(urb, GFP_ATOMIC);
0f64478c 1281 if (status) {
22a416c4 1282 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
194343d9 1283 "with status = %d\n", __func__, status);
0f64478c
GKH
1284 bytes_sent = status;
1285 goto exit;
1286 }
1287 bytes_sent = transfer_size;
1288
1289exit:
1290 return bytes_sent;
1291}
1292
95da310e 1293static void mos7720_throttle(struct tty_struct *tty)
0f64478c 1294{
95da310e 1295 struct usb_serial_port *port = tty->driver_data;
0f64478c 1296 struct moschip_port *mos7720_port;
0f64478c
GKH
1297 int status;
1298
0f64478c
GKH
1299 mos7720_port = usb_get_serial_port_data(port);
1300
1301 if (mos7720_port == NULL)
1302 return;
1303
1304 if (!mos7720_port->open) {
9eecf808 1305 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
0f64478c
GKH
1306 return;
1307 }
1308
0f64478c
GKH
1309 /* if we are implementing XON/XOFF, send the stop character */
1310 if (I_IXOFF(tty)) {
1311 unsigned char stop_char = STOP_CHAR(tty);
95da310e 1312 status = mos7720_write(tty, port, &stop_char, 1);
0f64478c
GKH
1313 if (status <= 0)
1314 return;
1315 }
1316
1317 /* if we are implementing RTS/CTS, toggle that line */
1318 if (tty->termios->c_cflag & CRTSCTS) {
1319 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
63b91767
MD
1320 write_mos_reg(port->serial, port->number - port->serial->minor,
1321 MCR, mos7720_port->shadowMCR);
0f64478c
GKH
1322 if (status != 0)
1323 return;
1324 }
1325}
1326
95da310e 1327static void mos7720_unthrottle(struct tty_struct *tty)
0f64478c 1328{
95da310e 1329 struct usb_serial_port *port = tty->driver_data;
0f64478c 1330 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
95da310e 1331 int status;
0f64478c
GKH
1332
1333 if (mos7720_port == NULL)
1334 return;
1335
1336 if (!mos7720_port->open) {
9eecf808 1337 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
0f64478c
GKH
1338 return;
1339 }
1340
0f64478c
GKH
1341 /* if we are implementing XON/XOFF, send the start character */
1342 if (I_IXOFF(tty)) {
1343 unsigned char start_char = START_CHAR(tty);
95da310e 1344 status = mos7720_write(tty, port, &start_char, 1);
0f64478c
GKH
1345 if (status <= 0)
1346 return;
1347 }
1348
1349 /* if we are implementing RTS/CTS, toggle that line */
1350 if (tty->termios->c_cflag & CRTSCTS) {
1351 mos7720_port->shadowMCR |= UART_MCR_RTS;
63b91767
MD
1352 write_mos_reg(port->serial, port->number - port->serial->minor,
1353 MCR, mos7720_port->shadowMCR);
0f64478c
GKH
1354 if (status != 0)
1355 return;
1356 }
1357}
1358
b69578df 1359/* FIXME: this function does not work */
0f64478c
GKH
1360static int set_higher_rates(struct moschip_port *mos7720_port,
1361 unsigned int baud)
1362{
0f64478c
GKH
1363 struct usb_serial_port *port;
1364 struct usb_serial *serial;
1365 int port_number;
63b91767 1366 enum mos_regs sp_reg;
0f64478c
GKH
1367 if (mos7720_port == NULL)
1368 return -EINVAL;
1369
1370 port = mos7720_port->port;
1371 serial = port->serial;
1372
4da1a17d
AC
1373 /***********************************************
1374 * Init Sequence for higher rates
1375 ***********************************************/
9eecf808 1376 dev_dbg(&port->dev, "Sending Setting Commands ..........\n");
0f64478c
GKH
1377 port_number = port->number - port->serial->minor;
1378
63b91767
MD
1379 write_mos_reg(serial, port_number, IER, 0x00);
1380 write_mos_reg(serial, port_number, FCR, 0x00);
1381 write_mos_reg(serial, port_number, FCR, 0xcf);
1382 mos7720_port->shadowMCR = 0x0b;
1383 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1384 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x00);
0f64478c 1385
4da1a17d
AC
1386 /***********************************************
1387 * Set for higher rates *
1388 ***********************************************/
b69578df 1389 /* writing baud rate verbatum into uart clock field clearly not right */
63b91767
MD
1390 if (port_number == 0)
1391 sp_reg = SP1_REG;
1392 else
1393 sp_reg = SP2_REG;
1394 write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
1395 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x03);
1396 mos7720_port->shadowMCR = 0x2b;
1397 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
0f64478c 1398
4da1a17d
AC
1399 /***********************************************
1400 * Set DLL/DLM
1401 ***********************************************/
63b91767
MD
1402 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1403 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1404 write_mos_reg(serial, port_number, DLL, 0x01);
1405 write_mos_reg(serial, port_number, DLM, 0x00);
1406 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1407 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
0f64478c
GKH
1408
1409 return 0;
1410}
1411
1412/* baud rate information */
4da1a17d 1413struct divisor_table_entry {
0f64478c
GKH
1414 __u32 baudrate;
1415 __u16 divisor;
1416};
1417
1418/* Define table of divisors for moschip 7720 hardware *
1419 * These assume a 3.6864MHz crystal, the standard /16, and *
1420 * MCR.7 = 0. */
1421static struct divisor_table_entry divisor_table[] = {
1422 { 50, 2304},
1423 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
1424 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
1425 { 150, 768},
1426 { 300, 384},
1427 { 600, 192},
1428 { 1200, 96},
1429 { 1800, 64},
1430 { 2400, 48},
1431 { 4800, 24},
1432 { 7200, 16},
1433 { 9600, 12},
1434 { 19200, 6},
1435 { 38400, 3},
1436 { 57600, 2},
1437 { 115200, 1},
1438};
1439
1440/*****************************************************************************
1441 * calc_baud_rate_divisor
1442 * this function calculates the proper baud rate divisor for the specified
1443 * baud rate.
1444 *****************************************************************************/
9eecf808 1445static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, int *divisor)
0f64478c
GKH
1446{
1447 int i;
1448 __u16 custom;
1449 __u16 round1;
1450 __u16 round;
1451
1452
9eecf808 1453 dev_dbg(&port->dev, "%s - %d\n", __func__, baudrate);
0f64478c
GKH
1454
1455 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
1456 if (divisor_table[i].baudrate == baudrate) {
1457 *divisor = divisor_table[i].divisor;
1458 return 0;
1459 }
1460 }
1461
4da1a17d
AC
1462 /* After trying for all the standard baud rates *
1463 * Try calculating the divisor for this baud rate */
0f64478c
GKH
1464 if (baudrate > 75 && baudrate < 230400) {
1465 /* get the divisor */
1466 custom = (__u16)(230400L / baudrate);
1467
1468 /* Check for round off */
1469 round1 = (__u16)(2304000L / baudrate);
1470 round = (__u16)(round1 - (custom * 10));
1471 if (round > 4)
1472 custom++;
1473 *divisor = custom;
1474
9eecf808 1475 dev_dbg(&port->dev, "Baud %d = %d\n", baudrate, custom);
0f64478c
GKH
1476 return 0;
1477 }
1478
9eecf808 1479 dev_dbg(&port->dev, "Baud calculation Failed...\n");
0f64478c
GKH
1480 return -EINVAL;
1481}
1482
1483/*
1484 * send_cmd_write_baud_rate
1485 * this function sends the proper command to change the baud rate of the
1486 * specified port.
1487 */
1488static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1489 int baudrate)
1490{
1491 struct usb_serial_port *port;
1492 struct usb_serial *serial;
1493 int divisor;
1494 int status;
0f64478c
GKH
1495 unsigned char number;
1496
1497 if (mos7720_port == NULL)
1498 return -1;
1499
1500 port = mos7720_port->port;
1501 serial = port->serial;
1502
0f64478c 1503 number = port->number - port->serial->minor;
9eecf808 1504 dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudrate);
0f64478c 1505
4da1a17d 1506 /* Calculate the Divisor */
9eecf808 1507 status = calc_baud_rate_divisor(port, baudrate, &divisor);
0f64478c 1508 if (status) {
194343d9 1509 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
0f64478c
GKH
1510 return status;
1511 }
1512
4da1a17d 1513 /* Enable access to divisor latch */
63b91767
MD
1514 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1515 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
0f64478c
GKH
1516
1517 /* Write the divisor */
63b91767
MD
1518 write_mos_reg(serial, number, DLL, (__u8)(divisor & 0xff));
1519 write_mos_reg(serial, number, DLM, (__u8)((divisor & 0xff00) >> 8));
0f64478c 1520
4da1a17d 1521 /* Disable access to divisor latch */
63b91767
MD
1522 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1523 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
0f64478c
GKH
1524
1525 return status;
1526}
1527
1528/*
1529 * change_port_settings
1530 * This routine is called to set the UART on the device to match
1531 * the specified new settings.
1532 */
95da310e
AC
1533static void change_port_settings(struct tty_struct *tty,
1534 struct moschip_port *mos7720_port,
606d099c 1535 struct ktermios *old_termios)
0f64478c
GKH
1536{
1537 struct usb_serial_port *port;
1538 struct usb_serial *serial;
0f64478c
GKH
1539 int baud;
1540 unsigned cflag;
1541 unsigned iflag;
1542 __u8 mask = 0xff;
1543 __u8 lData;
1544 __u8 lParity;
1545 __u8 lStop;
1546 int status;
1547 int port_number;
0f64478c
GKH
1548
1549 if (mos7720_port == NULL)
1550 return ;
1551
1552 port = mos7720_port->port;
1553 serial = port->serial;
1554 port_number = port->number - port->serial->minor;
1555
0f64478c 1556 if (!mos7720_port->open) {
9eecf808 1557 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
0f64478c
GKH
1558 return;
1559 }
1560
0f64478c
GKH
1561 lData = UART_LCR_WLEN8;
1562 lStop = 0x00; /* 1 stop bit */
1563 lParity = 0x00; /* No parity */
1564
1565 cflag = tty->termios->c_cflag;
1566 iflag = tty->termios->c_iflag;
1567
1568 /* Change the number of bits */
1569 switch (cflag & CSIZE) {
1570 case CS5:
1571 lData = UART_LCR_WLEN5;
1572 mask = 0x1f;
1573 break;
1574
1575 case CS6:
1576 lData = UART_LCR_WLEN6;
1577 mask = 0x3f;
1578 break;
1579
1580 case CS7:
1581 lData = UART_LCR_WLEN7;
1582 mask = 0x7f;
1583 break;
1584 default:
1585 case CS8:
1586 lData = UART_LCR_WLEN8;
1587 break;
1588 }
1589
1590 /* Change the Parity bit */
1591 if (cflag & PARENB) {
1592 if (cflag & PARODD) {
1593 lParity = UART_LCR_PARITY;
9eecf808 1594 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
0f64478c
GKH
1595 } else {
1596 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
9eecf808 1597 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
0f64478c
GKH
1598 }
1599
1600 } else {
9eecf808 1601 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
0f64478c
GKH
1602 }
1603
1604 if (cflag & CMSPAR)
1605 lParity = lParity | 0x20;
1606
1607 /* Change the Stop bit */
1608 if (cflag & CSTOPB) {
1609 lStop = UART_LCR_STOP;
9eecf808 1610 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
0f64478c
GKH
1611 } else {
1612 lStop = 0x00;
9eecf808 1613 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
0f64478c
GKH
1614 }
1615
1616#define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1617#define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1618#define LCR_PAR_MASK 0x38 /* Mask for parity field */
1619
1620 /* Update the LCR with the correct value */
4da1a17d 1621 mos7720_port->shadowLCR &=
63b91767 1622 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
0f64478c
GKH
1623 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1624
1625
1626 /* Disable Interrupts */
63b91767
MD
1627 write_mos_reg(serial, port_number, IER, 0x00);
1628 write_mos_reg(serial, port_number, FCR, 0x00);
1629 write_mos_reg(serial, port_number, FCR, 0xcf);
0f64478c
GKH
1630
1631 /* Send the updated LCR value to the mos7720 */
63b91767
MD
1632 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1633 mos7720_port->shadowMCR = 0x0b;
1634 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
0f64478c
GKH
1635
1636 /* set up the MCR register and send it to the mos7720 */
1637 mos7720_port->shadowMCR = UART_MCR_OUT2;
1638 if (cflag & CBAUD)
1639 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1640
1641 if (cflag & CRTSCTS) {
1642 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
4da1a17d
AC
1643 /* To set hardware flow control to the specified *
1644 * serial port, in SP1/2_CONTROL_REG */
63b91767
MD
1645 if (port->number)
1646 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x01);
1647 else
1648 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x02);
1649
1650 } else
0f64478c 1651 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
0f64478c 1652
63b91767 1653 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
0f64478c
GKH
1654
1655 /* Determine divisor based on baud rate */
1656 baud = tty_get_baud_rate(tty);
1657 if (!baud) {
1658 /* pick a default, any default... */
9eecf808 1659 dev_dbg(&port->dev, "Picked default baud...\n");
0f64478c
GKH
1660 baud = 9600;
1661 }
1662
1663 if (baud >= 230400) {
1664 set_higher_rates(mos7720_port, baud);
1665 /* Enable Interrupts */
63b91767 1666 write_mos_reg(serial, port_number, IER, 0x0c);
0f64478c
GKH
1667 return;
1668 }
1669
9eecf808 1670 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
0f64478c 1671 status = send_cmd_write_baud_rate(mos7720_port, baud);
65d063ab
AC
1672 /* FIXME: needs to write actual resulting baud back not just
1673 blindly do so */
1674 if (cflag & CBAUD)
1675 tty_encode_baud_rate(tty, baud, baud);
0f64478c 1676 /* Enable Interrupts */
63b91767 1677 write_mos_reg(serial, port_number, IER, 0x0c);
0f64478c
GKH
1678
1679 if (port->read_urb->status != -EINPROGRESS) {
0f64478c
GKH
1680 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1681 if (status)
9eecf808 1682 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
0f64478c 1683 }
0f64478c
GKH
1684}
1685
1686/*
1687 * mos7720_set_termios
1688 * this function is called by the tty driver when it wants to change the
1689 * termios structure.
1690 */
95da310e
AC
1691static void mos7720_set_termios(struct tty_struct *tty,
1692 struct usb_serial_port *port, struct ktermios *old_termios)
0f64478c
GKH
1693{
1694 int status;
1695 unsigned int cflag;
1696 struct usb_serial *serial;
1697 struct moschip_port *mos7720_port;
0f64478c
GKH
1698
1699 serial = port->serial;
1700
1701 mos7720_port = usb_get_serial_port_data(port);
1702
1703 if (mos7720_port == NULL)
1704 return;
1705
0f64478c 1706 if (!mos7720_port->open) {
9eecf808 1707 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
0f64478c
GKH
1708 return;
1709 }
1710
9eecf808 1711 dev_dbg(&port->dev, "setting termios - ASPIRE\n");
0f64478c
GKH
1712
1713 cflag = tty->termios->c_cflag;
1714
9eecf808
GKH
1715 dev_dbg(&port->dev, "%s - cflag %08x iflag %08x\n", __func__,
1716 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
0f64478c 1717
9eecf808
GKH
1718 dev_dbg(&port->dev, "%s - old cflag %08x old iflag %08x\n", __func__,
1719 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
0f64478c
GKH
1720
1721 /* change the port settings to the new ones specified */
95da310e 1722 change_port_settings(tty, mos7720_port, old_termios);
0f64478c 1723
4da1a17d 1724 if (port->read_urb->status != -EINPROGRESS) {
0f64478c
GKH
1725 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1726 if (status)
9eecf808 1727 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
0f64478c 1728 }
0f64478c
GKH
1729}
1730
1731/*
1732 * get_lsr_info - get line status register info
1733 *
1734 * Purpose: Let user call ioctl() to get info when the UART physically
1735 * is emptied. On bus types like RS485, the transmitter must
1736 * release the bus after transmitting. This must be done when
1737 * the transmit shift register is empty, not be done when the
1738 * transmit holding register is empty. This functionality
1739 * allows an RS485 driver to be written in user space.
1740 */
4da1a17d
AC
1741static int get_lsr_info(struct tty_struct *tty,
1742 struct moschip_port *mos7720_port, unsigned int __user *value)
0f64478c 1743{
2f9ea55c 1744 struct usb_serial_port *port = tty->driver_data;
0f64478c 1745 unsigned int result = 0;
2f9ea55c
KS
1746 unsigned char data = 0;
1747 int port_number = port->number - port->serial->minor;
1748 int count;
0f64478c 1749
95da310e 1750 count = mos7720_chars_in_buffer(tty);
0f64478c 1751 if (count == 0) {
63b91767 1752 read_mos_reg(port->serial, port_number, LSR, &data);
2f9ea55c
KS
1753 if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1754 == (UART_LSR_TEMT | UART_LSR_THRE)) {
9eecf808 1755 dev_dbg(&port->dev, "%s -- Empty\n", __func__);
2f9ea55c
KS
1756 result = TIOCSER_TEMT;
1757 }
0f64478c 1758 }
0f64478c
GKH
1759 if (copy_to_user(value, &result, sizeof(int)))
1760 return -EFAULT;
1761 return 0;
1762}
1763
60b33c13 1764static int mos7720_tiocmget(struct tty_struct *tty)
0f608f89
KS
1765{
1766 struct usb_serial_port *port = tty->driver_data;
1767 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1768 unsigned int result = 0;
1769 unsigned int mcr ;
1770 unsigned int msr ;
1771
0f608f89
KS
1772 mcr = mos7720_port->shadowMCR;
1773 msr = mos7720_port->shadowMSR;
1774
1775 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1776 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1777 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1778 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
1779 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1780 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
1781
0f608f89
KS
1782 return result;
1783}
1784
20b9d177 1785static int mos7720_tiocmset(struct tty_struct *tty,
63b91767 1786 unsigned int set, unsigned int clear)
0f608f89
KS
1787{
1788 struct usb_serial_port *port = tty->driver_data;
1789 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1790 unsigned int mcr ;
0f608f89
KS
1791
1792 mcr = mos7720_port->shadowMCR;
1793
1794 if (set & TIOCM_RTS)
1795 mcr |= UART_MCR_RTS;
1796 if (set & TIOCM_DTR)
1797 mcr |= UART_MCR_DTR;
1798 if (set & TIOCM_LOOP)
1799 mcr |= UART_MCR_LOOP;
1800
1801 if (clear & TIOCM_RTS)
1802 mcr &= ~UART_MCR_RTS;
1803 if (clear & TIOCM_DTR)
1804 mcr &= ~UART_MCR_DTR;
1805 if (clear & TIOCM_LOOP)
1806 mcr &= ~UART_MCR_LOOP;
1807
1808 mos7720_port->shadowMCR = mcr;
63b91767
MD
1809 write_mos_reg(port->serial, port->number - port->serial->minor,
1810 MCR, mos7720_port->shadowMCR);
0f608f89
KS
1811
1812 return 0;
1813}
1814
0bca1b91
AC
1815static int mos7720_get_icount(struct tty_struct *tty,
1816 struct serial_icounter_struct *icount)
1817{
1818 struct usb_serial_port *port = tty->driver_data;
1819 struct moschip_port *mos7720_port;
1820 struct async_icount cnow;
1821
1822 mos7720_port = usb_get_serial_port_data(port);
1823 cnow = mos7720_port->icount;
1824
1825 icount->cts = cnow.cts;
1826 icount->dsr = cnow.dsr;
1827 icount->rng = cnow.rng;
1828 icount->dcd = cnow.dcd;
1829 icount->rx = cnow.rx;
1830 icount->tx = cnow.tx;
1831 icount->frame = cnow.frame;
1832 icount->overrun = cnow.overrun;
1833 icount->parity = cnow.parity;
1834 icount->brk = cnow.brk;
1835 icount->buf_overrun = cnow.buf_overrun;
1836
9eecf808
GKH
1837 dev_dbg(&port->dev, "%s TIOCGICOUNT RX=%d, TX=%d\n", __func__,
1838 icount->rx, icount->tx);
0bca1b91
AC
1839 return 0;
1840}
1841
0f64478c
GKH
1842static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1843 unsigned int __user *value)
1844{
0bca1b91 1845 unsigned int mcr;
0f64478c 1846 unsigned int arg;
0f64478c
GKH
1847
1848 struct usb_serial_port *port;
1849
1850 if (mos7720_port == NULL)
1851 return -1;
1852
4da1a17d 1853 port = (struct usb_serial_port *)mos7720_port->port;
0f64478c
GKH
1854 mcr = mos7720_port->shadowMCR;
1855
1856 if (copy_from_user(&arg, value, sizeof(int)))
1857 return -EFAULT;
1858
1859 switch (cmd) {
1860 case TIOCMBIS:
1861 if (arg & TIOCM_RTS)
1862 mcr |= UART_MCR_RTS;
1863 if (arg & TIOCM_DTR)
1864 mcr |= UART_MCR_RTS;
1865 if (arg & TIOCM_LOOP)
1866 mcr |= UART_MCR_LOOP;
1867 break;
1868
1869 case TIOCMBIC:
1870 if (arg & TIOCM_RTS)
1871 mcr &= ~UART_MCR_RTS;
1872 if (arg & TIOCM_DTR)
1873 mcr &= ~UART_MCR_RTS;
1874 if (arg & TIOCM_LOOP)
1875 mcr &= ~UART_MCR_LOOP;
1876 break;
1877
0f64478c
GKH
1878 }
1879
1880 mos7720_port->shadowMCR = mcr;
63b91767
MD
1881 write_mos_reg(port->serial, port->number - port->serial->minor,
1882 MCR, mos7720_port->shadowMCR);
0f64478c
GKH
1883
1884 return 0;
1885}
1886
0f64478c
GKH
1887static int get_serial_info(struct moschip_port *mos7720_port,
1888 struct serial_struct __user *retinfo)
1889{
1890 struct serial_struct tmp;
1891
1892 if (!retinfo)
1893 return -EFAULT;
1894
1895 memset(&tmp, 0, sizeof(tmp));
1896
1897 tmp.type = PORT_16550A;
1898 tmp.line = mos7720_port->port->serial->minor;
1899 tmp.port = mos7720_port->port->number;
1900 tmp.irq = 0;
1901 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
4da1a17d 1902 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
0f64478c
GKH
1903 tmp.baud_base = 9600;
1904 tmp.close_delay = 5*HZ;
1905 tmp.closing_wait = 30*HZ;
1906
1907 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1908 return -EFAULT;
1909 return 0;
1910}
1911
00a0d0d6 1912static int mos7720_ioctl(struct tty_struct *tty,
0f64478c
GKH
1913 unsigned int cmd, unsigned long arg)
1914{
95da310e 1915 struct usb_serial_port *port = tty->driver_data;
0f64478c
GKH
1916 struct moschip_port *mos7720_port;
1917 struct async_icount cnow;
1918 struct async_icount cprev;
0f64478c
GKH
1919
1920 mos7720_port = usb_get_serial_port_data(port);
1921 if (mos7720_port == NULL)
1922 return -ENODEV;
1923
9eecf808 1924 dev_dbg(&port->dev, "%s - cmd = 0x%x", __func__, cmd);
0f64478c
GKH
1925
1926 switch (cmd) {
0f64478c 1927 case TIOCSERGETLSR:
9eecf808 1928 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
4da1a17d
AC
1929 return get_lsr_info(tty, mos7720_port,
1930 (unsigned int __user *)arg);
0f64478c 1931
95da310e 1932 /* FIXME: These should be using the mode methods */
0f64478c
GKH
1933 case TIOCMBIS:
1934 case TIOCMBIC:
9eecf808 1935 dev_dbg(&port->dev, "%s TIOCMSET/TIOCMBIC/TIOCMSET\n", __func__);
0f64478c
GKH
1936 return set_modem_info(mos7720_port, cmd,
1937 (unsigned int __user *)arg);
1938
0f64478c 1939 case TIOCGSERIAL:
9eecf808 1940 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
0f64478c
GKH
1941 return get_serial_info(mos7720_port,
1942 (struct serial_struct __user *)arg);
1943
0f64478c 1944 case TIOCMIWAIT:
9eecf808 1945 dev_dbg(&port->dev, "%s TIOCMIWAIT\n", __func__);
0f64478c
GKH
1946 cprev = mos7720_port->icount;
1947 while (1) {
1948 if (signal_pending(current))
1949 return -ERESTARTSYS;
1950 cnow = mos7720_port->icount;
1951 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1952 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1953 return -EIO; /* no change => error */
1954 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1955 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1956 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
4da1a17d 1957 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
0f64478c
GKH
1958 return 0;
1959 }
1960 cprev = cnow;
1961 }
1962 /* NOTREACHED */
1963 break;
0f64478c
GKH
1964 }
1965
1966 return -ENOIOCTLCMD;
1967}
1968
1969static int mos7720_startup(struct usb_serial *serial)
1970{
0f64478c
GKH
1971 struct moschip_port *mos7720_port;
1972 struct usb_device *dev;
1973 int i;
1974 char data;
91f58ae6 1975 u16 product;
b69578df 1976 int ret_val;
0f64478c 1977
91f58ae6 1978 product = le16_to_cpu(serial->dev->descriptor.idProduct);
0f64478c
GKH
1979 dev = serial->dev;
1980
fb088e33
MD
1981 /*
1982 * The 7715 uses the first bulk in/out endpoint pair for the parallel
1983 * port, and the second for the serial port. Because the usbserial core
1984 * assumes both pairs are serial ports, we must engage in a bit of
1985 * subterfuge and swap the pointers for ports 0 and 1 in order to make
1986 * port 0 point to the serial port. However, both moschip devices use a
1987 * single interrupt-in endpoint for both ports (as mentioned a little
1988 * further down), and this endpoint was assigned to port 0. So after
1989 * the swap, we must copy the interrupt endpoint elements from port 1
1990 * (as newly assigned) to port 0, and null out port 1 pointers.
1991 */
1992 if (product == MOSCHIP_DEVICE_ID_7715) {
1993 struct usb_serial_port *tmp = serial->port[0];
1994 serial->port[0] = serial->port[1];
1995 serial->port[1] = tmp;
1996 serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb;
1997 serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer;
1998 serial->port[0]->interrupt_in_endpointAddress =
1999 tmp->interrupt_in_endpointAddress;
2000 serial->port[1]->interrupt_in_urb = NULL;
2001 serial->port[1]->interrupt_in_buffer = NULL;
2002 }
2003
0f64478c 2004
b69578df 2005 /* set up serial port private structures */
0f64478c
GKH
2006 for (i = 0; i < serial->num_ports; ++i) {
2007 mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2008 if (mos7720_port == NULL) {
194343d9 2009 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
0f64478c
GKH
2010 return -ENOMEM;
2011 }
2012
2013 /* Initialize all port interrupt end point to port 0 int
2014 * endpoint. Our device has only one interrupt endpoint
fb088e33 2015 * common to all ports */
4da1a17d
AC
2016 serial->port[i]->interrupt_in_endpointAddress =
2017 serial->port[0]->interrupt_in_endpointAddress;
0f64478c
GKH
2018
2019 mos7720_port->port = serial->port[i];
2020 usb_set_serial_port_data(serial->port[i], mos7720_port);
2021
9eecf808
GKH
2022 dev_dbg(&dev->dev, "port number is %d\n", serial->port[i]->number);
2023 dev_dbg(&dev->dev, "serial number is %d\n", serial->minor);
0f64478c
GKH
2024 }
2025
2026
2027 /* setting configuration feature to one */
2028 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
4da1a17d 2029 (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5*HZ);
0f64478c 2030
b69578df
MD
2031 /* start the interrupt urb */
2032 ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
2033 if (ret_val)
2034 dev_err(&dev->dev,
2035 "%s - Error %d submitting control urb\n",
2036 __func__, ret_val);
2037
2038#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
2039 if (product == MOSCHIP_DEVICE_ID_7715) {
2040 ret_val = mos7715_parport_init(serial);
2041 if (ret_val < 0)
2042 return ret_val;
2043 }
2044#endif
4da1a17d 2045 /* LSR For Port 1 */
63b91767 2046 read_mos_reg(serial, 0, LSR, &data);
9eecf808 2047 dev_dbg(&dev->dev, "LSR:%x\n", data);
0f64478c
GKH
2048
2049 return 0;
2050}
2051
f9c99bb8 2052static void mos7720_release(struct usb_serial *serial)
0f64478c
GKH
2053{
2054 int i;
2055
b69578df
MD
2056#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
2057 /* close the parallel port */
2058
2059 if (le16_to_cpu(serial->dev->descriptor.idProduct)
2060 == MOSCHIP_DEVICE_ID_7715) {
2061 struct urbtracker *urbtrack;
2062 unsigned long flags;
2063 struct mos7715_parport *mos_parport =
2064 usb_get_serial_data(serial);
2065
2066 /* prevent NULL ptr dereference in port callbacks */
2067 spin_lock(&release_lock);
2068 mos_parport->pp->private_data = NULL;
2069 spin_unlock(&release_lock);
2070
2071 /* wait for synchronous usb calls to return */
2072 if (mos_parport->msg_pending)
2073 wait_for_completion_timeout(&mos_parport->syncmsg_compl,
2074 MOS_WDR_TIMEOUT);
2075
2076 parport_remove_port(mos_parport->pp);
2077 usb_set_serial_data(serial, NULL);
2078 mos_parport->serial = NULL;
2079
2080 /* if tasklet currently scheduled, wait for it to complete */
2081 tasklet_kill(&mos_parport->urb_tasklet);
2082
2083 /* unlink any urbs sent by the tasklet */
2084 spin_lock_irqsave(&mos_parport->listlock, flags);
2085 list_for_each_entry(urbtrack,
2086 &mos_parport->active_urbs,
2087 urblist_entry)
2088 usb_unlink_urb(urbtrack->urb);
2089 spin_unlock_irqrestore(&mos_parport->listlock, flags);
2090
2091 kref_put(&mos_parport->ref_count, destroy_mos_parport);
2092 }
2093#endif
0f64478c 2094 /* free private structure allocated for serial port */
f9c99bb8 2095 for (i = 0; i < serial->num_ports; ++i)
0f64478c 2096 kfree(usb_get_serial_port_data(serial->port[i]));
0f64478c
GKH
2097}
2098
2099static struct usb_serial_driver moschip7720_2port_driver = {
2100 .driver = {
2101 .owner = THIS_MODULE,
2102 .name = "moschip7720",
2103 },
2104 .description = "Moschip 2 port adapter",
68e24113 2105 .id_table = id_table,
fb088e33 2106 .calc_num_ports = mos77xx_calc_num_ports,
0f64478c
GKH
2107 .open = mos7720_open,
2108 .close = mos7720_close,
2109 .throttle = mos7720_throttle,
2110 .unthrottle = mos7720_unthrottle,
fb088e33 2111 .probe = mos77xx_probe,
0f64478c 2112 .attach = mos7720_startup,
f9c99bb8 2113 .release = mos7720_release,
0f64478c 2114 .ioctl = mos7720_ioctl,
0f608f89
KS
2115 .tiocmget = mos7720_tiocmget,
2116 .tiocmset = mos7720_tiocmset,
0bca1b91 2117 .get_icount = mos7720_get_icount,
0f64478c
GKH
2118 .set_termios = mos7720_set_termios,
2119 .write = mos7720_write,
2120 .write_room = mos7720_write_room,
2121 .chars_in_buffer = mos7720_chars_in_buffer,
2122 .break_ctl = mos7720_break,
2123 .read_bulk_callback = mos7720_bulk_in_callback,
fb088e33 2124 .read_int_callback = NULL /* dynamically assigned in probe() */
0f64478c
GKH
2125};
2126
4d2a7aff
AS
2127static struct usb_serial_driver * const serial_drivers[] = {
2128 &moschip7720_2port_driver, NULL
2129};
2130
68e24113 2131module_usb_serial_driver(serial_drivers, id_table);
0f64478c 2132
4da1a17d
AC
2133MODULE_AUTHOR(DRIVER_AUTHOR);
2134MODULE_DESCRIPTION(DRIVER_DESC);
0f64478c
GKH
2135MODULE_LICENSE("GPL");
2136
2137module_param(debug, bool, S_IRUGO | S_IWUSR);
2138MODULE_PARM_DESC(debug, "Debug enabled or not");