omninet.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 usb_serial_port *port);
  65. static void omninet_read_bulk_callback(struct urb *urb);
  66. static void omninet_write_bulk_callback(struct urb *urb);
  67. static int omninet_write(struct tty_struct *tty, struct usb_serial_port *port,
  68. const unsigned char *buf, int count);
  69. static int omninet_write_room(struct tty_struct *tty);
  70. static void omninet_disconnect(struct usb_serial *serial);
  71. static void omninet_release(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. .disconnect = omninet_disconnect,
  103. .release = omninet_release,
  104. };
  105. /* The protocol.
  106. *
  107. * The omni.net always exchange 64 bytes of data with the host. The first
  108. * four bytes are the control header, you can see it in the above structure.
  109. *
  110. * oh_seq is a sequence number. Don't know if/how it's used.
  111. * oh_len is the length of the data bytes in the packet.
  112. * oh_xxx Bit-mapped, related to handshaking and status info.
  113. * I normally set it to 0x03 in trasmitted frames.
  114. * 7: Active when the TA is in a CONNECTed state.
  115. * 6: unknown
  116. * 5: handshaking, unknown
  117. * 4: handshaking, unknown
  118. * 3: unknown, usually 0
  119. * 2: unknown, usually 0
  120. * 1: handshaking, unknown, usually set to 1 in trasmitted frames
  121. * 0: handshaking, unknown, usually set to 1 in trasmitted frames
  122. * oh_pad Probably a pad byte.
  123. *
  124. * After the header you will find data bytes if oh_len was greater than zero.
  125. *
  126. */
  127. struct omninet_header {
  128. __u8 oh_seq;
  129. __u8 oh_len;
  130. __u8 oh_xxx;
  131. __u8 oh_pad;
  132. };
  133. struct omninet_data {
  134. __u8 od_outseq; /* Sequence number for bulk_out URBs */
  135. };
  136. static int omninet_attach(struct usb_serial *serial)
  137. {
  138. struct omninet_data *od;
  139. struct usb_serial_port *port = serial->port[0];
  140. od = kmalloc(sizeof(struct omninet_data), GFP_KERNEL);
  141. if (!od) {
  142. dev_err(&port->dev, "%s- kmalloc(%Zd) failed.\n",
  143. __func__, sizeof(struct omninet_data));
  144. return -ENOMEM;
  145. }
  146. usb_set_serial_port_data(port, od);
  147. return 0;
  148. }
  149. static int omninet_open(struct tty_struct *tty,
  150. struct usb_serial_port *port, struct file *filp)
  151. {
  152. struct usb_serial *serial = port->serial;
  153. struct usb_serial_port *wport;
  154. int result = 0;
  155. dbg("%s - port %d", __func__, port->number);
  156. wport = serial->port[1];
  157. tty_port_tty_set(&wport->port, tty);
  158. /* Start reading from the device */
  159. usb_fill_bulk_urb(port->read_urb, serial->dev,
  160. usb_rcvbulkpipe(serial->dev,
  161. port->bulk_in_endpointAddress),
  162. port->read_urb->transfer_buffer,
  163. port->read_urb->transfer_buffer_length,
  164. omninet_read_bulk_callback, port);
  165. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  166. if (result)
  167. dev_err(&port->dev,
  168. "%s - failed submitting read urb, error %d\n",
  169. __func__, result);
  170. return result;
  171. }
  172. static void omninet_close(struct usb_serial_port *port)
  173. {
  174. dbg("%s - port %d", __func__, port->number);
  175. usb_kill_urb(port->read_urb);
  176. }
  177. #define OMNINET_DATAOFFSET 0x04
  178. #define OMNINET_HEADERLEN sizeof(struct omninet_header)
  179. #define OMNINET_BULKOUTSIZE (64 - OMNINET_HEADERLEN)
  180. static void omninet_read_bulk_callback(struct urb *urb)
  181. {
  182. struct usb_serial_port *port = urb->context;
  183. unsigned char *data = urb->transfer_buffer;
  184. struct omninet_header *header = (struct omninet_header *) &data[0];
  185. int status = urb->status;
  186. int result;
  187. int i;
  188. dbg("%s - port %d", __func__, port->number);
  189. if (status) {
  190. dbg("%s - nonzero read bulk status received: %d",
  191. __func__, status);
  192. return;
  193. }
  194. if (debug && header->oh_xxx != 0x30) {
  195. if (urb->actual_length) {
  196. printk(KERN_DEBUG __FILE__
  197. ": omninet_read %d: ", header->oh_len);
  198. for (i = 0; i < (header->oh_len +
  199. OMNINET_HEADERLEN); i++)
  200. printk("%.2x ", data[i]);
  201. printk("\n");
  202. }
  203. }
  204. if (urb->actual_length && header->oh_len) {
  205. struct tty_struct *tty = tty_port_tty_get(&port->port);
  206. tty_insert_flip_string(tty, data + OMNINET_DATAOFFSET,
  207. header->oh_len);
  208. tty_flip_buffer_push(tty);
  209. tty_kref_put(tty);
  210. }
  211. /* Continue trying to always read */
  212. usb_fill_bulk_urb(urb, port->serial->dev,
  213. usb_rcvbulkpipe(port->serial->dev,
  214. port->bulk_in_endpointAddress),
  215. urb->transfer_buffer, urb->transfer_buffer_length,
  216. omninet_read_bulk_callback, port);
  217. result = usb_submit_urb(urb, GFP_ATOMIC);
  218. if (result)
  219. dev_err(&port->dev,
  220. "%s - failed resubmitting read urb, error %d\n",
  221. __func__, result);
  222. return;
  223. }
  224. static int omninet_write(struct tty_struct *tty, struct usb_serial_port *port,
  225. const unsigned char *buf, int count)
  226. {
  227. struct usb_serial *serial = port->serial;
  228. struct usb_serial_port *wport = serial->port[1];
  229. struct omninet_data *od = usb_get_serial_port_data(port);
  230. struct omninet_header *header = (struct omninet_header *)
  231. wport->write_urb->transfer_buffer;
  232. int result;
  233. dbg("%s - port %d", __func__, port->number);
  234. if (count == 0) {
  235. dbg("%s - write request of 0 bytes", __func__);
  236. return 0;
  237. }
  238. spin_lock_bh(&wport->lock);
  239. if (wport->write_urb_busy) {
  240. spin_unlock_bh(&wport->lock);
  241. dbg("%s - already writing", __func__);
  242. return 0;
  243. }
  244. wport->write_urb_busy = 1;
  245. spin_unlock_bh(&wport->lock);
  246. count = (count > OMNINET_BULKOUTSIZE) ? OMNINET_BULKOUTSIZE : count;
  247. memcpy(wport->write_urb->transfer_buffer + OMNINET_DATAOFFSET,
  248. buf, count);
  249. usb_serial_debug_data(debug, &port->dev, __func__, count,
  250. wport->write_urb->transfer_buffer);
  251. header->oh_seq = od->od_outseq++;
  252. header->oh_len = count;
  253. header->oh_xxx = 0x03;
  254. header->oh_pad = 0x00;
  255. /* send the data out the bulk port, always 64 bytes */
  256. wport->write_urb->transfer_buffer_length = 64;
  257. wport->write_urb->dev = serial->dev;
  258. result = usb_submit_urb(wport->write_urb, GFP_ATOMIC);
  259. if (result) {
  260. wport->write_urb_busy = 0;
  261. dev_err(&port->dev,
  262. "%s - failed submitting write urb, error %d\n",
  263. __func__, result);
  264. } else
  265. result = count;
  266. return result;
  267. }
  268. static int omninet_write_room(struct tty_struct *tty)
  269. {
  270. struct usb_serial_port *port = tty->driver_data;
  271. struct usb_serial *serial = port->serial;
  272. struct usb_serial_port *wport = serial->port[1];
  273. int room = 0; /* Default: no room */
  274. /* FIXME: no consistent locking for write_urb_busy */
  275. if (wport->write_urb_busy)
  276. room = wport->bulk_out_size - OMNINET_HEADERLEN;
  277. dbg("%s - returns %d", __func__, room);
  278. return room;
  279. }
  280. static void omninet_write_bulk_callback(struct urb *urb)
  281. {
  282. /* struct omninet_header *header = (struct omninet_header *)
  283. urb->transfer_buffer; */
  284. struct usb_serial_port *port = urb->context;
  285. int status = urb->status;
  286. dbg("%s - port %0x\n", __func__, port->number);
  287. port->write_urb_busy = 0;
  288. if (status) {
  289. dbg("%s - nonzero write bulk status received: %d",
  290. __func__, status);
  291. return;
  292. }
  293. usb_serial_port_softint(port);
  294. }
  295. static void omninet_disconnect(struct usb_serial *serial)
  296. {
  297. struct usb_serial_port *wport = serial->port[1];
  298. dbg("%s", __func__);
  299. usb_kill_urb(wport->write_urb);
  300. }
  301. static void omninet_release(struct usb_serial *serial)
  302. {
  303. struct usb_serial_port *port = serial->port[0];
  304. dbg("%s", __func__);
  305. kfree(usb_get_serial_port_data(port));
  306. }
  307. static int __init omninet_init(void)
  308. {
  309. int retval;
  310. retval = usb_serial_register(&zyxel_omninet_device);
  311. if (retval)
  312. goto failed_usb_serial_register;
  313. retval = usb_register(&omninet_driver);
  314. if (retval)
  315. goto failed_usb_register;
  316. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  317. DRIVER_DESC "\n");
  318. return 0;
  319. failed_usb_register:
  320. usb_serial_deregister(&zyxel_omninet_device);
  321. failed_usb_serial_register:
  322. return retval;
  323. }
  324. static void __exit omninet_exit(void)
  325. {
  326. usb_deregister(&omninet_driver);
  327. usb_serial_deregister(&zyxel_omninet_device);
  328. }
  329. module_init(omninet_init);
  330. module_exit(omninet_exit);
  331. MODULE_AUTHOR(DRIVER_AUTHOR);
  332. MODULE_DESCRIPTION(DRIVER_DESC);
  333. MODULE_LICENSE("GPL");
  334. module_param(debug, bool, S_IRUGO | S_IWUSR);
  335. MODULE_PARM_DESC(debug, "Debug enabled or not");