omninet.c 10 KB

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