omninet.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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/spinlock.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/usb.h>
  24. #include <linux/usb/serial.h>
  25. static int debug;
  26. /*
  27. * Version Information
  28. */
  29. #define DRIVER_VERSION "v1.1"
  30. #define DRIVER_AUTHOR "Alessandro Zummo"
  31. #define DRIVER_DESC "USB ZyXEL omni.net LCD PLUS Driver"
  32. #define ZYXEL_VENDOR_ID 0x0586
  33. #define ZYXEL_OMNINET_ID 0x1000
  34. /* This one seems to be a re-branded ZyXEL device */
  35. #define BT_IGNITIONPRO_ID 0x2000
  36. /* function prototypes */
  37. static int omninet_open(struct tty_struct *tty, struct usb_serial_port *port);
  38. static void omninet_close(struct usb_serial_port *port);
  39. static void omninet_read_bulk_callback(struct urb *urb);
  40. static void omninet_write_bulk_callback(struct urb *urb);
  41. static int omninet_write(struct tty_struct *tty, struct usb_serial_port *port,
  42. const unsigned char *buf, int count);
  43. static int omninet_write_room(struct tty_struct *tty);
  44. static void omninet_disconnect(struct usb_serial *serial);
  45. static void omninet_release(struct usb_serial *serial);
  46. static int omninet_attach(struct usb_serial *serial);
  47. static const struct usb_device_id id_table[] = {
  48. { USB_DEVICE(ZYXEL_VENDOR_ID, ZYXEL_OMNINET_ID) },
  49. { USB_DEVICE(ZYXEL_VENDOR_ID, BT_IGNITIONPRO_ID) },
  50. { } /* Terminating entry */
  51. };
  52. MODULE_DEVICE_TABLE(usb, id_table);
  53. static struct usb_driver omninet_driver = {
  54. .name = "omninet",
  55. .probe = usb_serial_probe,
  56. .disconnect = usb_serial_disconnect,
  57. .id_table = id_table,
  58. .no_dynamic_id = 1,
  59. };
  60. static struct usb_serial_driver zyxel_omninet_device = {
  61. .driver = {
  62. .owner = THIS_MODULE,
  63. .name = "omninet",
  64. },
  65. .description = "ZyXEL - omni.net lcd plus usb",
  66. .usb_driver = &omninet_driver,
  67. .id_table = id_table,
  68. .num_ports = 1,
  69. .attach = omninet_attach,
  70. .open = omninet_open,
  71. .close = omninet_close,
  72. .write = omninet_write,
  73. .write_room = omninet_write_room,
  74. .read_bulk_callback = omninet_read_bulk_callback,
  75. .write_bulk_callback = omninet_write_bulk_callback,
  76. .disconnect = omninet_disconnect,
  77. .release = omninet_release,
  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. usb_fill_bulk_urb(port->read_urb, serial->dev,
  133. usb_rcvbulkpipe(serial->dev,
  134. port->bulk_in_endpointAddress),
  135. port->read_urb->transfer_buffer,
  136. port->read_urb->transfer_buffer_length,
  137. omninet_read_bulk_callback, port);
  138. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  139. if (result)
  140. dev_err(&port->dev,
  141. "%s - failed submitting read urb, error %d\n",
  142. __func__, result);
  143. return result;
  144. }
  145. static void omninet_close(struct usb_serial_port *port)
  146. {
  147. dbg("%s - port %d", __func__, port->number);
  148. usb_kill_urb(port->read_urb);
  149. }
  150. #define OMNINET_DATAOFFSET 0x04
  151. #define OMNINET_HEADERLEN sizeof(struct omninet_header)
  152. #define OMNINET_BULKOUTSIZE (64 - OMNINET_HEADERLEN)
  153. static void omninet_read_bulk_callback(struct urb *urb)
  154. {
  155. struct usb_serial_port *port = urb->context;
  156. unsigned char *data = urb->transfer_buffer;
  157. struct omninet_header *header = (struct omninet_header *) &data[0];
  158. int status = urb->status;
  159. int result;
  160. int i;
  161. dbg("%s - port %d", __func__, port->number);
  162. if (status) {
  163. dbg("%s - nonzero read bulk status received: %d",
  164. __func__, status);
  165. return;
  166. }
  167. if (debug && header->oh_xxx != 0x30) {
  168. if (urb->actual_length) {
  169. printk(KERN_DEBUG "%s: omninet_read %d: ",
  170. __FILE__, header->oh_len);
  171. for (i = 0; i < (header->oh_len +
  172. OMNINET_HEADERLEN); i++)
  173. printk("%.2x ", data[i]);
  174. printk("\n");
  175. }
  176. }
  177. if (urb->actual_length && header->oh_len) {
  178. struct tty_struct *tty = tty_port_tty_get(&port->port);
  179. tty_insert_flip_string(tty, data + OMNINET_DATAOFFSET,
  180. header->oh_len);
  181. tty_flip_buffer_push(tty);
  182. tty_kref_put(tty);
  183. }
  184. /* Continue trying to always read */
  185. usb_fill_bulk_urb(urb, port->serial->dev,
  186. usb_rcvbulkpipe(port->serial->dev,
  187. port->bulk_in_endpointAddress),
  188. urb->transfer_buffer, urb->transfer_buffer_length,
  189. omninet_read_bulk_callback, port);
  190. result = usb_submit_urb(urb, GFP_ATOMIC);
  191. if (result)
  192. dev_err(&port->dev,
  193. "%s - failed resubmitting read urb, error %d\n",
  194. __func__, result);
  195. }
  196. static int omninet_write(struct tty_struct *tty, struct usb_serial_port *port,
  197. const unsigned char *buf, int count)
  198. {
  199. struct usb_serial *serial = port->serial;
  200. struct usb_serial_port *wport = serial->port[1];
  201. struct omninet_data *od = usb_get_serial_port_data(port);
  202. struct omninet_header *header = (struct omninet_header *)
  203. wport->write_urb->transfer_buffer;
  204. int result;
  205. dbg("%s - port %d", __func__, port->number);
  206. if (count == 0) {
  207. dbg("%s - write request of 0 bytes", __func__);
  208. return 0;
  209. }
  210. spin_lock_bh(&wport->lock);
  211. if (wport->write_urb_busy) {
  212. spin_unlock_bh(&wport->lock);
  213. dbg("%s - already writing", __func__);
  214. return 0;
  215. }
  216. wport->write_urb_busy = 1;
  217. spin_unlock_bh(&wport->lock);
  218. count = (count > OMNINET_BULKOUTSIZE) ? OMNINET_BULKOUTSIZE : count;
  219. memcpy(wport->write_urb->transfer_buffer + OMNINET_DATAOFFSET,
  220. buf, count);
  221. usb_serial_debug_data(debug, &port->dev, __func__, count,
  222. wport->write_urb->transfer_buffer);
  223. header->oh_seq = od->od_outseq++;
  224. header->oh_len = count;
  225. header->oh_xxx = 0x03;
  226. header->oh_pad = 0x00;
  227. /* send the data out the bulk port, always 64 bytes */
  228. wport->write_urb->transfer_buffer_length = 64;
  229. wport->write_urb->dev = serial->dev;
  230. result = usb_submit_urb(wport->write_urb, GFP_ATOMIC);
  231. if (result) {
  232. wport->write_urb_busy = 0;
  233. dev_err(&port->dev,
  234. "%s - failed submitting write urb, error %d\n",
  235. __func__, result);
  236. } else
  237. result = count;
  238. return result;
  239. }
  240. static int omninet_write_room(struct tty_struct *tty)
  241. {
  242. struct usb_serial_port *port = tty->driver_data;
  243. struct usb_serial *serial = port->serial;
  244. struct usb_serial_port *wport = serial->port[1];
  245. int room = 0; /* Default: no room */
  246. /* FIXME: no consistent locking for write_urb_busy */
  247. if (wport->write_urb_busy)
  248. room = wport->bulk_out_size - OMNINET_HEADERLEN;
  249. dbg("%s - returns %d", __func__, room);
  250. return room;
  251. }
  252. static void omninet_write_bulk_callback(struct urb *urb)
  253. {
  254. /* struct omninet_header *header = (struct omninet_header *)
  255. urb->transfer_buffer; */
  256. struct usb_serial_port *port = urb->context;
  257. int status = urb->status;
  258. dbg("%s - port %0x", __func__, port->number);
  259. port->write_urb_busy = 0;
  260. if (status) {
  261. dbg("%s - nonzero write bulk status received: %d",
  262. __func__, status);
  263. return;
  264. }
  265. usb_serial_port_softint(port);
  266. }
  267. static void omninet_disconnect(struct usb_serial *serial)
  268. {
  269. struct usb_serial_port *wport = serial->port[1];
  270. dbg("%s", __func__);
  271. usb_kill_urb(wport->write_urb);
  272. }
  273. static void omninet_release(struct usb_serial *serial)
  274. {
  275. struct usb_serial_port *port = serial->port[0];
  276. dbg("%s", __func__);
  277. kfree(usb_get_serial_port_data(port));
  278. }
  279. static int __init omninet_init(void)
  280. {
  281. int retval;
  282. retval = usb_serial_register(&zyxel_omninet_device);
  283. if (retval)
  284. goto failed_usb_serial_register;
  285. retval = usb_register(&omninet_driver);
  286. if (retval)
  287. goto failed_usb_register;
  288. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  289. DRIVER_DESC "\n");
  290. return 0;
  291. failed_usb_register:
  292. usb_serial_deregister(&zyxel_omninet_device);
  293. failed_usb_serial_register:
  294. return retval;
  295. }
  296. static void __exit omninet_exit(void)
  297. {
  298. usb_deregister(&omninet_driver);
  299. usb_serial_deregister(&zyxel_omninet_device);
  300. }
  301. module_init(omninet_init);
  302. module_exit(omninet_exit);
  303. MODULE_AUTHOR(DRIVER_AUTHOR);
  304. MODULE_DESCRIPTION(DRIVER_DESC);
  305. MODULE_LICENSE("GPL");
  306. module_param(debug, bool, S_IRUGO | S_IWUSR);
  307. MODULE_PARM_DESC(debug, "Debug enabled or not");