omninet.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:10k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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/config.h>
  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 <asm/uaccess.h>
  48. #include <linux/usb.h>
  49. #ifdef CONFIG_USB_SERIAL_DEBUG
  50. static int debug = 1;
  51. #else
  52. static int debug;
  53. #endif
  54. #include "usb-serial.h"
  55. /*
  56.  * Version Information
  57.  */
  58. #define DRIVER_VERSION "v1.1"
  59. #define DRIVER_AUTHOR "Anonymous"
  60. #define DRIVER_DESC "USB ZyXEL omni.net LCD PLUS Driver"
  61. #define ZYXEL_VENDOR_ID 0x0586
  62. #define ZYXEL_OMNINET_ID 0x1000
  63. /* function prototypes */
  64. static int  omninet_open (struct usb_serial_port *port, struct file *filp);
  65. static void omninet_close (struct usb_serial_port *port, 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 usb_serial_port *port, int from_user, const unsigned char *buf, int count);
  69. static int  omninet_write_room (struct usb_serial_port *port);
  70. static void omninet_shutdown (struct usb_serial *serial);
  71. static struct usb_device_id id_table [] = {
  72. { USB_DEVICE(ZYXEL_VENDOR_ID, ZYXEL_OMNINET_ID) },
  73. { } /* Terminating entry */
  74. };
  75. MODULE_DEVICE_TABLE (usb, id_table);
  76. static struct usb_serial_device_type zyxel_omninet_device = {
  77. .owner = THIS_MODULE,
  78. .name = "ZyXEL - omni.net lcd plus usb",
  79. .id_table = id_table,
  80. .num_interrupt_in = 1,
  81. .num_bulk_in = 1,
  82. .num_bulk_out = 2,
  83. .num_ports = 1,
  84. .open = omninet_open,
  85. .close = omninet_close,
  86. .write = omninet_write,
  87. .write_room = omninet_write_room,
  88. .read_bulk_callback = omninet_read_bulk_callback,
  89. .write_bulk_callback = omninet_write_bulk_callback,
  90. .shutdown = omninet_shutdown,
  91. };
  92. /* The protocol.
  93.  *
  94.  * The omni.net always exchange 64 bytes of data with the host. The first
  95.  * four bytes are the control header, you can see it in the above structure.
  96.  *
  97.  * oh_seq is a sequence number. Don't know if/how it's used.
  98.  * oh_len is the length of the data bytes in the packet.
  99.  * oh_xxx Bit-mapped, related to handshaking and status info.
  100.  * I normally set it to 0x03 in trasmitted frames.
  101.  * 7: Active when the TA is in a CONNECTed state.
  102.  * 6: unknown
  103.  * 5: handshaking, unknown
  104.  * 4: handshaking, unknown
  105.  * 3: unknown, usually 0
  106.  * 2: unknown, usually 0
  107.  * 1: handshaking, unknown, usually set to 1 in trasmitted frames
  108.  * 0: handshaking, unknown, usually set to 1 in trasmitted frames
  109.  * oh_pad Probably a pad byte.
  110.  *
  111.  * After the header you will find data bytes if oh_len was greater than zero.
  112.  *
  113.  */
  114. struct omninet_header
  115. {
  116. __u8 oh_seq;
  117. __u8 oh_len;
  118. __u8 oh_xxx;
  119. __u8 oh_pad;
  120. };
  121. struct omninet_data
  122. {
  123. __u8 od_outseq; // Sequence number for bulk_out URBs
  124. };
  125. static int omninet_open (struct usb_serial_port *port, struct file *filp)
  126. {
  127. struct usb_serial *serial;
  128. struct usb_serial_port *wport;
  129. struct omninet_data *od;
  130. int result = 0;
  131. if (port_paranoia_check (port, __FUNCTION__))
  132. return -ENODEV;
  133. dbg("%s - port %d", __FUNCTION__, port->number);
  134. serial = get_usb_serial (port, __FUNCTION__);
  135. if (!serial)
  136. return -ENODEV;
  137. od = kmalloc( sizeof(struct omninet_data), GFP_KERNEL );
  138. if( !od ) {
  139. err("%s- kmalloc(%Zd) failed.", __FUNCTION__, sizeof(struct omninet_data));
  140. return -ENOMEM;
  141. }
  142. port->private = od;
  143. wport = &serial->port[1];
  144. wport->tty = port->tty;
  145. /* Start reading from the device */
  146. FILL_BULK_URB(port->read_urb, serial->dev, 
  147.       usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
  148.       port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
  149.       omninet_read_bulk_callback, port);
  150. result = usb_submit_urb(port->read_urb);
  151. if (result)
  152. err("%s - failed submitting read urb, error %d", __FUNCTION__, result);
  153. return result;
  154. }
  155. static void omninet_close (struct usb_serial_port *port, struct file * filp)
  156. {
  157. struct usb_serial  *serial;
  158. struct usb_serial_port  *wport;
  159. struct omninet_data  *od;
  160. if (port_paranoia_check (port, __FUNCTION__))
  161. return;
  162. dbg("%s - port %d", __FUNCTION__, port->number);
  163. serial = get_usb_serial (port, __FUNCTION__);
  164. if (!serial)
  165. return;
  166. if (serial->dev) {
  167. wport = &serial->port[1];
  168. usb_unlink_urb (wport->write_urb);
  169. usb_unlink_urb (port->read_urb);
  170. }
  171. od = (struct omninet_data *)port->private;
  172. if (od)
  173. kfree(od);
  174. }
  175. #define OMNINET_DATAOFFSET 0x04
  176. #define OMNINET_HEADERLEN sizeof(struct omninet_header)
  177. #define OMNINET_BULKOUTSIZE  (64 - OMNINET_HEADERLEN)
  178. static void omninet_read_bulk_callback (struct urb *urb)
  179. {
  180. struct usb_serial_port  *port  = (struct usb_serial_port *)urb->context;
  181. struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
  182. unsigned char  *data  = urb->transfer_buffer;
  183. struct omninet_header  *header = (struct omninet_header *) &data[0];
  184. int i;
  185. int result;
  186. // dbg("omninet_read_bulk_callback");
  187. if (!serial) {
  188. dbg("%s - bad serial pointer, exiting", __FUNCTION__);
  189. return;
  190. }
  191. if (urb->status) {
  192. dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
  193. return;
  194. }
  195. if ((debug) && (header->oh_xxx != 0x30)) {
  196. if (urb->actual_length) {
  197. printk (KERN_DEBUG __FILE__ ": omninet_read %d: ", header->oh_len);
  198. for (i = 0; i < (header->oh_len + OMNINET_HEADERLEN); i++) {
  199. printk ("%.2x ", data[i]);
  200. }
  201. printk ("n");
  202. }
  203. }
  204. if (urb->actual_length && header->oh_len) {
  205. for (i = 0; i < header->oh_len; i++) {
  206.  tty_insert_flip_char(port->tty, data[OMNINET_DATAOFFSET + i], 0);
  207.    }
  208.    tty_flip_buffer_push(port->tty);
  209. }
  210. /* Continue trying to always read  */
  211. FILL_BULK_URB(urb, serial->dev, 
  212.       usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
  213.       urb->transfer_buffer, urb->transfer_buffer_length,
  214.       omninet_read_bulk_callback, port);
  215. result = usb_submit_urb(urb);
  216. if (result)
  217. err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
  218. return;
  219. }
  220. static int omninet_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
  221. {
  222. struct usb_serial  *serial = port->serial;
  223. struct usb_serial_port  *wport = &serial->port[1];
  224. struct omninet_data  *od  = (struct omninet_data   *) port->private;
  225. struct omninet_header *header = (struct omninet_header *) wport->write_urb->transfer_buffer;
  226. int result;
  227. // dbg("omninet_write port %d", port->number);
  228. if (count == 0) {
  229. dbg("%s - write request of 0 bytes", __FUNCTION__);
  230. return (0);
  231. }
  232. if (wport->write_urb->status == -EINPROGRESS) {
  233. dbg("%s - already writing", __FUNCTION__);
  234. return (0);
  235. }
  236. count = (count > OMNINET_BULKOUTSIZE) ? OMNINET_BULKOUTSIZE : count;
  237. if (from_user) {
  238. if (copy_from_user(wport->write_urb->transfer_buffer + OMNINET_DATAOFFSET, buf, count) != 0) {
  239. result = -EFAULT;
  240. goto exit;
  241. }
  242. }
  243. else {
  244. memcpy (wport->write_urb->transfer_buffer + OMNINET_DATAOFFSET, buf, count);
  245. }
  246. usb_serial_debug_data (__FILE__, __FUNCTION__, count, 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);
  255. if (result)
  256. err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
  257. else
  258. result = count;
  259. exit:
  260. return result;
  261. }
  262. static int omninet_write_room (struct usb_serial_port *port)
  263. {
  264. struct usb_serial  *serial = port->serial;
  265. struct usb_serial_port  *wport  = &serial->port[1];
  266. int room = 0; // Default: no room
  267. if (wport->write_urb->status != -EINPROGRESS)
  268. room = wport->bulk_out_size - OMNINET_HEADERLEN;
  269. // dbg("omninet_write_room returns %d", room);
  270. return (room);
  271. }
  272. static void omninet_write_bulk_callback (struct urb *urb)
  273. {
  274. /* struct omninet_header *header = (struct omninet_header  *) urb->transfer_buffer; */
  275. struct usb_serial_port  *port   = (struct usb_serial_port *) urb->context;
  276. struct usb_serial  *serial;
  277. // dbg("omninet_write_bulk_callback, port %0xn", port);
  278. if (port_paranoia_check (port, __FUNCTION__)) {
  279. return;
  280. }
  281. serial = port->serial;
  282. if (serial_paranoia_check (serial, __FUNCTION__)) {
  283. return;
  284. }
  285. if (urb->status) {
  286. dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
  287. return;
  288. }
  289. queue_task(&port->tqueue, &tq_immediate);
  290. mark_bh(IMMEDIATE_BH);
  291. // dbg("omninet_write_bulk_callback, tty %0xn", tty);
  292. return;
  293. }
  294. static void omninet_shutdown (struct usb_serial *serial)
  295. {
  296. dbg ("%s", __FUNCTION__);
  297. }
  298. static int __init omninet_init (void)
  299. {
  300. usb_serial_register (&zyxel_omninet_device);
  301. info(DRIVER_VERSION ":" DRIVER_DESC);
  302. return 0;
  303. }
  304. static void __exit omninet_exit (void)
  305. {
  306. usb_serial_deregister (&zyxel_omninet_device);
  307. }
  308. module_init(omninet_init);
  309. module_exit(omninet_exit);
  310. MODULE_AUTHOR( DRIVER_AUTHOR );
  311. MODULE_DESCRIPTION( DRIVER_DESC );
  312. MODULE_LICENSE("GPL");
  313. MODULE_PARM(debug, "i");
  314. MODULE_PARM_DESC(debug, "Debug enabled or not");