omninet.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * USB ZyXEL omni.net LCD PLUS driver
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * See Documentation/usb/usb-serial.txt for more information on using this driver
  10. *
  11. * Please report both successes and troubles to the author at omninet@kroah.com
  12. *
  13. * (05/30/2001) gkh
  14. * switched from using spinlock to a semaphore, which fixes lots of problems.
  15. *
  16. * (04/08/2001) gb
  17. * Identify version on module load.
  18. *
  19. * (11/01/2000) Adam J. Richter
  20. * usb_device_id table support
  21. *
  22. * (10/05/2000) gkh
  23. * Fixed bug with urb->dev not being set properly, now that the usb
  24. * core needs it.
  25. *
  26. * (08/28/2000) gkh
  27. * Added locks for SMP safeness.
  28. * Fixed MOD_INC and MOD_DEC logic and the ability to open a port more
  29. * than once.
  30. * Fixed potential race in omninet_write_bulk_callback
  31. *
  32. * (07/19/2000) gkh
  33. * Added module_init and module_exit functions to handle the fact that this
  34. * driver is a loadable module now.
  35. *
  36. */
  37. #include <linux/kernel.h>
  38. #include <linux/errno.h>
  39. #include <linux/init.h>
  40. #include <linux/slab.h>
  41. #include <linux/tty.h>
  42. #include <linux/tty_driver.h>
  43. #include <linux/tty_flip.h>
  44. #include <linux/module.h>
  45. #include <linux/spinlock.h>
  46. #include <asm/uaccess.h>
  47. #include <linux/usb.h>
  48. #include <linux/usb/serial.h>
  49. static int debug;
  50. /*
  51. * Version Information
  52. */
  53. #define DRIVER_VERSION "v1.1"
  54. #define DRIVER_AUTHOR "Alessandro Zummo"
  55. #define DRIVER_DESC "USB ZyXEL omni.net LCD PLUS Driver"
  56. #define ZYXEL_VENDOR_ID 0x0586
  57. #define ZYXEL_OMNINET_ID 0x1000
  58. #define BT_IGNITIONPRO_ID 0x2000 /* This one seems to be a re-branded ZyXEL device */
  59. /* function prototypes */
  60. static int omninet_open (struct usb_serial_port *port, struct file *filp);
  61. static void omninet_close (struct usb_serial_port *port, struct file *filp);
  62. static void omninet_read_bulk_callback (struct urb *urb);
  63. static void omninet_write_bulk_callback (struct urb *urb);
  64. static int omninet_write (struct usb_serial_port *port, const unsigned char *buf, int count);
  65. static int omninet_write_room (struct usb_serial_port *port);
  66. static void omninet_shutdown (struct usb_serial *serial);
  67. static int omninet_attach (struct usb_serial *serial);
  68. static struct usb_device_id id_table [] = {
  69. { USB_DEVICE(ZYXEL_VENDOR_ID, ZYXEL_OMNINET_ID) },
  70. { USB_DEVICE(ZYXEL_VENDOR_ID, BT_IGNITIONPRO_ID) },
  71. { } /* Terminating entry */
  72. };
  73. MODULE_DEVICE_TABLE (usb, id_table);
  74. static struct usb_driver omninet_driver = {
  75. .name = "omninet",
  76. .probe = usb_serial_probe,
  77. .disconnect = usb_serial_disconnect,
  78. .id_table = id_table,
  79. .no_dynamic_id = 1,
  80. };
  81. static struct usb_serial_driver zyxel_omninet_device = {
  82. .driver = {
  83. .owner = THIS_MODULE,
  84. .name = "omninet",
  85. },
  86. .description = "ZyXEL - omni.net lcd plus usb",
  87. .usb_driver = &omninet_driver,
  88. .id_table = id_table,
  89. .num_interrupt_in = 1,
  90. .num_bulk_in = 1,
  91. .num_bulk_out = 2,
  92. .num_ports = 1,
  93. .attach = omninet_attach,
  94. .open = omninet_open,
  95. .close = omninet_close,
  96. .write = omninet_write,
  97. .write_room = omninet_write_room,
  98. .read_bulk_callback = omninet_read_bulk_callback,
  99. .write_bulk_callback = omninet_write_bulk_callback,
  100. .shutdown = omninet_shutdown,
  101. };
  102. /* The protocol.
  103. *
  104. * The omni.net always exchange 64 bytes of data with the host. The first
  105. * four bytes are the control header, you can see it in the above structure.
  106. *
  107. * oh_seq is a sequence number. Don't know if/how it's used.
  108. * oh_len is the length of the data bytes in the packet.
  109. * oh_xxx Bit-mapped, related to handshaking and status info.
  110. * I normally set it to 0x03 in trasmitted frames.
  111. * 7: Active when the TA is in a CONNECTed state.
  112. * 6: unknown
  113. * 5: handshaking, unknown
  114. * 4: handshaking, unknown
  115. * 3: unknown, usually 0
  116. * 2: unknown, usually 0
  117. * 1: handshaking, unknown, usually set to 1 in trasmitted frames
  118. * 0: handshaking, unknown, usually set to 1 in trasmitted frames
  119. * oh_pad Probably a pad byte.
  120. *
  121. * After the header you will find data bytes if oh_len was greater than zero.
  122. *
  123. */
  124. struct omninet_header
  125. {
  126. __u8 oh_seq;
  127. __u8 oh_len;
  128. __u8 oh_xxx;
  129. __u8 oh_pad;
  130. };
  131. struct omninet_data
  132. {
  133. __u8 od_outseq; // Sequence number for bulk_out URBs
  134. };
  135. static int omninet_attach (struct usb_serial *serial)
  136. {
  137. struct omninet_data *od;
  138. struct usb_serial_port *port = serial->port[0];
  139. od = kmalloc( sizeof(struct omninet_data), GFP_KERNEL );
  140. if( !od ) {
  141. err("%s- kmalloc(%Zd) failed.", __FUNCTION__, sizeof(struct omninet_data));
  142. return -ENOMEM;
  143. }
  144. usb_set_serial_port_data(port, od);
  145. return 0;
  146. }
  147. static int omninet_open (struct usb_serial_port *port, struct file *filp)
  148. {
  149. struct usb_serial *serial = port->serial;
  150. struct usb_serial_port *wport;
  151. int result = 0;
  152. dbg("%s - port %d", __FUNCTION__, port->number);
  153. wport = serial->port[1];
  154. wport->tty = port->tty;
  155. /* Start reading from the device */
  156. usb_fill_bulk_urb(port->read_urb, serial->dev,
  157. usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
  158. port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
  159. omninet_read_bulk_callback, port);
  160. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  161. if (result) {
  162. err("%s - failed submitting read urb, error %d", __FUNCTION__, result);
  163. }
  164. return result;
  165. }
  166. static void omninet_close (struct usb_serial_port *port, struct file * filp)
  167. {
  168. dbg("%s - port %d", __FUNCTION__, port->number);
  169. usb_kill_urb(port->read_urb);
  170. }
  171. #define OMNINET_DATAOFFSET 0x04
  172. #define OMNINET_HEADERLEN sizeof(struct omninet_header)
  173. #define OMNINET_BULKOUTSIZE (64 - OMNINET_HEADERLEN)
  174. static void omninet_read_bulk_callback (struct urb *urb)
  175. {
  176. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  177. unsigned char *data = urb->transfer_buffer;
  178. struct omninet_header *header = (struct omninet_header *) &data[0];
  179. int i;
  180. int result;
  181. dbg("%s - port %d", __FUNCTION__, port->number);
  182. if (urb->status) {
  183. dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
  184. return;
  185. }
  186. if ((debug) && (header->oh_xxx != 0x30)) {
  187. if (urb->actual_length) {
  188. printk (KERN_DEBUG __FILE__ ": omninet_read %d: ", header->oh_len);
  189. for (i = 0; i < (header->oh_len + OMNINET_HEADERLEN); i++) {
  190. printk ("%.2x ", data[i]);
  191. }
  192. printk ("\n");
  193. }
  194. }
  195. if (urb->actual_length && header->oh_len) {
  196. for (i = 0; i < header->oh_len; i++) {
  197. tty_insert_flip_char(port->tty, data[OMNINET_DATAOFFSET + i], 0);
  198. }
  199. tty_flip_buffer_push(port->tty);
  200. }
  201. /* Continue trying to always read */
  202. usb_fill_bulk_urb(urb, port->serial->dev,
  203. usb_rcvbulkpipe(port->serial->dev, port->bulk_in_endpointAddress),
  204. urb->transfer_buffer, urb->transfer_buffer_length,
  205. omninet_read_bulk_callback, port);
  206. result = usb_submit_urb(urb, GFP_ATOMIC);
  207. if (result)
  208. err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
  209. return;
  210. }
  211. static int omninet_write (struct usb_serial_port *port, const unsigned char *buf, int count)
  212. {
  213. struct usb_serial *serial = port->serial;
  214. struct usb_serial_port *wport = serial->port[1];
  215. struct omninet_data *od = usb_get_serial_port_data(port);
  216. struct omninet_header *header = (struct omninet_header *) wport->write_urb->transfer_buffer;
  217. int result;
  218. dbg("%s - port %d", __FUNCTION__, port->number);
  219. if (count == 0) {
  220. dbg("%s - write request of 0 bytes", __FUNCTION__);
  221. return (0);
  222. }
  223. spin_lock_bh(&wport->lock);
  224. if (wport->write_urb_busy) {
  225. spin_unlock_bh(&wport->lock);
  226. dbg("%s - already writing", __FUNCTION__);
  227. return 0;
  228. }
  229. wport->write_urb_busy = 1;
  230. spin_unlock_bh(&wport->lock);
  231. count = (count > OMNINET_BULKOUTSIZE) ? OMNINET_BULKOUTSIZE : count;
  232. memcpy (wport->write_urb->transfer_buffer + OMNINET_DATAOFFSET, buf, count);
  233. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, wport->write_urb->transfer_buffer);
  234. header->oh_seq = od->od_outseq++;
  235. header->oh_len = count;
  236. header->oh_xxx = 0x03;
  237. header->oh_pad = 0x00;
  238. /* send the data out the bulk port, always 64 bytes */
  239. wport->write_urb->transfer_buffer_length = 64;
  240. wport->write_urb->dev = serial->dev;
  241. result = usb_submit_urb(wport->write_urb, GFP_ATOMIC);
  242. if (result) {
  243. wport->write_urb_busy = 0;
  244. err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
  245. } else
  246. result = count;
  247. return result;
  248. }
  249. static int omninet_write_room (struct usb_serial_port *port)
  250. {
  251. struct usb_serial *serial = port->serial;
  252. struct usb_serial_port *wport = serial->port[1];
  253. int room = 0; // Default: no room
  254. if (wport->write_urb_busy)
  255. room = wport->bulk_out_size - OMNINET_HEADERLEN;
  256. dbg("%s - returns %d", __FUNCTION__, room);
  257. return (room);
  258. }
  259. static void omninet_write_bulk_callback (struct urb *urb)
  260. {
  261. /* struct omninet_header *header = (struct omninet_header *) urb->transfer_buffer; */
  262. struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
  263. dbg("%s - port %0x\n", __FUNCTION__, port->number);
  264. port->write_urb_busy = 0;
  265. if (urb->status) {
  266. dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
  267. return;
  268. }
  269. usb_serial_port_softint(port);
  270. }
  271. static void omninet_shutdown (struct usb_serial *serial)
  272. {
  273. struct usb_serial_port *wport = serial->port[1];
  274. struct usb_serial_port *port = serial->port[0];
  275. dbg ("%s", __FUNCTION__);
  276. usb_kill_urb(wport->write_urb);
  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. info(DRIVER_VERSION ":" DRIVER_DESC);
  289. return 0;
  290. failed_usb_register:
  291. usb_serial_deregister(&zyxel_omninet_device);
  292. failed_usb_serial_register:
  293. return retval;
  294. }
  295. static void __exit omninet_exit (void)
  296. {
  297. usb_deregister (&omninet_driver);
  298. usb_serial_deregister (&zyxel_omninet_device);
  299. }
  300. module_init(omninet_init);
  301. module_exit(omninet_exit);
  302. MODULE_AUTHOR( DRIVER_AUTHOR );
  303. MODULE_DESCRIPTION( DRIVER_DESC );
  304. MODULE_LICENSE("GPL");
  305. module_param(debug, bool, S_IRUGO | S_IWUSR);
  306. MODULE_PARM_DESC(debug, "Debug enabled or not");