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