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