omninet.c 8.5 KB

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