omninet.c 8.2 KB

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