serial.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * USB Serial Converter stuff
  3. *
  4. * Copyright (C) 1999 - 2005
  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; version 2 of the License.
  10. *
  11. */
  12. #ifndef __LINUX_USB_SERIAL_H
  13. #define __LINUX_USB_SERIAL_H
  14. #include <linux/kref.h>
  15. #include <linux/mutex.h>
  16. #include <linux/sysrq.h>
  17. #define SERIAL_TTY_MAJOR 188 /* Nice legal number now */
  18. #define SERIAL_TTY_MINORS 254 /* loads of devices :) */
  19. #define SERIAL_TTY_NO_MINOR 255 /* No minor was assigned */
  20. /* The maximum number of ports one device can grab at once */
  21. #define MAX_NUM_PORTS 8
  22. /* parity check flag */
  23. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
  24. enum port_dev_state {
  25. PORT_UNREGISTERED,
  26. PORT_REGISTERING,
  27. PORT_REGISTERED,
  28. PORT_UNREGISTERING,
  29. };
  30. /**
  31. * usb_serial_port: structure for the specific ports of a device.
  32. * @serial: pointer back to the struct usb_serial owner of this port.
  33. * @port: pointer to the corresponding tty_port for this port.
  34. * @lock: spinlock to grab when updating portions of this structure.
  35. * @mutex: mutex used to synchronize serial_open() and serial_close()
  36. * access for this port.
  37. * @number: the number of the port (the minor number).
  38. * @interrupt_in_buffer: pointer to the interrupt in buffer for this port.
  39. * @interrupt_in_urb: pointer to the interrupt in struct urb for this port.
  40. * @interrupt_in_endpointAddress: endpoint address for the interrupt in pipe
  41. * for this port.
  42. * @interrupt_out_buffer: pointer to the interrupt out buffer for this port.
  43. * @interrupt_out_size: the size of the interrupt_out_buffer, in bytes.
  44. * @interrupt_out_urb: pointer to the interrupt out struct urb for this port.
  45. * @interrupt_out_endpointAddress: endpoint address for the interrupt out pipe
  46. * for this port.
  47. * @bulk_in_buffer: pointer to the bulk in buffer for this port.
  48. * @bulk_in_size: the size of the bulk_in_buffer, in bytes.
  49. * @read_urb: pointer to the bulk in struct urb for this port.
  50. * @bulk_in_endpointAddress: endpoint address for the bulk in pipe for this
  51. * port.
  52. * @bulk_out_buffer: pointer to the bulk out buffer for this port.
  53. * @bulk_out_size: the size of the bulk_out_buffer, in bytes.
  54. * @write_urb: pointer to the bulk out struct urb for this port.
  55. * @write_fifo: kfifo used to buffer outgoing data
  56. * @write_urb_busy: port`s writing status
  57. * @bulk_out_endpointAddress: endpoint address for the bulk out pipe for this
  58. * port.
  59. * @write_wait: a wait_queue_head_t used by the port.
  60. * @work: work queue entry for the line discipline waking up.
  61. * @throttled: nonzero if the read urb is inactive to throttle the device
  62. * @throttle_req: nonzero if the tty wants to throttle us
  63. * @console: attached usb serial console
  64. * @dev: pointer to the serial device
  65. *
  66. * This structure is used by the usb-serial core and drivers for the specific
  67. * ports of a device.
  68. */
  69. struct usb_serial_port {
  70. struct usb_serial *serial;
  71. struct tty_port port;
  72. spinlock_t lock;
  73. struct mutex mutex;
  74. unsigned char number;
  75. unsigned char *interrupt_in_buffer;
  76. struct urb *interrupt_in_urb;
  77. __u8 interrupt_in_endpointAddress;
  78. unsigned char *interrupt_out_buffer;
  79. int interrupt_out_size;
  80. struct urb *interrupt_out_urb;
  81. __u8 interrupt_out_endpointAddress;
  82. unsigned char *bulk_in_buffer;
  83. int bulk_in_size;
  84. struct urb *read_urb;
  85. __u8 bulk_in_endpointAddress;
  86. unsigned char *bulk_out_buffer;
  87. int bulk_out_size;
  88. struct urb *write_urb;
  89. struct kfifo *write_fifo;
  90. int write_urb_busy;
  91. __u8 bulk_out_endpointAddress;
  92. int tx_bytes_flight;
  93. int urbs_in_flight;
  94. wait_queue_head_t write_wait;
  95. struct work_struct work;
  96. char throttled;
  97. char throttle_req;
  98. char console;
  99. unsigned long sysrq; /* sysrq timeout */
  100. struct device dev;
  101. enum port_dev_state dev_state;
  102. };
  103. #define to_usb_serial_port(d) container_of(d, struct usb_serial_port, dev)
  104. /* get and set the port private data pointer helper functions */
  105. static inline void *usb_get_serial_port_data(struct usb_serial_port *port)
  106. {
  107. return dev_get_drvdata(&port->dev);
  108. }
  109. static inline void usb_set_serial_port_data(struct usb_serial_port *port,
  110. void *data)
  111. {
  112. dev_set_drvdata(&port->dev, data);
  113. }
  114. /**
  115. * usb_serial - structure used by the usb-serial core for a device
  116. * @dev: pointer to the struct usb_device for this device
  117. * @type: pointer to the struct usb_serial_driver for this device
  118. * @interface: pointer to the struct usb_interface for this device
  119. * @minor: the starting minor number for this device
  120. * @num_ports: the number of ports this device has
  121. * @num_interrupt_in: number of interrupt in endpoints we have
  122. * @num_interrupt_out: number of interrupt out endpoints we have
  123. * @num_bulk_in: number of bulk in endpoints we have
  124. * @num_bulk_out: number of bulk out endpoints we have
  125. * @port: array of struct usb_serial_port structures for the different ports.
  126. * @private: place to put any driver specific information that is needed. The
  127. * usb-serial driver is required to manage this data, the usb-serial core
  128. * will not touch this. Use usb_get_serial_data() and
  129. * usb_set_serial_data() to access this.
  130. */
  131. struct usb_serial {
  132. struct usb_device *dev;
  133. struct usb_serial_driver *type;
  134. struct usb_interface *interface;
  135. unsigned char disconnected:1;
  136. unsigned char suspending:1;
  137. unsigned char attached:1;
  138. unsigned char minor;
  139. unsigned char num_ports;
  140. unsigned char num_port_pointers;
  141. char num_interrupt_in;
  142. char num_interrupt_out;
  143. char num_bulk_in;
  144. char num_bulk_out;
  145. struct usb_serial_port *port[MAX_NUM_PORTS];
  146. struct kref kref;
  147. struct mutex disc_mutex;
  148. void *private;
  149. };
  150. #define to_usb_serial(d) container_of(d, struct usb_serial, kref)
  151. /* get and set the serial private data pointer helper functions */
  152. static inline void *usb_get_serial_data(struct usb_serial *serial)
  153. {
  154. return serial->private;
  155. }
  156. static inline void usb_set_serial_data(struct usb_serial *serial, void *data)
  157. {
  158. serial->private = data;
  159. }
  160. /**
  161. * usb_serial_driver - describes a usb serial driver
  162. * @description: pointer to a string that describes this driver. This string
  163. * used in the syslog messages when a device is inserted or removed.
  164. * @id_table: pointer to a list of usb_device_id structures that define all
  165. * of the devices this structure can support.
  166. * @num_ports: the number of different ports this device will have.
  167. * @calc_num_ports: pointer to a function to determine how many ports this
  168. * device has dynamically. It will be called after the probe()
  169. * callback is called, but before attach()
  170. * @probe: pointer to the driver's probe function.
  171. * This will be called when the device is inserted into the system,
  172. * but before the device has been fully initialized by the usb_serial
  173. * subsystem. Use this function to download any firmware to the device,
  174. * or any other early initialization that might be needed.
  175. * Return 0 to continue on with the initialization sequence. Anything
  176. * else will abort it.
  177. * @attach: pointer to the driver's attach function.
  178. * This will be called when the struct usb_serial structure is fully set
  179. * set up. Do any local initialization of the device, or any private
  180. * memory structure allocation at this point in time.
  181. * @disconnect: pointer to the driver's disconnect function. This will be
  182. * called when the device is unplugged or unbound from the driver.
  183. * @release: pointer to the driver's release function. This will be called
  184. * when the usb_serial data structure is about to be destroyed.
  185. * @usb_driver: pointer to the struct usb_driver that controls this
  186. * device. This is necessary to allow dynamic ids to be added to
  187. * the driver from sysfs.
  188. *
  189. * This structure is defines a USB Serial driver. It provides all of
  190. * the information that the USB serial core code needs. If the function
  191. * pointers are defined, then the USB serial core code will call them when
  192. * the corresponding tty port functions are called. If they are not
  193. * called, the generic serial function will be used instead.
  194. *
  195. * The driver.owner field should be set to the module owner of this driver.
  196. * The driver.name field should be set to the name of this driver (remember
  197. * it will show up in sysfs, so it needs to be short and to the point.
  198. * Using the module name is a good idea.)
  199. */
  200. struct usb_serial_driver {
  201. const char *description;
  202. const struct usb_device_id *id_table;
  203. char num_ports;
  204. struct list_head driver_list;
  205. struct device_driver driver;
  206. struct usb_driver *usb_driver;
  207. struct usb_dynids dynids;
  208. int max_in_flight_urbs;
  209. int (*probe)(struct usb_serial *serial, const struct usb_device_id *id);
  210. int (*attach)(struct usb_serial *serial);
  211. int (*calc_num_ports) (struct usb_serial *serial);
  212. void (*disconnect)(struct usb_serial *serial);
  213. void (*release)(struct usb_serial *serial);
  214. int (*port_probe)(struct usb_serial_port *port);
  215. int (*port_remove)(struct usb_serial_port *port);
  216. int (*suspend)(struct usb_serial *serial, pm_message_t message);
  217. int (*resume)(struct usb_serial *serial);
  218. /* serial function calls */
  219. /* Called by console and by the tty layer */
  220. int (*open)(struct tty_struct *tty, struct usb_serial_port *port);
  221. void (*close)(struct usb_serial_port *port);
  222. int (*write)(struct tty_struct *tty, struct usb_serial_port *port,
  223. const unsigned char *buf, int count);
  224. /* Called only by the tty layer */
  225. int (*write_room)(struct tty_struct *tty);
  226. int (*ioctl)(struct tty_struct *tty, struct file *file,
  227. unsigned int cmd, unsigned long arg);
  228. void (*set_termios)(struct tty_struct *tty,
  229. struct usb_serial_port *port, struct ktermios *old);
  230. void (*break_ctl)(struct tty_struct *tty, int break_state);
  231. int (*chars_in_buffer)(struct tty_struct *tty);
  232. void (*throttle)(struct tty_struct *tty);
  233. void (*unthrottle)(struct tty_struct *tty);
  234. int (*tiocmget)(struct tty_struct *tty, struct file *file);
  235. int (*tiocmset)(struct tty_struct *tty, struct file *file,
  236. unsigned int set, unsigned int clear);
  237. /* Called by the tty layer for port level work. There may or may not
  238. be an attached tty at this point */
  239. void (*dtr_rts)(struct usb_serial_port *port, int on);
  240. int (*carrier_raised)(struct usb_serial_port *port);
  241. /* Called by the usb serial hooks to allow the user to rework the
  242. termios state */
  243. void (*init_termios)(struct tty_struct *tty);
  244. /* USB events */
  245. void (*read_int_callback)(struct urb *urb);
  246. void (*write_int_callback)(struct urb *urb);
  247. void (*read_bulk_callback)(struct urb *urb);
  248. void (*write_bulk_callback)(struct urb *urb);
  249. };
  250. #define to_usb_serial_driver(d) \
  251. container_of(d, struct usb_serial_driver, driver)
  252. extern int usb_serial_register(struct usb_serial_driver *driver);
  253. extern void usb_serial_deregister(struct usb_serial_driver *driver);
  254. extern void usb_serial_port_softint(struct usb_serial_port *port);
  255. extern int usb_serial_probe(struct usb_interface *iface,
  256. const struct usb_device_id *id);
  257. extern void usb_serial_disconnect(struct usb_interface *iface);
  258. extern int usb_serial_suspend(struct usb_interface *intf, pm_message_t message);
  259. extern int usb_serial_resume(struct usb_interface *intf);
  260. extern int ezusb_writememory(struct usb_serial *serial, int address,
  261. unsigned char *data, int length, __u8 bRequest);
  262. extern int ezusb_set_reset(struct usb_serial *serial, unsigned char reset_bit);
  263. /* USB Serial console functions */
  264. #ifdef CONFIG_USB_SERIAL_CONSOLE
  265. extern void usb_serial_console_init(int debug, int minor);
  266. extern void usb_serial_console_exit(void);
  267. extern void usb_serial_console_disconnect(struct usb_serial *serial);
  268. #else
  269. static inline void usb_serial_console_init(int debug, int minor) { }
  270. static inline void usb_serial_console_exit(void) { }
  271. static inline void usb_serial_console_disconnect(struct usb_serial *serial) {}
  272. #endif
  273. /* Functions needed by other parts of the usbserial core */
  274. extern struct usb_serial *usb_serial_get_by_index(unsigned int minor);
  275. extern void usb_serial_put(struct usb_serial *serial);
  276. extern int usb_serial_generic_open(struct tty_struct *tty,
  277. struct usb_serial_port *port);
  278. extern int usb_serial_generic_write(struct tty_struct *tty,
  279. struct usb_serial_port *port, const unsigned char *buf, int count);
  280. extern void usb_serial_generic_close(struct usb_serial_port *port);
  281. extern int usb_serial_generic_resume(struct usb_serial *serial);
  282. extern int usb_serial_generic_write_room(struct tty_struct *tty);
  283. extern int usb_serial_generic_chars_in_buffer(struct tty_struct *tty);
  284. extern void usb_serial_generic_read_bulk_callback(struct urb *urb);
  285. extern void usb_serial_generic_write_bulk_callback(struct urb *urb);
  286. extern void usb_serial_generic_throttle(struct tty_struct *tty);
  287. extern void usb_serial_generic_unthrottle(struct tty_struct *tty);
  288. extern void usb_serial_generic_disconnect(struct usb_serial *serial);
  289. extern void usb_serial_generic_release(struct usb_serial *serial);
  290. extern int usb_serial_generic_register(int debug);
  291. extern void usb_serial_generic_deregister(void);
  292. extern void usb_serial_generic_resubmit_read_urb(struct usb_serial_port *port,
  293. gfp_t mem_flags);
  294. extern int usb_serial_handle_sysrq_char(struct tty_struct *tty,
  295. struct usb_serial_port *port,
  296. unsigned int ch);
  297. extern int usb_serial_handle_break(struct usb_serial_port *port);
  298. extern int usb_serial_bus_register(struct usb_serial_driver *device);
  299. extern void usb_serial_bus_deregister(struct usb_serial_driver *device);
  300. extern struct usb_serial_driver usb_serial_generic_device;
  301. extern struct bus_type usb_serial_bus_type;
  302. extern struct tty_driver *usb_serial_tty_driver;
  303. static inline void usb_serial_debug_data(int debug,
  304. struct device *dev,
  305. const char *function, int size,
  306. const unsigned char *data)
  307. {
  308. int i;
  309. if (debug) {
  310. dev_printk(KERN_DEBUG, dev, "%s - length = %d, data = ",
  311. function, size);
  312. for (i = 0; i < size; ++i)
  313. printk("%.2x ", data[i]);
  314. printk("\n");
  315. }
  316. }
  317. /* Use our own dbg macro */
  318. #undef dbg
  319. #define dbg(format, arg...) \
  320. do { \
  321. if (debug) \
  322. printk(KERN_DEBUG "%s: " format "\n" , __FILE__ , \
  323. ## arg); \
  324. } while (0)
  325. #endif /* __LINUX_USB_SERIAL_H */