omninet.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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/init.h>
  16. #include <linux/slab.h>
  17. #include <linux/tty.h>
  18. #include <linux/tty_driver.h>
  19. #include <linux/tty_flip.h>
  20. #include <linux/module.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/usb.h>
  23. #include <linux/usb/serial.h>
  24. /*
  25. * Version Information
  26. */
  27. #define DRIVER_VERSION "v1.1"
  28. #define DRIVER_AUTHOR "Alessandro Zummo"
  29. #define DRIVER_DESC "USB ZyXEL omni.net LCD PLUS Driver"
  30. #define ZYXEL_VENDOR_ID 0x0586
  31. #define ZYXEL_OMNINET_ID 0x1000
  32. /* This one seems to be a re-branded ZyXEL device */
  33. #define BT_IGNITIONPRO_ID 0x2000
  34. /* function prototypes */
  35. static int omninet_open(struct tty_struct *tty, struct usb_serial_port *port);
  36. static void omninet_close(struct usb_serial_port *port);
  37. static void omninet_read_bulk_callback(struct urb *urb);
  38. static void omninet_write_bulk_callback(struct urb *urb);
  39. static int omninet_write(struct tty_struct *tty, struct usb_serial_port *port,
  40. const unsigned char *buf, int count);
  41. static int omninet_write_room(struct tty_struct *tty);
  42. static void omninet_disconnect(struct usb_serial *serial);
  43. static void omninet_release(struct usb_serial *serial);
  44. static int omninet_attach(struct usb_serial *serial);
  45. static const struct usb_device_id id_table[] = {
  46. { USB_DEVICE(ZYXEL_VENDOR_ID, ZYXEL_OMNINET_ID) },
  47. { USB_DEVICE(ZYXEL_VENDOR_ID, BT_IGNITIONPRO_ID) },
  48. { } /* Terminating entry */
  49. };
  50. MODULE_DEVICE_TABLE(usb, id_table);
  51. static struct usb_serial_driver zyxel_omninet_device = {
  52. .driver = {
  53. .owner = THIS_MODULE,
  54. .name = "omninet",
  55. },
  56. .description = "ZyXEL - omni.net lcd plus usb",
  57. .id_table = id_table,
  58. .num_ports = 1,
  59. .attach = omninet_attach,
  60. .open = omninet_open,
  61. .close = omninet_close,
  62. .write = omninet_write,
  63. .write_room = omninet_write_room,
  64. .read_bulk_callback = omninet_read_bulk_callback,
  65. .write_bulk_callback = omninet_write_bulk_callback,
  66. .disconnect = omninet_disconnect,
  67. .release = omninet_release,
  68. };
  69. static struct usb_serial_driver * const serial_drivers[] = {
  70. &zyxel_omninet_device, NULL
  71. };
  72. /* The protocol.
  73. *
  74. * The omni.net always exchange 64 bytes of data with the host. The first
  75. * four bytes are the control header, you can see it in the above structure.
  76. *
  77. * oh_seq is a sequence number. Don't know if/how it's used.
  78. * oh_len is the length of the data bytes in the packet.
  79. * oh_xxx Bit-mapped, related to handshaking and status info.
  80. * I normally set it to 0x03 in trasmitted frames.
  81. * 7: Active when the TA is in a CONNECTed state.
  82. * 6: unknown
  83. * 5: handshaking, unknown
  84. * 4: handshaking, unknown
  85. * 3: unknown, usually 0
  86. * 2: unknown, usually 0
  87. * 1: handshaking, unknown, usually set to 1 in trasmitted frames
  88. * 0: handshaking, unknown, usually set to 1 in trasmitted frames
  89. * oh_pad Probably a pad byte.
  90. *
  91. * After the header you will find data bytes if oh_len was greater than zero.
  92. *
  93. */
  94. struct omninet_header {
  95. __u8 oh_seq;
  96. __u8 oh_len;
  97. __u8 oh_xxx;
  98. __u8 oh_pad;
  99. };
  100. struct omninet_data {
  101. __u8 od_outseq; /* Sequence number for bulk_out URBs */
  102. };
  103. static int omninet_attach(struct usb_serial *serial)
  104. {
  105. struct omninet_data *od;
  106. struct usb_serial_port *port = serial->port[0];
  107. od = kmalloc(sizeof(struct omninet_data), GFP_KERNEL);
  108. if (!od) {
  109. dev_err(&port->dev, "%s- kmalloc(%Zd) failed.\n",
  110. __func__, sizeof(struct omninet_data));
  111. return -ENOMEM;
  112. }
  113. usb_set_serial_port_data(port, od);
  114. return 0;
  115. }
  116. static int omninet_open(struct tty_struct *tty, struct usb_serial_port *port)
  117. {
  118. struct usb_serial *serial = port->serial;
  119. struct usb_serial_port *wport;
  120. int result = 0;
  121. wport = serial->port[1];
  122. tty_port_tty_set(&wport->port, tty);
  123. /* Start reading from the device */
  124. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  125. if (result)
  126. dev_err(&port->dev,
  127. "%s - failed submitting read urb, error %d\n",
  128. __func__, result);
  129. return result;
  130. }
  131. static void omninet_close(struct usb_serial_port *port)
  132. {
  133. usb_kill_urb(port->read_urb);
  134. }
  135. #define OMNINET_DATAOFFSET 0x04
  136. #define OMNINET_HEADERLEN sizeof(struct omninet_header)
  137. #define OMNINET_BULKOUTSIZE (64 - OMNINET_HEADERLEN)
  138. static void omninet_read_bulk_callback(struct urb *urb)
  139. {
  140. struct usb_serial_port *port = urb->context;
  141. unsigned char *data = urb->transfer_buffer;
  142. struct omninet_header *header = (struct omninet_header *) &data[0];
  143. int status = urb->status;
  144. int result;
  145. if (status) {
  146. dev_dbg(&port->dev, "%s - nonzero read bulk status received: %d\n",
  147. __func__, status);
  148. return;
  149. }
  150. if (urb->actual_length && header->oh_len) {
  151. struct tty_struct *tty = tty_port_tty_get(&port->port);
  152. if (tty) {
  153. tty_insert_flip_string(tty, data + OMNINET_DATAOFFSET,
  154. header->oh_len);
  155. tty_flip_buffer_push(tty);
  156. tty_kref_put(tty);
  157. }
  158. }
  159. /* Continue trying to always read */
  160. result = usb_submit_urb(urb, GFP_ATOMIC);
  161. if (result)
  162. dev_err(&port->dev,
  163. "%s - failed resubmitting read urb, error %d\n",
  164. __func__, result);
  165. }
  166. static int omninet_write(struct tty_struct *tty, struct usb_serial_port *port,
  167. const unsigned char *buf, int count)
  168. {
  169. struct usb_serial *serial = port->serial;
  170. struct usb_serial_port *wport = serial->port[1];
  171. struct omninet_data *od = usb_get_serial_port_data(port);
  172. struct omninet_header *header = (struct omninet_header *)
  173. wport->write_urb->transfer_buffer;
  174. int result;
  175. if (count == 0) {
  176. dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
  177. return 0;
  178. }
  179. if (!test_and_clear_bit(0, &port->write_urbs_free)) {
  180. dev_dbg(&port->dev, "%s - already writing\n", __func__);
  181. return 0;
  182. }
  183. count = (count > OMNINET_BULKOUTSIZE) ? OMNINET_BULKOUTSIZE : count;
  184. memcpy(wport->write_urb->transfer_buffer + OMNINET_DATAOFFSET,
  185. buf, count);
  186. usb_serial_debug_data(&port->dev, __func__, count,
  187. wport->write_urb->transfer_buffer);
  188. header->oh_seq = od->od_outseq++;
  189. header->oh_len = count;
  190. header->oh_xxx = 0x03;
  191. header->oh_pad = 0x00;
  192. /* send the data out the bulk port, always 64 bytes */
  193. wport->write_urb->transfer_buffer_length = 64;
  194. result = usb_submit_urb(wport->write_urb, GFP_ATOMIC);
  195. if (result) {
  196. set_bit(0, &wport->write_urbs_free);
  197. dev_err_console(port,
  198. "%s - failed submitting write urb, error %d\n",
  199. __func__, result);
  200. } else
  201. result = count;
  202. return result;
  203. }
  204. static int omninet_write_room(struct tty_struct *tty)
  205. {
  206. struct usb_serial_port *port = tty->driver_data;
  207. struct usb_serial *serial = port->serial;
  208. struct usb_serial_port *wport = serial->port[1];
  209. int room = 0; /* Default: no room */
  210. if (test_bit(0, &wport->write_urbs_free))
  211. room = wport->bulk_out_size - OMNINET_HEADERLEN;
  212. dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
  213. return room;
  214. }
  215. static void omninet_write_bulk_callback(struct urb *urb)
  216. {
  217. /* struct omninet_header *header = (struct omninet_header *)
  218. urb->transfer_buffer; */
  219. struct usb_serial_port *port = urb->context;
  220. int status = urb->status;
  221. set_bit(0, &port->write_urbs_free);
  222. if (status) {
  223. dev_dbg(&port->dev, "%s - nonzero write bulk status received: %d\n",
  224. __func__, status);
  225. return;
  226. }
  227. usb_serial_port_softint(port);
  228. }
  229. static void omninet_disconnect(struct usb_serial *serial)
  230. {
  231. struct usb_serial_port *wport = serial->port[1];
  232. usb_kill_urb(wport->write_urb);
  233. }
  234. static void omninet_release(struct usb_serial *serial)
  235. {
  236. struct usb_serial_port *port = serial->port[0];
  237. kfree(usb_get_serial_port_data(port));
  238. }
  239. module_usb_serial_driver(serial_drivers, id_table);
  240. MODULE_AUTHOR(DRIVER_AUTHOR);
  241. MODULE_DESCRIPTION(DRIVER_DESC);
  242. MODULE_LICENSE("GPL");