2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Copyright (c) 2015 Exar Corporation, Inc.
20 * This driver will work with any USB UART function in these Exar devices:
26 * The driver has been tested on various kernel versions from 3.6.x to 3.17.x.
27 * This driver may work on newer versions as well. There is a different driver available
28 * from www.exar.com that will work with kernel versions 2.6.18 to 3.4.x.
31 * Version 1A - Initial released version.
37 #include <linux/kernel.h>
38 #include <linux/errno.h>
39 #include <linux/init.h>
40 #include <linux/slab.h>
41 #include <linux/tty.h>
42 #include <linux/serial.h>
43 #include <linux/tty_driver.h>
44 #include <linux/tty_flip.h>
45 #include <linux/module.h>
46 #include <linux/mutex.h>
47 #include <linux/uaccess.h>
48 #include <linux/usb.h>
49 #include <linux/usb/cdc.h>
50 #include <asm/byteorder.h>
51 #include <asm/unaligned.h>
52 #include <linux/list.h>
53 #include "linux/version.h"
55 #include "xr_usb_serial_common.h"
56 #include "xr_usb_serial_ioctl.h"
59 #define DRIVER_AUTHOR "<uarttechsupport@exar.com>"
60 #define DRIVER_DESC "Exar USB UART (serial port) driver"
62 static struct usb_driver xr_usb_serial_driver
;
63 static struct tty_driver
*xr_usb_serial_tty_driver
;
64 static struct xr_usb_serial
*xr_usb_serial_table
[XR_USB_SERIAL_TTY_MINORS
];
66 static DEFINE_MUTEX(xr_usb_serial_table_lock
);
69 * xr_usb_serial_table accessors
73 * Look up an XR_USB_SERIAL structure by index. If found and not disconnected, increment
74 * its refcount and return it with its mutex held.
76 static struct xr_usb_serial
*xr_usb_serial_get_by_index(unsigned index
)
78 struct xr_usb_serial
*xr_usb_serial
;
80 mutex_lock(&xr_usb_serial_table_lock
);
81 xr_usb_serial
= xr_usb_serial_table
[index
];
83 mutex_lock(&xr_usb_serial
->mutex
);
84 if (xr_usb_serial
->disconnected
) {
85 mutex_unlock(&xr_usb_serial
->mutex
);
88 tty_port_get(&xr_usb_serial
->port
);
89 mutex_unlock(&xr_usb_serial
->mutex
);
92 mutex_unlock(&xr_usb_serial_table_lock
);
97 * Try to find an available minor number and if found, associate it with 'xr_usb_serial'.
99 static int xr_usb_serial_alloc_minor(struct xr_usb_serial
*xr_usb_serial
)
103 mutex_lock(&xr_usb_serial_table_lock
);
104 for (minor
= 0; minor
< XR_USB_SERIAL_TTY_MINORS
; minor
++) {
105 if (!xr_usb_serial_table
[minor
]) {
106 xr_usb_serial_table
[minor
] = xr_usb_serial
;
110 mutex_unlock(&xr_usb_serial_table_lock
);
115 /* Release the minor number associated with 'xr_usb_serial'. */
116 static void xr_usb_serial_release_minor(struct xr_usb_serial
*xr_usb_serial
)
118 mutex_lock(&xr_usb_serial_table_lock
);
119 xr_usb_serial_table
[xr_usb_serial
->minor
] = NULL
;
120 mutex_unlock(&xr_usb_serial_table_lock
);
124 * Functions for XR_USB_SERIAL control messages.
127 static int xr_usb_serial_ctrl_msg(struct xr_usb_serial
*xr_usb_serial
, int request
, int value
,
130 int retval
= usb_control_msg(xr_usb_serial
->dev
, usb_sndctrlpipe(xr_usb_serial
->dev
, 0),
131 request
, USB_RT_XR_USB_SERIAL
, value
,
132 xr_usb_serial
->control
->altsetting
[0].desc
.bInterfaceNumber
,
134 dev_dbg(&xr_usb_serial
->control
->dev
,
135 "%s - rq 0x%02x, val %#x, len %#x, result %d\n",
136 __func__
, request
, value
, len
, retval
);
137 return retval
< 0 ? retval
: 0;
140 #include "xr_usb_serial_hal.c"
144 * Write buffer management.
145 * All of these assume proper locks taken by the caller.
148 static int xr_usb_serial_wb_alloc(struct xr_usb_serial
*xr_usb_serial
)
151 struct xr_usb_serial_wb
*wb
;
156 wb
= &xr_usb_serial
->wb
[wbn
];
161 wbn
= (wbn
+ 1) % XR_USB_SERIAL_NW
;
162 if (++i
>= XR_USB_SERIAL_NW
)
167 static int xr_usb_serial_wb_is_avail(struct xr_usb_serial
*xr_usb_serial
)
172 n
= XR_USB_SERIAL_NW
;
173 spin_lock_irqsave(&xr_usb_serial
->write_lock
, flags
);
174 for (i
= 0; i
< XR_USB_SERIAL_NW
; i
++)
175 n
-= xr_usb_serial
->wb
[i
].use
;
176 spin_unlock_irqrestore(&xr_usb_serial
->write_lock
, flags
);
181 * Finish write. Caller must hold xr_usb_serial->write_lock
183 static void xr_usb_serial_write_done(struct xr_usb_serial
*xr_usb_serial
, struct xr_usb_serial_wb
*wb
)
186 xr_usb_serial
->transmitting
--;
187 usb_autopm_put_interface_async(xr_usb_serial
->control
);
193 * the caller is responsible for locking
196 static int xr_usb_serial_start_wb(struct xr_usb_serial
*xr_usb_serial
, struct xr_usb_serial_wb
*wb
)
200 xr_usb_serial
->transmitting
++;
202 wb
->urb
->transfer_buffer
= wb
->buf
;
203 wb
->urb
->transfer_dma
= wb
->dmah
;
204 wb
->urb
->transfer_buffer_length
= wb
->len
;
205 wb
->urb
->dev
= xr_usb_serial
->dev
;
207 rc
= usb_submit_urb(wb
->urb
, GFP_ATOMIC
);
209 dev_err(&xr_usb_serial
->data
->dev
,
210 "%s - usb_submit_urb(write bulk) failed: %d\n",
212 xr_usb_serial_write_done(xr_usb_serial
, wb
);
218 * attributes exported through sysfs
220 static ssize_t show_caps
221 (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
223 struct usb_interface
*intf
= to_usb_interface(dev
);
224 struct xr_usb_serial
*xr_usb_serial
= usb_get_intfdata(intf
);
226 return sprintf(buf
, "%d", xr_usb_serial
->ctrl_caps
);
228 static DEVICE_ATTR(bmCapabilities
, S_IRUGO
, show_caps
, NULL
);
230 static ssize_t show_country_codes
231 (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
233 struct usb_interface
*intf
= to_usb_interface(dev
);
234 struct xr_usb_serial
*xr_usb_serial
= usb_get_intfdata(intf
);
236 memcpy(buf
, xr_usb_serial
->country_codes
, xr_usb_serial
->country_code_size
);
237 return xr_usb_serial
->country_code_size
;
240 static DEVICE_ATTR(wCountryCodes
, S_IRUGO
, show_country_codes
, NULL
);
242 static ssize_t show_country_rel_date
243 (struct device
*dev
, struct device_attribute
*attr
, char *buf
)
245 struct usb_interface
*intf
= to_usb_interface(dev
);
246 struct xr_usb_serial
*xr_usb_serial
= usb_get_intfdata(intf
);
248 return sprintf(buf
, "%d", xr_usb_serial
->country_rel_date
);
251 static DEVICE_ATTR(iCountryCodeRelDate
, S_IRUGO
, show_country_rel_date
, NULL
);
253 static ssize_t
set_rs485_422_en(struct device
*dev
,
254 struct device_attribute
*attr
, const char *buf
,
257 struct usb_interface
*intf
= to_usb_interface(dev
);
258 struct xr_usb_serial
*xr_usb_serial
= usb_get_intfdata(intf
);
259 int error
, value
= 0;
261 error
= kstrtoint(buf
, 0, &value
);
266 xr_usb_serial
->rs485_422_en
= false;
267 } else if (value
== 1) {
268 // RS485,RS422 HD/FD mode
269 xr_usb_serial
->rs485_422_en
= true;
275 static ssize_t
show_rs485_422_en(struct device
*dev
,
276 struct device_attribute
*attr
, char *buf
)
278 struct usb_interface
*intf
= to_usb_interface(dev
);
279 struct xr_usb_serial
*xr_usb_serial
= usb_get_intfdata(intf
);
281 if (xr_usb_serial
->rs485_422_en
== false) {
282 return sprintf(buf
, "0");
283 } else if (xr_usb_serial
->rs485_422_en
== true) {
284 // RS485,RS422 HD/FD mode
285 return sprintf(buf
, "1");
290 static DEVICE_ATTR(bRS485_422_en
, 0644, show_rs485_422_en
, set_rs485_422_en
);
293 * Interrupt handlers for various XR_USB_SERIAL device responses
296 /* control interface reports status changes with "interrupt" transfers */
297 static void xr_usb_serial_ctrl_irq(struct urb
*urb
)
299 struct xr_usb_serial
*xr_usb_serial
= urb
->context
;
300 struct usb_cdc_notification
*dr
= urb
->transfer_buffer
;
301 struct tty_struct
*tty
;
305 int status
= urb
->status
;
311 p
= (unsigned char *)(urb
->transfer_buffer
);
312 for(i
=0;i
<urb
->actual_length
;i
++)
314 dev_dbg(&xr_usb_serial
->control
->dev
,"0x%02x\n",p
[i
]);
321 /* this urb is terminated, clean up */
322 dev_dbg(&xr_usb_serial
->control
->dev
,
323 "%s - urb shutting down with status: %d\n",
327 dev_dbg(&xr_usb_serial
->control
->dev
,
328 "%s - nonzero urb status received: %d\n",
333 usb_mark_last_busy(xr_usb_serial
->dev
);
335 data
= (unsigned char *)(dr
+ 1);
336 switch (dr
->bNotificationType
) {
337 case USB_CDC_NOTIFY_NETWORK_CONNECTION
:
338 dev_dbg(&xr_usb_serial
->control
->dev
, "%s - network connection: %d\n",
339 __func__
, dr
->wValue
);
342 case USB_CDC_NOTIFY_SERIAL_STATE
:
343 #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 9, 0)
344 newctrl
= get_unaligned_le16(data
);
345 if (!xr_usb_serial
->clocal
&& (xr_usb_serial
->ctrlin
& ~newctrl
& XR_USB_SERIAL_CTRL_DCD
)) {
346 dev_dbg(&xr_usb_serial
->control
->dev
, "%s - calling hangup\n",
348 tty_port_tty_hangup(&xr_usb_serial
->port
, false);
351 tty
= tty_port_tty_get(&xr_usb_serial
->port
);
352 newctrl
= get_unaligned_le16(data
);
355 if (!xr_usb_serial
->clocal
&&
356 (xr_usb_serial
->ctrlin
& ~newctrl
& XR_USB_SERIAL_CTRL_DCD
)) {
357 dev_dbg(&xr_usb_serial
->control
->dev
,
358 "%s - calling hangup\n", __func__
);
364 xr_usb_serial
->ctrlin
= newctrl
;
366 dev_dbg(&xr_usb_serial
->control
->dev
,
367 "%s - input control lines: dcd%c dsr%c break%c "
368 "ring%c framing%c parity%c overrun%c\n",
370 xr_usb_serial
->ctrlin
& XR_USB_SERIAL_CTRL_DCD
? '+' : '-',
371 xr_usb_serial
->ctrlin
& XR_USB_SERIAL_CTRL_DSR
? '+' : '-',
372 xr_usb_serial
->ctrlin
& XR_USB_SERIAL_CTRL_BRK
? '+' : '-',
373 xr_usb_serial
->ctrlin
& XR_USB_SERIAL_CTRL_RI
? '+' : '-',
374 xr_usb_serial
->ctrlin
& XR_USB_SERIAL_CTRL_FRAMING
? '+' : '-',
375 xr_usb_serial
->ctrlin
& XR_USB_SERIAL_CTRL_PARITY
? '+' : '-',
376 xr_usb_serial
->ctrlin
& XR_USB_SERIAL_CTRL_OVERRUN
? '+' : '-');
380 dev_dbg(&xr_usb_serial
->control
->dev
,
381 "%s - unknown notification %d received: index %d "
382 "len %d data0 %d data1 %d\n",
384 dr
->bNotificationType
, dr
->wIndex
,
385 dr
->wLength
, data
[0], data
[1]);
389 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
391 dev_err(&xr_usb_serial
->control
->dev
, "%s - usb_submit_urb failed: %d\n",
395 static int xr_usb_serial_submit_read_urb(struct xr_usb_serial
*xr_usb_serial
, int index
, gfp_t mem_flags
)
399 if (!test_and_clear_bit(index
, &xr_usb_serial
->read_urbs_free
))
402 dev_vdbg(&xr_usb_serial
->data
->dev
, "%s - urb %d\n", __func__
, index
);
404 res
= usb_submit_urb(xr_usb_serial
->read_urbs
[index
], mem_flags
);
407 dev_err(&xr_usb_serial
->data
->dev
,
408 "%s - usb_submit_urb failed: %d\n",
411 set_bit(index
, &xr_usb_serial
->read_urbs_free
);
418 static int xr_usb_serial_submit_read_urbs(struct xr_usb_serial
*xr_usb_serial
, gfp_t mem_flags
)
423 for (i
= 0; i
< xr_usb_serial
->rx_buflimit
; ++i
) {
424 res
= xr_usb_serial_submit_read_urb(xr_usb_serial
, i
, mem_flags
);
431 static void xr_usb_serial_process_read_urb(struct xr_usb_serial
*xr_usb_serial
, struct urb
*urb
)
433 struct tty_struct
*tty
;
434 if (!urb
->actual_length
)
436 #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 9, 0)
437 tty_insert_flip_string(&xr_usb_serial
->port
, urb
->transfer_buffer
,
439 tty_flip_buffer_push(&xr_usb_serial
->port
);
441 tty
= tty_port_tty_get(&xr_usb_serial
->port
);
444 tty_insert_flip_string(tty
, urb
->transfer_buffer
, urb
->actual_length
);
445 tty_flip_buffer_push(tty
);
451 static void xr_usb_serial_read_bulk_callback(struct urb
*urb
)
453 struct xr_usb_serial_rb
*rb
= urb
->context
;
454 struct xr_usb_serial
*xr_usb_serial
= rb
->instance
;
457 dev_vdbg(&xr_usb_serial
->data
->dev
, "%s - urb %d, len %d\n", __func__
,
458 rb
->index
, urb
->actual_length
);
459 set_bit(rb
->index
, &xr_usb_serial
->read_urbs_free
);
461 if (!xr_usb_serial
->dev
) {
462 dev_dbg(&xr_usb_serial
->data
->dev
, "%s - disconnected\n", __func__
);
465 usb_mark_last_busy(xr_usb_serial
->dev
);
468 dev_dbg(&xr_usb_serial
->data
->dev
, "%s - non-zero urb status: %d\n",
469 __func__
, urb
->status
);
472 xr_usb_serial_process_read_urb(xr_usb_serial
, urb
);
474 /* throttle device if requested by tty */
475 spin_lock_irqsave(&xr_usb_serial
->read_lock
, flags
);
476 xr_usb_serial
->throttled
= xr_usb_serial
->throttle_req
;
477 if (!xr_usb_serial
->throttled
&& !xr_usb_serial
->susp_count
) {
478 spin_unlock_irqrestore(&xr_usb_serial
->read_lock
, flags
);
479 xr_usb_serial_submit_read_urb(xr_usb_serial
, rb
->index
, GFP_ATOMIC
);
481 spin_unlock_irqrestore(&xr_usb_serial
->read_lock
, flags
);
485 /* data interface wrote those outgoing bytes */
486 static void xr_usb_serial_write_bulk(struct urb
*urb
)
488 struct xr_usb_serial_wb
*wb
= urb
->context
;
489 struct xr_usb_serial
*xr_usb_serial
= wb
->instance
;
492 if (urb
->status
|| (urb
->actual_length
!= urb
->transfer_buffer_length
))
493 dev_vdbg(&xr_usb_serial
->data
->dev
, "%s - len %d/%d, status %d\n",
496 urb
->transfer_buffer_length
,
499 spin_lock_irqsave(&xr_usb_serial
->write_lock
, flags
);
500 xr_usb_serial_write_done(xr_usb_serial
, wb
);
501 spin_unlock_irqrestore(&xr_usb_serial
->write_lock
, flags
);
502 schedule_work(&xr_usb_serial
->work
);
505 static void xr_usb_serial_softint(struct work_struct
*work
)
507 struct xr_usb_serial
*xr_usb_serial
= container_of(work
, struct xr_usb_serial
, work
);
508 struct tty_struct
*tty
;
510 dev_vdbg(&xr_usb_serial
->data
->dev
, "%s\n", __func__
);
511 #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 9, 0)
512 tty_port_tty_wakeup(&xr_usb_serial
->port
);
514 tty
= tty_port_tty_get(&xr_usb_serial
->port
);
526 static int xr_usb_serial_tty_install(struct tty_driver
*driver
, struct tty_struct
*tty
)
528 struct xr_usb_serial
*xr_usb_serial
;
531 dev_dbg(tty
->dev
, "%s\n", __func__
);
533 xr_usb_serial
= xr_usb_serial_get_by_index(tty
->index
);
537 retval
= tty_standard_install(driver
, tty
);
539 goto error_init_termios
;
541 tty
->driver_data
= xr_usb_serial
;
546 tty_port_put(&xr_usb_serial
->port
);
550 static int xr_usb_serial_tty_open(struct tty_struct
*tty
, struct file
*filp
)
552 struct xr_usb_serial
*xr_usb_serial
= tty
->driver_data
;
554 dev_dbg(tty
->dev
, "%s\n", __func__
);
556 return tty_port_open(&xr_usb_serial
->port
, tty
, filp
);
559 static int xr_usb_serial_port_activate(struct tty_port
*port
, struct tty_struct
*tty
)
561 struct xr_usb_serial
*xr_usb_serial
= container_of(port
, struct xr_usb_serial
, port
);
562 int retval
= -ENODEV
;
564 dev_dbg(&xr_usb_serial
->control
->dev
, "%s\n", __func__
);
566 mutex_lock(&xr_usb_serial
->mutex
);
567 if (xr_usb_serial
->disconnected
)
570 retval
= usb_autopm_get_interface(xr_usb_serial
->control
);
572 goto error_get_interface
;
575 * FIXME: Why do we need this? Allocating 64K of physically contiguous
576 * memory is really nasty...
578 set_bit(TTY_NO_WRITE_SPLIT
, &tty
->flags
);
579 xr_usb_serial
->control
->needs_remote_wakeup
= 1;
581 xr_usb_serial
->ctrlurb
->dev
= xr_usb_serial
->dev
;
582 if (usb_submit_urb(xr_usb_serial
->ctrlurb
, GFP_KERNEL
)) {
583 dev_err(&xr_usb_serial
->control
->dev
,
584 "%s - usb_submit_urb(ctrl irq) failed\n", __func__
);
585 goto error_submit_urb
;
588 xr_usb_serial
->ctrlout
= XR_USB_SERIAL_CTRL_DTR
| XR_USB_SERIAL_CTRL_RTS
;
589 if (xr_usb_serial_set_control(xr_usb_serial
, xr_usb_serial
->ctrlout
) < 0 &&
590 (xr_usb_serial
->ctrl_caps
& USB_CDC_CAP_LINE
))
591 goto error_set_control
;
593 usb_autopm_put_interface(xr_usb_serial
->control
);
596 * Unthrottle device in case the TTY was closed while throttled.
598 spin_lock_irq(&xr_usb_serial
->read_lock
);
599 xr_usb_serial
->throttled
= 0;
600 xr_usb_serial
->throttle_req
= 0;
601 spin_unlock_irq(&xr_usb_serial
->read_lock
);
603 if (xr_usb_serial_submit_read_urbs(xr_usb_serial
, GFP_KERNEL
))
604 goto error_submit_read_urbs
;
606 mutex_unlock(&xr_usb_serial
->mutex
);
610 error_submit_read_urbs
:
611 xr_usb_serial
->ctrlout
= 0;
612 xr_usb_serial_set_control(xr_usb_serial
, xr_usb_serial
->ctrlout
);
614 usb_kill_urb(xr_usb_serial
->ctrlurb
);
616 usb_autopm_put_interface(xr_usb_serial
->control
);
619 mutex_unlock(&xr_usb_serial
->mutex
);
623 static void xr_usb_serial_port_destruct(struct tty_port
*port
)
625 struct xr_usb_serial
*xr_usb_serial
= container_of(port
, struct xr_usb_serial
, port
);
627 dev_dbg(&xr_usb_serial
->control
->dev
, "%s\n", __func__
);
628 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)
629 tty_unregister_device(xr_usb_serial_tty_driver
, xr_usb_serial
->minor
);
631 xr_usb_serial_release_minor(xr_usb_serial
);
632 usb_put_intf(xr_usb_serial
->control
);
633 kfree(xr_usb_serial
->country_codes
);
634 kfree(xr_usb_serial
);
637 static void xr_usb_serial_port_shutdown(struct tty_port
*port
)
639 struct xr_usb_serial
*xr_usb_serial
= container_of(port
, struct xr_usb_serial
, port
);
642 dev_dbg(&xr_usb_serial
->control
->dev
, "%s\n", __func__
);
644 mutex_lock(&xr_usb_serial
->mutex
);
645 if (!xr_usb_serial
->disconnected
) {
646 usb_autopm_get_interface(xr_usb_serial
->control
);
647 xr_usb_serial_set_control(xr_usb_serial
, xr_usb_serial
->ctrlout
= 0);
648 usb_kill_urb(xr_usb_serial
->ctrlurb
);
649 for (i
= 0; i
< XR_USB_SERIAL_NW
; i
++)
650 usb_kill_urb(xr_usb_serial
->wb
[i
].urb
);
651 for (i
= 0; i
< xr_usb_serial
->rx_buflimit
; i
++)
652 usb_kill_urb(xr_usb_serial
->read_urbs
[i
]);
653 xr_usb_serial
->control
->needs_remote_wakeup
= 0;
654 usb_autopm_put_interface(xr_usb_serial
->control
);
656 mutex_unlock(&xr_usb_serial
->mutex
);
659 static void xr_usb_serial_tty_cleanup(struct tty_struct
*tty
)
661 struct xr_usb_serial
*xr_usb_serial
= tty
->driver_data
;
662 dev_dbg(&xr_usb_serial
->control
->dev
, "%s\n", __func__
);
663 tty_port_put(&xr_usb_serial
->port
);
666 static void xr_usb_serial_tty_hangup(struct tty_struct
*tty
)
668 struct xr_usb_serial
*xr_usb_serial
= tty
->driver_data
;
669 dev_dbg(&xr_usb_serial
->control
->dev
, "%s\n", __func__
);
670 tty_port_hangup(&xr_usb_serial
->port
);
673 static void xr_usb_serial_tty_close(struct tty_struct
*tty
, struct file
*filp
)
675 struct xr_usb_serial
*xr_usb_serial
= tty
->driver_data
;
676 dev_dbg(&xr_usb_serial
->control
->dev
, "%s\n", __func__
);
677 tty_port_close(&xr_usb_serial
->port
, tty
, filp
);
680 static int xr_usb_serial_tty_write(struct tty_struct
*tty
,
681 const unsigned char *buf
, int count
)
683 struct xr_usb_serial
*xr_usb_serial
= tty
->driver_data
;
687 struct xr_usb_serial_wb
*wb
;
692 dev_vdbg(&xr_usb_serial
->data
->dev
, "%s - count %d\n", __func__
, count
);
694 spin_lock_irqsave(&xr_usb_serial
->write_lock
, flags
);
695 wbn
= xr_usb_serial_wb_alloc(xr_usb_serial
);
697 spin_unlock_irqrestore(&xr_usb_serial
->write_lock
, flags
);
700 wb
= &xr_usb_serial
->wb
[wbn
];
702 if (!xr_usb_serial
->dev
) {
704 spin_unlock_irqrestore(&xr_usb_serial
->write_lock
, flags
);
708 count
= (count
> xr_usb_serial
->writesize
) ? xr_usb_serial
->writesize
: count
;
709 dev_vdbg(&xr_usb_serial
->data
->dev
, "%s - write %d\n", __func__
, count
);
710 memcpy(wb
->buf
, buf
, count
);
713 usb_autopm_get_interface_async(xr_usb_serial
->control
);
714 if (xr_usb_serial
->susp_count
) {
715 if (!xr_usb_serial
->delayed_wb
)
716 xr_usb_serial
->delayed_wb
= wb
;
718 usb_autopm_put_interface_async(xr_usb_serial
->control
);
719 spin_unlock_irqrestore(&xr_usb_serial
->write_lock
, flags
);
720 return count
; /* A white lie */
722 usb_mark_last_busy(xr_usb_serial
->dev
);
724 stat
= xr_usb_serial_start_wb(xr_usb_serial
, wb
);
725 spin_unlock_irqrestore(&xr_usb_serial
->write_lock
, flags
);
732 static int xr_usb_serial_tty_write_room(struct tty_struct
*tty
)
734 struct xr_usb_serial
*xr_usb_serial
= tty
->driver_data
;
736 * Do not let the line discipline to know that we have a reserve,
737 * or it might get too enthusiastic.
739 return xr_usb_serial_wb_is_avail(xr_usb_serial
) ? xr_usb_serial
->writesize
: 0;
742 static int xr_usb_serial_tty_chars_in_buffer(struct tty_struct
*tty
)
744 struct xr_usb_serial
*xr_usb_serial
= tty
->driver_data
;
746 * if the device was unplugged then any remaining characters fell out
747 * of the connector ;)
749 if (xr_usb_serial
->disconnected
)
752 * This is inaccurate (overcounts), but it works.
754 return (XR_USB_SERIAL_NW
- xr_usb_serial_wb_is_avail(xr_usb_serial
)) * xr_usb_serial
->writesize
;
757 static void xr_usb_serial_tty_throttle(struct tty_struct
*tty
)
759 struct xr_usb_serial
*xr_usb_serial
= tty
->driver_data
;
761 spin_lock_irq(&xr_usb_serial
->read_lock
);
762 xr_usb_serial
->throttle_req
= 1;
763 spin_unlock_irq(&xr_usb_serial
->read_lock
);
766 static void xr_usb_serial_tty_unthrottle(struct tty_struct
*tty
)
768 struct xr_usb_serial
*xr_usb_serial
= tty
->driver_data
;
769 unsigned int was_throttled
;
771 spin_lock_irq(&xr_usb_serial
->read_lock
);
772 was_throttled
= xr_usb_serial
->throttled
;
773 xr_usb_serial
->throttled
= 0;
774 xr_usb_serial
->throttle_req
= 0;
775 spin_unlock_irq(&xr_usb_serial
->read_lock
);
778 xr_usb_serial_submit_read_urbs(xr_usb_serial
, GFP_KERNEL
);
781 static int xr_usb_serial_tty_break_ctl(struct tty_struct
*tty
, int state
)
783 struct xr_usb_serial
*xr_usb_serial
= tty
->driver_data
;
786 retval
= xr_usb_serial_send_break(xr_usb_serial
, state
? 0xffff : 0);
788 dev_dbg(&xr_usb_serial
->control
->dev
, "%s - send break failed\n",
793 static int xr_usb_serial_tty_tiocmget(struct tty_struct
*tty
)
795 struct xr_usb_serial
*xr_usb_serial
= tty
->driver_data
;
796 dev_dbg(&xr_usb_serial
->control
->dev
, "xr_usb_serial_tty_tiocmget\n");
797 return xr_usb_serial_tiocmget(xr_usb_serial
);
801 static int xr_usb_serial_tty_tiocmset(struct tty_struct
*tty
,
802 unsigned int set
, unsigned int clear
)
804 struct xr_usb_serial
*xr_usb_serial
= tty
->driver_data
;
805 dev_dbg(&xr_usb_serial
->control
->dev
, "xr_usb_serial_tty_tiocmset set=0x%x clear=0x%x\n",set
,clear
);
806 return xr_usb_serial_tiocmset(xr_usb_serial
,set
,clear
);
810 static int get_serial_info(struct xr_usb_serial
*xr_usb_serial
, struct serial_struct __user
*info
)
812 struct serial_struct tmp
;
817 memset(&tmp
, 0, sizeof(tmp
));
818 tmp
.flags
= ASYNC_LOW_LATENCY
;
819 tmp
.xmit_fifo_size
= xr_usb_serial
->writesize
;
820 tmp
.baud_base
= le32_to_cpu(xr_usb_serial
->line
.dwDTERate
);
821 tmp
.close_delay
= xr_usb_serial
->port
.close_delay
/ 10;
822 tmp
.closing_wait
= xr_usb_serial
->port
.closing_wait
== ASYNC_CLOSING_WAIT_NONE
?
823 ASYNC_CLOSING_WAIT_NONE
:
824 xr_usb_serial
->port
.closing_wait
/ 10;
826 if (copy_to_user(info
, &tmp
, sizeof(tmp
)))
832 static int set_serial_info(struct xr_usb_serial
*xr_usb_serial
,
833 struct serial_struct __user
*newinfo
)
835 struct serial_struct new_serial
;
836 unsigned int closing_wait
, close_delay
;
839 if (copy_from_user(&new_serial
, newinfo
, sizeof(new_serial
)))
842 close_delay
= new_serial
.close_delay
* 10;
843 closing_wait
= new_serial
.closing_wait
== ASYNC_CLOSING_WAIT_NONE
?
844 ASYNC_CLOSING_WAIT_NONE
: new_serial
.closing_wait
* 10;
846 mutex_lock(&xr_usb_serial
->port
.mutex
);
848 if (!capable(CAP_SYS_ADMIN
)) {
849 if ((close_delay
!= xr_usb_serial
->port
.close_delay
) ||
850 (closing_wait
!= xr_usb_serial
->port
.closing_wait
))
853 retval
= -EOPNOTSUPP
;
855 xr_usb_serial
->port
.close_delay
= close_delay
;
856 xr_usb_serial
->port
.closing_wait
= closing_wait
;
859 mutex_unlock(&xr_usb_serial
->port
.mutex
);
863 static int xr_usb_serial_tty_ioctl(struct tty_struct
*tty
,
864 unsigned int cmd
, unsigned long arg
)
866 struct xr_usb_serial
*xr_usb_serial
= tty
->driver_data
;
867 int rv
= -ENOIOCTLCMD
;
868 unsigned int channel
, reg
, val
;
872 case TIOCGSERIAL
: /* gets serial port data */
873 rv
= get_serial_info(xr_usb_serial
, (struct serial_struct __user
*) arg
);
876 rv
= set_serial_info(xr_usb_serial
, (struct serial_struct __user
*) arg
);
878 case XR_USB_SERIAL_GET_REG
:
879 if (get_user(channel
, (int __user
*)arg
))
881 if (get_user(reg
, (int __user
*)(arg
+ sizeof(int))))
884 data
= kmalloc(2, GFP_KERNEL
);
886 dev_err(&xr_usb_serial
->control
->dev
, "%s - Cannot allocate USB buffer.\n", __func__
);
892 rv
= xr_usb_serial_get_reg(xr_usb_serial
,reg
, data
);
896 rv
= xr_usb_serial_get_reg_ext(xr_usb_serial
,channel
,reg
, data
);
899 dev_err(&xr_usb_serial
->control
->dev
, "Cannot get register (%d)\n", rv
);
903 if (put_user(le16_to_cpu(*data
), (int __user
*)(arg
+ 2 * sizeof(int))))
905 dev_err(&xr_usb_serial
->control
->dev
, "Cannot put user result\n");
913 case XR_USB_SERIAL_SET_REG
:
914 if (get_user(channel
, (int __user
*)arg
))
916 if (get_user(reg
, (int __user
*)(arg
+ sizeof(int))))
918 if (get_user(val
, (int __user
*)(arg
+ 2 * sizeof(int))))
923 rv
= xr_usb_serial_set_reg(xr_usb_serial
,reg
, val
);
927 rv
= xr_usb_serial_set_reg_ext(xr_usb_serial
,channel
,reg
, val
);
934 case XR_USB_SERIAL_LOOPBACK
:
935 if (get_user(channel
, (int __user
*)arg
))
938 channel
= xr_usb_serial
->channel
;
939 rv
= xr_usb_serial_set_loopback(xr_usb_serial
,channel
);
950 static void xr_usb_serial_tty_set_termios(struct tty_struct
*tty
,
951 struct ktermios
*termios_old
)
953 struct xr_usb_serial
*xr_usb_serial
= tty
->driver_data
;
954 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)
955 struct ktermios
*termios
= tty
->termios
;
957 struct ktermios
*termios
= &tty
->termios
;
959 unsigned int cflag
= termios
->c_cflag
;
960 struct usb_cdc_line_coding newline
;
961 int newctrl
= xr_usb_serial
->ctrlout
;
962 xr_usb_serial_disable(xr_usb_serial
);
963 newline
.dwDTERate
= cpu_to_le32(tty_get_baud_rate(tty
));
964 newline
.bCharFormat
= termios
->c_cflag
& CSTOPB
? 1 : 0;
965 newline
.bParityType
= termios
->c_cflag
& PARENB
?
966 (termios
->c_cflag
& PARODD
? 1 : 2) +
967 (termios
->c_cflag
& CMSPAR
? 2 : 0) : 0;
968 switch (termios
->c_cflag
& CSIZE
) {
969 case CS5
:/*using CS5 replace of the 9 bit data mode*/
970 newline
.bDataBits
= 9;
973 newline
.bDataBits
= 6;
976 newline
.bDataBits
= 7;
980 newline
.bDataBits
= 8;
983 /* FIXME: Needs to clear unsupported bits in the termios */
984 xr_usb_serial
->clocal
= ((termios
->c_cflag
& CLOCAL
) != 0);
986 if (!newline
.dwDTERate
) {
987 newline
.dwDTERate
= xr_usb_serial
->line
.dwDTERate
;
988 newctrl
&= ~XR_USB_SERIAL_CTRL_DTR
;
990 newctrl
|= XR_USB_SERIAL_CTRL_DTR
;
992 if (newctrl
!= xr_usb_serial
->ctrlout
)
993 xr_usb_serial_set_control(xr_usb_serial
, xr_usb_serial
->ctrlout
= newctrl
);
995 xr_usb_serial_set_flow_mode(xr_usb_serial
,tty
,cflag
);/*set the serial flow mode*/
997 if (memcmp(&xr_usb_serial
->line
, &newline
, sizeof newline
))
999 memcpy(&xr_usb_serial
->line
, &newline
, sizeof newline
);
1000 dev_dbg(&xr_usb_serial
->control
->dev
, "%s - set line: %d %d %d %d\n",
1002 le32_to_cpu(newline
.dwDTERate
),
1003 newline
.bCharFormat
, newline
.bParityType
,
1005 xr_usb_serial_set_line(xr_usb_serial
, &xr_usb_serial
->line
);
1007 xr_usb_serial_enable(xr_usb_serial
);
1010 static const struct tty_port_operations xr_usb_serial_port_ops
= {
1011 .shutdown
= xr_usb_serial_port_shutdown
,
1012 .activate
= xr_usb_serial_port_activate
,
1013 .destruct
= xr_usb_serial_port_destruct
,
1017 * USB probe and disconnect routines.
1020 /* Little helpers: write/read buffers free */
1021 static void xr_usb_serial_write_buffers_free(struct xr_usb_serial
*xr_usb_serial
)
1024 struct xr_usb_serial_wb
*wb
;
1025 struct usb_device
*usb_dev
= interface_to_usbdev(xr_usb_serial
->control
);
1027 for (wb
= &xr_usb_serial
->wb
[0], i
= 0; i
< XR_USB_SERIAL_NW
; i
++, wb
++)
1028 usb_free_coherent(usb_dev
, xr_usb_serial
->writesize
, wb
->buf
, wb
->dmah
);
1031 static void xr_usb_serial_read_buffers_free(struct xr_usb_serial
*xr_usb_serial
)
1033 struct usb_device
*usb_dev
= interface_to_usbdev(xr_usb_serial
->control
);
1036 for (i
= 0; i
< xr_usb_serial
->rx_buflimit
; i
++)
1037 usb_free_coherent(usb_dev
, xr_usb_serial
->readsize
,
1038 xr_usb_serial
->read_buffers
[i
].base
, xr_usb_serial
->read_buffers
[i
].dma
);
1041 /* Little helper: write buffers allocate */
1042 static int xr_usb_serial_write_buffers_alloc(struct xr_usb_serial
*xr_usb_serial
)
1045 struct xr_usb_serial_wb
*wb
;
1047 for (wb
= &xr_usb_serial
->wb
[0], i
= 0; i
< XR_USB_SERIAL_NW
; i
++, wb
++) {
1048 wb
->buf
= usb_alloc_coherent(xr_usb_serial
->dev
, xr_usb_serial
->writesize
, GFP_KERNEL
,
1054 usb_free_coherent(xr_usb_serial
->dev
, xr_usb_serial
->writesize
,
1063 static int xr_usb_serial_probe(struct usb_interface
*intf
,
1064 const struct usb_device_id
*id
)
1066 struct usb_cdc_union_desc
*union_header
= NULL
;
1067 struct usb_cdc_country_functional_desc
*cfd
= NULL
;
1068 unsigned char *buffer
= intf
->altsetting
->extra
;
1069 int buflen
= intf
->altsetting
->extralen
;
1070 struct usb_interface
*control_interface
;
1071 struct usb_interface
*data_interface
;
1072 struct usb_endpoint_descriptor
*epctrl
= NULL
;
1073 struct usb_endpoint_descriptor
*epread
= NULL
;
1074 struct usb_endpoint_descriptor
*epwrite
= NULL
;
1075 struct usb_device
*usb_dev
= interface_to_usbdev(intf
);
1076 struct xr_usb_serial
*xr_usb_serial
;
1078 int ctrlsize
, readsize
;
1080 u8 ac_management_function
= 0;
1081 u8 call_management_function
= 0;
1082 int call_interface_num
= -1;
1083 int data_interface_num
= -1;
1084 unsigned long quirks
;
1087 int combined_interfaces
= 0;
1088 struct device
*tty_dev
;
1092 quirks
= (unsigned long)id
->driver_info
;
1094 if (quirks
== IGNORE_DEVICE
)
1097 num_rx_buf
= (quirks
== SINGLE_RX_URB
) ? 1 : XR_USB_SERIAL_NR
;
1099 dev_dbg(&intf
->dev
, "USB_device_id idVendor:%04x, idProduct %04x\n",id
->idVendor
,id
->idProduct
);
1101 /* handle quirks deadly to normal probing*/
1102 if (quirks
== NO_UNION_NORMAL
) {
1103 data_interface
= usb_ifnum_to_if(usb_dev
, 1);
1104 control_interface
= usb_ifnum_to_if(usb_dev
, 0);
1105 goto skip_normal_probe
;
1110 dev_err(&intf
->dev
, "Weird descriptor references\n");
1115 if (intf
->cur_altsetting
->endpoint
&&
1116 intf
->cur_altsetting
->endpoint
->extralen
&&
1117 intf
->cur_altsetting
->endpoint
->extra
) {
1119 "Seeking extra descriptors on endpoint\n");
1120 buflen
= intf
->cur_altsetting
->endpoint
->extralen
;
1121 buffer
= intf
->cur_altsetting
->endpoint
->extra
;
1124 "Zero length descriptor references\n");
1129 while (buflen
> 0) {
1130 if (buffer
[1] != USB_DT_CS_INTERFACE
) {
1131 dev_err(&intf
->dev
, "skipping garbage\n");
1135 switch (buffer
[2]) {
1136 case USB_CDC_UNION_TYPE
: /* we've found it */
1138 dev_err(&intf
->dev
, "More than one "
1139 "union descriptor, skipping ...\n");
1142 union_header
= (struct usb_cdc_union_desc
*)buffer
;
1144 case USB_CDC_COUNTRY_TYPE
: /* export through sysfs*/
1145 cfd
= (struct usb_cdc_country_functional_desc
*)buffer
;
1147 case USB_CDC_HEADER_TYPE
: /* maybe check version */
1148 break; /* for now we ignore it */
1149 case USB_CDC_ACM_TYPE
:
1150 ac_management_function
= buffer
[3];
1152 case USB_CDC_CALL_MANAGEMENT_TYPE
:
1153 call_management_function
= buffer
[3];
1154 call_interface_num
= buffer
[4];
1155 if ((quirks
& NOT_A_MODEM
) == 0 && (call_management_function
& 3) != 3)
1156 dev_err(&intf
->dev
, "This device cannot do calls on its own. It is not a modem.\n");
1159 /* there are LOTS more CDC descriptors that
1160 * could legitimately be found here.
1162 dev_dbg(&intf
->dev
, "Ignoring descriptor: "
1163 "type %02x, length %d\n",
1164 buffer
[2], buffer
[0]);
1168 buflen
-= buffer
[0];
1169 buffer
+= buffer
[0];
1172 if (!union_header
) {
1173 if (call_interface_num
> 0) {
1174 dev_dbg(&intf
->dev
, "No union descriptor, using call management descriptor\n");
1175 /* quirks for Droids MuIn LCD */
1176 if (quirks
& NO_DATA_INTERFACE
)
1177 data_interface
= usb_ifnum_to_if(usb_dev
, 0);
1179 data_interface
= usb_ifnum_to_if(usb_dev
, (data_interface_num
= call_interface_num
));
1180 control_interface
= intf
;
1182 if (intf
->cur_altsetting
->desc
.bNumEndpoints
!= 3) {
1183 dev_dbg(&intf
->dev
,"No union descriptor, giving up\n");
1186 dev_warn(&intf
->dev
,"No union descriptor, testing for castrated device\n");
1187 combined_interfaces
= 1;
1188 control_interface
= data_interface
= intf
;
1189 goto look_for_collapsed_interface
;
1193 control_interface
= usb_ifnum_to_if(usb_dev
, union_header
->bMasterInterface0
);
1194 data_interface
= usb_ifnum_to_if(usb_dev
, (data_interface_num
= union_header
->bSlaveInterface0
));
1195 if (!control_interface
|| !data_interface
) {
1196 dev_dbg(&intf
->dev
, "no interfaces\n");
1201 if (data_interface_num
!= call_interface_num
)
1202 dev_dbg(&intf
->dev
, "Separate call control interface. That is not fully supported.\n");
1204 if (control_interface
== data_interface
) {
1205 /* some broken devices designed for windows work this way */
1206 dev_warn(&intf
->dev
,"Control and data interfaces are not separated!\n");
1207 combined_interfaces
= 1;
1208 /* a popular other OS doesn't use it */
1209 quirks
|= NO_CAP_LINE
;
1210 if (data_interface
->cur_altsetting
->desc
.bNumEndpoints
!= 3) {
1211 dev_err(&intf
->dev
, "This needs exactly 3 endpoints\n");
1214 look_for_collapsed_interface
:
1215 for (i
= 0; i
< 3; i
++) {
1216 struct usb_endpoint_descriptor
*ep
;
1217 ep
= &data_interface
->cur_altsetting
->endpoint
[i
].desc
;
1219 if (usb_endpoint_is_int_in(ep
))
1221 else if (usb_endpoint_is_bulk_out(ep
))
1223 else if (usb_endpoint_is_bulk_in(ep
))
1228 if (!epctrl
|| !epread
|| !epwrite
)
1231 goto made_compressed_probe
;
1236 /*workaround for switched interfaces */
1237 if (data_interface
->cur_altsetting
->desc
.bInterfaceClass
1238 != CDC_DATA_INTERFACE_TYPE
) {
1239 if (control_interface
->cur_altsetting
->desc
.bInterfaceClass
1240 == CDC_DATA_INTERFACE_TYPE
) {
1241 struct usb_interface
*t
;
1243 "Your device has switched interfaces.\n");
1244 t
= control_interface
;
1245 control_interface
= data_interface
;
1252 /* Accept probe requests only for the control interface */
1253 if (!combined_interfaces
&& intf
!= control_interface
)
1256 if (!combined_interfaces
&& usb_interface_claimed(data_interface
)) {
1257 /* valid in this context */
1258 dev_dbg(&intf
->dev
, "The data interface isn't available\n");
1263 if (data_interface
->cur_altsetting
->desc
.bNumEndpoints
< 2 ||
1264 control_interface
->cur_altsetting
->desc
.bNumEndpoints
== 0)
1267 epctrl
= &control_interface
->cur_altsetting
->endpoint
[0].desc
;
1268 epread
= &data_interface
->cur_altsetting
->endpoint
[0].desc
;
1269 epwrite
= &data_interface
->cur_altsetting
->endpoint
[1].desc
;
1272 /* workaround for switched endpoints */
1273 if (!usb_endpoint_dir_in(epread
)) {
1274 /* descriptors are swapped */
1275 struct usb_endpoint_descriptor
*t
;
1277 "The data interface has switched endpoints\n");
1282 made_compressed_probe
:
1283 dev_dbg(&intf
->dev
, "interfaces are valid\n");
1285 xr_usb_serial
= kzalloc(sizeof(struct xr_usb_serial
), GFP_KERNEL
);
1286 if (xr_usb_serial
== NULL
) {
1287 dev_err(&intf
->dev
, "out of memory (xr_usb_serial kzalloc)\n");
1291 minor
= xr_usb_serial_alloc_minor(xr_usb_serial
);
1292 if (minor
== XR_USB_SERIAL_TTY_MINORS
) {
1293 dev_err(&intf
->dev
, "no more free xr_usb_serial devices\n");
1294 kfree(xr_usb_serial
);
1298 ctrlsize
= usb_endpoint_maxp(epctrl
);
1299 readsize
= usb_endpoint_maxp(epread
) *
1300 (quirks
== SINGLE_RX_URB
? 1 : 2);
1301 xr_usb_serial
->combined_interfaces
= combined_interfaces
;
1302 xr_usb_serial
->writesize
= usb_endpoint_maxp(epwrite
) * 20;
1303 xr_usb_serial
->control
= control_interface
;
1304 xr_usb_serial
->data
= data_interface
;
1305 xr_usb_serial
->minor
= minor
;
1306 xr_usb_serial
->dev
= usb_dev
;
1307 xr_usb_serial
->ctrl_caps
= ac_management_function
;
1308 if (quirks
& NO_CAP_LINE
)
1309 xr_usb_serial
->ctrl_caps
&= ~USB_CDC_CAP_LINE
;
1310 xr_usb_serial
->ctrlsize
= ctrlsize
;
1311 xr_usb_serial
->readsize
= readsize
;
1312 xr_usb_serial
->rx_buflimit
= num_rx_buf
;
1313 INIT_WORK(&xr_usb_serial
->work
, xr_usb_serial_softint
);
1314 spin_lock_init(&xr_usb_serial
->write_lock
);
1315 spin_lock_init(&xr_usb_serial
->read_lock
);
1316 mutex_init(&xr_usb_serial
->mutex
);
1317 xr_usb_serial
->rx_endpoint
= usb_rcvbulkpipe(usb_dev
, epread
->bEndpointAddress
);
1318 xr_usb_serial
->is_int_ep
= usb_endpoint_xfer_int(epread
);
1319 if (xr_usb_serial
->is_int_ep
)
1320 xr_usb_serial
->bInterval
= epread
->bInterval
;
1321 tty_port_init(&xr_usb_serial
->port
);
1322 xr_usb_serial
->port
.ops
= &xr_usb_serial_port_ops
;
1323 xr_usb_serial
->DeviceVendor
= id
->idVendor
;
1324 xr_usb_serial
->DeviceProduct
= id
->idProduct
;
1326 if((xr_usb_serial
->DeviceProduct
&0xfff0) == 0x1410)
1327 {//map the serial port A B C D to blocknum 0 1 2 3 for the xr21v141x device
1328 xr_usb_serial
->channel
= epwrite
->bEndpointAddress
- 1;
1330 else if((xr_usb_serial
->DeviceProduct
&0xfff0) == 0x1420)
1331 {//map the serial port A B C D to blocknum 0 2 4 6 for the xr21B142x device
1332 xr_usb_serial
->channel
= (epwrite
->bEndpointAddress
- 4)*2;
1336 xr_usb_serial
->channel
= epwrite
->bEndpointAddress
;
1339 xr_usb_serial
->channel
= epwrite
->bEndpointAddress
;
1340 dev_dbg(&intf
->dev
, "epwrite->bEndpointAddress =%d\n",epwrite
->bEndpointAddress
);
1342 buf
= usb_alloc_coherent(usb_dev
, ctrlsize
, GFP_KERNEL
, &xr_usb_serial
->ctrl_dma
);
1344 dev_err(&intf
->dev
, "out of memory (ctrl buffer alloc)\n");
1347 xr_usb_serial
->ctrl_buffer
= buf
;
1349 if (xr_usb_serial_write_buffers_alloc(xr_usb_serial
) < 0) {
1350 dev_err(&intf
->dev
, "out of memory (write buffer alloc)\n");
1354 xr_usb_serial
->ctrlurb
= usb_alloc_urb(0, GFP_KERNEL
);
1355 if (!xr_usb_serial
->ctrlurb
) {
1356 dev_err(&intf
->dev
, "out of memory (ctrlurb kmalloc)\n");
1359 for (i
= 0; i
< num_rx_buf
; i
++) {
1360 struct xr_usb_serial_rb
*rb
= &(xr_usb_serial
->read_buffers
[i
]);
1363 rb
->base
= usb_alloc_coherent(xr_usb_serial
->dev
, readsize
, GFP_KERNEL
,
1366 dev_err(&intf
->dev
, "out of memory "
1367 "(read bufs usb_alloc_coherent)\n");
1371 rb
->instance
= xr_usb_serial
;
1373 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1376 "out of memory (read urbs usb_alloc_urb)\n");
1379 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1380 urb
->transfer_dma
= rb
->dma
;
1381 if (xr_usb_serial
->is_int_ep
) {
1382 usb_fill_int_urb(urb
, xr_usb_serial
->dev
,
1383 xr_usb_serial
->rx_endpoint
,
1385 xr_usb_serial
->readsize
,
1386 xr_usb_serial_read_bulk_callback
, rb
,
1387 xr_usb_serial
->bInterval
);
1389 usb_fill_bulk_urb(urb
, xr_usb_serial
->dev
,
1390 xr_usb_serial
->rx_endpoint
,
1392 xr_usb_serial
->readsize
,
1393 xr_usb_serial_read_bulk_callback
, rb
);
1396 xr_usb_serial
->read_urbs
[i
] = urb
;
1397 __set_bit(i
, &xr_usb_serial
->read_urbs_free
);
1399 for (i
= 0; i
< XR_USB_SERIAL_NW
; i
++) {
1400 struct xr_usb_serial_wb
*snd
= &(xr_usb_serial
->wb
[i
]);
1402 snd
->urb
= usb_alloc_urb(0, GFP_KERNEL
);
1403 if (snd
->urb
== NULL
) {
1405 "out of memory (write urbs usb_alloc_urb)\n");
1409 if (usb_endpoint_xfer_int(epwrite
))
1410 usb_fill_int_urb(snd
->urb
, usb_dev
,
1411 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)
1412 usb_sndbulkpipe(usb_dev
, epwrite
->bEndpointAddress
),
1414 usb_sndintpipe(usb_dev
, epwrite
->bEndpointAddress
),
1416 NULL
, xr_usb_serial
->writesize
, xr_usb_serial_write_bulk
, snd
, epwrite
->bInterval
);
1418 usb_fill_bulk_urb(snd
->urb
, usb_dev
,
1419 usb_sndbulkpipe(usb_dev
, epwrite
->bEndpointAddress
),
1420 NULL
, xr_usb_serial
->writesize
, xr_usb_serial_write_bulk
, snd
);
1421 snd
->urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1422 snd
->instance
= xr_usb_serial
;
1425 usb_set_intfdata(intf
, xr_usb_serial
);
1427 xr_usb_serial
->rs485_422_en
= false; //default enable rs232
1428 i
= device_create_file(&intf
->dev
, &dev_attr_bRS485_422_en
);
1432 i
= device_create_file(&intf
->dev
, &dev_attr_bmCapabilities
);
1436 if (cfd
) { /* export the country data */
1437 xr_usb_serial
->country_codes
= kmalloc(cfd
->bLength
- 4, GFP_KERNEL
);
1438 if (!xr_usb_serial
->country_codes
)
1439 goto skip_countries
;
1440 xr_usb_serial
->country_code_size
= cfd
->bLength
- 4;
1441 memcpy(xr_usb_serial
->country_codes
, (u8
*)&cfd
->wCountyCode0
,
1443 xr_usb_serial
->country_rel_date
= cfd
->iCountryCodeRelDate
;
1445 i
= device_create_file(&intf
->dev
, &dev_attr_wCountryCodes
);
1447 kfree(xr_usb_serial
->country_codes
);
1448 xr_usb_serial
->country_codes
= NULL
;
1449 xr_usb_serial
->country_code_size
= 0;
1450 goto skip_countries
;
1453 i
= device_create_file(&intf
->dev
,
1454 &dev_attr_iCountryCodeRelDate
);
1456 device_remove_file(&intf
->dev
, &dev_attr_wCountryCodes
);
1457 kfree(xr_usb_serial
->country_codes
);
1458 xr_usb_serial
->country_codes
= NULL
;
1459 xr_usb_serial
->country_code_size
= 0;
1460 goto skip_countries
;
1465 usb_fill_int_urb(xr_usb_serial
->ctrlurb
, usb_dev
,
1466 usb_rcvintpipe(usb_dev
, epctrl
->bEndpointAddress
),
1467 xr_usb_serial
->ctrl_buffer
, ctrlsize
, xr_usb_serial_ctrl_irq
, xr_usb_serial
,
1468 /* works around buggy devices */
1469 epctrl
->bInterval
? epctrl
->bInterval
: 0xff);
1470 xr_usb_serial
->ctrlurb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1471 xr_usb_serial
->ctrlurb
->transfer_dma
= xr_usb_serial
->ctrl_dma
;
1473 dev_info(&intf
->dev
, "ttyXR_USB_SERIAL%d: USB XR_USB_SERIAL device\n", minor
);
1475 xr_usb_serial_pre_setup(xr_usb_serial
);
1477 xr_usb_serial_set_control(xr_usb_serial
, xr_usb_serial
->ctrlout
);
1479 xr_usb_serial
->line
.dwDTERate
= cpu_to_le32(9600);
1480 xr_usb_serial
->line
.bDataBits
= 8;
1481 xr_usb_serial_set_line(xr_usb_serial
, &xr_usb_serial
->line
);
1483 usb_driver_claim_interface(&xr_usb_serial_driver
, data_interface
, xr_usb_serial
);
1484 usb_set_intfdata(data_interface
, xr_usb_serial
);
1486 usb_get_intf(control_interface
);
1487 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)
1488 tty_register_device(xr_usb_serial_tty_driver
, minor
, &control_interface
->dev
);
1490 tty_dev
= tty_port_register_device(&xr_usb_serial
->port
, xr_usb_serial_tty_driver
, minor
,
1491 &control_interface
->dev
);
1492 if (IS_ERR(tty_dev
)) {
1493 rv
= PTR_ERR(tty_dev
);
1500 if (xr_usb_serial
->country_codes
) {
1501 device_remove_file(&xr_usb_serial
->control
->dev
,
1502 &dev_attr_wCountryCodes
);
1503 device_remove_file(&xr_usb_serial
->control
->dev
,
1504 &dev_attr_iCountryCodeRelDate
);
1506 device_remove_file(&xr_usb_serial
->control
->dev
, &dev_attr_bmCapabilities
);
1508 device_remove_file(&xr_usb_serial
->control
->dev
, &dev_attr_bRS485_422_en
);
1510 usb_set_intfdata(intf
, NULL
);
1511 for (i
= 0; i
< XR_USB_SERIAL_NW
; i
++)
1512 usb_free_urb(xr_usb_serial
->wb
[i
].urb
);
1514 for (i
= 0; i
< num_rx_buf
; i
++)
1515 usb_free_urb(xr_usb_serial
->read_urbs
[i
]);
1516 xr_usb_serial_read_buffers_free(xr_usb_serial
);
1517 usb_free_urb(xr_usb_serial
->ctrlurb
);
1519 xr_usb_serial_write_buffers_free(xr_usb_serial
);
1521 usb_free_coherent(usb_dev
, ctrlsize
, xr_usb_serial
->ctrl_buffer
, xr_usb_serial
->ctrl_dma
);
1523 xr_usb_serial_release_minor(xr_usb_serial
);
1524 kfree(xr_usb_serial
);
1529 static void stop_data_traffic(struct xr_usb_serial
*xr_usb_serial
)
1533 dev_dbg(&xr_usb_serial
->control
->dev
, "%s\n", __func__
);
1535 usb_kill_urb(xr_usb_serial
->ctrlurb
);
1536 for (i
= 0; i
< XR_USB_SERIAL_NW
; i
++)
1537 usb_kill_urb(xr_usb_serial
->wb
[i
].urb
);
1538 for (i
= 0; i
< xr_usb_serial
->rx_buflimit
; i
++)
1539 usb_kill_urb(xr_usb_serial
->read_urbs
[i
]);
1541 cancel_work_sync(&xr_usb_serial
->work
);
1544 static void xr_usb_serial_disconnect(struct usb_interface
*intf
)
1546 struct xr_usb_serial
*xr_usb_serial
= usb_get_intfdata(intf
);
1547 struct usb_device
*usb_dev
= interface_to_usbdev(intf
);
1548 struct tty_struct
*tty
;
1551 dev_dbg(&intf
->dev
, "%s\n", __func__
);
1553 /* sibling interface is already cleaning up */
1557 mutex_lock(&xr_usb_serial
->mutex
);
1558 xr_usb_serial
->disconnected
= true;
1559 if (xr_usb_serial
->country_codes
) {
1560 device_remove_file(&xr_usb_serial
->control
->dev
,
1561 &dev_attr_wCountryCodes
);
1562 device_remove_file(&xr_usb_serial
->control
->dev
,
1563 &dev_attr_iCountryCodeRelDate
);
1565 device_remove_file(&xr_usb_serial
->control
->dev
, &dev_attr_bmCapabilities
);
1566 device_remove_file(&xr_usb_serial
->control
->dev
, &dev_attr_bRS485_422_en
);
1567 usb_set_intfdata(xr_usb_serial
->control
, NULL
);
1568 usb_set_intfdata(xr_usb_serial
->data
, NULL
);
1569 mutex_unlock(&xr_usb_serial
->mutex
);
1571 tty
= tty_port_tty_get(&xr_usb_serial
->port
);
1576 stop_data_traffic(xr_usb_serial
);
1577 #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 7, 0)
1578 tty_unregister_device(xr_usb_serial_tty_driver
, xr_usb_serial
->minor
);
1581 usb_free_urb(xr_usb_serial
->ctrlurb
);
1582 for (i
= 0; i
< XR_USB_SERIAL_NW
; i
++)
1583 usb_free_urb(xr_usb_serial
->wb
[i
].urb
);
1584 for (i
= 0; i
< xr_usb_serial
->rx_buflimit
; i
++)
1585 usb_free_urb(xr_usb_serial
->read_urbs
[i
]);
1586 xr_usb_serial_write_buffers_free(xr_usb_serial
);
1587 usb_free_coherent(usb_dev
, xr_usb_serial
->ctrlsize
, xr_usb_serial
->ctrl_buffer
, xr_usb_serial
->ctrl_dma
);
1588 xr_usb_serial_read_buffers_free(xr_usb_serial
);
1590 if (!xr_usb_serial
->combined_interfaces
)
1591 usb_driver_release_interface(&xr_usb_serial_driver
, intf
== xr_usb_serial
->control
?
1592 xr_usb_serial
->data
: xr_usb_serial
->control
);
1594 tty_port_put(&xr_usb_serial
->port
);
1598 static int xr_usb_serial_suspend(struct usb_interface
*intf
, pm_message_t message
)
1600 struct xr_usb_serial
*xr_usb_serial
= usb_get_intfdata(intf
);
1603 if (PMSG_IS_AUTO(message
)) {
1606 spin_lock_irq(&xr_usb_serial
->write_lock
);
1607 b
= xr_usb_serial
->transmitting
;
1608 spin_unlock_irq(&xr_usb_serial
->write_lock
);
1613 spin_lock_irq(&xr_usb_serial
->read_lock
);
1614 spin_lock(&xr_usb_serial
->write_lock
);
1615 cnt
= xr_usb_serial
->susp_count
++;
1616 spin_unlock(&xr_usb_serial
->write_lock
);
1617 spin_unlock_irq(&xr_usb_serial
->read_lock
);
1622 if (test_bit(ASYNCB_INITIALIZED
, &xr_usb_serial
->port
.flags
))
1623 stop_data_traffic(xr_usb_serial
);
1628 static int xr_usb_serial_resume(struct usb_interface
*intf
)
1630 struct xr_usb_serial
*xr_usb_serial
= usb_get_intfdata(intf
);
1631 struct xr_usb_serial_wb
*wb
;
1635 spin_lock_irq(&xr_usb_serial
->read_lock
);
1636 xr_usb_serial
->susp_count
-= 1;
1637 cnt
= xr_usb_serial
->susp_count
;
1638 spin_unlock_irq(&xr_usb_serial
->read_lock
);
1643 if (test_bit(ASYNCB_INITIALIZED
, &xr_usb_serial
->port
.flags
)) {
1644 rv
= usb_submit_urb(xr_usb_serial
->ctrlurb
, GFP_NOIO
);
1646 spin_lock_irq(&xr_usb_serial
->write_lock
);
1647 if (xr_usb_serial
->delayed_wb
) {
1648 wb
= xr_usb_serial
->delayed_wb
;
1649 xr_usb_serial
->delayed_wb
= NULL
;
1650 spin_unlock_irq(&xr_usb_serial
->write_lock
);
1651 xr_usb_serial_start_wb(xr_usb_serial
, wb
);
1653 spin_unlock_irq(&xr_usb_serial
->write_lock
);
1657 * delayed error checking because we must
1658 * do the write path at all cost
1663 rv
= xr_usb_serial_submit_read_urbs(xr_usb_serial
, GFP_NOIO
);
1670 static int xr_usb_serial_reset_resume(struct usb_interface
*intf
)
1672 struct xr_usb_serial
*xr_usb_serial
= usb_get_intfdata(intf
);
1673 struct tty_struct
*tty
;
1674 if (test_bit(ASYNCB_INITIALIZED
, &xr_usb_serial
->port
.flags
)){
1675 #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 9, 0)
1676 tty_port_tty_hangup(&xr_usb_serial
->port
, false);
1678 tty
= tty_port_tty_get(&xr_usb_serial
->port
);
1685 return xr_usb_serial_resume(intf
);
1688 #endif /* CONFIG_PM */
1691 * USB driver structure.
1693 static const struct usb_device_id xr_usb_serial_ids
[] = {
1694 { USB_DEVICE(0x04e2, 0x1410)},
1695 { USB_DEVICE(0x04e2, 0x1411)},
1696 { USB_DEVICE(0x04e2, 0x1412)},
1697 { USB_DEVICE(0x04e2, 0x1414)},
1698 { USB_DEVICE(0x04e2, 0x1420)},
1699 { USB_DEVICE(0x04e2, 0x1421)},
1700 { USB_DEVICE(0x04e2, 0x1422)},
1701 { USB_DEVICE(0x04e2, 0x1424)},
1702 { USB_DEVICE(0x04e2, 0x1400)},
1703 { USB_DEVICE(0x04e2, 0x1401)},
1704 { USB_DEVICE(0x04e2, 0x1402)},
1705 { USB_DEVICE(0x04e2, 0x1403)},
1709 MODULE_DEVICE_TABLE(usb
, xr_usb_serial_ids
);
1711 static struct usb_driver xr_usb_serial_driver
= {
1712 .name
= "cdc_xr_usb_serial",
1713 .probe
= xr_usb_serial_probe
,
1714 .disconnect
= xr_usb_serial_disconnect
,
1716 .suspend
= xr_usb_serial_suspend
,
1717 .resume
= xr_usb_serial_resume
,
1718 .reset_resume
= xr_usb_serial_reset_resume
,
1720 .id_table
= xr_usb_serial_ids
,
1722 .supports_autosuspend
= 1,
1724 .disable_hub_initiated_lpm
= 1,
1728 * TTY driver structures.
1731 static const struct tty_operations xr_usb_serial_ops
= {
1732 .install
= xr_usb_serial_tty_install
,
1733 .open
= xr_usb_serial_tty_open
,
1734 .close
= xr_usb_serial_tty_close
,
1735 .cleanup
= xr_usb_serial_tty_cleanup
,
1736 .hangup
= xr_usb_serial_tty_hangup
,
1737 .write
= xr_usb_serial_tty_write
,
1738 .write_room
= xr_usb_serial_tty_write_room
,
1739 .ioctl
= xr_usb_serial_tty_ioctl
,
1740 .throttle
= xr_usb_serial_tty_throttle
,
1741 .unthrottle
= xr_usb_serial_tty_unthrottle
,
1742 .chars_in_buffer
= xr_usb_serial_tty_chars_in_buffer
,
1743 .break_ctl
= xr_usb_serial_tty_break_ctl
,
1744 .set_termios
= xr_usb_serial_tty_set_termios
,
1745 .tiocmget
= xr_usb_serial_tty_tiocmget
,
1746 .tiocmset
= xr_usb_serial_tty_tiocmset
,
1753 static int __init
xr_usb_serial_init(void)
1756 xr_usb_serial_tty_driver
= alloc_tty_driver(XR_USB_SERIAL_TTY_MINORS
);
1757 if (!xr_usb_serial_tty_driver
)
1759 xr_usb_serial_tty_driver
->driver_name
= "xr_usb_serial",
1760 xr_usb_serial_tty_driver
->name
= "ttyXRUSB",
1761 xr_usb_serial_tty_driver
->major
= XR_USB_SERIAL_TTY_MAJOR
,
1762 xr_usb_serial_tty_driver
->minor_start
= 0,
1763 xr_usb_serial_tty_driver
->type
= TTY_DRIVER_TYPE_SERIAL
,
1764 xr_usb_serial_tty_driver
->subtype
= SERIAL_TYPE_NORMAL
,
1765 xr_usb_serial_tty_driver
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
1766 xr_usb_serial_tty_driver
->init_termios
= tty_std_termios
;
1767 xr_usb_serial_tty_driver
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
|
1769 tty_set_operations(xr_usb_serial_tty_driver
, &xr_usb_serial_ops
);
1771 retval
= tty_register_driver(xr_usb_serial_tty_driver
);
1773 put_tty_driver(xr_usb_serial_tty_driver
);
1777 retval
= usb_register(&xr_usb_serial_driver
);
1779 tty_unregister_driver(xr_usb_serial_tty_driver
);
1780 put_tty_driver(xr_usb_serial_tty_driver
);
1784 printk(KERN_INFO KBUILD_MODNAME
": " DRIVER_DESC
"\n");
1789 static void __exit
xr_usb_serial_exit(void)
1791 usb_deregister(&xr_usb_serial_driver
);
1792 tty_unregister_driver(xr_usb_serial_tty_driver
);
1793 put_tty_driver(xr_usb_serial_tty_driver
);
1796 module_init(xr_usb_serial_init
);
1797 module_exit(xr_usb_serial_exit
);
1799 MODULE_AUTHOR(DRIVER_AUTHOR
);
1800 MODULE_DESCRIPTION(DRIVER_DESC
);
1801 MODULE_LICENSE("GPL");
1802 MODULE_ALIAS_CHARDEV_MAJOR(XR_USB_SERIAL_TTY_MAJOR
);