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