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