Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * USB Serial Converter stuff
0004  *
0005  *  Copyright (C) 1999 - 2012
0006  *      Greg Kroah-Hartman (greg@kroah.com)
0007  */
0008 
0009 #ifndef __LINUX_USB_SERIAL_H
0010 #define __LINUX_USB_SERIAL_H
0011 
0012 #include <linux/kref.h>
0013 #include <linux/mutex.h>
0014 #include <linux/serial.h>
0015 #include <linux/kfifo.h>
0016 
0017 /* The maximum number of ports one device can grab at once */
0018 #define MAX_NUM_PORTS       16
0019 
0020 /* USB serial flags */
0021 #define USB_SERIAL_WRITE_BUSY   0
0022 #define USB_SERIAL_THROTTLED    1
0023 
0024 /**
0025  * usb_serial_port: structure for the specific ports of a device.
0026  * @serial: pointer back to the struct usb_serial owner of this port.
0027  * @port: pointer to the corresponding tty_port for this port.
0028  * @lock: spinlock to grab when updating portions of this structure.
0029  * @minor: the minor number of the port
0030  * @port_number: the struct usb_serial port number of this port (starts at 0)
0031  * @interrupt_in_buffer: pointer to the interrupt in buffer for this port.
0032  * @interrupt_in_urb: pointer to the interrupt in struct urb for this port.
0033  * @interrupt_in_endpointAddress: endpoint address for the interrupt in pipe
0034  *  for this port.
0035  * @interrupt_out_buffer: pointer to the interrupt out buffer for this port.
0036  * @interrupt_out_size: the size of the interrupt_out_buffer, in bytes.
0037  * @interrupt_out_urb: pointer to the interrupt out struct urb for this port.
0038  * @interrupt_out_endpointAddress: endpoint address for the interrupt out pipe
0039  *  for this port.
0040  * @bulk_in_buffer: pointer to the bulk in buffer for this port.
0041  * @bulk_in_size: the size of the bulk_in_buffer, in bytes.
0042  * @read_urb: pointer to the bulk in struct urb for this port.
0043  * @bulk_in_endpointAddress: endpoint address for the bulk in pipe for this
0044  *  port.
0045  * @bulk_in_buffers: pointers to the bulk in buffers for this port
0046  * @read_urbs: pointers to the bulk in urbs for this port
0047  * @read_urbs_free: status bitmap the for bulk in urbs
0048  * @bulk_out_buffer: pointer to the bulk out buffer for this port.
0049  * @bulk_out_size: the size of the bulk_out_buffer, in bytes.
0050  * @write_urb: pointer to the bulk out struct urb for this port.
0051  * @write_fifo: kfifo used to buffer outgoing data
0052  * @bulk_out_buffers: pointers to the bulk out buffers for this port
0053  * @write_urbs: pointers to the bulk out urbs for this port
0054  * @write_urbs_free: status bitmap the for bulk out urbs
0055  * @icount: interrupt counters
0056  * @tx_bytes: number of bytes currently in host stack queues
0057  * @bulk_out_endpointAddress: endpoint address for the bulk out pipe for this
0058  *  port.
0059  * @flags: usb serial port flags
0060  * @work: work queue entry for the line discipline waking up.
0061  * @dev: pointer to the serial device
0062  *
0063  * This structure is used by the usb-serial core and drivers for the specific
0064  * ports of a device.
0065  */
0066 struct usb_serial_port {
0067     struct usb_serial   *serial;
0068     struct tty_port     port;
0069     spinlock_t      lock;
0070     u32         minor;
0071     u8          port_number;
0072 
0073     unsigned char       *interrupt_in_buffer;
0074     struct urb      *interrupt_in_urb;
0075     __u8            interrupt_in_endpointAddress;
0076 
0077     unsigned char       *interrupt_out_buffer;
0078     int         interrupt_out_size;
0079     struct urb      *interrupt_out_urb;
0080     __u8            interrupt_out_endpointAddress;
0081 
0082     unsigned char       *bulk_in_buffer;
0083     int         bulk_in_size;
0084     struct urb      *read_urb;
0085     __u8            bulk_in_endpointAddress;
0086 
0087     unsigned char       *bulk_in_buffers[2];
0088     struct urb      *read_urbs[2];
0089     unsigned long       read_urbs_free;
0090 
0091     unsigned char       *bulk_out_buffer;
0092     int         bulk_out_size;
0093     struct urb      *write_urb;
0094     struct kfifo        write_fifo;
0095 
0096     unsigned char       *bulk_out_buffers[2];
0097     struct urb      *write_urbs[2];
0098     unsigned long       write_urbs_free;
0099     __u8            bulk_out_endpointAddress;
0100 
0101     struct async_icount icount;
0102     int         tx_bytes;
0103 
0104     unsigned long       flags;
0105     struct work_struct  work;
0106     unsigned long       sysrq; /* sysrq timeout */
0107     struct device       dev;
0108 };
0109 #define to_usb_serial_port(d) container_of(d, struct usb_serial_port, dev)
0110 
0111 /* get and set the port private data pointer helper functions */
0112 static inline void *usb_get_serial_port_data(struct usb_serial_port *port)
0113 {
0114     return dev_get_drvdata(&port->dev);
0115 }
0116 
0117 static inline void usb_set_serial_port_data(struct usb_serial_port *port,
0118                         void *data)
0119 {
0120     dev_set_drvdata(&port->dev, data);
0121 }
0122 
0123 /**
0124  * usb_serial - structure used by the usb-serial core for a device
0125  * @dev: pointer to the struct usb_device for this device
0126  * @type: pointer to the struct usb_serial_driver for this device
0127  * @interface: pointer to the struct usb_interface for this device
0128  * @sibling: pointer to the struct usb_interface of any sibling interface
0129  * @suspend_count: number of suspended (sibling) interfaces
0130  * @num_ports: the number of ports this device has
0131  * @num_interrupt_in: number of interrupt in endpoints we have
0132  * @num_interrupt_out: number of interrupt out endpoints we have
0133  * @num_bulk_in: number of bulk in endpoints we have
0134  * @num_bulk_out: number of bulk out endpoints we have
0135  * @port: array of struct usb_serial_port structures for the different ports.
0136  * @private: place to put any driver specific information that is needed.  The
0137  *  usb-serial driver is required to manage this data, the usb-serial core
0138  *  will not touch this.  Use usb_get_serial_data() and
0139  *  usb_set_serial_data() to access this.
0140  */
0141 struct usb_serial {
0142     struct usb_device       *dev;
0143     struct usb_serial_driver    *type;
0144     struct usb_interface        *interface;
0145     struct usb_interface        *sibling;
0146     unsigned int            suspend_count;
0147     unsigned char           disconnected:1;
0148     unsigned char           attached:1;
0149     unsigned char           minors_reserved:1;
0150     unsigned char           num_ports;
0151     unsigned char           num_port_pointers;
0152     unsigned char           num_interrupt_in;
0153     unsigned char           num_interrupt_out;
0154     unsigned char           num_bulk_in;
0155     unsigned char           num_bulk_out;
0156     struct usb_serial_port      *port[MAX_NUM_PORTS];
0157     struct kref         kref;
0158     struct mutex            disc_mutex;
0159     void                *private;
0160 };
0161 #define to_usb_serial(d) container_of(d, struct usb_serial, kref)
0162 
0163 /* get and set the serial private data pointer helper functions */
0164 static inline void *usb_get_serial_data(struct usb_serial *serial)
0165 {
0166     return serial->private;
0167 }
0168 
0169 static inline void usb_set_serial_data(struct usb_serial *serial, void *data)
0170 {
0171     serial->private = data;
0172 }
0173 
0174 struct usb_serial_endpoints {
0175     unsigned char num_bulk_in;
0176     unsigned char num_bulk_out;
0177     unsigned char num_interrupt_in;
0178     unsigned char num_interrupt_out;
0179     struct usb_endpoint_descriptor *bulk_in[MAX_NUM_PORTS];
0180     struct usb_endpoint_descriptor *bulk_out[MAX_NUM_PORTS];
0181     struct usb_endpoint_descriptor *interrupt_in[MAX_NUM_PORTS];
0182     struct usb_endpoint_descriptor *interrupt_out[MAX_NUM_PORTS];
0183 };
0184 
0185 /**
0186  * usb_serial_driver - describes a usb serial driver
0187  * @description: pointer to a string that describes this driver.  This string
0188  *  used in the syslog messages when a device is inserted or removed.
0189  * @id_table: pointer to a list of usb_device_id structures that define all
0190  *  of the devices this structure can support.
0191  * @num_ports: the number of different ports this device will have.
0192  * @num_bulk_in: minimum number of bulk-in endpoints
0193  * @num_bulk_out: minimum number of bulk-out endpoints
0194  * @num_interrupt_in: minimum number of interrupt-in endpoints
0195  * @num_interrupt_out: minimum number of interrupt-out endpoints
0196  * @bulk_in_size: minimum number of bytes to allocate for bulk-in buffer
0197  *  (0 = end-point size)
0198  * @bulk_out_size: bytes to allocate for bulk-out buffer (0 = end-point size)
0199  * @calc_num_ports: pointer to a function to determine how many ports this
0200  *  device has dynamically. It can also be used to verify the number of
0201  *  endpoints or to modify the port-endpoint mapping. It will be called
0202  *  after the probe() callback is called, but before attach().
0203  * @probe: pointer to the driver's probe function.
0204  *  This will be called when the device is inserted into the system,
0205  *  but before the device has been fully initialized by the usb_serial
0206  *  subsystem.  Use this function to download any firmware to the device,
0207  *  or any other early initialization that might be needed.
0208  *  Return 0 to continue on with the initialization sequence.  Anything
0209  *  else will abort it.
0210  * @attach: pointer to the driver's attach function.
0211  *  This will be called when the struct usb_serial structure is fully
0212  *  set up.  Do any local initialization of the device, or any private
0213  *  memory structure allocation at this point in time.
0214  * @disconnect: pointer to the driver's disconnect function.  This will be
0215  *  called when the device is unplugged or unbound from the driver.
0216  * @release: pointer to the driver's release function.  This will be called
0217  *  when the usb_serial data structure is about to be destroyed.
0218  * @usb_driver: pointer to the struct usb_driver that controls this
0219  *  device.  This is necessary to allow dynamic ids to be added to
0220  *  the driver from sysfs.
0221  *
0222  * This structure is defines a USB Serial driver.  It provides all of
0223  * the information that the USB serial core code needs.  If the function
0224  * pointers are defined, then the USB serial core code will call them when
0225  * the corresponding tty port functions are called.  If they are not
0226  * called, the generic serial function will be used instead.
0227  *
0228  * The driver.owner field should be set to the module owner of this driver.
0229  * The driver.name field should be set to the name of this driver (remember
0230  * it will show up in sysfs, so it needs to be short and to the point.
0231  * Using the module name is a good idea.)
0232  */
0233 struct usb_serial_driver {
0234     const char *description;
0235     const struct usb_device_id *id_table;
0236 
0237     struct list_head    driver_list;
0238     struct device_driver    driver;
0239     struct usb_driver   *usb_driver;
0240     struct usb_dynids   dynids;
0241 
0242     unsigned char       num_ports;
0243 
0244     unsigned char       num_bulk_in;
0245     unsigned char       num_bulk_out;
0246     unsigned char       num_interrupt_in;
0247     unsigned char       num_interrupt_out;
0248 
0249     size_t          bulk_in_size;
0250     size_t          bulk_out_size;
0251 
0252     int (*probe)(struct usb_serial *serial, const struct usb_device_id *id);
0253     int (*attach)(struct usb_serial *serial);
0254     int (*calc_num_ports)(struct usb_serial *serial,
0255             struct usb_serial_endpoints *epds);
0256 
0257     void (*disconnect)(struct usb_serial *serial);
0258     void (*release)(struct usb_serial *serial);
0259 
0260     int (*port_probe)(struct usb_serial_port *port);
0261     void (*port_remove)(struct usb_serial_port *port);
0262 
0263     int (*suspend)(struct usb_serial *serial, pm_message_t message);
0264     int (*resume)(struct usb_serial *serial);
0265     int (*reset_resume)(struct usb_serial *serial);
0266 
0267     /* serial function calls */
0268     /* Called by console and by the tty layer */
0269     int  (*open)(struct tty_struct *tty, struct usb_serial_port *port);
0270     void (*close)(struct usb_serial_port *port);
0271     int  (*write)(struct tty_struct *tty, struct usb_serial_port *port,
0272             const unsigned char *buf, int count);
0273     /* Called only by the tty layer */
0274     unsigned int (*write_room)(struct tty_struct *tty);
0275     int  (*ioctl)(struct tty_struct *tty,
0276               unsigned int cmd, unsigned long arg);
0277     void (*get_serial)(struct tty_struct *tty, struct serial_struct *ss);
0278     int  (*set_serial)(struct tty_struct *tty, struct serial_struct *ss);
0279     void (*set_termios)(struct tty_struct *tty,
0280             struct usb_serial_port *port, struct ktermios *old);
0281     void (*break_ctl)(struct tty_struct *tty, int break_state);
0282     unsigned int (*chars_in_buffer)(struct tty_struct *tty);
0283     void (*wait_until_sent)(struct tty_struct *tty, long timeout);
0284     bool (*tx_empty)(struct usb_serial_port *port);
0285     void (*throttle)(struct tty_struct *tty);
0286     void (*unthrottle)(struct tty_struct *tty);
0287     int  (*tiocmget)(struct tty_struct *tty);
0288     int  (*tiocmset)(struct tty_struct *tty,
0289              unsigned int set, unsigned int clear);
0290     int  (*tiocmiwait)(struct tty_struct *tty, unsigned long arg);
0291     int  (*get_icount)(struct tty_struct *tty,
0292             struct serial_icounter_struct *icount);
0293     /* Called by the tty layer for port level work. There may or may not
0294        be an attached tty at this point */
0295     void (*dtr_rts)(struct usb_serial_port *port, int on);
0296     int  (*carrier_raised)(struct usb_serial_port *port);
0297     /* Called by the usb serial hooks to allow the user to rework the
0298        termios state */
0299     void (*init_termios)(struct tty_struct *tty);
0300     /* USB events */
0301     void (*read_int_callback)(struct urb *urb);
0302     void (*write_int_callback)(struct urb *urb);
0303     void (*read_bulk_callback)(struct urb *urb);
0304     void (*write_bulk_callback)(struct urb *urb);
0305     /* Called by the generic read bulk callback */
0306     void (*process_read_urb)(struct urb *urb);
0307     /* Called by the generic write implementation */
0308     int (*prepare_write_buffer)(struct usb_serial_port *port,
0309                         void *dest, size_t size);
0310 };
0311 #define to_usb_serial_driver(d) \
0312     container_of(d, struct usb_serial_driver, driver)
0313 
0314 int usb_serial_register_drivers(struct usb_serial_driver *const serial_drivers[],
0315         const char *name, const struct usb_device_id *id_table);
0316 void usb_serial_deregister_drivers(struct usb_serial_driver *const serial_drivers[]);
0317 void usb_serial_port_softint(struct usb_serial_port *port);
0318 
0319 int usb_serial_suspend(struct usb_interface *intf, pm_message_t message);
0320 int usb_serial_resume(struct usb_interface *intf);
0321 
0322 /* USB Serial console functions */
0323 #ifdef CONFIG_USB_SERIAL_CONSOLE
0324 void usb_serial_console_init(int minor);
0325 void usb_serial_console_exit(void);
0326 void usb_serial_console_disconnect(struct usb_serial *serial);
0327 #else
0328 static inline void usb_serial_console_init(int minor) { }
0329 static inline void usb_serial_console_exit(void) { }
0330 static inline void usb_serial_console_disconnect(struct usb_serial *serial) {}
0331 #endif
0332 
0333 /* Functions needed by other parts of the usbserial core */
0334 struct usb_serial_port *usb_serial_port_get_by_minor(unsigned int minor);
0335 void usb_serial_put(struct usb_serial *serial);
0336 
0337 int usb_serial_claim_interface(struct usb_serial *serial, struct usb_interface *intf);
0338 
0339 int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port);
0340 int usb_serial_generic_write_start(struct usb_serial_port *port, gfp_t mem_flags);
0341 int usb_serial_generic_write(struct tty_struct *tty, struct usb_serial_port *port,
0342         const unsigned char *buf, int count);
0343 void usb_serial_generic_close(struct usb_serial_port *port);
0344 int usb_serial_generic_resume(struct usb_serial *serial);
0345 unsigned int usb_serial_generic_write_room(struct tty_struct *tty);
0346 unsigned int usb_serial_generic_chars_in_buffer(struct tty_struct *tty);
0347 void usb_serial_generic_wait_until_sent(struct tty_struct *tty, long timeout);
0348 void usb_serial_generic_read_bulk_callback(struct urb *urb);
0349 void usb_serial_generic_write_bulk_callback(struct urb *urb);
0350 void usb_serial_generic_throttle(struct tty_struct *tty);
0351 void usb_serial_generic_unthrottle(struct tty_struct *tty);
0352 int usb_serial_generic_tiocmiwait(struct tty_struct *tty, unsigned long arg);
0353 int usb_serial_generic_get_icount(struct tty_struct *tty, struct serial_icounter_struct *icount);
0354 int usb_serial_generic_register(void);
0355 void usb_serial_generic_deregister(void);
0356 int usb_serial_generic_submit_read_urbs(struct usb_serial_port *port, gfp_t mem_flags);
0357 void usb_serial_generic_process_read_urb(struct urb *urb);
0358 int usb_serial_generic_prepare_write_buffer(struct usb_serial_port *port, void *dest, size_t size);
0359 
0360 #if defined(CONFIG_USB_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
0361 int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch);
0362 int usb_serial_handle_break(struct usb_serial_port *port);
0363 #else
0364 static inline int usb_serial_handle_sysrq_char(struct usb_serial_port *port, unsigned int ch)
0365 {
0366     return 0;
0367 }
0368 static inline int usb_serial_handle_break(struct usb_serial_port *port)
0369 {
0370     return 0;
0371 }
0372 #endif
0373 
0374 void usb_serial_handle_dcd_change(struct usb_serial_port *usb_port,
0375         struct tty_struct *tty, unsigned int status);
0376 
0377 
0378 int usb_serial_bus_register(struct usb_serial_driver *device);
0379 void usb_serial_bus_deregister(struct usb_serial_driver *device);
0380 
0381 extern struct bus_type usb_serial_bus_type;
0382 extern struct tty_driver *usb_serial_tty_driver;
0383 
0384 static inline void usb_serial_debug_data(struct device *dev,
0385                      const char *function, int size,
0386                      const unsigned char *data)
0387 {
0388     dev_dbg(dev, "%s - length = %d, data = %*ph\n",
0389         function, size, size, data);
0390 }
0391 
0392 /*
0393  * Macro for reporting errors in write path to avoid infinite loop
0394  * when port is used as a console.
0395  */
0396 #define dev_err_console(usport, fmt, ...)               \
0397 do {                                    \
0398     static bool __print_once;                   \
0399     struct usb_serial_port *__port = (usport);          \
0400                                     \
0401     if (!__port->port.console || !__print_once) {           \
0402         __print_once = true;                    \
0403         dev_err(&__port->dev, fmt, ##__VA_ARGS__);      \
0404     }                               \
0405 } while (0)
0406 
0407 /*
0408  * module_usb_serial_driver() - Helper macro for registering a USB Serial driver
0409  * @__serial_drivers: list of usb_serial drivers to register
0410  * @__ids: all device ids that @__serial_drivers bind to
0411  *
0412  * Helper macro for USB serial drivers which do not do anything special
0413  * in module init/exit. This eliminates a lot of boilerplate. Each
0414  * module may only use this macro once, and calling it replaces
0415  * module_init() and module_exit()
0416  *
0417  */
0418 #define usb_serial_module_driver(__name, __serial_drivers, __ids)   \
0419 static int __init usb_serial_module_init(void)              \
0420 {                                   \
0421     return usb_serial_register_drivers(__serial_drivers,        \
0422                        __name, __ids);      \
0423 }                                   \
0424 module_init(usb_serial_module_init);                    \
0425 static void __exit usb_serial_module_exit(void)             \
0426 {                                   \
0427     usb_serial_deregister_drivers(__serial_drivers);        \
0428 }                                   \
0429 module_exit(usb_serial_module_exit);
0430 
0431 #define module_usb_serial_driver(__serial_drivers, __ids)       \
0432     usb_serial_module_driver(KBUILD_MODNAME, __serial_drivers, __ids)
0433 
0434 #endif /* __LINUX_USB_SERIAL_H */
0435