omninet.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * USB ZyXEL omni.net LCD PLUS driver
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License version
  6. * 2 as published by the Free Software Foundation.
  7. *
  8. * See Documentation/usb/usb-serial.txt for more information on using this
  9. * driver
  10. *
  11. * Please report both successes and troubles to the author at omninet@kroah.com
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/slab.h>
  16. #include <linux/tty.h>
  17. #include <linux/tty_driver.h>
  18. #include <linux/tty_flip.h>
  19. #include <linux/module.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/usb.h>
  22. #include <linux/usb/serial.h>
  23. #define DRIVER_AUTHOR "Alessandro Zummo"
  24. #define DRIVER_DESC "USB ZyXEL omni.net LCD PLUS Driver"
  25. #define ZYXEL_VENDOR_ID 0x0586
  26. #define ZYXEL_OMNINET_ID 0x1000
  27. /* This one seems to be a re-branded ZyXEL device */
  28. #define BT_IGNITIONPRO_ID 0x2000
  29. /* function prototypes */
  30. static int omninet_open(struct tty_struct *tty, struct usb_serial_port *port);
  31. static void omninet_process_read_urb(struct urb *urb);
  32. static void omninet_write_bulk_callback(struct urb *urb);
  33. static int omninet_write(struct tty_struct *tty, struct usb_serial_port *port,
  34. const unsigned char *buf, int count);
  35. static int omninet_write_room(struct tty_struct *tty);
  36. static void omninet_disconnect(struct usb_serial *serial);
  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_ports = 1,
  53. .port_probe = omninet_port_probe,
  54. .port_remove = omninet_port_remove,
  55. .open = omninet_open,
  56. .write = omninet_write,
  57. .write_room = omninet_write_room,
  58. .write_bulk_callback = omninet_write_bulk_callback,
  59. .process_read_urb = omninet_process_read_urb,
  60. .disconnect = omninet_disconnect,
  61. };
  62. static struct usb_serial_driver * const serial_drivers[] = {
  63. &zyxel_omninet_device, NULL
  64. };
  65. /*
  66. * The protocol.
  67. *
  68. * The omni.net always exchange 64 bytes of data with the host. The first
  69. * four bytes are the control header.
  70. *
  71. * oh_seq is a sequence number. Don't know if/how it's used.
  72. * oh_len is the length of the data bytes in the packet.
  73. * oh_xxx Bit-mapped, related to handshaking and status info.
  74. * I normally set it to 0x03 in transmitted frames.
  75. * 7: Active when the TA is in a CONNECTed state.
  76. * 6: unknown
  77. * 5: handshaking, unknown
  78. * 4: handshaking, unknown
  79. * 3: unknown, usually 0
  80. * 2: unknown, usually 0
  81. * 1: handshaking, unknown, usually set to 1 in transmitted frames
  82. * 0: handshaking, unknown, usually set to 1 in transmitted frames
  83. * oh_pad Probably a pad byte.
  84. *
  85. * After the header you will find data bytes if oh_len was greater than zero.
  86. */
  87. struct omninet_header {
  88. __u8 oh_seq;
  89. __u8 oh_len;
  90. __u8 oh_xxx;
  91. __u8 oh_pad;
  92. };
  93. struct omninet_data {
  94. __u8 od_outseq; /* Sequence number for bulk_out URBs */
  95. };
  96. static int omninet_port_probe(struct usb_serial_port *port)
  97. {
  98. struct omninet_data *od;
  99. od = kzalloc(sizeof(*od), GFP_KERNEL);
  100. if (!od)
  101. return -ENOMEM;
  102. usb_set_serial_port_data(port, od);
  103. return 0;
  104. }
  105. static int omninet_port_remove(struct usb_serial_port *port)
  106. {
  107. struct omninet_data *od;
  108. od = usb_get_serial_port_data(port);
  109. kfree(od);
  110. return 0;
  111. }
  112. static int omninet_open(struct tty_struct *tty, struct usb_serial_port *port)
  113. {
  114. struct usb_serial *serial = port->serial;
  115. struct usb_serial_port *wport;
  116. wport = serial->port[1];
  117. tty_port_tty_set(&wport->port, tty);
  118. return usb_serial_generic_open(tty, port);
  119. }
  120. #define OMNINET_HEADERLEN 4
  121. #define OMNINET_BULKOUTSIZE 64
  122. #define OMNINET_PAYLOADSIZE (OMNINET_BULKOUTSIZE - OMNINET_HEADERLEN)
  123. static void omninet_process_read_urb(struct urb *urb)
  124. {
  125. struct usb_serial_port *port = urb->context;
  126. const struct omninet_header *hdr = urb->transfer_buffer;
  127. const unsigned char *data;
  128. size_t data_len;
  129. if (urb->actual_length <= OMNINET_HEADERLEN || !hdr->oh_len)
  130. return;
  131. data = (char *)urb->transfer_buffer + OMNINET_HEADERLEN;
  132. data_len = min_t(size_t, urb->actual_length - OMNINET_HEADERLEN,
  133. hdr->oh_len);
  134. tty_insert_flip_string(&port->port, data, data_len);
  135. tty_flip_buffer_push(&port->port);
  136. }
  137. static int omninet_write(struct tty_struct *tty, struct usb_serial_port *port,
  138. const unsigned char *buf, int count)
  139. {
  140. struct usb_serial *serial = port->serial;
  141. struct usb_serial_port *wport = serial->port[1];
  142. struct omninet_data *od = usb_get_serial_port_data(port);
  143. struct omninet_header *header = (struct omninet_header *)
  144. wport->write_urb->transfer_buffer;
  145. int result;
  146. if (count == 0) {
  147. dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
  148. return 0;
  149. }
  150. if (!test_and_clear_bit(0, &port->write_urbs_free)) {
  151. dev_dbg(&port->dev, "%s - already writing\n", __func__);
  152. return 0;
  153. }
  154. count = (count > OMNINET_PAYLOADSIZE) ? OMNINET_PAYLOADSIZE : count;
  155. memcpy(wport->write_urb->transfer_buffer + OMNINET_HEADERLEN,
  156. buf, count);
  157. usb_serial_debug_data(&port->dev, __func__, count,
  158. wport->write_urb->transfer_buffer);
  159. header->oh_seq = od->od_outseq++;
  160. header->oh_len = count;
  161. header->oh_xxx = 0x03;
  162. header->oh_pad = 0x00;
  163. /* send the data out the bulk port, always 64 bytes */
  164. wport->write_urb->transfer_buffer_length = OMNINET_BULKOUTSIZE;
  165. result = usb_submit_urb(wport->write_urb, GFP_ATOMIC);
  166. if (result) {
  167. set_bit(0, &wport->write_urbs_free);
  168. dev_err_console(port,
  169. "%s - failed submitting write urb, error %d\n",
  170. __func__, result);
  171. } else
  172. result = count;
  173. return result;
  174. }
  175. static int omninet_write_room(struct tty_struct *tty)
  176. {
  177. struct usb_serial_port *port = tty->driver_data;
  178. struct usb_serial *serial = port->serial;
  179. struct usb_serial_port *wport = serial->port[1];
  180. int room = 0; /* Default: no room */
  181. if (test_bit(0, &wport->write_urbs_free))
  182. room = wport->bulk_out_size - OMNINET_HEADERLEN;
  183. dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
  184. return room;
  185. }
  186. static void omninet_write_bulk_callback(struct urb *urb)
  187. {
  188. /* struct omninet_header *header = (struct omninet_header *)
  189. urb->transfer_buffer; */
  190. struct usb_serial_port *port = urb->context;
  191. int status = urb->status;
  192. set_bit(0, &port->write_urbs_free);
  193. if (status) {
  194. dev_dbg(&port->dev, "%s - nonzero write bulk status received: %d\n",
  195. __func__, status);
  196. return;
  197. }
  198. usb_serial_port_softint(port);
  199. }
  200. static void omninet_disconnect(struct usb_serial *serial)
  201. {
  202. struct usb_serial_port *wport = serial->port[1];
  203. usb_kill_urb(wport->write_urb);
  204. }
  205. module_usb_serial_driver(serial_drivers, id_table);
  206. MODULE_AUTHOR(DRIVER_AUTHOR);
  207. MODULE_DESCRIPTION(DRIVER_DESC);
  208. MODULE_LICENSE("GPL");