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