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