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