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