omninet.c 4.8 KB

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