Nicholas Flintham | 1e3d311 | 2013-04-10 10:48:38 +0100 | [diff] [blame^] | 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 | |
| 13 | #ifndef __LINUX_USB_SERIAL_H |
| 14 | #define __LINUX_USB_SERIAL_H |
| 15 | |
| 16 | #include <linux/kref.h> |
| 17 | #include <linux/mutex.h> |
| 18 | #include <linux/sysrq.h> |
| 19 | #include <linux/kfifo.h> |
| 20 | |
| 21 | #define SERIAL_TTY_MAJOR 188 |
| 22 | #define SERIAL_TTY_MINORS 254 |
| 23 | #define SERIAL_TTY_NO_MINOR 255 |
| 24 | |
| 25 | #define MAX_NUM_PORTS 8 |
| 26 | |
| 27 | #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK)) |
| 28 | |
| 29 | #define USB_SERIAL_WRITE_BUSY 0 |
| 30 | |
| 31 | struct usb_serial_port { |
| 32 | struct usb_serial *serial; |
| 33 | struct tty_port port; |
| 34 | spinlock_t lock; |
| 35 | unsigned char number; |
| 36 | |
| 37 | unsigned char *interrupt_in_buffer; |
| 38 | struct urb *interrupt_in_urb; |
| 39 | __u8 interrupt_in_endpointAddress; |
| 40 | |
| 41 | unsigned char *interrupt_out_buffer; |
| 42 | int interrupt_out_size; |
| 43 | struct urb *interrupt_out_urb; |
| 44 | __u8 interrupt_out_endpointAddress; |
| 45 | |
| 46 | unsigned char *bulk_in_buffer; |
| 47 | int bulk_in_size; |
| 48 | struct urb *read_urb; |
| 49 | __u8 bulk_in_endpointAddress; |
| 50 | |
| 51 | unsigned char *bulk_in_buffers[2]; |
| 52 | struct urb *read_urbs[2]; |
| 53 | unsigned long read_urbs_free; |
| 54 | |
| 55 | unsigned char *bulk_out_buffer; |
| 56 | int bulk_out_size; |
| 57 | struct urb *write_urb; |
| 58 | struct kfifo write_fifo; |
| 59 | |
| 60 | unsigned char *bulk_out_buffers[2]; |
| 61 | struct urb *write_urbs[2]; |
| 62 | unsigned long write_urbs_free; |
| 63 | __u8 bulk_out_endpointAddress; |
| 64 | |
| 65 | int tx_bytes; |
| 66 | |
| 67 | unsigned long flags; |
| 68 | wait_queue_head_t write_wait; |
| 69 | struct work_struct work; |
| 70 | char throttled; |
| 71 | char throttle_req; |
| 72 | unsigned long sysrq; |
| 73 | struct device dev; |
| 74 | }; |
| 75 | #define to_usb_serial_port(d) container_of(d, struct usb_serial_port, dev) |
| 76 | |
| 77 | static inline void *usb_get_serial_port_data(struct usb_serial_port *port) |
| 78 | { |
| 79 | return dev_get_drvdata(&port->dev); |
| 80 | } |
| 81 | |
| 82 | static inline void usb_set_serial_port_data(struct usb_serial_port *port, |
| 83 | void *data) |
| 84 | { |
| 85 | dev_set_drvdata(&port->dev, data); |
| 86 | } |
| 87 | |
| 88 | struct usb_serial { |
| 89 | struct usb_device *dev; |
| 90 | struct usb_serial_driver *type; |
| 91 | struct usb_interface *interface; |
| 92 | unsigned char disconnected:1; |
| 93 | unsigned char suspending:1; |
| 94 | unsigned char attached:1; |
| 95 | unsigned char minor; |
| 96 | unsigned char num_ports; |
| 97 | unsigned char num_port_pointers; |
| 98 | char num_interrupt_in; |
| 99 | char num_interrupt_out; |
| 100 | char num_bulk_in; |
| 101 | char num_bulk_out; |
| 102 | struct usb_serial_port *port[MAX_NUM_PORTS]; |
| 103 | struct kref kref; |
| 104 | struct mutex disc_mutex; |
| 105 | void *private; |
| 106 | }; |
| 107 | #define to_usb_serial(d) container_of(d, struct usb_serial, kref) |
| 108 | |
| 109 | static inline void *usb_get_serial_data(struct usb_serial *serial) |
| 110 | { |
| 111 | return serial->private; |
| 112 | } |
| 113 | |
| 114 | static inline void usb_set_serial_data(struct usb_serial *serial, void *data) |
| 115 | { |
| 116 | serial->private = data; |
| 117 | } |
| 118 | |
| 119 | struct usb_serial_driver { |
| 120 | const char *description; |
| 121 | const struct usb_device_id *id_table; |
| 122 | char num_ports; |
| 123 | |
| 124 | struct list_head driver_list; |
| 125 | struct device_driver driver; |
| 126 | struct usb_driver *usb_driver; |
| 127 | struct usb_dynids dynids; |
| 128 | |
| 129 | size_t bulk_in_size; |
| 130 | size_t bulk_out_size; |
| 131 | |
| 132 | int (*probe)(struct usb_serial *serial, const struct usb_device_id *id); |
| 133 | int (*attach)(struct usb_serial *serial); |
| 134 | int (*calc_num_ports) (struct usb_serial *serial); |
| 135 | |
| 136 | void (*disconnect)(struct usb_serial *serial); |
| 137 | void (*release)(struct usb_serial *serial); |
| 138 | |
| 139 | int (*port_probe)(struct usb_serial_port *port); |
| 140 | int (*port_remove)(struct usb_serial_port *port); |
| 141 | |
| 142 | int (*suspend)(struct usb_serial *serial, pm_message_t message); |
| 143 | int (*resume)(struct usb_serial *serial); |
| 144 | |
| 145 | |
| 146 | |
| 147 | int (*open)(struct tty_struct *tty, struct usb_serial_port *port); |
| 148 | void (*close)(struct usb_serial_port *port); |
| 149 | int (*write)(struct tty_struct *tty, struct usb_serial_port *port, |
| 150 | const unsigned char *buf, int count); |
| 151 | |
| 152 | int (*write_room)(struct tty_struct *tty); |
| 153 | int (*ioctl)(struct tty_struct *tty, |
| 154 | unsigned int cmd, unsigned long arg); |
| 155 | void (*set_termios)(struct tty_struct *tty, |
| 156 | struct usb_serial_port *port, struct ktermios *old); |
| 157 | void (*break_ctl)(struct tty_struct *tty, int break_state); |
| 158 | int (*chars_in_buffer)(struct tty_struct *tty); |
| 159 | void (*throttle)(struct tty_struct *tty); |
| 160 | void (*unthrottle)(struct tty_struct *tty); |
| 161 | int (*tiocmget)(struct tty_struct *tty); |
| 162 | int (*tiocmset)(struct tty_struct *tty, |
| 163 | unsigned int set, unsigned int clear); |
| 164 | int (*get_icount)(struct tty_struct *tty, |
| 165 | struct serial_icounter_struct *icount); |
| 166 | void (*dtr_rts)(struct usb_serial_port *port, int on); |
| 167 | int (*carrier_raised)(struct usb_serial_port *port); |
| 168 | void (*init_termios)(struct tty_struct *tty); |
| 169 | |
| 170 | void (*read_int_callback)(struct urb *urb); |
| 171 | void (*write_int_callback)(struct urb *urb); |
| 172 | void (*read_bulk_callback)(struct urb *urb); |
| 173 | void (*write_bulk_callback)(struct urb *urb); |
| 174 | |
| 175 | void (*process_read_urb)(struct urb *urb); |
| 176 | |
| 177 | int (*prepare_write_buffer)(struct usb_serial_port *port, |
| 178 | void *dest, size_t size); |
| 179 | }; |
| 180 | #define to_usb_serial_driver(d) \ |
| 181 | container_of(d, struct usb_serial_driver, driver) |
| 182 | |
| 183 | extern int usb_serial_register_drivers(struct usb_driver *udriver, |
| 184 | struct usb_serial_driver * const serial_drivers[]); |
| 185 | extern void usb_serial_deregister_drivers(struct usb_driver *udriver, |
| 186 | struct usb_serial_driver * const serial_drivers[]); |
| 187 | extern void usb_serial_port_softint(struct usb_serial_port *port); |
| 188 | |
| 189 | extern int usb_serial_probe(struct usb_interface *iface, |
| 190 | const struct usb_device_id *id); |
| 191 | extern void usb_serial_disconnect(struct usb_interface *iface); |
| 192 | |
| 193 | extern int usb_serial_suspend(struct usb_interface *intf, pm_message_t message); |
| 194 | extern int usb_serial_resume(struct usb_interface *intf); |
| 195 | |
| 196 | extern int ezusb_writememory(struct usb_serial *serial, int address, |
| 197 | unsigned char *data, int length, __u8 bRequest); |
| 198 | extern int ezusb_set_reset(struct usb_serial *serial, unsigned char reset_bit); |
| 199 | |
| 200 | #ifdef CONFIG_USB_SERIAL_CONSOLE |
| 201 | extern void usb_serial_console_init(int debug, int minor); |
| 202 | extern void usb_serial_console_exit(void); |
| 203 | extern void usb_serial_console_disconnect(struct usb_serial *serial); |
| 204 | #else |
| 205 | static inline void usb_serial_console_init(int debug, int minor) { } |
| 206 | static inline void usb_serial_console_exit(void) { } |
| 207 | static inline void usb_serial_console_disconnect(struct usb_serial *serial) {} |
| 208 | #endif |
| 209 | |
| 210 | extern struct usb_serial *usb_serial_get_by_index(unsigned int minor); |
| 211 | extern void usb_serial_put(struct usb_serial *serial); |
| 212 | extern int usb_serial_generic_open(struct tty_struct *tty, |
| 213 | struct usb_serial_port *port); |
| 214 | extern int usb_serial_generic_write(struct tty_struct *tty, |
| 215 | struct usb_serial_port *port, const unsigned char *buf, int count); |
| 216 | extern void usb_serial_generic_close(struct usb_serial_port *port); |
| 217 | extern int usb_serial_generic_resume(struct usb_serial *serial); |
| 218 | extern int usb_serial_generic_write_room(struct tty_struct *tty); |
| 219 | extern int usb_serial_generic_chars_in_buffer(struct tty_struct *tty); |
| 220 | extern void usb_serial_generic_read_bulk_callback(struct urb *urb); |
| 221 | extern void usb_serial_generic_write_bulk_callback(struct urb *urb); |
| 222 | extern void usb_serial_generic_throttle(struct tty_struct *tty); |
| 223 | extern void usb_serial_generic_unthrottle(struct tty_struct *tty); |
| 224 | extern void usb_serial_generic_disconnect(struct usb_serial *serial); |
| 225 | extern void usb_serial_generic_release(struct usb_serial *serial); |
| 226 | extern int usb_serial_generic_register(int debug); |
| 227 | extern void usb_serial_generic_deregister(void); |
| 228 | extern int usb_serial_generic_submit_read_urbs(struct usb_serial_port *port, |
| 229 | gfp_t mem_flags); |
| 230 | extern void usb_serial_generic_process_read_urb(struct urb *urb); |
| 231 | extern int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port, |
| 232 | void *dest, size_t size); |
| 233 | extern int usb_serial_handle_sysrq_char(struct usb_serial_port *port, |
| 234 | unsigned int ch); |
| 235 | extern int usb_serial_handle_break(struct usb_serial_port *port); |
| 236 | extern void usb_serial_handle_dcd_change(struct usb_serial_port *usb_port, |
| 237 | struct tty_struct *tty, |
| 238 | unsigned int status); |
| 239 | |
| 240 | |
| 241 | extern int usb_serial_bus_register(struct usb_serial_driver *device); |
| 242 | extern void usb_serial_bus_deregister(struct usb_serial_driver *device); |
| 243 | |
| 244 | extern struct usb_serial_driver usb_serial_generic_device; |
| 245 | extern struct bus_type usb_serial_bus_type; |
| 246 | extern struct tty_driver *usb_serial_tty_driver; |
| 247 | |
| 248 | static inline void usb_serial_debug_data(int debug, |
| 249 | struct device *dev, |
| 250 | const char *function, int size, |
| 251 | const unsigned char *data) |
| 252 | { |
| 253 | int i; |
| 254 | |
| 255 | if (debug) { |
| 256 | dev_printk(KERN_DEBUG, dev, "%s - length = %d, data = ", |
| 257 | function, size); |
| 258 | for (i = 0; i < size; ++i) |
| 259 | printk("%.2x ", data[i]); |
| 260 | printk("\n"); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | #undef dbg |
| 265 | #define dbg(format, arg...) \ |
| 266 | do { \ |
| 267 | if (debug) \ |
| 268 | printk(KERN_DEBUG "%s: " format "\n", __FILE__, ##arg); \ |
| 269 | } while (0) |
| 270 | |
| 271 | #define dev_err_console(usport, fmt, ...) \ |
| 272 | do { \ |
| 273 | static bool __print_once; \ |
| 274 | struct usb_serial_port *__port = (usport); \ |
| 275 | \ |
| 276 | if (!__port->port.console || !__print_once) { \ |
| 277 | __print_once = true; \ |
| 278 | dev_err(&__port->dev, fmt, ##__VA_ARGS__); \ |
| 279 | } \ |
| 280 | } while (0) |
| 281 | |
| 282 | #define module_usb_serial_driver(__usb_driver, __serial_drivers) \ |
| 283 | module_driver(__usb_driver, usb_serial_register_drivers, \ |
| 284 | usb_serial_deregister_drivers, __serial_drivers) |
| 285 | |
| 286 | #endif |
| 287 | |