]> git.decadent.org.uk Git - exar-uart-driver.git/blob - vizzini.c
Sync code with cdc-acm.{c,h} in Linux 4.3
[exar-uart-driver.git] / vizzini.c
1 /*
2  * vizzini.c
3  *
4  * Copyright (c) 1999 Armin Fuerst      <fuerst@in.tum.de>
5  * Copyright (c) 1999 Pavel Machek      <pavel@ucw.cz>
6  * Copyright (c) 1999 Johannes Erdfelt  <johannes@erdfelt.com>
7  * Copyright (c) 2000 Vojtech Pavlik    <vojtech@suse.cz>
8  * Copyright (c) 2004 Oliver Neukum     <oliver@neukum.name>
9  * Copyright (c) 2005 David Kubicek     <dave@awk.cz>
10  * Copyright (c) 2011 Johan Hovold      <jhovold@gmail.com>
11  * Copyright (c) 2013 Exar Corporation, Inc.
12  *
13  * ChangeLog:
14  * 
15  *            v1.0- Support for Kernel 3.5 and newer,
16  *                  based on cdc-acm.c sample of 3.6 kernel.
17  */
18
19
20 /*
21  * This program is free software; you can redistribute it and/or modify
22  * it under the terms of the GNU General Public License as published by
23  * the Free Software Foundation; either version 2 of the License, or
24  * (at your option) any later version.
25  *
26  * This program is distributed in the hope that it will be useful,
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29  * GNU General Public License for more details.
30  *
31  * You should have received a copy of the GNU General Public License
32  * along with this program; if not, write to the Free Software
33  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34  */
35
36 #undef DEBUG
37 #undef VERBOSE_DEBUG
38
39 #include <linux/kernel.h>
40 #include <linux/errno.h>
41 #include <linux/init.h>
42 #include <linux/slab.h>
43 #include <linux/tty.h>
44 #include <linux/serial.h>
45 #include <linux/tty_driver.h>
46 #include <linux/tty_flip.h>
47 #include <linux/module.h>
48 #include <linux/mutex.h>
49 #include <linux/uaccess.h>
50 #include <linux/usb.h>
51 #include <linux/usb/cdc.h>
52 #include <asm/byteorder.h>
53 #include <asm/unaligned.h>
54 #include <linux/idr.h>
55 #include <linux/list.h>
56
57 #include "vizzini.h"
58 #include "vzioctl.h"
59
60 #define DRIVER_AUTHOR "Ravi Reddy"
61 #define DRIVER_DESC "Exar USB UART Driver for XR21V141x "
62
63 static struct usb_driver acm_driver;
64 static struct tty_driver *acm_tty_driver;
65
66 static DEFINE_IDR(acm_minors);
67 static DEFINE_MUTEX(acm_minors_lock);
68
69 static void acm_tty_set_termios(struct tty_struct *tty,
70                                 struct ktermios *termios_old);
71
72 /*
73  * acm_minors accessors
74  */
75
76 /*
77  * Look up an ACM structure by minor. If found and not disconnected, increment
78  * its refcount and return it with its mutex held.
79  */
80 static struct acm *acm_get_by_minor(unsigned int minor)
81 {
82         struct acm *acm;
83
84         mutex_lock(&acm_minors_lock);
85         acm = idr_find(&acm_minors, minor);
86         if (acm) {
87                 mutex_lock(&acm->mutex);
88                 if (acm->disconnected) {
89                         mutex_unlock(&acm->mutex);
90                         acm = NULL;
91                 } else {
92                         tty_port_get(&acm->port);
93                         mutex_unlock(&acm->mutex);
94                 }
95         }
96         mutex_unlock(&acm_minors_lock);
97         return acm;
98 }
99
100 /*
101  * Try to find an available minor number and if found, associate it with 'acm'.
102  */
103 static int acm_alloc_minor(struct acm *acm)
104 {
105         int minor;
106
107         mutex_lock(&acm_minors_lock);
108         minor = idr_alloc(&acm_minors, acm, 0, ACM_TTY_MINORS, GFP_KERNEL);
109         mutex_unlock(&acm_minors_lock);
110
111         return minor;
112 }
113
114 /* Release the minor number associated with 'acm'.  */
115 static void acm_release_minor(struct acm *acm)
116 {
117         mutex_lock(&acm_minors_lock);
118         idr_remove(&acm_minors, acm->minor);
119         mutex_unlock(&acm_minors_lock);
120 }
121
122 /*
123  * Functions for ACM control messages.
124  */
125
126 static int acm_ctrl_msg(struct acm *acm, int request, int value,
127                                                         void *buf, int len)
128 {
129         int retval;
130
131         retval = usb_autopm_get_interface(acm->control);
132         if (retval)
133                 return retval;
134
135         retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
136                 request, USB_RT_ACM, value,
137                 acm->control->altsetting[0].desc.bInterfaceNumber,
138                 buf, len, 5000);
139
140         dev_dbg(&acm->control->dev,
141                         "%s - rq 0x%02x, val %#x, len %#x, result %d\n",
142                         __func__, request, value, len, retval);
143
144         usb_autopm_put_interface(acm->control);
145
146         return retval < 0 ? retval : 0;
147 }
148
149 /* devices aren't required to support these requests.
150  * the cdc acm descriptor tells whether they do...
151  */
152 static inline int acm_set_control(struct acm *acm, int control)
153 {
154         if (acm->quirks & QUIRK_CONTROL_LINE_STATE)
155                 return -EOPNOTSUPP;
156
157         return acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE,
158                         control, NULL, 0);
159 }
160
161 #define acm_set_line(acm, line) \
162         acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
163 #define acm_send_break(acm, ms) \
164         acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
165
166 /*
167  * Write buffer management.
168  * All of these assume proper locks taken by the caller.
169  */
170
171 static int acm_wb_alloc(struct acm *acm)
172 {
173         int i, wbn;
174         struct acm_wb *wb;
175
176         wbn = 0;
177         i = 0;
178         for (;;) {
179                 wb = &acm->wb[wbn];
180                 if (!wb->use) {
181                         wb->use = 1;
182                         return wbn;
183                 }
184                 wbn = (wbn + 1) % ACM_NW;
185                 if (++i >= ACM_NW)
186                         return -1;
187         }
188 }
189
190 static int acm_wb_is_avail(struct acm *acm)
191 {
192         int i, n;
193         unsigned long flags;
194
195         n = ACM_NW;
196         spin_lock_irqsave(&acm->write_lock, flags);
197         for (i = 0; i < ACM_NW; i++)
198                 n -= acm->wb[i].use;
199         spin_unlock_irqrestore(&acm->write_lock, flags);
200         return n;
201 }
202
203 /*
204  * Finish write. Caller must hold acm->write_lock
205  */
206 static void acm_write_done(struct acm *acm, struct acm_wb *wb)
207 {
208         wb->use = 0;
209         acm->transmitting--;
210         usb_autopm_put_interface_async(acm->control);
211 }
212
213 /*
214  * Poke write.
215  *
216  * the caller is responsible for locking
217  */
218
219 static int acm_start_wb(struct acm *acm, struct acm_wb *wb)
220 {
221         int rc;
222
223         acm->transmitting++;
224
225         wb->urb->transfer_buffer = wb->buf;
226         wb->urb->transfer_dma = wb->dmah;
227         wb->urb->transfer_buffer_length = wb->len;
228         wb->urb->dev = acm->dev;
229
230         rc = usb_submit_urb(wb->urb, GFP_ATOMIC);
231         if (rc < 0) {
232                 dev_err(&acm->data->dev,
233                         "%s - usb_submit_urb(write bulk) failed: %d\n",
234                         __func__, rc);
235                 acm_write_done(acm, wb);
236         }
237         return rc;
238 }
239
240 /*
241  * attributes exported through sysfs
242  */
243 static ssize_t show_caps
244 (struct device *dev, struct device_attribute *attr, char *buf)
245 {
246         struct usb_interface *intf = to_usb_interface(dev);
247         struct acm *acm = usb_get_intfdata(intf);
248
249         return sprintf(buf, "%d", acm->ctrl_caps);
250 }
251 static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
252
253 static ssize_t show_country_codes
254 (struct device *dev, struct device_attribute *attr, char *buf)
255 {
256         struct usb_interface *intf = to_usb_interface(dev);
257         struct acm *acm = usb_get_intfdata(intf);
258
259         memcpy(buf, acm->country_codes, acm->country_code_size);
260         return acm->country_code_size;
261 }
262
263 static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
264
265 static ssize_t show_country_rel_date
266 (struct device *dev, struct device_attribute *attr, char *buf)
267 {
268         struct usb_interface *intf = to_usb_interface(dev);
269         struct acm *acm = usb_get_intfdata(intf);
270
271         return sprintf(buf, "%d", acm->country_rel_date);
272 }
273
274 static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
275 /*
276  * Interrupt handlers for various ACM device responses
277  */
278
279 /* control interface reports status changes with "interrupt" transfers */
280 static void acm_ctrl_irq(struct urb *urb)
281 {
282         struct acm *acm = urb->context;
283         struct usb_cdc_notification *dr = urb->transfer_buffer;
284         unsigned char *data;
285         int newctrl;
286         int difference;
287         int retval;
288         int status = urb->status;
289
290         switch (status) {
291         case 0:
292                 /* success */
293                 break;
294         case -ECONNRESET:
295         case -ENOENT:
296         case -ESHUTDOWN:
297                 /* this urb is terminated, clean up */
298                 dev_dbg(&acm->control->dev,
299                                 "%s - urb shutting down with status: %d\n",
300                                 __func__, status);
301                 return;
302         default:
303                 dev_dbg(&acm->control->dev,
304                                 "%s - nonzero urb status received: %d\n",
305                                 __func__, status);
306                 goto exit;
307         }
308
309         usb_mark_last_busy(acm->dev);
310
311         data = (unsigned char *)(dr + 1);
312         switch (dr->bNotificationType) {
313         case USB_CDC_NOTIFY_NETWORK_CONNECTION:
314                 dev_dbg(&acm->control->dev, "%s - network connection: %d\n",
315                                                         __func__, dr->wValue);
316                 break;
317
318         case USB_CDC_NOTIFY_SERIAL_STATE:
319                 newctrl = get_unaligned_le16(data);
320
321                 if (!acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
322                         dev_dbg(&acm->control->dev, "%s - calling hangup\n",
323                                         __func__);
324                         tty_port_tty_hangup(&acm->port, false);
325                 }
326
327                 difference = acm->ctrlin ^ newctrl;
328                 spin_lock(&acm->read_lock);
329                 acm->ctrlin = newctrl;
330                 acm->oldcount = acm->iocount;
331
332                 if (difference & ACM_CTRL_DSR)
333                         acm->iocount.dsr++;
334                 if (difference & ACM_CTRL_BRK)
335                         acm->iocount.brk++;
336                 if (difference & ACM_CTRL_RI)
337                         acm->iocount.rng++;
338                 if (difference & ACM_CTRL_DCD)
339                         acm->iocount.dcd++;
340                 if (difference & ACM_CTRL_FRAMING)
341                         acm->iocount.frame++;
342                 if (difference & ACM_CTRL_PARITY)
343                         acm->iocount.parity++;
344                 if (difference & ACM_CTRL_OVERRUN)
345                         acm->iocount.overrun++;
346                 spin_unlock(&acm->read_lock);
347
348                 if (difference)
349                         wake_up_all(&acm->wioctl);
350
351                 break;
352
353         default:
354                 dev_dbg(&acm->control->dev,
355                         "%s - unknown notification %d received: index %d "
356                         "len %d data0 %d data1 %d\n",
357                         __func__,
358                         dr->bNotificationType, dr->wIndex,
359                         dr->wLength, data[0], data[1]);
360                 break;
361         }
362 exit:
363         retval = usb_submit_urb(urb, GFP_ATOMIC);
364         if (retval && retval != -EPERM)
365                 dev_err(&acm->control->dev, "%s - usb_submit_urb failed: %d\n",
366                                                         __func__, retval);
367 }
368
369 static int acm_submit_read_urb(struct acm *acm, int index, gfp_t mem_flags)
370 {
371         int res;
372
373         if (!test_and_clear_bit(index, &acm->read_urbs_free))
374                 return 0;
375
376         dev_vdbg(&acm->data->dev, "%s - urb %d\n", __func__, index);
377
378         res = usb_submit_urb(acm->read_urbs[index], mem_flags);
379         if (res) {
380                 if (res != -EPERM) {
381                         dev_err(&acm->data->dev,
382                                         "%s - usb_submit_urb failed: %d\n",
383                                         __func__, res);
384                 }
385                 set_bit(index, &acm->read_urbs_free);
386                 return res;
387         }
388
389         return 0;
390 }
391
392 static int acm_submit_read_urbs(struct acm *acm, gfp_t mem_flags)
393 {
394         int res;
395         int i;
396
397         for (i = 0; i < acm->rx_buflimit; ++i) {
398                 res = acm_submit_read_urb(acm, i, mem_flags);
399                 if (res)
400                         return res;
401         }
402
403         return 0;
404 }
405
406 static void acm_process_read_urb(struct acm *acm, struct urb *urb)
407 {
408         if (!urb->actual_length)
409                 return;
410
411         tty_insert_flip_string(&acm->port, urb->transfer_buffer,
412                         urb->actual_length);
413         tty_flip_buffer_push(&acm->port);
414 }
415
416 static void acm_read_bulk_callback(struct urb *urb)
417 {
418         struct acm_rb *rb = urb->context;
419         struct acm *acm = rb->instance;
420         unsigned long flags;
421         int status = urb->status;
422
423         dev_vdbg(&acm->data->dev, "%s - urb %d, len %d\n", __func__,
424                                         rb->index, urb->actual_length);
425
426         if (!acm->dev) {
427                 set_bit(rb->index, &acm->read_urbs_free);
428                 dev_dbg(&acm->data->dev, "%s - disconnected\n", __func__);
429                 return;
430         }
431
432         if (status) {
433                 set_bit(rb->index, &acm->read_urbs_free);
434                 dev_dbg(&acm->data->dev, "%s - non-zero urb status: %d\n",
435                                                         __func__, status);
436                 return;
437         }
438
439         usb_mark_last_busy(acm->dev);
440
441         acm_process_read_urb(acm, urb);
442         /*
443          * Unthrottle may run on another CPU which needs to see events
444          * in the same order. Submission has an implict barrier
445          */
446         smp_mb__before_atomic();
447         set_bit(rb->index, &acm->read_urbs_free);
448
449         /* throttle device if requested by tty */
450         spin_lock_irqsave(&acm->read_lock, flags);
451         acm->throttled = acm->throttle_req;
452         if (!acm->throttled) {
453                 spin_unlock_irqrestore(&acm->read_lock, flags);
454                 acm_submit_read_urb(acm, rb->index, GFP_ATOMIC);
455         } else {
456                 spin_unlock_irqrestore(&acm->read_lock, flags);
457         }
458 }
459
460 /* data interface wrote those outgoing bytes */
461 static void acm_write_bulk(struct urb *urb)
462 {
463         struct acm_wb *wb = urb->context;
464         struct acm *acm = wb->instance;
465         unsigned long flags;
466         int status = urb->status;
467
468         if (status || (urb->actual_length != urb->transfer_buffer_length))
469                 dev_vdbg(&acm->data->dev, "%s - len %d/%d, status %d\n",
470                         __func__,
471                         urb->actual_length,
472                         urb->transfer_buffer_length,
473                         status);
474
475         spin_lock_irqsave(&acm->write_lock, flags);
476         acm_write_done(acm, wb);
477         spin_unlock_irqrestore(&acm->write_lock, flags);
478         schedule_work(&acm->work);
479 }
480
481 static void acm_softint(struct work_struct *work)
482 {
483         struct acm *acm = container_of(work, struct acm, work);
484
485         dev_vdbg(&acm->data->dev, "%s\n", __func__);
486
487         tty_port_tty_wakeup(&acm->port);
488 }
489
490 /*
491  * TTY handlers
492  */
493
494 static int acm_tty_install(struct tty_driver *driver, struct tty_struct *tty)
495 {
496         struct acm *acm;
497         int retval;
498
499         dev_dbg(tty->dev, "%s\n", __func__);
500
501         acm = acm_get_by_minor(tty->index);
502         if (!acm)
503                 return -ENODEV;
504
505         retval = tty_standard_install(driver, tty);
506         if (retval)
507                 goto error_init_termios;
508
509         tty->driver_data = acm;
510
511         return 0;
512
513 error_init_termios:
514         tty_port_put(&acm->port);
515         return retval;
516 }
517
518 static int acm_tty_open(struct tty_struct *tty, struct file *filp)
519 {
520         struct acm *acm = tty->driver_data;
521
522         dev_dbg(tty->dev, "%s\n", __func__);
523
524         return tty_port_open(&acm->port, tty, filp);
525 }
526
527 static void acm_port_dtr_rts(struct tty_port *port, int raise)
528 {
529         struct acm *acm = container_of(port, struct acm, port);
530         int val;
531         int res;
532
533         if (raise)
534                 val = ACM_CTRL_DTR | ACM_CTRL_RTS;
535         else
536                 val = 0;
537
538         /* FIXME: add missing ctrlout locking throughout driver */
539         acm->ctrlout = val;
540
541         res = acm_set_control(acm, val);
542         if (res && (acm->ctrl_caps & USB_CDC_CAP_LINE))
543                 dev_err(&acm->control->dev, "failed to set dtr/rts\n");
544 }
545
546 static int acm_port_activate(struct tty_port *port, struct tty_struct *tty)
547 {
548         struct acm *acm = container_of(port, struct acm, port);
549         int retval = -ENODEV;
550         int i;
551
552         dev_dbg(&acm->control->dev, "%s\n", __func__);
553
554         mutex_lock(&acm->mutex);
555         if (acm->disconnected)
556                 goto disconnected;
557
558         retval = usb_autopm_get_interface(acm->control);
559         if (retval)
560                 goto error_get_interface;
561
562         /*
563          * FIXME: Why do we need this? Allocating 64K of physically contiguous
564          * memory is really nasty...
565          */
566         set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
567         acm->control->needs_remote_wakeup = 1;
568
569         acm->ctrlurb->dev = acm->dev;
570         retval = usb_submit_urb(acm->ctrlurb, GFP_KERNEL);
571         if (retval) {
572                 dev_err(&acm->control->dev,
573                         "%s - usb_submit_urb(ctrl irq) failed\n", __func__);
574                 goto error_submit_urb;
575         }
576
577         acm_tty_set_termios(tty, NULL);
578
579         /*
580          * Unthrottle device in case the TTY was closed while throttled.
581          */
582         spin_lock_irq(&acm->read_lock);
583         acm->throttled = 0;
584         acm->throttle_req = 0;
585         spin_unlock_irq(&acm->read_lock);
586
587         retval = acm_submit_read_urbs(acm, GFP_KERNEL);
588         if (retval)
589                 goto error_submit_read_urbs;
590
591         usb_autopm_put_interface(acm->control);
592
593         mutex_unlock(&acm->mutex);
594
595         return 0;
596
597 error_submit_read_urbs:
598         for (i = 0; i < acm->rx_buflimit; i++)
599                 usb_kill_urb(acm->read_urbs[i]);
600         usb_kill_urb(acm->ctrlurb);
601 error_submit_urb:
602         usb_autopm_put_interface(acm->control);
603 error_get_interface:
604 disconnected:
605         mutex_unlock(&acm->mutex);
606
607         return usb_translate_errors(retval);
608 }
609
610 static void acm_port_destruct(struct tty_port *port)
611 {
612         struct acm *acm = container_of(port, struct acm, port);
613
614         dev_dbg(&acm->control->dev, "%s\n", __func__);
615
616         acm_release_minor(acm);
617         usb_put_intf(acm->control);
618         kfree(acm->country_codes);
619         kfree(acm);
620 }
621
622 static void acm_port_shutdown(struct tty_port *port)
623 {
624         struct acm *acm = container_of(port, struct acm, port);
625         struct urb *urb;
626         struct acm_wb *wb;
627         int i;
628
629         dev_dbg(&acm->control->dev, "%s\n", __func__);
630
631         /*
632          * Need to grab write_lock to prevent race with resume, but no need to
633          * hold it due to the tty-port initialised flag.
634          */
635         spin_lock_irq(&acm->write_lock);
636         spin_unlock_irq(&acm->write_lock);
637
638         usb_autopm_get_interface_no_resume(acm->control);
639         acm->control->needs_remote_wakeup = 0;
640         usb_autopm_put_interface(acm->control);
641
642         for (;;) {
643                 urb = usb_get_from_anchor(&acm->delayed);
644                 if (!urb)
645                         break;
646                 wb = urb->context;
647                 wb->use = 0;
648                 usb_autopm_put_interface_async(acm->control);
649         }
650
651         usb_kill_urb(acm->ctrlurb);
652         for (i = 0; i < ACM_NW; i++)
653                 usb_kill_urb(acm->wb[i].urb);
654         for (i = 0; i < acm->rx_buflimit; i++)
655                 usb_kill_urb(acm->read_urbs[i]);
656 }
657
658 static void acm_tty_cleanup(struct tty_struct *tty)
659 {
660         struct acm *acm = tty->driver_data;
661         dev_dbg(&acm->control->dev, "%s\n", __func__);
662         tty_port_put(&acm->port);
663 }
664
665 static void acm_tty_hangup(struct tty_struct *tty)
666 {
667         struct acm *acm = tty->driver_data;
668         dev_dbg(&acm->control->dev, "%s\n", __func__);
669         tty_port_hangup(&acm->port);
670 }
671
672 static void acm_tty_close(struct tty_struct *tty, struct file *filp)
673 {
674         struct acm *acm = tty->driver_data;
675         dev_dbg(&acm->control->dev, "%s\n", __func__);
676         tty_port_close(&acm->port, tty, filp);
677 }
678
679 static int acm_tty_write(struct tty_struct *tty,
680                                         const unsigned char *buf, int count)
681 {
682         struct acm *acm = tty->driver_data;
683         int stat;
684         unsigned long flags;
685         int wbn;
686         struct acm_wb *wb;
687
688         if (!count)
689                 return 0;
690
691         dev_vdbg(&acm->data->dev, "%s - count %d\n", __func__, count);
692
693         spin_lock_irqsave(&acm->write_lock, flags);
694         wbn = acm_wb_alloc(acm);
695         if (wbn < 0) {
696                 spin_unlock_irqrestore(&acm->write_lock, flags);
697                 return 0;
698         }
699         wb = &acm->wb[wbn];
700
701         if (!acm->dev) {
702                 wb->use = 0;
703                 spin_unlock_irqrestore(&acm->write_lock, flags);
704                 return -ENODEV;
705         }
706
707         count = (count > acm->writesize) ? acm->writesize : count;
708         dev_vdbg(&acm->data->dev, "%s - write %d\n", __func__, count);
709         memcpy(wb->buf, buf, count);
710         wb->len = count;
711
712         stat = usb_autopm_get_interface_async(acm->control);
713         if (stat) {
714                 wb->use = 0;
715                 spin_unlock_irqrestore(&acm->write_lock, flags);
716                 return stat;
717         }
718
719         if (acm->susp_count) {
720                 usb_anchor_urb(wb->urb, &acm->delayed);
721                 spin_unlock_irqrestore(&acm->write_lock, flags);
722                 return count;
723         }
724
725         stat = acm_start_wb(acm, wb);
726         spin_unlock_irqrestore(&acm->write_lock, flags);
727
728         if (stat < 0)
729                 return stat;
730         return count;
731 }
732
733 static int acm_tty_write_room(struct tty_struct *tty)
734 {
735         struct acm *acm = tty->driver_data;
736         /*
737          * Do not let the line discipline to know that we have a reserve,
738          * or it might get too enthusiastic.
739          */
740         return acm_wb_is_avail(acm) ? acm->writesize : 0;
741 }
742
743 static int acm_tty_chars_in_buffer(struct tty_struct *tty)
744 {
745         struct acm *acm = tty->driver_data;
746         /*
747          * if the device was unplugged then any remaining characters fell out
748          * of the connector ;)
749          */
750         if (acm->disconnected)
751                 return 0;
752         /*
753          * This is inaccurate (overcounts), but it works.
754          */
755         return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
756 }
757
758 static void acm_tty_throttle(struct tty_struct *tty)
759 {
760         struct acm *acm = tty->driver_data;
761
762         spin_lock_irq(&acm->read_lock);
763         acm->throttle_req = 1;
764         spin_unlock_irq(&acm->read_lock);
765 }
766
767 static void acm_tty_unthrottle(struct tty_struct *tty)
768 {
769         struct acm *acm = tty->driver_data;
770         unsigned int was_throttled;
771
772         spin_lock_irq(&acm->read_lock);
773         was_throttled = acm->throttled;
774         acm->throttled = 0;
775         acm->throttle_req = 0;
776         spin_unlock_irq(&acm->read_lock);
777
778         if (was_throttled)
779                 acm_submit_read_urbs(acm, GFP_KERNEL);
780 }
781
782 static int acm_tty_break_ctl(struct tty_struct *tty, int state)
783 {
784         struct acm *acm = tty->driver_data;
785         int retval;
786
787         retval = acm_send_break(acm, state ? 0xffff : 0);
788         if (retval < 0)
789                 dev_dbg(&acm->control->dev, "%s - send break failed\n",
790                                                                 __func__);
791         return retval;
792 }
793
794 static int acm_tty_tiocmget(struct tty_struct *tty)
795 {
796         struct acm *acm = tty->driver_data;
797
798         return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
799                (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
800                (acm->ctrlin  & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
801                (acm->ctrlin  & ACM_CTRL_RI  ? TIOCM_RI  : 0) |
802                (acm->ctrlin  & ACM_CTRL_DCD ? TIOCM_CD  : 0) |
803                TIOCM_CTS;
804 }
805
806 static int acm_tty_tiocmset(struct tty_struct *tty,
807                             unsigned int set, unsigned int clear)
808 {
809         struct acm *acm = tty->driver_data;
810         unsigned int newctrl;
811
812         newctrl = acm->ctrlout;
813         set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
814                                         (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
815         clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
816                                         (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
817
818         newctrl = (newctrl & ~clear) | set;
819
820         if (acm->ctrlout == newctrl)
821                 return 0;
822         return acm_set_control(acm, acm->ctrlout = newctrl);
823 }
824
825 static int get_serial_info(struct acm *acm, struct serial_struct __user *info)
826 {
827         struct serial_struct tmp;
828
829         if (!info)
830                 return -EINVAL;
831
832         memset(&tmp, 0, sizeof(tmp));
833         tmp.flags = ASYNC_LOW_LATENCY;
834         tmp.xmit_fifo_size = acm->writesize;
835         tmp.baud_base = le32_to_cpu(acm->line.dwDTERate);
836         tmp.close_delay = acm->port.close_delay / 10;
837         tmp.closing_wait = acm->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
838                                 ASYNC_CLOSING_WAIT_NONE :
839                                 acm->port.closing_wait / 10;
840
841         if (copy_to_user(info, &tmp, sizeof(tmp)))
842                 return -EFAULT;
843         else
844                 return 0;
845 }
846
847 static int set_serial_info(struct acm *acm,
848                                 struct serial_struct __user *newinfo)
849 {
850         struct serial_struct new_serial;
851         unsigned int closing_wait, close_delay;
852         int retval = 0;
853
854         if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
855                 return -EFAULT;
856
857         close_delay = new_serial.close_delay * 10;
858         closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
859                         ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
860
861         mutex_lock(&acm->port.mutex);
862
863         if (!capable(CAP_SYS_ADMIN)) {
864                 if ((close_delay != acm->port.close_delay) ||
865                     (closing_wait != acm->port.closing_wait))
866                         retval = -EPERM;
867                 else
868                         retval = -EOPNOTSUPP;
869         } else {
870                 acm->port.close_delay  = close_delay;
871                 acm->port.closing_wait = closing_wait;
872         }
873
874         mutex_unlock(&acm->port.mutex);
875         return retval;
876 }
877
878 static int wait_serial_change(struct acm *acm, unsigned long arg)
879 {
880         int rv = 0;
881         DECLARE_WAITQUEUE(wait, current);
882         struct async_icount old, new;
883
884         if (arg & (TIOCM_DSR | TIOCM_RI | TIOCM_CD ))
885                 return -EINVAL;
886         do {
887                 spin_lock_irq(&acm->read_lock);
888                 old = acm->oldcount;
889                 new = acm->iocount;
890                 acm->oldcount = new;
891                 spin_unlock_irq(&acm->read_lock);
892
893                 if ((arg & TIOCM_DSR) &&
894                         old.dsr != new.dsr)
895                         break;
896                 if ((arg & TIOCM_CD)  &&
897                         old.dcd != new.dcd)
898                         break;
899                 if ((arg & TIOCM_RI) &&
900                         old.rng != new.rng)
901                         break;
902
903                 add_wait_queue(&acm->wioctl, &wait);
904                 set_current_state(TASK_INTERRUPTIBLE);
905                 schedule();
906                 remove_wait_queue(&acm->wioctl, &wait);
907                 if (acm->disconnected) {
908                         if (arg & TIOCM_CD)
909                                 break;
910                         else
911                                 rv = -ENODEV;
912                 } else {
913                         if (signal_pending(current))
914                                 rv = -ERESTARTSYS;
915                 }
916         } while (!rv);
917
918         
919
920         return rv;
921 }
922
923 static int get_serial_usage(struct acm *acm,
924                             struct serial_icounter_struct __user *count)
925 {
926         struct serial_icounter_struct icount;
927         int rv = 0;
928
929         memset(&icount, 0, sizeof(icount));
930         icount.dsr = acm->iocount.dsr;
931         icount.rng = acm->iocount.rng;
932         icount.dcd = acm->iocount.dcd;
933         icount.frame = acm->iocount.frame;
934         icount.overrun = acm->iocount.overrun;
935         icount.parity = acm->iocount.parity;
936         icount.brk = acm->iocount.brk;
937
938         if (copy_to_user(count, &icount, sizeof(icount)) > 0)
939                 rv = -EFAULT;
940
941         return rv;
942 }
943
944 static int vizzini_set_reg(struct acm *acm,
945                            int block, int regnum, int value)
946 {
947         int result;
948
949         dev_dbg(&acm->control->dev, "%s 0x%02x:0x%02x = 0x%02x\n", __func__, block, regnum, value);
950
951         result = usb_control_msg(acm->dev,                        /* usb device */
952                                  usb_sndctrlpipe(acm->dev, 0), /* endpoint pipe */
953                                  XR_SET_REG,                      /* request */
954                                  USB_DIR_OUT | USB_TYPE_VENDOR,   /* request_type */
955                                  value,                           /* request value */
956                                  regnum | (block << 8),           /* index */
957                                  NULL,                            /* data */
958                                  0,                               /* size */
959                                  5000);                           /* timeout */
960         return result;
961 }
962
963 static int vizzini_get_reg(struct acm *acm,
964                            int block, int reg, char *value)
965 {
966         int result;
967
968         result = usb_control_msg(acm->dev,                     /* usb device */
969                                  usb_rcvctrlpipe(acm->dev, 0), /* endpoint pipe */
970                                  XR_GETN_REG,                     /* request */
971                                  USB_DIR_IN | USB_TYPE_VENDOR,    /* request_type */
972                                  0,                               /* request value */
973                                  reg | (block << 8),              /* index */
974                                  value,                           /* data */
975                                  1,                               /* size */
976                                  5000);                           /* timeout */
977
978         return result;
979 }
980
981 static void vizzini_disable(struct acm *acm)
982 {
983         int block = acm->block;
984
985         vizzini_set_reg(acm, block, UART_ENABLE, 0);
986         vizzini_set_reg(acm, URM_REG_BLOCK, URM_ENABLE_BASE + block, 0);
987 }
988
989 static void vizzini_enable(struct acm *acm)
990 {
991         int block = acm->block;
992
993         vizzini_set_reg(acm, URM_REG_BLOCK, URM_ENABLE_BASE + block, URM_ENABLE_0_TX);
994         vizzini_set_reg(acm, block, UART_ENABLE, UART_ENABLE_TX | UART_ENABLE_RX);
995         vizzini_set_reg(acm, URM_REG_BLOCK, URM_ENABLE_BASE + block, URM_ENABLE_0_TX | URM_ENABLE_0_RX);
996 }
997
998 static void vizzini_loopback(struct acm *acm, int from)
999 {
1000         int block = acm->block;
1001         int lb;
1002
1003         switch (from)
1004         {
1005         case 0: lb = UART_LOOPBACK_CTL_RX_UART0; break;
1006         case 1: lb = UART_LOOPBACK_CTL_RX_UART1; break;
1007         case 2: lb = UART_LOOPBACK_CTL_RX_UART2; break;
1008         case 3: lb = UART_LOOPBACK_CTL_RX_UART3; break;
1009         default: return;
1010         }
1011
1012         dev_info(&acm->control->dev, "Internal loopback from %d\n", from);
1013
1014         vizzini_disable(acm);
1015         vizzini_set_reg(acm, block, UART_LOOPBACK_CTL, UART_LOOPBACK_CTL_ENABLE | lb);
1016         vizzini_enable(acm);
1017 }
1018
1019 static int vizzini_test_mode(struct acm *acm,
1020                              int selector)
1021 {
1022         int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
1023                                      USB_REQ_SET_FEATURE,
1024                                      USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
1025                                      USB_DEVICE_TEST_MODE,
1026                                      selector << 8,
1027                                      NULL, 0, 5000);
1028         dev_dbg(&acm->control->dev, "vz_test_mode: selector=0x%02x\n", selector);
1029         return retval < 0 ? retval : 0;
1030 }
1031
1032 static int acm_tty_ioctl(struct tty_struct *tty,
1033                                         unsigned int cmd, unsigned long arg)
1034 {
1035         struct acm *acm = tty->driver_data;
1036         int rv = -ENOIOCTLCMD;
1037
1038         unsigned int  block, reg, val, match, preciseflags, unicast, broadcast, flow, selector;
1039         char    *data;
1040
1041         switch (cmd) {
1042         case TIOCGSERIAL: /* gets serial port data */
1043                 rv = get_serial_info(acm, (struct serial_struct __user *) arg);
1044                 break;
1045         case TIOCSSERIAL:
1046                 rv = set_serial_info(acm, (struct serial_struct __user *) arg);
1047                 break;
1048         case TIOCMIWAIT:
1049                 rv = usb_autopm_get_interface(acm->control);
1050                 if (rv < 0) {
1051                         rv = -EIO;
1052                         break;
1053                 }
1054                 rv = wait_serial_change(acm, arg);
1055                 usb_autopm_put_interface(acm->control);
1056                 break;
1057         case TIOCGICOUNT:
1058                 rv = get_serial_usage(acm, (struct serial_icounter_struct __user *) arg);
1059                 break;
1060
1061         case VZIOC_GET_REG:
1062                 if (get_user(block, (int __user *)arg))
1063                         return -EFAULT;
1064                 if (get_user(reg, (int __user *)(arg + sizeof(int))))
1065                         return -EFAULT;
1066
1067                 data = kmalloc(1, GFP_KERNEL);
1068                 if (data == NULL) {
1069                         dev_err(&acm->control->dev, "%s - Cannot allocate USB buffer.\n", __func__);
1070                         return -ENOMEM;
1071                 }
1072
1073                 if (block == -1)
1074                         block = acm->block;
1075
1076                 rv = vizzini_get_reg(acm, block, reg, data);
1077                 if (rv != 1) {
1078                         dev_err(&acm->control->dev, "Cannot get register (%d)\n", rv);
1079                         kfree(data);
1080                         return -EFAULT;
1081                 }
1082
1083                 if (put_user(data[0], (int __user *)(arg + 2 * sizeof(int)))) {
1084                         dev_err(&acm->control->dev, "Cannot put user result\n");
1085                         kfree(data);
1086                         return -EFAULT;
1087                 }
1088
1089                 kfree(data);
1090                 break;
1091
1092         case VZIOC_SET_REG:
1093                 if (get_user(block, (int __user *)arg))
1094                         return -EFAULT;
1095                 if (get_user(reg, (int __user *)(arg + sizeof(int))))
1096                         return -EFAULT;
1097                 if (get_user(val, (int __user *)(arg + 2 * sizeof(int))))
1098                         return -EFAULT;
1099
1100                 if (block == -1)
1101                         block = acm->block;
1102
1103                 rv = vizzini_set_reg(acm, block, reg, val);
1104                 if (rv < 0)
1105                         return -EFAULT;
1106                 break;
1107
1108         case VZIOC_SET_ADDRESS_MATCH:
1109                 match = arg;
1110
1111                 dev_dbg(&acm->control->dev, "%s VIOC_SET_ADDRESS_MATCH %d\n", __func__, match);
1112
1113                 vizzini_disable(acm);
1114
1115                 if (match & VZ_ADDRESS_MATCH_DISABLE) {
1116                         flow      = UART_FLOW_MODE_NONE;
1117                 } else {
1118                         flow      = UART_FLOW_MODE_ADDR_MATCH_TX;
1119                         unicast   = (match >> VZ_ADDRESS_UNICAST_S) & 0xff;
1120                         broadcast = (match >> VZ_ADDRESS_BROADCAST_S) & 0xff;
1121                 }
1122
1123                 dev_dbg(&acm->control->dev, "address match: flow=%d ucast=%d bcast=%u\n",
1124                                    flow, unicast, broadcast);
1125                 vizzini_set_reg(acm, acm->block, UART_FLOW, flow);
1126                 vizzini_set_reg(acm, acm->block, UART_XON_CHAR, unicast);
1127                 vizzini_set_reg(acm, acm->block, UART_XOFF_CHAR, broadcast);
1128
1129                 vizzini_enable(acm);
1130                 break;
1131
1132         case VZIOC_SET_PRECISE_FLAGS:
1133                 preciseflags = arg;
1134
1135                 dev_dbg(&acm->control->dev, "%s VIOC_SET_PRECISE_FLAGS %d\n", __func__, preciseflags);
1136
1137                 vizzini_disable(acm);
1138
1139                 if (preciseflags) {
1140                         acm->preciseflags = 1;
1141                 } else {
1142                         acm->preciseflags = 0;
1143                 }
1144
1145                 vizzini_set_reg(acm, EPLOCALS_REG_BLOCK,
1146                                 (acm->block * MEM_EP_LOCALS_SIZE) + EP_WIDE_MODE,
1147                                 acm->preciseflags);
1148
1149                 vizzini_enable(acm);
1150                 rv = 0;
1151                 break;
1152
1153         case VZIOC_TEST_MODE:
1154                 selector = arg;
1155                 dev_dbg(&acm->control->dev, "%s VIOC_TEST_MODE 0x%02x\n", __func__, selector);
1156                 vizzini_test_mode(acm, selector);
1157                 rv = 0;
1158                 break;
1159
1160         case VZIOC_LOOPBACK:
1161                 selector = arg;
1162                 dev_dbg(&acm->control->dev, "VIOC_LOOPBACK 0x%02x\n", selector);
1163                 vizzini_loopback(acm, selector);
1164                 rv = 0;
1165                 break;
1166         }
1167
1168         return rv;
1169 }
1170
1171 struct vizzini_baud_rate
1172 {
1173         unsigned int tx;
1174         unsigned int rx0;
1175         unsigned int rx1;
1176 };
1177
1178 static struct vizzini_baud_rate vizzini_baud_rates[] = {
1179         { 0x000, 0x000, 0x000 },
1180         { 0x000, 0x000, 0x000 },
1181         { 0x100, 0x000, 0x100 },
1182         { 0x020, 0x400, 0x020 },
1183         { 0x010, 0x100, 0x010 },
1184         { 0x208, 0x040, 0x208 },
1185         { 0x104, 0x820, 0x108 },
1186         { 0x844, 0x210, 0x884 },
1187         { 0x444, 0x110, 0x444 },
1188         { 0x122, 0x888, 0x224 },
1189         { 0x912, 0x448, 0x924 },
1190         { 0x492, 0x248, 0x492 },
1191         { 0x252, 0x928, 0x292 },
1192         { 0X94A, 0X4A4, 0XA52 },
1193         { 0X52A, 0XAA4, 0X54A },
1194         { 0XAAA, 0x954, 0X4AA },
1195         { 0XAAA, 0x554, 0XAAA },
1196         { 0x555, 0XAD4, 0X5AA },
1197         { 0XB55, 0XAB4, 0X55A },
1198         { 0X6B5, 0X5AC, 0XB56 },
1199         { 0X5B5, 0XD6C, 0X6D6 },
1200         { 0XB6D, 0XB6A, 0XDB6 },
1201         { 0X76D, 0X6DA, 0XBB6 },
1202         { 0XEDD, 0XDDA, 0X76E },
1203         { 0XDDD, 0XBBA, 0XEEE },
1204         { 0X7BB, 0XF7A, 0XDDE },
1205         { 0XF7B, 0XEF6, 0X7DE },
1206         { 0XDF7, 0XBF6, 0XF7E },
1207         { 0X7F7, 0XFEE, 0XEFE },
1208         { 0XFDF, 0XFBE, 0X7FE },
1209         { 0XF7F, 0XEFE, 0XFFE },
1210         { 0XFFF, 0XFFE, 0XFFD },
1211 };
1212
1213 static int vizzini_set_baud_rate(struct acm *acm, unsigned int rate)
1214 {
1215         int             block   = acm->block;
1216         unsigned int    divisor = 48000000 / rate;
1217         unsigned int    i       = ((32 * 48000000) / rate) & 0x1f;
1218         unsigned int    tx_mask = vizzini_baud_rates[i].tx;
1219         unsigned int    rx_mask = (divisor & 1) ? vizzini_baud_rates[i].rx1 : vizzini_baud_rates[i].rx0;
1220
1221         dev_dbg(&acm->control->dev, "Setting baud rate to %d: i=%u div=%u tx=%03x rx=%03x\n", rate, i, divisor, tx_mask, rx_mask);
1222
1223         vizzini_set_reg(acm, block, UART_CLOCK_DIVISOR_0, (divisor >>  0) & 0xff);
1224         vizzini_set_reg(acm, block, UART_CLOCK_DIVISOR_1, (divisor >>  8) & 0xff);
1225         vizzini_set_reg(acm, block, UART_CLOCK_DIVISOR_2, (divisor >> 16) & 0xff);
1226         vizzini_set_reg(acm, block, UART_TX_CLOCK_MASK_0, (tx_mask >>  0) & 0xff);
1227         vizzini_set_reg(acm, block, UART_TX_CLOCK_MASK_1, (tx_mask >>  8) & 0xff);
1228         vizzini_set_reg(acm, block, UART_RX_CLOCK_MASK_0, (rx_mask >>  0) & 0xff);
1229         vizzini_set_reg(acm, block, UART_RX_CLOCK_MASK_1, (rx_mask >>  8) & 0xff);
1230         
1231         return -EINVAL;
1232 }
1233
1234
1235 static void acm_tty_set_termios(struct tty_struct *tty,
1236                                                 struct ktermios *termios_old)
1237 {
1238         struct acm *acm = tty->driver_data;
1239         unsigned int             cflag, block;
1240         speed_t                  rate;
1241         unsigned int             format_size, format_parity, format_stop, flow, gpio_mode;
1242
1243         cflag = tty->termios.c_cflag;
1244
1245         acm->clocal = ((cflag & CLOCAL) != 0);
1246
1247         block = acm->block;
1248
1249         vizzini_disable(acm);
1250
1251         if ((cflag & CSIZE) == CS7) {
1252                 format_size = UART_FORMAT_SIZE_7;
1253         } else if ((cflag & CSIZE) == CS5) {
1254                 /* Enabling 5-bit mode is really 9-bit mode! */
1255                 format_size = UART_FORMAT_SIZE_9;
1256         } else {
1257                 format_size = UART_FORMAT_SIZE_8;
1258         }
1259         acm->trans9 = (format_size == UART_FORMAT_SIZE_9);
1260
1261         if (cflag & PARENB) {
1262                 if (cflag & PARODD) {
1263                         if (cflag & CMSPAR) {
1264                                 format_parity = UART_FORMAT_PARITY_1;
1265                         } else {
1266                                 format_parity = UART_FORMAT_PARITY_ODD;
1267                         }
1268                 } else {
1269                         if (cflag & CMSPAR) {
1270                                 format_parity = UART_FORMAT_PARITY_0;
1271                         } else {
1272                                 format_parity = UART_FORMAT_PARITY_EVEN;
1273                         }
1274                 }
1275         } else {
1276                 format_parity = UART_FORMAT_PARITY_NONE;
1277         }
1278
1279         if (cflag & CSTOPB) {
1280                 format_stop = UART_FORMAT_STOP_2;
1281         } else {
1282                 format_stop = UART_FORMAT_STOP_1;
1283         }
1284
1285 #ifdef VIZZINI_IWA
1286         if (format_size == UART_FORMAT_SIZE_8) {
1287                 acm->iwa = format_parity;
1288                 if (portdata->iwa != UART_FORMAT_PARITY_NONE) {
1289                         format_size = UART_FORMAT_SIZE_9;
1290                         format_parity = UART_FORMAT_PARITY_NONE;
1291                 }
1292         } else {
1293                 acm->iwa = UART_FORMAT_PARITY_NONE;
1294         }
1295 #endif
1296         vizzini_set_reg(acm, block, UART_FORMAT, format_size | format_parity | format_stop);
1297
1298         if (cflag & CRTSCTS) {
1299                 flow      = UART_FLOW_MODE_HW;
1300                 gpio_mode = UART_GPIO_MODE_SEL_RTS_CTS;
1301         } else if (I_IXOFF(tty) || I_IXON(tty)) {
1302                 unsigned char   start_char = START_CHAR(tty);
1303                 unsigned char   stop_char  = STOP_CHAR(tty);
1304
1305                 flow      = UART_FLOW_MODE_SW;
1306                 gpio_mode = UART_GPIO_MODE_SEL_GPIO;
1307
1308                 vizzini_set_reg(acm, block, UART_XON_CHAR, start_char);
1309                 vizzini_set_reg(acm, block, UART_XOFF_CHAR, stop_char);
1310         } else {
1311                 flow      = UART_FLOW_MODE_NONE;
1312                 gpio_mode = UART_GPIO_MODE_SEL_GPIO;
1313         }
1314
1315         vizzini_set_reg(acm, block, UART_FLOW, flow);
1316         vizzini_set_reg(acm, block, UART_GPIO_MODE, gpio_mode);
1317
1318         if (acm->trans9) {
1319                 /* Turn on wide mode if we're 9-bit transparent. */
1320                 vizzini_set_reg(acm, EPLOCALS_REG_BLOCK, (block * MEM_EP_LOCALS_SIZE) + EP_WIDE_MODE, 1);
1321 #ifdef VIZZINI_IWA
1322         } else if (acm->iwa != UART_FORMAT_PARITY_NONE) {
1323                 vizzini_set_reg(acm, EPLOCALS_REG_BLOCK, (block * MEM_EP_LOCALS_SIZE) + EP_WIDE_MODE, 1);
1324 #endif
1325         } else if (!acm->preciseflags) {
1326                 /* Turn off wide mode unless we have precise flags. */
1327                 vizzini_set_reg(acm, EPLOCALS_REG_BLOCK, (block * MEM_EP_LOCALS_SIZE) + EP_WIDE_MODE, 0);
1328         }
1329
1330         rate = cpu_to_le32(tty_get_baud_rate(tty));
1331         if(rate)
1332                 vizzini_set_baud_rate(acm, rate);
1333
1334         acm->line.dwDTERate   = rate;
1335         acm->line.bCharFormat = format_stop;
1336         acm->line.bParityType = format_parity;
1337         acm->line.bDataBits   = format_size;
1338
1339         vizzini_enable(acm);
1340 }
1341
1342 static const struct tty_port_operations acm_port_ops = {
1343         .dtr_rts = acm_port_dtr_rts,
1344         .shutdown = acm_port_shutdown,
1345         .activate = acm_port_activate,
1346         .destruct = acm_port_destruct,
1347 };
1348
1349 /*
1350  * USB probe and disconnect routines.
1351  */
1352
1353 /* Little helpers: write/read buffers free */
1354 static void acm_write_buffers_free(struct acm *acm)
1355 {
1356         int i;
1357         struct acm_wb *wb;
1358         struct usb_device *usb_dev = interface_to_usbdev(acm->control);
1359
1360         for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++)
1361                 usb_free_coherent(usb_dev, acm->writesize, wb->buf, wb->dmah);
1362 }
1363
1364 static void acm_read_buffers_free(struct acm *acm)
1365 {
1366         struct usb_device *usb_dev = interface_to_usbdev(acm->control);
1367         int i;
1368
1369         for (i = 0; i < acm->rx_buflimit; i++)
1370                 usb_free_coherent(usb_dev, acm->readsize,
1371                           acm->read_buffers[i].base, acm->read_buffers[i].dma);
1372 }
1373
1374 /* Little helper: write buffers allocate */
1375 static int acm_write_buffers_alloc(struct acm *acm)
1376 {
1377         int i;
1378         struct acm_wb *wb;
1379
1380         for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
1381                 wb->buf = usb_alloc_coherent(acm->dev, acm->writesize, GFP_KERNEL,
1382                     &wb->dmah);
1383                 if (!wb->buf) {
1384                         while (i != 0) {
1385                                 --i;
1386                                 --wb;
1387                                 usb_free_coherent(acm->dev, acm->writesize,
1388                                     wb->buf, wb->dmah);
1389                         }
1390                         return -ENOMEM;
1391                 }
1392         }
1393         return 0;
1394 }
1395
1396 static int acm_probe(struct usb_interface *intf,
1397                      const struct usb_device_id *id)
1398 {
1399         struct usb_cdc_union_desc *union_header = NULL;
1400         struct usb_cdc_country_functional_desc *cfd = NULL;
1401         unsigned char *buffer = intf->altsetting->extra;
1402         int buflen = intf->altsetting->extralen;
1403         struct usb_interface *control_interface;
1404         struct usb_interface *data_interface;
1405         struct usb_endpoint_descriptor *epctrl = NULL;
1406         struct usb_endpoint_descriptor *epread = NULL;
1407         struct usb_endpoint_descriptor *epwrite = NULL;
1408         struct usb_device *usb_dev = interface_to_usbdev(intf);
1409         struct acm *acm;
1410         int minor;
1411         int ctrlsize, readsize;
1412         u8 *buf;
1413         u8 ac_management_function = 0;
1414         u8 call_management_function = 0;
1415         int call_interface_num = -1;
1416         int data_interface_num = -1;
1417         unsigned long quirks;
1418         int num_rx_buf;
1419         int i;
1420         unsigned int elength = 0;
1421         int combined_interfaces = 0;
1422         struct device *tty_dev;
1423         int rv = -ENOMEM;
1424
1425         /* normal quirks */
1426         quirks = (unsigned long)id->driver_info;
1427
1428         if (quirks == IGNORE_DEVICE)
1429                 return -ENODEV;
1430
1431         num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
1432
1433         /* handle quirks deadly to normal probing*/
1434         if (quirks == NO_UNION_NORMAL) {
1435                 data_interface = usb_ifnum_to_if(usb_dev, 1);
1436                 control_interface = usb_ifnum_to_if(usb_dev, 0);
1437                 goto skip_normal_probe;
1438         }
1439
1440         /* normal probing*/
1441         if (!buffer) {
1442                 dev_err(&intf->dev, "Weird descriptor references\n");
1443                 return -EINVAL;
1444         }
1445
1446         if (!buflen) {
1447                 if (intf->cur_altsetting->endpoint &&
1448                                 intf->cur_altsetting->endpoint->extralen &&
1449                                 intf->cur_altsetting->endpoint->extra) {
1450                         dev_dbg(&intf->dev,
1451                                 "Seeking extra descriptors on endpoint\n");
1452                         buflen = intf->cur_altsetting->endpoint->extralen;
1453                         buffer = intf->cur_altsetting->endpoint->extra;
1454                 } else {
1455                         dev_err(&intf->dev,
1456                                 "Zero length descriptor references\n");
1457                         return -EINVAL;
1458                 }
1459         }
1460
1461         while (buflen > 0) {
1462                 elength = buffer[0];
1463                 if (!elength) {
1464                         dev_err(&intf->dev, "skipping garbage byte\n");
1465                         elength = 1;
1466                         goto next_desc;
1467                 }
1468                 if (buffer[1] != USB_DT_CS_INTERFACE) {
1469                         dev_err(&intf->dev, "skipping garbage\n");
1470                         goto next_desc;
1471                 }
1472
1473                 switch (buffer[2]) {
1474                 case USB_CDC_UNION_TYPE: /* we've found it */
1475                         if (elength < sizeof(struct usb_cdc_union_desc))
1476                                 goto next_desc;
1477                         if (union_header) {
1478                                 dev_err(&intf->dev, "More than one "
1479                                         "union descriptor, skipping ...\n");
1480                                 goto next_desc;
1481                         }
1482                         union_header = (struct usb_cdc_union_desc *)buffer;
1483                         break;
1484                 case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/
1485                         if (elength < sizeof(struct usb_cdc_country_functional_desc))
1486                                 goto next_desc;
1487                         cfd = (struct usb_cdc_country_functional_desc *)buffer;
1488                         break;
1489                 case USB_CDC_HEADER_TYPE: /* maybe check version */
1490                         break; /* for now we ignore it */
1491                 case USB_CDC_ACM_TYPE:
1492                         if (elength < 4)
1493                                 goto next_desc;
1494                         ac_management_function = buffer[3];
1495                         break;
1496                 case USB_CDC_CALL_MANAGEMENT_TYPE:
1497                         if (elength < 5)
1498                                 goto next_desc;
1499                         call_management_function = buffer[3];
1500                         call_interface_num = buffer[4];
1501                         break;
1502                 default:
1503                         /*
1504                          * there are LOTS more CDC descriptors that
1505                          * could legitimately be found here.
1506                          */
1507                         dev_dbg(&intf->dev, "Ignoring descriptor: "
1508                                         "type %02x, length %ud\n",
1509                                         buffer[2], elength);
1510                         break;
1511                 }
1512 next_desc:
1513                 buflen -= elength;
1514                 buffer += elength;
1515         }
1516
1517         if (!union_header) {
1518                 if (call_interface_num > 0) {
1519                         dev_dbg(&intf->dev, "No union descriptor, using call management descriptor\n");
1520                         /* quirks for Droids MuIn LCD */
1521                         if (quirks & NO_DATA_INTERFACE)
1522                                 data_interface = usb_ifnum_to_if(usb_dev, 0);
1523                         else
1524                                 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
1525                         control_interface = intf;
1526                 } else {
1527                         if (intf->cur_altsetting->desc.bNumEndpoints != 3) {
1528                                 dev_dbg(&intf->dev,"No union descriptor, giving up\n");
1529                                 return -ENODEV;
1530                         } else {
1531                                 dev_warn(&intf->dev,"No union descriptor, testing for castrated device\n");
1532                                 combined_interfaces = 1;
1533                                 control_interface = data_interface = intf;
1534                                 goto look_for_collapsed_interface;
1535                         }
1536                 }
1537         } else {
1538                 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
1539                 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
1540         }
1541
1542         if (!control_interface || !data_interface) {
1543                 dev_dbg(&intf->dev, "no interfaces\n");
1544                 return -ENODEV;
1545         }
1546
1547         if (data_interface_num != call_interface_num)
1548                 dev_dbg(&intf->dev, "Separate call control interface. That is not fully supported.\n");
1549
1550         if (control_interface == data_interface) {
1551                 /* some broken devices designed for windows work this way */
1552                 dev_warn(&intf->dev,"Control and data interfaces are not separated!\n");
1553                 combined_interfaces = 1;
1554                 /* a popular other OS doesn't use it */
1555                 quirks |= NO_CAP_LINE;
1556                 if (data_interface->cur_altsetting->desc.bNumEndpoints != 3) {
1557                         dev_err(&intf->dev, "This needs exactly 3 endpoints\n");
1558                         return -EINVAL;
1559                 }
1560 look_for_collapsed_interface:
1561                 for (i = 0; i < 3; i++) {
1562                         struct usb_endpoint_descriptor *ep;
1563                         ep = &data_interface->cur_altsetting->endpoint[i].desc;
1564
1565                         if (usb_endpoint_is_int_in(ep))
1566                                 epctrl = ep;
1567                         else if (usb_endpoint_is_bulk_out(ep))
1568                                 epwrite = ep;
1569                         else if (usb_endpoint_is_bulk_in(ep))
1570                                 epread = ep;
1571                         else
1572                                 return -EINVAL;
1573                 }
1574                 if (!epctrl || !epread || !epwrite)
1575                         return -ENODEV;
1576                 else
1577                         goto made_compressed_probe;
1578         }
1579
1580 skip_normal_probe:
1581
1582         /*workaround for switched interfaces */
1583         if (data_interface->cur_altsetting->desc.bInterfaceClass
1584                                                 != CDC_DATA_INTERFACE_TYPE) {
1585                 if (control_interface->cur_altsetting->desc.bInterfaceClass
1586                                                 == CDC_DATA_INTERFACE_TYPE) {
1587                         dev_dbg(&intf->dev,
1588                                 "Your device has switched interfaces.\n");
1589                         swap(control_interface, data_interface);
1590                 } else {
1591                         return -EINVAL;
1592                 }
1593         }
1594
1595         /* Accept probe requests only for the control interface */
1596         if (!combined_interfaces && intf != control_interface)
1597                 return -ENODEV;
1598
1599         if (!combined_interfaces && usb_interface_claimed(data_interface)) {
1600                 /* valid in this context */
1601                 dev_dbg(&intf->dev, "The data interface isn't available\n");
1602                 return -EBUSY;
1603         }
1604
1605
1606         if (data_interface->cur_altsetting->desc.bNumEndpoints < 2 ||
1607             control_interface->cur_altsetting->desc.bNumEndpoints == 0)
1608                 return -EINVAL;
1609
1610         epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
1611         epread = &data_interface->cur_altsetting->endpoint[0].desc;
1612         epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
1613
1614
1615         /* workaround for switched endpoints */
1616         if (!usb_endpoint_dir_in(epread)) {
1617                 /* descriptors are swapped */
1618                 dev_dbg(&intf->dev,
1619                         "The data interface has switched endpoints\n");
1620                 swap(epread, epwrite);
1621         }
1622 made_compressed_probe:
1623         dev_dbg(&intf->dev, "interfaces are valid\n");
1624
1625         acm = kzalloc(sizeof(struct acm), GFP_KERNEL);
1626         if (acm == NULL)
1627                 goto alloc_fail;
1628
1629         minor = acm_alloc_minor(acm);
1630         if (minor < 0) {
1631                 dev_err(&intf->dev, "no more free acm devices\n");
1632                 kfree(acm);
1633                 return -ENODEV;
1634         }
1635
1636         ctrlsize = usb_endpoint_maxp(epctrl);
1637         readsize = usb_endpoint_maxp(epread) *
1638                                 (quirks == SINGLE_RX_URB ? 1 : 2);
1639         acm->combined_interfaces = combined_interfaces;
1640         acm->writesize = usb_endpoint_maxp(epwrite) * 20;
1641         acm->control = control_interface;
1642         acm->data = data_interface;
1643         acm->minor = minor;
1644         acm->dev = usb_dev;
1645         acm->ctrl_caps = ac_management_function;
1646         if (quirks & NO_CAP_LINE)
1647                 acm->ctrl_caps &= ~USB_CDC_CAP_LINE;
1648         acm->ctrlsize = ctrlsize;
1649         acm->readsize = readsize;
1650         acm->rx_buflimit = num_rx_buf;
1651         INIT_WORK(&acm->work, acm_softint);
1652         init_waitqueue_head(&acm->wioctl);
1653         spin_lock_init(&acm->write_lock);
1654         spin_lock_init(&acm->read_lock);
1655         mutex_init(&acm->mutex);
1656         acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
1657         acm->is_int_ep = usb_endpoint_xfer_int(epread);
1658         if (acm->is_int_ep)
1659                 acm->bInterval = epread->bInterval;
1660         tty_port_init(&acm->port);
1661         acm->port.ops = &acm_port_ops;
1662         init_usb_anchor(&acm->delayed);
1663         acm->quirks = quirks;
1664
1665         acm->block = epwrite->bEndpointAddress - 1;
1666
1667         buf = usb_alloc_coherent(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
1668         if (!buf)
1669                 goto alloc_fail2;
1670         acm->ctrl_buffer = buf;
1671
1672         if (acm_write_buffers_alloc(acm) < 0)
1673                 goto alloc_fail4;
1674
1675         acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
1676         if (!acm->ctrlurb)
1677                 goto alloc_fail5;
1678
1679         for (i = 0; i < num_rx_buf; i++) {
1680                 struct acm_rb *rb = &(acm->read_buffers[i]);
1681                 struct urb *urb;
1682
1683                 rb->base = usb_alloc_coherent(acm->dev, readsize, GFP_KERNEL,
1684                                                                 &rb->dma);
1685                 if (!rb->base)
1686                         goto alloc_fail6;
1687                 rb->index = i;
1688                 rb->instance = acm;
1689
1690                 urb = usb_alloc_urb(0, GFP_KERNEL);
1691                 if (!urb)
1692                         goto alloc_fail6;
1693
1694                 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1695                 urb->transfer_dma = rb->dma;
1696                 if (acm->is_int_ep) {
1697                         usb_fill_int_urb(urb, acm->dev,
1698                                          acm->rx_endpoint,
1699                                          rb->base,
1700                                          acm->readsize,
1701                                          acm_read_bulk_callback, rb,
1702                                          acm->bInterval);
1703                 } else {
1704                         usb_fill_bulk_urb(urb, acm->dev,
1705                                           acm->rx_endpoint,
1706                                           rb->base,
1707                                           acm->readsize,
1708                                           acm_read_bulk_callback, rb);
1709                 }
1710
1711                 acm->read_urbs[i] = urb;
1712                 __set_bit(i, &acm->read_urbs_free);
1713         }
1714         for (i = 0; i < ACM_NW; i++) {
1715                 struct acm_wb *snd = &(acm->wb[i]);
1716
1717                 snd->urb = usb_alloc_urb(0, GFP_KERNEL);
1718                 if (snd->urb == NULL)
1719                         goto alloc_fail7;
1720
1721                 if (usb_endpoint_xfer_int(epwrite))
1722                         usb_fill_int_urb(snd->urb, usb_dev,
1723                                 usb_sndintpipe(usb_dev, epwrite->bEndpointAddress),
1724                                 NULL, acm->writesize, acm_write_bulk, snd, epwrite->bInterval);
1725                 else
1726                         usb_fill_bulk_urb(snd->urb, usb_dev,
1727                                 usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
1728                                 NULL, acm->writesize, acm_write_bulk, snd);
1729                 snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1730                 snd->instance = acm;
1731         }
1732
1733         usb_set_intfdata(intf, acm);
1734
1735         i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
1736         if (i < 0)
1737                 goto alloc_fail7;
1738
1739         if (cfd) { /* export the country data */
1740                 acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
1741                 if (!acm->country_codes)
1742                         goto skip_countries;
1743                 acm->country_code_size = cfd->bLength - 4;
1744                 memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0,
1745                                                         cfd->bLength - 4);
1746                 acm->country_rel_date = cfd->iCountryCodeRelDate;
1747
1748                 i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
1749                 if (i < 0) {
1750                         kfree(acm->country_codes);
1751                         acm->country_codes = NULL;
1752                         acm->country_code_size = 0;
1753                         goto skip_countries;
1754                 }
1755
1756                 i = device_create_file(&intf->dev,
1757                                                 &dev_attr_iCountryCodeRelDate);
1758                 if (i < 0) {
1759                         device_remove_file(&intf->dev, &dev_attr_wCountryCodes);
1760                         kfree(acm->country_codes);
1761                         acm->country_codes = NULL;
1762                         acm->country_code_size = 0;
1763                         goto skip_countries;
1764                 }
1765         }
1766
1767 skip_countries:
1768         usb_fill_int_urb(acm->ctrlurb, usb_dev,
1769                          usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
1770                          acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm,
1771                          /* works around buggy devices */
1772                          epctrl->bInterval ? epctrl->bInterval : 16);
1773         acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1774         acm->ctrlurb->transfer_dma = acm->ctrl_dma;
1775
1776         dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
1777
1778         acm->line.dwDTERate = cpu_to_le32(9600);
1779         acm->line.bDataBits = 8;
1780         acm_set_line(acm, &acm->line);
1781
1782         usb_driver_claim_interface(&acm_driver, data_interface, acm);
1783         usb_set_intfdata(data_interface, acm);
1784
1785         usb_get_intf(control_interface);
1786         tty_dev = tty_port_register_device(&acm->port, acm_tty_driver, minor,
1787                         &control_interface->dev);
1788         if (IS_ERR(tty_dev)) {
1789                 rv = PTR_ERR(tty_dev);
1790                 goto alloc_fail8;
1791         }
1792
1793         if (quirks & CLEAR_HALT_CONDITIONS) {
1794                 usb_clear_halt(usb_dev, usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress));
1795                 usb_clear_halt(usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress));
1796         }
1797
1798         return 0;
1799 alloc_fail8:
1800         if (acm->country_codes) {
1801                 device_remove_file(&acm->control->dev,
1802                                 &dev_attr_wCountryCodes);
1803                 device_remove_file(&acm->control->dev,
1804                                 &dev_attr_iCountryCodeRelDate);
1805                 kfree(acm->country_codes);
1806         }
1807         device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
1808 alloc_fail7:
1809         usb_set_intfdata(intf, NULL);
1810         for (i = 0; i < ACM_NW; i++)
1811                 usb_free_urb(acm->wb[i].urb);
1812 alloc_fail6:
1813         for (i = 0; i < num_rx_buf; i++)
1814                 usb_free_urb(acm->read_urbs[i]);
1815         acm_read_buffers_free(acm);
1816         usb_free_urb(acm->ctrlurb);
1817 alloc_fail5:
1818         acm_write_buffers_free(acm);
1819 alloc_fail4:
1820         usb_free_coherent(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1821 alloc_fail2:
1822         acm_release_minor(acm);
1823         kfree(acm);
1824 alloc_fail:
1825         return rv;
1826 }
1827
1828 static void stop_data_traffic(struct acm *acm)
1829 {
1830         int i;
1831
1832         dev_dbg(&acm->control->dev, "%s\n", __func__);
1833
1834         usb_kill_urb(acm->ctrlurb);
1835         for (i = 0; i < ACM_NW; i++)
1836                 usb_kill_urb(acm->wb[i].urb);
1837         for (i = 0; i < acm->rx_buflimit; i++)
1838                 usb_kill_urb(acm->read_urbs[i]);
1839
1840         cancel_work_sync(&acm->work);
1841 }
1842
1843 static void acm_disconnect(struct usb_interface *intf)
1844 {
1845         struct acm *acm = usb_get_intfdata(intf);
1846         struct usb_device *usb_dev = interface_to_usbdev(intf);
1847         struct tty_struct *tty;
1848         int i;
1849
1850         dev_dbg(&intf->dev, "%s\n", __func__);
1851
1852         /* sibling interface is already cleaning up */
1853         if (!acm)
1854                 return;
1855
1856         mutex_lock(&acm->mutex);
1857         acm->disconnected = true;
1858         if (acm->country_codes) {
1859                 device_remove_file(&acm->control->dev,
1860                                 &dev_attr_wCountryCodes);
1861                 device_remove_file(&acm->control->dev,
1862                                 &dev_attr_iCountryCodeRelDate);
1863         }
1864         wake_up_all(&acm->wioctl);
1865         device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
1866         usb_set_intfdata(acm->control, NULL);
1867         usb_set_intfdata(acm->data, NULL);
1868         mutex_unlock(&acm->mutex);
1869
1870         tty = tty_port_tty_get(&acm->port);
1871         if (tty) {
1872                 tty_vhangup(tty);
1873                 tty_kref_put(tty);
1874         }
1875
1876         stop_data_traffic(acm);
1877
1878         tty_unregister_device(acm_tty_driver, acm->minor);
1879
1880         usb_free_urb(acm->ctrlurb);
1881         for (i = 0; i < ACM_NW; i++)
1882                 usb_free_urb(acm->wb[i].urb);
1883         for (i = 0; i < acm->rx_buflimit; i++)
1884                 usb_free_urb(acm->read_urbs[i]);
1885         acm_write_buffers_free(acm);
1886         usb_free_coherent(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1887         acm_read_buffers_free(acm);
1888
1889         if (!acm->combined_interfaces)
1890                 usb_driver_release_interface(&acm_driver, intf == acm->control ?
1891                                         acm->data : acm->control);
1892
1893         tty_port_put(&acm->port);
1894 }
1895
1896 #ifdef CONFIG_PM
1897 static int acm_suspend(struct usb_interface *intf, pm_message_t message)
1898 {
1899         struct acm *acm = usb_get_intfdata(intf);
1900         int cnt;
1901
1902         spin_lock_irq(&acm->write_lock);
1903         if (PMSG_IS_AUTO(message)) {
1904                 if (acm->transmitting) {
1905                         spin_unlock_irq(&acm->write_lock);
1906                         return -EBUSY;
1907                 }
1908         }
1909         cnt = acm->susp_count++;
1910         spin_unlock_irq(&acm->write_lock);
1911
1912         if (cnt)
1913                 return 0;
1914
1915         stop_data_traffic(acm);
1916
1917         return 0;
1918 }
1919
1920 static int acm_resume(struct usb_interface *intf)
1921 {
1922         struct acm *acm = usb_get_intfdata(intf);
1923         struct urb *urb;
1924         int rv = 0;
1925
1926         spin_lock_irq(&acm->write_lock);
1927
1928         if (--acm->susp_count)
1929                 goto out;
1930
1931         if (test_bit(ASYNCB_INITIALIZED, &acm->port.flags)) {
1932                 rv = usb_submit_urb(acm->ctrlurb, GFP_ATOMIC);
1933
1934                 for (;;) {
1935                         urb = usb_get_from_anchor(&acm->delayed);
1936                         if (!urb)
1937                                 break;
1938
1939                         acm_start_wb(acm, urb->context);
1940                 }
1941
1942                 /*
1943                  * delayed error checking because we must
1944                  * do the write path at all cost
1945                  */
1946                 if (rv < 0)
1947                         goto out;
1948
1949                 rv = acm_submit_read_urbs(acm, GFP_ATOMIC);
1950         }
1951 out:
1952         spin_unlock_irq(&acm->write_lock);
1953
1954         return rv;
1955 }
1956
1957 static int acm_reset_resume(struct usb_interface *intf)
1958 {
1959         struct acm *acm = usb_get_intfdata(intf);
1960
1961         if (test_bit(ASYNCB_INITIALIZED, &acm->port.flags))
1962                 tty_port_tty_hangup(&acm->port, false);
1963
1964         return acm_resume(intf);
1965 }
1966
1967 #endif /* CONFIG_PM */
1968
1969 /*
1970  * USB driver structure.
1971  */
1972
1973 static const struct usb_device_id xrusb_ids[] = {
1974         { USB_DEVICE(0x04e2, 0x1410), },
1975         { USB_DEVICE(0x04e2, 0x1412), },
1976         { USB_DEVICE(0x04e2, 0x1414), },
1977         { }
1978 };
1979
1980 MODULE_DEVICE_TABLE(usb, xrusb_ids);
1981
1982 static struct usb_driver acm_driver = {
1983         .name =         "vizzini",
1984         .probe =        acm_probe,
1985         .disconnect =   acm_disconnect,
1986 #ifdef CONFIG_PM
1987         .suspend =      acm_suspend,
1988         .resume =       acm_resume,
1989         .reset_resume = acm_reset_resume,
1990 #endif
1991         .id_table =     xrusb_ids,
1992 #ifdef CONFIG_PM
1993         .supports_autosuspend = 1,
1994 #endif
1995         .disable_hub_initiated_lpm = 1,
1996 };
1997
1998 /*
1999  * TTY driver structures.
2000  */
2001
2002 static const struct tty_operations acm_ops = {
2003         .install =              acm_tty_install,
2004         .open =                 acm_tty_open,
2005         .close =                acm_tty_close,
2006         .cleanup =              acm_tty_cleanup,
2007         .hangup =               acm_tty_hangup,
2008         .write =                acm_tty_write,
2009         .write_room =           acm_tty_write_room,
2010         .ioctl =                acm_tty_ioctl,
2011         .throttle =             acm_tty_throttle,
2012         .unthrottle =           acm_tty_unthrottle,
2013         .chars_in_buffer =      acm_tty_chars_in_buffer,
2014         .break_ctl =            acm_tty_break_ctl,
2015         .set_termios =          acm_tty_set_termios,
2016         .tiocmget =             acm_tty_tiocmget,
2017         .tiocmset =             acm_tty_tiocmset,
2018 };
2019
2020 /*
2021  * Init / exit.
2022  */
2023
2024 static int __init acm_init(void)
2025 {
2026         int retval;
2027         acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
2028         if (!acm_tty_driver)
2029                 return -ENOMEM;
2030         acm_tty_driver->driver_name = "vizzini",
2031         acm_tty_driver->name = "ttyVIZ",
2032         acm_tty_driver->major = 0, // Dynamically allocate the major number
2033         acm_tty_driver->minor_start = 0,
2034         acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
2035         acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
2036         acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2037         acm_tty_driver->init_termios = tty_std_termios;
2038         acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD |
2039                                                                 HUPCL | CLOCAL;
2040         tty_set_operations(acm_tty_driver, &acm_ops);
2041
2042         retval = tty_register_driver(acm_tty_driver);
2043         if (retval) {
2044                 put_tty_driver(acm_tty_driver);
2045                 return retval;
2046         }
2047
2048         retval = usb_register(&acm_driver);
2049         if (retval) {
2050                 tty_unregister_driver(acm_tty_driver);
2051                 put_tty_driver(acm_tty_driver);
2052                 return retval;
2053         }
2054
2055         printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
2056
2057         return 0;
2058 }
2059
2060 static void __exit acm_exit(void)
2061 {
2062         usb_deregister(&acm_driver);
2063         tty_unregister_driver(acm_tty_driver);
2064         put_tty_driver(acm_tty_driver);
2065         idr_destroy(&acm_minors);
2066 }
2067
2068 module_init(acm_init);
2069 module_exit(acm_exit);
2070
2071 MODULE_AUTHOR(DRIVER_AUTHOR);
2072 MODULE_DESCRIPTION(DRIVER_DESC);
2073 MODULE_LICENSE("GPL");
2074 MODULE_ALIAS_CHARDEV_MAJOR(ACM_TTY_MAJOR);