53 #include <linux/sched.h>
54 #include <linux/slab.h>
57 #include <linux/netdevice.h>
58 #include <linux/module.h>
59 #include <linux/ethtool.h>
62 #include <linux/tty.h>
66 #include <linux/rfkill.h>
71 #include <asm/byteorder.h>
72 #include <linux/serial_core.h>
73 #include <linux/serial.h>
76 #define MOD_AUTHOR "Option Wireless"
77 #define MOD_DESCRIPTION "USB High Speed Option driver"
78 #define MOD_LICENSE "GPL"
80 #define HSO_MAX_NET_DEVICES 10
81 #define HSO__MAX_MTU 2048
82 #define DEFAULT_MTU 1500
83 #define DEFAULT_MRU 1500
85 #define CTRL_URB_RX_SIZE 1024
86 #define CTRL_URB_TX_SIZE 64
88 #define BULK_URB_RX_SIZE 4096
89 #define BULK_URB_TX_SIZE 8192
91 #define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
92 #define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
93 #define MUX_BULK_RX_BUF_COUNT 4
94 #define USB_TYPE_OPTION_VENDOR 0x20
98 #define HSO_NET_RUNNING 0
100 #define HSO_NET_TX_TIMEOUT (HZ*10)
102 #define HSO_SERIAL_MAGIC 0x48534f31
105 #define HSO_SERIAL_TTY_MINORS 256
107 #define MAX_RX_URBS 2
112 #define D__(lvl_, fmt, arg...) \
114 printk(lvl_ "[%d:%s]: " fmt "\n", \
115 __LINE__, __func__, ## arg); \
118 #define D_(lvl, args...) \
121 D__(KERN_INFO, args); \
124 #define D1(args...) D_(0x01, ##args)
125 #define D2(args...) D_(0x02, ##args)
126 #define D3(args...) D_(0x04, ##args)
127 #define D4(args...) D_(0x08, ##args)
128 #define D5(args...) D_(0x10, ##args)
185 #define BM_REQUEST_TYPE (0xa1)
186 #define B_NOTIFICATION (0x20)
187 #define W_VALUE (0x0)
188 #define W_INDEX (0x2)
189 #define W_LENGTH (0x2)
191 #define B_OVERRUN (0x1<<6)
192 #define B_PARITY (0x1<<5)
193 #define B_FRAMING (0x1<<4)
194 #define B_RING_SIGNAL (0x1<<3)
195 #define B_BREAK (0x1<<2)
196 #define B_TX_CARRIER (0x1<<1)
197 #define B_RX_CARRIER (0x1<<0)
291 #define HSO_INTF_MASK 0xFF00
292 #define HSO_INTF_MUX 0x0100
293 #define HSO_INTF_BULK 0x0200
296 #define HSO_PORT_MASK 0xFF
297 #define HSO_PORT_NO_PORT 0x0
298 #define HSO_PORT_CONTROL 0x1
299 #define HSO_PORT_APP 0x2
300 #define HSO_PORT_GPS 0x3
301 #define HSO_PORT_PCSC 0x4
302 #define HSO_PORT_APP2 0x5
303 #define HSO_PORT_GPS_CONTROL 0x6
304 #define HSO_PORT_MSD 0x7
305 #define HSO_PORT_VOICE 0x8
306 #define HSO_PORT_DIAG2 0x9
307 #define HSO_PORT_DIAG 0x10
308 #define HSO_PORT_MODEM 0x11
309 #define HSO_PORT_NETWORK 0x12
312 #define HSO_INFO_MASK 0xFF000000
313 #define HSO_INFO_CRC_BUG 0x01000000
319 static int hso_serial_tiocmset(
struct tty_struct *tty,
320 unsigned int set,
unsigned int clear);
321 static void ctrl_callback(
struct urb *
urb);
325 static int hso_mux_submit_intr_urb(
struct hso_shared_int *mux_int,
327 static void handle_usb_error(
int status,
const char *
function,
334 static int hso_stop_serial_device(
struct hso_device *hso_dev);
335 static int hso_start_net_device(
struct hso_device *hso_dev);
336 static void hso_free_shared_int(
struct hso_shared_int *shared_int);
337 static int hso_stop_net_device(
struct hso_device *hso_dev);
338 static void hso_serial_ref_free(
struct kref *ref);
339 static void hso_std_serial_read_bulk_callback(
struct urb *
urb);
343 static int hso_put_activity(
struct hso_device *hso_dev);
344 static int hso_get_activity(
struct hso_device *hso_dev);
345 static void tiocmget_intr_callback(
struct urb *
urb);
365 static void dbg_dump(
int line_count,
const char *func_name,
unsigned char *
buf,
368 static char name[255];
370 sprintf(name,
"hso[%d:%s]", line_count, func_name);
374 #define DUMP(buf_, len_) \
375 dbg_dump(__LINE__, __func__, (unsigned char *)buf_, len_)
377 #define DUMP1(buf_, len_) \
383 #define DUMP(buf_, len_)
384 #define DUMP1(buf_, len_)
389 static int tty_major;
390 static int disable_net;
394 static const char tty_filename[] =
"ttyHS";
397 static struct usb_driver hso_driver;
404 static const s32 default_port_spec[] = {
411 static const s32 icon321_port_spec[] = {
419 #define default_port_device(vendor, product) \
420 USB_DEVICE(vendor, product), \
421 .driver_info = (kernel_ulong_t)default_port_spec
423 #define icon321_port_device(vendor, product) \
424 USB_DEVICE(vendor, product), \
425 .driver_info = (kernel_ulong_t)icon321_port_spec
451 {USB_DEVICE(0x0af0, 0x7301)},
452 {USB_DEVICE(0x0af0, 0x7361)},
453 {USB_DEVICE(0x0af0, 0x7381)},
454 {USB_DEVICE(0x0af0, 0x7401)},
455 {USB_DEVICE(0x0af0, 0x7501)},
456 {USB_DEVICE(0x0af0, 0x7601)},
457 {USB_DEVICE(0x0af0, 0x7701)},
458 {USB_DEVICE(0x0af0, 0x7706)},
459 {USB_DEVICE(0x0af0, 0x7801)},
460 {USB_DEVICE(0x0af0, 0x7901)},
461 {USB_DEVICE(0x0af0, 0x7A01)},
462 {USB_DEVICE(0x0af0, 0x7A05)},
463 {USB_DEVICE(0x0af0, 0x8200)},
464 {USB_DEVICE(0x0af0, 0x8201)},
465 {USB_DEVICE(0x0af0, 0x8300)},
466 {USB_DEVICE(0x0af0, 0x8302)},
467 {USB_DEVICE(0x0af0, 0x8304)},
468 {USB_DEVICE(0x0af0, 0x8400)},
469 {USB_DEVICE(0x0af0, 0x8600)},
470 {USB_DEVICE(0x0af0, 0x8800)},
471 {USB_DEVICE(0x0af0, 0x8900)},
472 {USB_DEVICE(0x0af0, 0x9000)},
473 {USB_DEVICE(0x0af0, 0xd035)},
474 {USB_DEVICE(0x0af0, 0xd055)},
475 {USB_DEVICE(0x0af0, 0xd155)},
476 {USB_DEVICE(0x0af0, 0xd255)},
477 {USB_DEVICE(0x0af0, 0xd057)},
478 {USB_DEVICE(0x0af0, 0xd157)},
479 {USB_DEVICE(0x0af0, 0xd257)},
480 {USB_DEVICE(0x0af0, 0xd357)},
481 {USB_DEVICE(0x0af0, 0xd058)},
482 {USB_DEVICE(0x0af0, 0xc100)},
500 port_name =
"Control";
503 port_name =
"Application";
506 port_name =
"Application2";
512 port_name =
"GPS Control";
518 port_name =
"Diagnostic";
521 port_name =
"Diagnostic2";
527 port_name =
"Network";
530 port_name =
"Unknown";
534 return sprintf(buf,
"%s\n", port_name);
543 if (serial->
rx_urb[idx] == urb)
550 static u32 hso_mux_to_port(
int mux)
577 static u32 hso_port_to_mux(
int port)
603 static struct hso_serial *get_serial_by_shared_int_and_type(
609 port = hso_mux_to_port(mux);
612 if (serial_table[i] &&
613 (dev2ser(serial_table[i])->shared_int == shared_int) &&
615 return dev2ser(serial_table[i]);
628 if (serial_table[index])
629 serial = dev2ser(serial_table[index]);
630 spin_unlock_irqrestore(&serial_table_lock, flags);
635 static int get_free_serial_index(
void)
642 if (serial_table[index] ==
NULL) {
643 spin_unlock_irqrestore(&serial_table_lock, flags);
647 spin_unlock_irqrestore(&serial_table_lock, flags);
649 printk(
KERN_ERR "%s: no free serial devices in table\n", __func__);
653 static void set_serial_by_index(
unsigned index,
struct hso_serial *serial)
662 spin_unlock_irqrestore(&serial_table_lock, flags);
665 static void handle_usb_error(
int status,
const char *
function,
672 explanation =
"no device";
675 explanation =
"endpoint not enabled";
678 explanation =
"endpoint stalled";
681 explanation =
"not enough bandwidth";
684 explanation =
"device disabled";
687 explanation =
"device suspended";
693 explanation =
"internal error";
699 explanation =
"protocol error";
704 explanation =
"unknown status";
709 D1(
"%s: received USB status - %s (%d)",
function, explanation, status);
717 struct hso_net *odev = netdev_priv(net);
718 unsigned long flags = 0;
732 spin_unlock_irqrestore(&odev->
net_lock, flags);
736 hso_start_net_device(odev->
parent);
739 netif_start_queue(net);
745 static int hso_net_close(
struct net_device *net)
747 struct hso_net *odev = netdev_priv(net);
750 netif_stop_queue(net);
754 hso_stop_net_device(odev->
parent);
761 static void write_bulk_callback(
struct urb *urb)
763 struct hso_net *odev = urb->context;
764 int status = urb->status;
768 dev_err(&urb->dev->dev,
"%s: device not running\n", __func__);
773 if (!netif_device_present(odev->
net)) {
774 dev_err(&urb->dev->dev,
"%s: net device not present\n",
782 handle_usb_error(status, __func__, odev->
parent);
784 hso_put_activity(odev->
parent);
787 netif_wake_queue(odev->
net);
794 struct hso_net *odev = netdev_priv(net);
798 netif_stop_queue(net);
813 usb_sndbulkpipe(odev->
parent->usb,
826 "failed mux_bulk_tx_urb %d\n", result);
827 net->
stats.tx_errors++;
828 netif_start_queue(net);
830 net->
stats.tx_packets++;
843 static void hso_net_tx_timeout(
struct net_device *net)
845 struct hso_net *odev = netdev_priv(net);
859 net->
stats.tx_errors++;
863 static void packetizeRx(
struct hso_net *odev,
unsigned char *ip_pkt,
864 unsigned int count,
unsigned char is_eop)
866 unsigned short temp_bytes;
867 unsigned short buffer_offset = 0;
868 unsigned short frame_len;
869 unsigned char *tmp_rx_buf;
872 D1(
"Rx %d bytes", count);
873 DUMP(ip_pkt,
min(128, (
int)count));
890 buffer_offset += temp_bytes;
900 (frame_len <
sizeof(
struct iphdr))) {
902 "Invalid frame (%d) length\n",
912 D1(
"could not allocate memory");
921 sizeof(
struct iphdr));
923 sizeof(
struct iphdr));
930 frame_len -
sizeof(
struct iphdr);
943 memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
947 buffer_offset += temp_bytes;
961 odev->
net->stats.rx_packets++;
992 static void fix_crc_bug(
struct urb *urb,
__le16 max_packet_size)
994 static const u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
997 if (((rest == 5) || (rest == 6)) &&
998 !
memcmp(((
u8 *)urb->transfer_buffer) + urb->actual_length - 4,
1000 urb->actual_length -= 4;
1005 static void read_bulk_callback(
struct urb *urb)
1007 struct hso_net *odev = urb->context;
1010 int status = urb->status;
1014 handle_usb_error(status, __func__, odev->
parent);
1020 D1(
"BULK IN callback but driver is not active!");
1023 usb_mark_last_busy(urb->dev);
1027 if (!netif_device_present(net)) {
1033 fix_crc_bug(urb, odev->
in_endp->wMaxPacketSize);
1036 if (urb->actual_length) {
1040 packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
1041 (urb->transfer_buffer_length >
1042 urb->actual_length) ? 1 : 0);
1048 usb_fill_bulk_urb(urb,
1050 usb_rcvbulkpipe(odev->
parent->usb,
1054 read_bulk_callback, odev);
1061 "%s failed submit mux_bulk_rx_urb %d\n", __func__,
1106 static void _hso_serial_set_termios(
struct tty_struct *tty,
1121 tty->termios.c_iflag &= ~
IXON;
1123 tty->termios.c_cflag &=
1129 tty->termios.c_cflag |=
CS8;
1135 static void hso_resubmit_rx_bulk_urb(
struct hso_serial *serial,
struct urb *urb)
1140 usb_fill_bulk_urb(urb, serial->
parent->usb,
1141 usb_rcvbulkpipe(serial->
parent->usb,
1145 hso_std_serial_read_bulk_callback, serial);
1150 dev_err(&urb->dev->dev,
"%s failed submit serial rx_urb %d\n",
1158 static void put_rxbuf_data_and_resubmit_bulk_urb(
struct hso_serial *serial)
1161 struct urb *curr_urb;
1165 count = put_rxbuf_data(curr_urb, serial);
1172 hso_resubmit_rx_bulk_urb(serial, curr_urb);
1177 static void put_rxbuf_data_and_resubmit_ctrl_urb(
struct hso_serial *serial)
1183 if (serial->
port.count > 0) {
1184 count = put_rxbuf_data(urb, serial);
1190 if (count == 0 && ((urb->actual_length != 0) ||
1193 hso_mux_serial_read(serial);
1200 static void hso_std_serial_read_bulk_callback(
struct urb *urb)
1203 int status = urb->status;
1207 D1(
"serial == NULL");
1209 }
else if (status) {
1210 handle_usb_error(status, __func__, serial->
parent);
1214 D4(
"\n--- Got serial_read_bulk callback %02x ---", status);
1215 D1(
"Actual length = %d\n", urb->actual_length);
1216 DUMP1(urb->transfer_buffer, urb->actual_length);
1219 if (serial->
port.count == 0)
1224 fix_crc_bug(urb, serial->
in_endp->wMaxPacketSize);
1228 put_rxbuf_data_and_resubmit_bulk_urb(serial);
1232 D2(
"Port %d, successfully unlinked urb", serial->
minor);
1235 hso_resubmit_rx_bulk_urb(serial, urb);
1238 D2(
"Port %d, status = %d for read urb", serial->
minor, status);
1247 static void hso_unthrottle_tasklet(
struct hso_serial *serial)
1249 unsigned long flags;
1253 put_rxbuf_data_and_resubmit_ctrl_urb(serial);
1255 put_rxbuf_data_and_resubmit_bulk_urb(serial);
1256 spin_unlock_irqrestore(&serial->
serial_lock, flags);
1259 static void hso_unthrottle(
struct tty_struct *tty)
1271 hso_unthrottle_tasklet(serial);
1275 static int hso_serial_open(
struct tty_struct *tty,
struct file *filp)
1284 D1(
"Failed to open port");
1289 result = usb_autopm_get_interface(serial->
parent->interface);
1293 D1(
"Opening %d", serial->
minor);
1294 kref_get(&serial->
parent->ref);
1301 serial->
port.count++;
1302 if (serial->
port.count == 1) {
1305 _hso_serial_set_termios(tty,
NULL);
1307 (
void (*)(
unsigned long))hso_unthrottle_tasklet,
1308 (
unsigned long)serial);
1310 hso_unthrottle_workfunc);
1313 hso_stop_serial_device(serial->
parent);
1314 serial->
port.count--;
1315 kref_put(&serial->
parent->ref, hso_serial_ref_free);
1318 D1(
"Port was already open");
1321 usb_autopm_put_interface(serial->
parent->interface);
1332 static void hso_serial_close(
struct tty_struct *tty,
struct file *filp)
1337 D1(
"Closing serial port");
1344 usb_gone = serial->
parent->usb_gone;
1347 usb_autopm_get_interface(serial->
parent->interface);
1351 serial->
port.count--;
1353 if (serial->
port.count <= 0) {
1354 serial->
port.count = 0;
1357 hso_stop_serial_device(serial->
parent);
1363 usb_autopm_put_interface(serial->
parent->interface);
1367 kref_put(&serial->
parent->ref, hso_serial_ref_free);
1371 static int hso_serial_write(
struct tty_struct *tty,
const unsigned char *buf,
1376 unsigned long flags;
1379 if (serial ==
NULL) {
1387 tx_bytes = (count < space) ? count : space;
1396 spin_unlock_irqrestore(&serial->
serial_lock, flags);
1398 hso_kick_transmit(serial);
1404 static int hso_serial_write_room(
struct tty_struct *tty)
1408 unsigned long flags;
1412 spin_unlock_irqrestore(&serial->
serial_lock, flags);
1422 unsigned long flags;
1425 D5(
"Termios called with: cflags new[%d] - old[%d]",
1426 tty->termios.c_cflag, old->
c_cflag);
1430 if (serial->
port.count)
1431 _hso_serial_set_termios(tty, old);
1433 tty->termios = *old;
1434 spin_unlock_irqrestore(&serial->
serial_lock, flags);
1440 static int hso_serial_chars_in_buffer(
struct tty_struct *tty)
1444 unsigned long flags;
1452 spin_unlock_irqrestore(&serial->
serial_lock, flags);
1456 static int tiocmget_submit_urb(
struct hso_serial *serial,
1458 struct usb_device *
usb)
1462 if (serial->
parent->usb_gone)
1464 usb_fill_int_urb(tiocmget->
urb, usb,
1470 tiocmget_intr_callback, serial,
1471 tiocmget->
endp->bInterval);
1474 dev_warn(&usb->dev,
"%s usb_submit_urb failed %d\n", __func__,
1481 static void tiocmget_intr_callback(
struct urb *urb)
1485 int status = urb->status;
1489 struct usb_device *
usb;
1495 handle_usb_error(status, __func__, serial->
parent);
1501 usb = serial->
parent->usb;
1509 "hso received invalid serial state notification\n");
1510 DUMP(serial_state_notification,
1514 UART_state_bitmap =
le16_to_cpu(serial_state_notification->
1517 icount = &tiocmget->
icount;
1520 (prev_UART_state_bitmap & B_OVERRUN))
1522 if ((UART_state_bitmap &
B_PARITY) !=
1523 (prev_UART_state_bitmap & B_PARITY))
1526 (prev_UART_state_bitmap & B_FRAMING))
1529 !(prev_UART_state_bitmap & B_RING_SIGNAL))
1531 if ((UART_state_bitmap &
B_BREAK) !=
1532 (prev_UART_state_bitmap & B_BREAK))
1535 (prev_UART_state_bitmap & B_TX_CARRIER))
1538 (prev_UART_state_bitmap & B_RX_CARRIER))
1545 memset(serial_state_notification, 0,
1547 tiocmget_submit_urb(serial,
1561 hso_wait_modem_status(
struct hso_serial *serial,
unsigned long arg)
1583 if (((arg &
TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1584 ((arg &
TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1585 ((arg &
TIOCM_CD) && (cnow.dcd != cprev.dcd))) {
1591 if (signal_pending(
current)) {
1609 static int hso_get_count(
struct tty_struct *tty,
1624 icount->
cts = cnow.cts;
1625 icount->
dsr = cnow.dsr;
1626 icount->
rng = cnow.rng;
1627 icount->
dcd = cnow.dcd;
1628 icount->
rx = cnow.rx;
1629 icount->
tx = cnow.tx;
1630 icount->
frame = cnow.frame;
1631 icount->
overrun = cnow.overrun;
1632 icount->
parity = cnow.parity;
1633 icount->
brk = cnow.brk;
1640 static int hso_serial_tiocmget(
struct tty_struct *tty)
1645 u16 UART_state_bitmap;
1649 D1(
"no tty structures");
1660 if (UART_state_bitmap & B_RING_SIGNAL)
1662 if (UART_state_bitmap & B_RX_CARRIER)
1664 if (UART_state_bitmap & B_TX_CARRIER)
1671 static int hso_serial_tiocmset(
struct tty_struct *tty,
1672 unsigned int set,
unsigned int clear)
1675 unsigned long flags;
1681 D1(
"no tty structures");
1688 if_num = serial->
parent->interface->altsetting->desc.bInterfaceNumber;
1693 if (
set & TIOCM_DTR)
1698 if (clear & TIOCM_DTR)
1706 spin_unlock_irqrestore(&serial->
serial_lock, flags);
1709 usb_rcvctrlpipe(serial->
parent->usb, 0), 0x22,
1710 0x21, val, if_num,
NULL, 0,
1711 USB_CTRL_SET_TIMEOUT);
1714 static int hso_serial_ioctl(
struct tty_struct *tty,
1715 unsigned int cmd,
unsigned long arg)
1719 D4(
"IOCTL cmd: %d, arg: %ld", cmd, arg);
1725 ret = hso_wait_modem_status(serial, arg);
1736 static void hso_kick_transmit(
struct hso_serial *serial)
1739 unsigned long flags;
1767 spin_unlock_irqrestore(&serial->
serial_lock, flags);
1772 struct urb *ctrl_urb,
1780 if (!serial || !ctrl_urb || !ctrl_req) {
1796 pipe = usb_rcvctrlpipe(serial->
parent->usb, 0);
1803 pipe = usb_sndctrlpipe(serial->
parent->usb, 0);
1806 D2(
"%s command (%02x) len: %d, port: %d",
1811 ctrl_urb->transfer_flags = 0;
1812 usb_fill_control_urb(ctrl_urb,
1816 ctrl_urb_data, size, ctrl_callback, serial);
1821 "%s failed submit ctrl_urb %d type %d\n", __func__,
1831 static int hso_mux_serial_read(
struct hso_serial *serial)
1842 "ERROR: mux'd reads with multiple buffers "
1846 return mux_device_request(serial,
1855 static void intr_callback(
struct urb *urb)
1859 unsigned char *port_req;
1860 int status = urb->status;
1863 usb_mark_last_busy(urb->dev);
1871 handle_usb_error(status, __func__,
NULL);
1874 D4(
"\n--- Got intr callback 0x%02X ---", status);
1877 port_req = urb->transfer_buffer;
1878 D4(
" port_req = 0x%.2X\n", *port_req);
1880 for (i = 0; i < 8; i++) {
1882 if (*port_req & (1 << i)) {
1883 serial = get_serial_by_shared_int_and_type(shared_int,
1885 if (serial !=
NULL) {
1886 D1(
"Pending read interrupt on port %d\n", i);
1889 serial->
port.count > 0) {
1894 hso_mux_serial_read(serial);
1898 D1(
"Already a read pending on "
1899 "port %d or port not open\n", i);
1906 hso_mux_submit_intr_urb(shared_int, urb->dev,
GFP_ATOMIC);
1910 static int hso_mux_serial_write_data(
struct hso_serial *serial)
1915 return mux_device_request(serial,
1924 static void hso_std_serial_write_bulk_callback(
struct urb *urb)
1927 int status = urb->status;
1932 D1(
"serial == NULL");
1940 handle_usb_error(status, __func__, serial->
parent);
1943 hso_put_activity(serial->
parent);
1949 hso_kick_transmit(serial);
1955 static int hso_std_serial_write_data(
struct hso_serial *serial)
1960 usb_fill_bulk_urb(serial->
tx_urb,
1962 usb_sndbulkpipe(serial->
parent->usb,
1966 hso_std_serial_write_bulk_callback, serial);
1971 "Failed to submit urb - res %d\n", result);
1979 static void ctrl_callback(
struct urb *urb)
1983 int status = urb->status;
1993 handle_usb_error(status, __func__, serial->
parent);
1999 D4(
"\n--- Got muxed ctrl callback 0x%02X ---", status);
2000 D4(
"Actual length of urb = %d\n", urb->actual_length);
2001 DUMP1(urb->transfer_buffer, urb->actual_length);
2008 put_rxbuf_data_and_resubmit_ctrl_urb(serial);
2012 hso_put_activity(serial->
parent);
2018 hso_kick_transmit(serial);
2023 static int put_rxbuf_data(
struct urb *urb,
struct hso_serial *serial)
2026 int write_length_remaining = 0;
2030 if (urb ==
NULL || serial ==
NULL) {
2031 D1(
"serial = NULL");
2039 write_length_remaining = urb->actual_length -
2041 D1(
"data to push to tty");
2042 while (write_length_remaining) {
2047 curr_write_len = tty_insert_flip_string
2048 (tty, urb->transfer_buffer +
2050 write_length_remaining);
2052 write_length_remaining -= curr_write_len;
2057 if (write_length_remaining == 0) {
2061 return write_length_remaining;
2067 static void hso_log_port(
struct hso_device *hso_dev)
2074 port_type =
"Control";
2077 port_type =
"Application";
2083 port_type =
"GPS control";
2086 port_type =
"Application2";
2092 port_type =
"Diagnostic";
2095 port_type =
"Diagnostic2";
2098 port_type =
"Modem";
2101 port_type =
"Network";
2104 port_type =
"Unknown";
2108 sprintf(port_dev,
"%s", dev2net(hso_dev)->net->
name);
2110 sprintf(port_dev,
"/dev/%s%d", tty_filename,
2111 dev2ser(hso_dev)->minor);
2114 port_type, port_dev);
2117 static int hso_start_net_device(
struct hso_device *hso_dev)
2131 usb_rcvbulkpipe(hso_dev->
usb,
2143 "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
2150 static int hso_stop_net_device(
struct hso_device *hso_dev)
2153 struct hso_net *hso_net = dev2net(hso_dev);
2169 static int hso_start_serial_device(
struct hso_device *hso_dev,
gfp_t flags)
2172 struct hso_serial *serial = dev2ser(hso_dev);
2181 usb_fill_bulk_urb(serial->
rx_urb[i],
2183 usb_rcvbulkpipe(serial->
parent->usb,
2189 hso_std_serial_read_bulk_callback,
2194 "Failed to submit urb - res %d\n",
2204 hso_dev->
usb, flags);
2210 tiocmget_submit_urb(serial,
2216 static int hso_stop_serial_device(
struct hso_device *hso_dev)
2219 struct hso_serial *serial = dev2ser(hso_dev);
2258 static void hso_serial_common_free(
struct hso_serial *serial)
2279 static int hso_serial_common_create(
struct hso_serial *serial,
int num_urbs,
2280 int rx_size,
int tx_size)
2286 minor = get_free_serial_index();
2294 minor, &serial->
parent->interface->dev);
2295 dev = serial->
parent->dev;
2300 serial->
minor = minor;
2311 if (!serial->
rx_urb[i]) {
2312 dev_err(dev,
"Could not allocate urb?\n");
2316 serial->
rx_urb[
i]->transfer_buffer_length = 0;
2320 dev_err(dev,
"%s - Out of memory\n", __func__);
2328 dev_err(dev,
"Could not allocate urb?\n");
2332 serial->
tx_urb->transfer_buffer_length = 0;
2339 dev_err(dev,
"%s - Out of memory\n", __func__);
2344 dev_err(dev,
"%s - Out of memory\n", __func__);
2350 hso_serial_common_free(serial);
2360 hso_dev = kzalloc(
sizeof(*hso_dev),
GFP_ATOMIC);
2365 hso_dev->
usb = interface_to_usbdev(intf);
2367 kref_init(&hso_dev->
ref);
2378 static int remove_net_device(
struct hso_device *hso_dev)
2383 if (network_table[i] == hso_dev) {
2384 network_table[
i] =
NULL;
2388 if (i == HSO_MAX_NET_DEVICES)
2394 static void hso_free_net_device(
struct hso_device *hso_dev)
2397 struct hso_net *hso_net = dev2net(hso_dev);
2402 remove_net_device(hso_net->
parent);
2424 .ndo_open = hso_net_open,
2425 .ndo_stop = hso_net_close,
2426 .ndo_start_xmit = hso_net_start_xmit,
2427 .ndo_tx_timeout = hso_net_tx_timeout,
2431 static void hso_net_init(
struct net_device *net)
2433 struct hso_net *hso_net = netdev_priv(net);
2435 D1(
"sizeof hso_net is %d", (
int)
sizeof(*hso_net));
2451 static int add_net_device(
struct hso_device *hso_dev)
2456 if (network_table[i] ==
NULL) {
2457 network_table[
i] = hso_dev;
2461 if (i == HSO_MAX_NET_DEVICES)
2466 static int hso_rfkill_set_block(
void *
data,
bool blocked)
2477 enabled ? 0x82 : 0x81, 0x40, 0, 0,
NULL, 0,
2478 USB_CTRL_SET_TIMEOUT);
2483 static const struct rfkill_ops hso_rfkill_ops = {
2484 .set_block = hso_rfkill_set_block,
2488 static void hso_create_rfkill(
struct hso_device *hso_dev,
2491 struct hso_net *hso_net = dev2net(hso_dev);
2492 struct device *dev = &hso_net->
net->dev;
2497 dev_err(dev,
"%s - Out of memory\n", __func__);
2500 interface->altsetting->desc.bInterfaceNumber);
2503 &interface_to_usbdev(interface)->dev,
2505 &hso_rfkill_ops, hso_dev);
2507 dev_err(dev,
"%s - Out of memory\n", __func__);
2515 dev_err(dev,
"%s - Failed to register rfkill\n", __func__);
2530 struct hso_net *hso_net;
2533 hso_dev = hso_create_device(interface, port_spec);
2539 net =
alloc_netdev(
sizeof(
struct hso_net),
"hso%d", hso_net_init);
2541 dev_err(&interface->dev,
"Unable to create ethernet device\n");
2545 hso_net = netdev_priv(net);
2549 hso_net->
parent = hso_dev;
2554 dev_err(&interface->dev,
"Can't find BULK IN endpoint\n");
2560 dev_err(&interface->dev,
"Can't find BULK OUT endpoint\n");
2569 dev_err(&interface->dev,
"Failed to register device\n");
2577 dev_err(&interface->dev,
"Could not allocate rx urb\n");
2583 dev_err(&interface->dev,
"Could not allocate rx buf\n");
2589 dev_err(&interface->dev,
"Could not allocate tx urb\n");
2594 dev_err(&interface->dev,
"Could not allocate tx buf\n");
2598 add_net_device(hso_dev);
2600 hso_log_port(hso_dev);
2602 hso_create_rfkill(hso_dev, interface);
2606 hso_free_net_device(hso_dev);
2610 static void hso_free_tiomget(
struct hso_serial *serial)
2625 static void hso_free_serial_device(
struct hso_device *hso_dev)
2627 struct hso_serial *serial = dev2ser(hso_dev);
2631 set_serial_by_index(serial->
minor,
NULL);
2633 hso_serial_common_free(serial);
2642 hso_free_tiomget(serial);
2648 static struct hso_device *hso_create_bulk_serial_device(
2656 hso_dev = hso_create_device(interface, port);
2660 serial = kzalloc(
sizeof(*serial),
GFP_KERNEL);
2664 serial->
parent = hso_dev;
2677 if (tiocmget->
urb) {
2680 tiocmget->
endp = hso_get_ep(
2685 hso_free_tiomget(serial);
2698 dev_err(&interface->dev,
"Failed to find BULK IN ep\n");
2705 dev_err(&interface->dev,
"Failed to find BULK IN ep\n");
2709 serial->
write_data = hso_std_serial_write_data;
2712 set_serial_by_index(serial->
minor, serial);
2715 hso_log_port(hso_dev);
2721 hso_serial_common_free(serial);
2723 hso_free_tiomget(serial);
2740 port_spec &= ~HSO_PORT_MASK;
2742 port_spec |= hso_mux_to_port(port);
2746 hso_dev = hso_create_device(interface, port_spec);
2750 serial = kzalloc(
sizeof(*serial),
GFP_KERNEL);
2755 serial->
parent = hso_dev;
2757 if (hso_serial_common_create
2762 serial->
write_data = hso_mux_serial_write_data;
2770 set_serial_by_index(serial->
minor, serial);
2773 hso_log_port(hso_dev);
2808 dev_err(&interface->dev,
"Can't find INT IN endpoint\n");
2814 dev_err(&interface->dev,
"Could not allocate intr urb?\n");
2821 dev_err(&interface->dev,
"Could not allocate intr buf?\n");
2837 static int hso_get_config_data(
struct usb_interface *interface)
2839 struct usb_device *usbdev = interface_to_usbdev(interface);
2841 u32 if_num = interface->altsetting->desc.bInterfaceNumber;
2845 0
x86, 0xC0, 0, 0, config_data, 17,
2846 USB_CTRL_SET_TIMEOUT) != 0x11) {
2850 switch (config_data[if_num]) {
2894 if (config_data[16] & 0x1)
2904 int mux,
i, if_num, port_spec;
2905 unsigned char port_mask;
2910 if_num = interface->altsetting->desc.bInterfaceNumber;
2914 if (id->driver_info)
2915 port_spec = ((
u32 *)(id->driver_info))[if_num];
2917 port_spec = hso_get_config_data(interface);
2919 if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
2920 dev_err(&interface->dev,
"Not our interface\n");
2925 if (interface->num_altsetting > 1)
2927 interface->needs_remote_wakeup = 1;
2935 hso_dev = hso_create_net_device(interface,
2943 if (hso_get_mux_ports(interface, &port_mask))
2947 shared_int = hso_create_shared_int(interface);
2951 for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
2952 if (port_mask & i) {
2953 hso_dev = hso_create_mux_serial_device(
2954 interface, i, shared_int);
2969 hso_create_net_device(interface, port_spec);
2972 hso_create_bulk_serial_device(interface, port_spec);
2982 usb_set_intfdata(interface, hso_dev);
2987 hso_free_interface(interface);
2994 hso_free_interface(interface);
2997 usb_set_intfdata(interface,
NULL);
3004 usb_autopm_get_interface(hso_dev->
interface);
3011 usb_autopm_put_interface(hso_dev->
interface);
3014 static int hso_get_activity(
struct hso_device *hso_dev)
3026 usb_mark_last_busy(hso_dev->
usb);
3031 static int hso_put_activity(
struct hso_device *hso_dev)
3051 if (serial_table[i] && (serial_table[i]->interface == iface)) {
3052 result = hso_stop_serial_device(serial_table[i]);
3060 if (network_table[i] &&
3061 (network_table[i]->interface == iface)) {
3062 result = hso_stop_net_device(network_table[i]);
3076 struct hso_net *hso_net;
3080 if (serial_table[i] && (serial_table[i]->interface == iface)) {
3081 if (dev2ser(serial_table[i])->port.count) {
3083 hso_start_serial_device(serial_table[i],
GFP_NOIO);
3084 hso_kick_transmit(dev2ser(serial_table[i]));
3093 if (network_table[i] &&
3094 (network_table[i]->interface == iface)) {
3095 hso_net = dev2net(network_table[i]);
3102 " lingering data\n");
3107 result = hso_start_net_device(network_table[i]);
3118 static void reset_device(
struct work_struct *data)
3122 struct usb_device *usb = hso_dev->
usb;
3126 D1(
"No reset during disconnect\n");
3130 D1(
"unable to lock device for reset: %d\n", result);
3133 usb_unlock_device(usb);
3138 static void hso_serial_ref_free(
struct kref *ref)
3142 hso_free_serial_device(hso_dev);
3145 static void hso_free_interface(
struct usb_interface *interface)
3152 if (serial_table[i] &&
3153 (serial_table[i]->interface == interface)) {
3154 hso_dev = dev2ser(serial_table[i]);
3161 hso_dev->
parent->usb_gone = 1;
3163 kref_put(&serial_table[i]->ref, hso_serial_ref_free);
3168 if (network_table[i] &&
3169 (network_table[i]->interface == interface)) {
3170 struct rfkill *rfk = dev2net(network_table[i])->rfkill;
3173 netif_stop_queue(dev2net(network_table[i])->net);
3174 hso_stop_net_device(network_table[i]);
3181 hso_free_net_device(network_table[i]);
3193 struct usb_host_interface *iface = intf->cur_altsetting;
3196 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
3197 endp = &iface->endpoint[
i].desc;
3199 (usb_endpoint_type(endp) == type))
3207 static int hso_get_mux_ports(
struct usb_interface *intf,
unsigned char *ports)
3210 struct usb_host_interface *iface = intf->cur_altsetting;
3212 if (iface->extralen == 3) {
3213 *ports = iface->extra[2];
3217 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
3218 if (iface->endpoint[i].extralen == 3) {
3219 *ports = iface->endpoint[
i].extra[2];
3228 static int hso_mux_submit_intr_urb(
struct hso_shared_int *shared_int,
3229 struct usb_device *usb,
gfp_t gfp)
3235 shared_int->
intr_endp->bEndpointAddress & 0x7F),
3238 intr_callback, shared_int,
3243 dev_warn(&usb->dev,
"%s failed mux_intr_urb %d\n", __func__,
3251 .open = hso_serial_open,
3252 .close = hso_serial_close,
3253 .write = hso_serial_write,
3254 .write_room = hso_serial_write_room,
3255 .ioctl = hso_serial_ioctl,
3256 .set_termios = hso_serial_set_termios,
3257 .chars_in_buffer = hso_serial_chars_in_buffer,
3258 .tiocmget = hso_serial_tiocmget,
3259 .tiocmset = hso_serial_tiocmset,
3260 .get_icount = hso_get_count,
3261 .unthrottle = hso_unthrottle
3264 static struct usb_driver hso_driver = {
3267 .disconnect = hso_disconnect,
3268 .id_table = hso_ids,
3269 .suspend = hso_suspend,
3270 .resume = hso_resume,
3271 .reset_resume = hso_resume,
3272 .supports_autosuspend = 1,
3273 .disable_hub_initiated_lpm = 1,
3276 static int __init hso_init(
void)
3287 serial_table[i] =
NULL;
3290 tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
3296 tty_drv->
name = tty_filename;
3300 tty_drv->
major = tty_major;
3319 result = usb_register(&hso_driver);
3335 static void __exit hso_exit(
void)