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