omninet.c 4.9 KB

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