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