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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * USB Serial Converter driver
  3.  *
  4.  * Copyright (C) 1999 - 2002
  5.  *     Greg Kroah-Hartman (greg@kroah.com)
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 2 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * See Documentation/usb/usb-serial.txt for more information on using this driver
  13.  *
  14.  * (12/03/2001) gkh
  15.  * removed active from the port structure.
  16.  * added documentation to the usb_serial_device_type structure
  17.  *
  18.  * (10/10/2001) gkh
  19.  * added vendor and product to serial structure.  Needed to determine device
  20.  * owner when the device is disconnected.
  21.  *
  22.  * (05/30/2001) gkh
  23.  * added sem to port structure and removed port_lock
  24.  *
  25.  * (10/05/2000) gkh
  26.  * Added interrupt_in_endpointAddress and bulk_in_endpointAddress to help
  27.  * fix bug with urb->dev not being set properly, now that the usb core
  28.  * needs it.
  29.  * 
  30.  * (09/11/2000) gkh
  31.  * Added usb_serial_debug_data function to help get rid of #DEBUG in the
  32.  * drivers.
  33.  *
  34.  * (08/28/2000) gkh
  35.  * Added port_lock to port structure.
  36.  *
  37.  * (08/08/2000) gkh
  38.  * Added open_count to port structure.
  39.  *
  40.  * (07/23/2000) gkh
  41.  * Added bulk_out_endpointAddress to port structure.
  42.  *
  43.  * (07/19/2000) gkh, pberger, and borchers
  44.  * Modifications to allow usb-serial drivers to be modules.
  45.  *
  46.  * 
  47.  */
  48. #ifndef __LINUX_USB_SERIAL_H
  49. #define __LINUX_USB_SERIAL_H
  50. #include <linux/config.h>
  51. #define SERIAL_TTY_MAJOR 188 /* Nice legal number now */
  52. #define SERIAL_TTY_MINORS 255 /* loads of devices :) */
  53. #define MAX_NUM_PORTS 8 /* The maximum number of ports one device can grab at once */
  54. #define USB_SERIAL_MAGIC 0x6702 /* magic number for usb_serial struct */
  55. #define USB_SERIAL_PORT_MAGIC 0x7301 /* magic number for usb_serial_port struct */
  56. /* parity check flag */
  57. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
  58. /**
  59.  * usb_serial_port: structure for the specific ports of a device.
  60.  * @magic: magic number for internal validity of this pointer.
  61.  * @serial: pointer back to the struct usb_serial owner of this port.
  62.  * @tty: pointer to the coresponding tty for this port.
  63.  * @number: the number of the port (the minor number).
  64.  * @interrupt_in_buffer: pointer to the interrupt in buffer for this port.
  65.  * @interrupt_in_urb: pointer to the interrupt in struct urb for this port.
  66.  * @interrupt_in_endpointAddress: endpoint address for the interrupt in pipe
  67.  * for this port.
  68.  * @bulk_in_buffer: pointer to the bulk in buffer for this port.
  69.  * @read_urb: pointer to the bulk in struct urb for this port.
  70.  * @bulk_in_endpointAddress: endpoint address for the bulk in pipe for this
  71.  * port.
  72.  * @bulk_out_buffer: pointer to the bulk out buffer for this port.
  73.  * @bulk_out_size: the size of the bulk_out_buffer, in bytes.
  74.  * @write_urb: pointer to the bulk out struct urb for this port.
  75.  * @bulk_out_endpointAddress: endpoint address for the bulk out pipe for this
  76.  * port.
  77.  * @write_wait: a wait_queue_head_t used by the port.
  78.  * @tqueue: task queue for the line discipline waking up.
  79.  * @open_count: number of times this port has been opened.
  80.  * @sem: struct semaphore used to lock this structure.
  81.  * @private: place to put any driver specific information that is needed.  The
  82.  * usb-serial driver is required to manage this data, the usb-serial core
  83.  * will not touch this.
  84.  *
  85.  * This structure is used by the usb-serial core and drivers for the specific
  86.  * ports of a device.
  87.  */
  88. struct usb_serial_port {
  89. int magic;
  90. struct usb_serial *serial;
  91. struct tty_struct * tty;
  92. unsigned char number;
  93. unsigned char * interrupt_in_buffer;
  94. struct urb * interrupt_in_urb;
  95. __u8 interrupt_in_endpointAddress;
  96. unsigned char * bulk_in_buffer;
  97. struct urb * read_urb;
  98. __u8 bulk_in_endpointAddress;
  99. unsigned char * bulk_out_buffer;
  100. int bulk_out_size;
  101. struct urb * write_urb;
  102. __u8 bulk_out_endpointAddress;
  103. wait_queue_head_t write_wait;
  104. struct tq_struct tqueue;
  105. int open_count;
  106. struct semaphore sem;
  107. void * private;
  108. };
  109. /**
  110.  * usb_serial - structure used by the usb-serial core for a device
  111.  * @magic: magic number for internal validity of this pointer.
  112.  * @dev: pointer to the struct usb_device for this device
  113.  * @type: pointer to the struct usb_serial_device_type for this device
  114.  * @interface: pointer to the struct usb_interface for this device
  115.  * @minor: the starting minor number for this device
  116.  * @num_ports: the number of ports this device has
  117.  * @num_interrupt_in: number of interrupt in endpoints we have
  118.  * @num_bulk_in: number of bulk in endpoints we have
  119.  * @num_bulk_out: number of bulk out endpoints we have
  120.  * @vendor: vendor id of this device
  121.  * @product: product id of this device
  122.  * @port: array of struct usb_serial_port structures for the different ports.
  123.  * @private: place to put any driver specific information that is needed.  The
  124.  * usb-serial driver is required to manage this data, the usb-serial core
  125.  * will not touch this.
  126.  */
  127. struct usb_serial {
  128. int magic;
  129. struct usb_device * dev;
  130. struct usb_serial_device_type * type;
  131. struct usb_interface * interface;
  132. unsigned char minor;
  133. unsigned char num_ports;
  134. char num_interrupt_in;
  135. char num_bulk_in;
  136. char num_bulk_out;
  137. __u16 vendor;
  138. __u16 product;
  139. struct usb_serial_port port[MAX_NUM_PORTS];
  140. void * private;
  141. };
  142. #define NUM_DONT_CARE (-1)
  143. /**
  144.  * usb_serial_device_type - a structure that defines a usb serial device
  145.  * @owner: pointer to the module that owns this device.
  146.  * @name: pointer to a string that describes this device.  This string used
  147.  * in the syslog messages when a device is inserted or removed.
  148.  * @id_table: pointer to a list of usb_device_id structures that define all
  149.  * of the devices this structure can support.
  150.  * @num_interrupt_in: the number of interrupt in endpoints this device will
  151.  * have.
  152.  * @num_bulk_in: the number of bulk in endpoints this device will have.
  153.  * @num_bulk_out: the number of bulk out endpoints this device will have.
  154.  * @num_ports: the number of different ports this device will have.
  155.  * @calc_num_ports: pointer to a function to determine how many ports this
  156.  * device has dynamically.  It will be called after the probe()
  157.  * callback is called, but before attach()
  158.  * @startup: pointer to the driver's startup function.
  159.  * This will be called when the device is inserted into the system,
  160.  * but before the device has been fully initialized by the usb_serial
  161.  * subsystem.  Use this function to download any firmware to the device,
  162.  * or any other early initialization that might be needed.
  163.  * Return 0 to continue on with the initialization sequence.  Anything 
  164.  * else will abort it.
  165.  * @shutdown: pointer to the driver's shutdown function.  This will be
  166.  * called when the device is removed from the system.
  167.  *
  168.  * This structure is defines a USB Serial device.  It provides all of
  169.  * the information that the USB serial core code needs.  If the function
  170.  * pointers are defined, then the USB serial core code will call them when
  171.  * the corresponding tty port functions are called.  If they are not
  172.  * called, the generic serial function will be used instead.
  173.  */
  174. struct usb_serial_device_type {
  175. struct module *owner;
  176. char *name;
  177. const struct usb_device_id *id_table;
  178. char num_interrupt_in;
  179. char num_bulk_in;
  180. char num_bulk_out;
  181. char num_ports;
  182. struct list_head driver_list;
  183. int (*startup) (struct usb_serial *serial);
  184. void (*shutdown) (struct usb_serial *serial);
  185. /* serial function calls */
  186. int  (*open) (struct usb_serial_port *port, struct file * filp);
  187. void (*close) (struct usb_serial_port *port, struct file * filp);
  188. int  (*write) (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count);
  189. int  (*write_room) (struct usb_serial_port *port);
  190. int  (*ioctl) (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
  191. void (*set_termios) (struct usb_serial_port *port, struct termios * old);
  192. void (*break_ctl) (struct usb_serial_port *port, int break_state);
  193. int  (*chars_in_buffer) (struct usb_serial_port *port);
  194. void (*throttle) (struct usb_serial_port *port);
  195. void (*unthrottle) (struct usb_serial_port *port);
  196. void (*read_int_callback)(struct urb *urb);
  197. void (*read_bulk_callback)(struct urb *urb);
  198. void (*write_bulk_callback)(struct urb *urb);
  199. };
  200. extern int  usb_serial_register(struct usb_serial_device_type *new_device);
  201. extern void usb_serial_deregister(struct usb_serial_device_type *device);
  202. /* determine if we should include the EzUSB loader functions */
  203. #undef USES_EZUSB_FUNCTIONS
  204. #if defined(CONFIG_USB_SERIAL_KEYSPAN_PDA) || defined(CONFIG_USB_SERIAL_KEYSPAN_PDA_MODULE)
  205. #define USES_EZUSB_FUNCTIONS
  206. #endif
  207. #if defined(CONFIG_USB_SERIAL_XIRCOM) || defined(CONFIG_USB_SERIAL_XIRCOM_MODULE)
  208. #define USES_EZUSB_FUNCTIONS
  209. #endif
  210. #if defined(CONFIG_USB_SERIAL_KEYSPAN) || defined(CONFIG_USB_SERIAL_KEYSPAN_MODULE)
  211. #define USES_EZUSB_FUNCTIONS
  212. #endif
  213. #if defined(CONFIG_USB_SERIAL_WHITEHEAT) || defined(CONFIG_USB_SERIAL_WHITEHEAT_MODULE)
  214. #define USES_EZUSB_FUNCTIONS
  215. #endif
  216. #ifdef USES_EZUSB_FUNCTIONS
  217. extern int ezusb_writememory (struct usb_serial *serial, int address, unsigned char *data, int length, __u8 bRequest);
  218. extern int ezusb_set_reset (struct usb_serial *serial, unsigned char reset_bit);
  219. #endif
  220. /* Inline functions to check the sanity of a pointer that is passed to us */
  221. static inline int serial_paranoia_check (struct usb_serial *serial, const char *function)
  222. {
  223. if (!serial) {
  224. dbg("%s - serial == NULL", function);
  225. return -1;
  226. }
  227. if (serial->magic != USB_SERIAL_MAGIC) {
  228. dbg("%s - bad magic number for serial", function);
  229. return -1;
  230. }
  231. if (!serial->type) {
  232. dbg("%s - serial->type == NULL!", function);
  233. return -1;
  234. }
  235. return 0;
  236. }
  237. static inline int port_paranoia_check (struct usb_serial_port *port, const char *function)
  238. {
  239. if (!port) {
  240. dbg("%s - port == NULL", function);
  241. return -1;
  242. }
  243. if (port->magic != USB_SERIAL_PORT_MAGIC) {
  244. dbg("%s - bad magic number for port", function);
  245. return -1;
  246. }
  247. if (!port->serial) {
  248. dbg("%s - port->serial == NULL", function);
  249. return -1;
  250. }
  251. if (!port->tty) {
  252. dbg("%s - port->tty == NULL", function);
  253. return -1;
  254. }
  255. return 0;
  256. }
  257. static inline struct usb_serial* get_usb_serial (struct usb_serial_port *port, const char *function) 
  258. /* if no port was specified, or it fails a paranoia check */
  259. if (!port || 
  260. port_paranoia_check (port, function) ||
  261. serial_paranoia_check (port->serial, function)) {
  262. /* then say that we dont have a valid usb_serial thing, which will
  263.  * end up genrating -ENODEV return values */ 
  264. return NULL;
  265. }
  266. return port->serial;
  267. }
  268. static inline void usb_serial_debug_data (const char *file, const char *function, int size, const unsigned char *data)
  269. {
  270. int i;
  271. if (!debug)
  272. return;
  273. printk (KERN_DEBUG "%s: %s - length = %d, data = ", file, function, size);
  274. for (i = 0; i < size; ++i) {
  275. printk ("%.2x ", data[i]);
  276. }
  277. printk ("n");
  278. }
  279. /* Use our own dbg macro */
  280. #undef dbg
  281. #define dbg(format, arg...) do { if (debug) printk(KERN_DEBUG __FILE__ ": " format "n" , ## arg); } while (0)
  282. #endif /* ifdef __LINUX_USB_SERIAL_H */