omninet.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * USB ZyXEL omni.net driver
  4. *
  5. * Copyright (C) 2013,2017 Johan Hovold <[email protected]>
  6. *
  7. * See Documentation/usb/usb-serial.rst for more information on using this
  8. * driver
  9. *
  10. * Please report both successes and troubles to the author at [email protected]
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/errno.h>
  14. #include <linux/slab.h>
  15. #include <linux/tty.h>
  16. #include <linux/tty_driver.h>
  17. #include <linux/tty_flip.h>
  18. #include <linux/module.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/usb.h>
  21. #include <linux/usb/serial.h>
  22. #define DRIVER_AUTHOR "Alessandro Zummo"
  23. #define DRIVER_DESC "USB ZyXEL omni.net Driver"
  24. #define ZYXEL_VENDOR_ID 0x0586
  25. #define ZYXEL_OMNINET_ID 0x1000
  26. #define ZYXEL_OMNI_56K_PLUS_ID 0x1500
  27. /* This one seems to be a re-branded ZyXEL device */
  28. #define BT_IGNITIONPRO_ID 0x2000
  29. /* function prototypes */
  30. static void omninet_process_read_urb(struct urb *urb);
  31. static int omninet_prepare_write_buffer(struct usb_serial_port *port,
  32. void *buf, size_t count);
  33. static int omninet_calc_num_ports(struct usb_serial *serial,
  34. struct usb_serial_endpoints *epds);
  35. static int omninet_port_probe(struct usb_serial_port *port);
  36. static void omninet_port_remove(struct usb_serial_port *port);
  37. static const struct usb_device_id id_table[] = {
  38. { USB_DEVICE(ZYXEL_VENDOR_ID, ZYXEL_OMNINET_ID) },
  39. { USB_DEVICE(ZYXEL_VENDOR_ID, ZYXEL_OMNI_56K_PLUS_ID) },
  40. { USB_DEVICE(ZYXEL_VENDOR_ID, BT_IGNITIONPRO_ID) },
  41. { } /* Terminating entry */
  42. };
  43. MODULE_DEVICE_TABLE(usb, id_table);
  44. static struct usb_serial_driver zyxel_omninet_device = {
  45. .driver = {
  46. .owner = THIS_MODULE,
  47. .name = "omninet",
  48. },
  49. .description = "ZyXEL - omni.net usb",
  50. .id_table = id_table,
  51. .num_bulk_out = 2,
  52. .calc_num_ports = omninet_calc_num_ports,
  53. .port_probe = omninet_port_probe,
  54. .port_remove = omninet_port_remove,
  55. .process_read_urb = omninet_process_read_urb,
  56. .prepare_write_buffer = omninet_prepare_write_buffer,
  57. };
  58. static struct usb_serial_driver * const serial_drivers[] = {
  59. &zyxel_omninet_device, NULL
  60. };
  61. /*
  62. * The protocol.
  63. *
  64. * The omni.net always exchange 64 bytes of data with the host. The first
  65. * four bytes are the control header.
  66. *
  67. * oh_seq is a sequence number. Don't know if/how it's used.
  68. * oh_len is the length of the data bytes in the packet.
  69. * oh_xxx Bit-mapped, related to handshaking and status info.
  70. * I normally set it to 0x03 in transmitted frames.
  71. * 7: Active when the TA is in a CONNECTed state.
  72. * 6: unknown
  73. * 5: handshaking, unknown
  74. * 4: handshaking, unknown
  75. * 3: unknown, usually 0
  76. * 2: unknown, usually 0
  77. * 1: handshaking, unknown, usually set to 1 in transmitted frames
  78. * 0: handshaking, unknown, usually set to 1 in transmitted frames
  79. * oh_pad Probably a pad byte.
  80. *
  81. * After the header you will find data bytes if oh_len was greater than zero.
  82. */
  83. struct omninet_header {
  84. __u8 oh_seq;
  85. __u8 oh_len;
  86. __u8 oh_xxx;
  87. __u8 oh_pad;
  88. };
  89. struct omninet_data {
  90. __u8 od_outseq; /* Sequence number for bulk_out URBs */
  91. };
  92. static int omninet_calc_num_ports(struct usb_serial *serial,
  93. struct usb_serial_endpoints *epds)
  94. {
  95. /* We need only the second bulk-out for our single-port device. */
  96. epds->bulk_out[0] = epds->bulk_out[1];
  97. epds->num_bulk_out = 1;
  98. return 1;
  99. }
  100. static int omninet_port_probe(struct usb_serial_port *port)
  101. {
  102. struct omninet_data *od;
  103. od = kzalloc(sizeof(*od), GFP_KERNEL);
  104. if (!od)
  105. return -ENOMEM;
  106. usb_set_serial_port_data(port, od);
  107. return 0;
  108. }
  109. static void omninet_port_remove(struct usb_serial_port *port)
  110. {
  111. struct omninet_data *od;
  112. od = usb_get_serial_port_data(port);
  113. kfree(od);
  114. }
  115. #define OMNINET_HEADERLEN 4
  116. #define OMNINET_BULKOUTSIZE 64
  117. #define OMNINET_PAYLOADSIZE (OMNINET_BULKOUTSIZE - OMNINET_HEADERLEN)
  118. static void omninet_process_read_urb(struct urb *urb)
  119. {
  120. struct usb_serial_port *port = urb->context;
  121. const struct omninet_header *hdr = urb->transfer_buffer;
  122. const unsigned char *data;
  123. size_t data_len;
  124. if (urb->actual_length <= OMNINET_HEADERLEN || !hdr->oh_len)
  125. return;
  126. data = (char *)urb->transfer_buffer + OMNINET_HEADERLEN;
  127. data_len = min_t(size_t, urb->actual_length - OMNINET_HEADERLEN,
  128. hdr->oh_len);
  129. tty_insert_flip_string(&port->port, data, data_len);
  130. tty_flip_buffer_push(&port->port);
  131. }
  132. static int omninet_prepare_write_buffer(struct usb_serial_port *port,
  133. void *buf, size_t count)
  134. {
  135. struct omninet_data *od = usb_get_serial_port_data(port);
  136. struct omninet_header *header = buf;
  137. count = min_t(size_t, count, OMNINET_PAYLOADSIZE);
  138. count = kfifo_out_locked(&port->write_fifo, buf + OMNINET_HEADERLEN,
  139. count, &port->lock);
  140. header->oh_seq = od->od_outseq++;
  141. header->oh_len = count;
  142. header->oh_xxx = 0x03;
  143. header->oh_pad = 0x00;
  144. /* always 64 bytes */
  145. return OMNINET_BULKOUTSIZE;
  146. }
  147. module_usb_serial_driver(serial_drivers, id_table);
  148. MODULE_AUTHOR(DRIVER_AUTHOR);
  149. MODULE_DESCRIPTION(DRIVER_DESC);
  150. MODULE_LICENSE("GPL v2");