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