omninet.c 10 KB

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