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