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