Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003   USB Driver for Sierra Wireless
0004 
0005   Copyright (C) 2006, 2007, 2008  Kevin Lloyd <klloyd@sierrawireless.com>,
0006 
0007   Copyright (C) 2008, 2009  Elina Pasheva, Matthew Safar, Rory Filer
0008             <linux@sierrawireless.com>
0009 
0010   IMPORTANT DISCLAIMER: This driver is not commercially supported by
0011   Sierra Wireless. Use at your own risk.
0012 
0013   Portions based on the option driver by Matthias Urlichs <smurf@smurf.noris.de>
0014   Whom based his on the Keyspan driver by Hugh Blemings <hugh@blemings.org>
0015 */
0016 /* Uncomment to log function calls */
0017 /* #define DEBUG */
0018 
0019 #define DRIVER_AUTHOR "Kevin Lloyd, Elina Pasheva, Matthew Safar, Rory Filer"
0020 #define DRIVER_DESC "USB Driver for Sierra Wireless USB modems"
0021 
0022 #include <linux/kernel.h>
0023 #include <linux/jiffies.h>
0024 #include <linux/errno.h>
0025 #include <linux/tty.h>
0026 #include <linux/slab.h>
0027 #include <linux/tty_flip.h>
0028 #include <linux/module.h>
0029 #include <linux/usb.h>
0030 #include <linux/usb/serial.h>
0031 
0032 #define SWIMS_USB_REQUEST_SetPower  0x00
0033 #define SWIMS_USB_REQUEST_SetNmea   0x07
0034 
0035 #define N_IN_URB_HM 8
0036 #define N_OUT_URB_HM    64
0037 #define N_IN_URB    4
0038 #define N_OUT_URB   4
0039 #define IN_BUFLEN   4096
0040 
0041 #define MAX_TRANSFER        (PAGE_SIZE - 512)
0042 /* MAX_TRANSFER is chosen so that the VM is not stressed by
0043    allocations > PAGE_SIZE and the number of packets in a page
0044    is an integer 512 is the largest possible packet on EHCI */
0045 
0046 static bool nmea;
0047 
0048 struct sierra_iface_list {
0049     const u8 *nums;     /* array of interface numbers */
0050     size_t count;       /* number of elements in array */
0051 };
0052 
0053 struct sierra_intf_private {
0054     spinlock_t susp_lock;
0055     unsigned int suspended:1;
0056     int in_flight;
0057     unsigned int open_ports;
0058 };
0059 
0060 static int sierra_set_power_state(struct usb_device *udev, __u16 swiState)
0061 {
0062     return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
0063             SWIMS_USB_REQUEST_SetPower, /* __u8 request      */
0064             USB_TYPE_VENDOR,        /* __u8 request type */
0065             swiState,           /* __u16 value       */
0066             0,              /* __u16 index       */
0067             NULL,               /* void *data        */
0068             0,              /* __u16 size        */
0069             USB_CTRL_SET_TIMEOUT);      /* int timeout       */
0070 }
0071 
0072 static int sierra_vsc_set_nmea(struct usb_device *udev, __u16 enable)
0073 {
0074     return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
0075             SWIMS_USB_REQUEST_SetNmea,  /* __u8 request      */
0076             USB_TYPE_VENDOR,        /* __u8 request type */
0077             enable,             /* __u16 value       */
0078             0x0000,             /* __u16 index       */
0079             NULL,               /* void *data        */
0080             0,              /* __u16 size        */
0081             USB_CTRL_SET_TIMEOUT);      /* int timeout       */
0082 }
0083 
0084 static int sierra_calc_num_ports(struct usb_serial *serial,
0085                     struct usb_serial_endpoints *epds)
0086 {
0087     int num_ports = 0;
0088     u8 ifnum, numendpoints;
0089 
0090     ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
0091     numendpoints = serial->interface->cur_altsetting->desc.bNumEndpoints;
0092 
0093     /* Dummy interface present on some SKUs should be ignored */
0094     if (ifnum == 0x99)
0095         num_ports = 0;
0096     else if (numendpoints <= 3)
0097         num_ports = 1;
0098     else
0099         num_ports = (numendpoints-1)/2;
0100     return num_ports;
0101 }
0102 
0103 static bool is_listed(const u8 ifnum, const struct sierra_iface_list *list)
0104 {
0105     int i;
0106 
0107     if (!list)
0108         return false;
0109 
0110     for (i = 0; i < list->count; i++) {
0111         if (list->nums[i] == ifnum)
0112             return true;
0113     }
0114 
0115     return false;
0116 }
0117 
0118 static u8 sierra_interface_num(struct usb_serial *serial)
0119 {
0120     return serial->interface->cur_altsetting->desc.bInterfaceNumber;
0121 }
0122 
0123 static int sierra_probe(struct usb_serial *serial,
0124             const struct usb_device_id *id)
0125 {
0126     const struct sierra_iface_list *ignore_list;
0127     int result = 0;
0128     struct usb_device *udev;
0129     u8 ifnum;
0130 
0131     udev = serial->dev;
0132     ifnum = sierra_interface_num(serial);
0133 
0134     /*
0135      * If this interface supports more than 1 alternate
0136      * select the 2nd one
0137      */
0138     if (serial->interface->num_altsetting == 2) {
0139         dev_dbg(&udev->dev, "Selecting alt setting for interface %d\n",
0140             ifnum);
0141         /* We know the alternate setting is 1 for the MC8785 */
0142         usb_set_interface(udev, ifnum, 1);
0143     }
0144 
0145     ignore_list = (const struct sierra_iface_list *)id->driver_info;
0146 
0147     if (is_listed(ifnum, ignore_list)) {
0148         dev_dbg(&serial->dev->dev, "Ignoring interface #%d\n", ifnum);
0149         return -ENODEV;
0150     }
0151 
0152     return result;
0153 }
0154 
0155 /* interfaces with higher memory requirements */
0156 static const u8 hi_memory_typeA_ifaces[] = { 0, 2 };
0157 static const struct sierra_iface_list typeA_interface_list = {
0158     .nums   = hi_memory_typeA_ifaces,
0159     .count  = ARRAY_SIZE(hi_memory_typeA_ifaces),
0160 };
0161 
0162 static const u8 hi_memory_typeB_ifaces[] = { 3, 4, 5, 6 };
0163 static const struct sierra_iface_list typeB_interface_list = {
0164     .nums   = hi_memory_typeB_ifaces,
0165     .count  = ARRAY_SIZE(hi_memory_typeB_ifaces),
0166 };
0167 
0168 /* 'ignorelist' of interfaces not served by this driver */
0169 static const u8 direct_ip_non_serial_ifaces[] = { 7, 8, 9, 10, 11, 19, 20 };
0170 static const struct sierra_iface_list direct_ip_interface_ignore = {
0171     .nums   = direct_ip_non_serial_ifaces,
0172     .count  = ARRAY_SIZE(direct_ip_non_serial_ifaces),
0173 };
0174 
0175 static const struct usb_device_id id_table[] = {
0176     { USB_DEVICE(0x0F3D, 0x0112) }, /* Airprime/Sierra PC 5220 */
0177     { USB_DEVICE(0x03F0, 0x1B1D) }, /* HP ev2200 a.k.a MC5720 */
0178     { USB_DEVICE(0x03F0, 0x211D) }, /* HP ev2210 a.k.a MC5725 */
0179     { USB_DEVICE(0x03F0, 0x1E1D) }, /* HP hs2300 a.k.a MC8775 */
0180 
0181     { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
0182     { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
0183     { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
0184     { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
0185     { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
0186     { USB_DEVICE(0x1199, 0x0022) }, /* Sierra Wireless EM5725 */
0187     { USB_DEVICE(0x1199, 0x0024) }, /* Sierra Wireless MC5727 */
0188     { USB_DEVICE(0x1199, 0x0224) }, /* Sierra Wireless MC5727 */
0189     { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
0190     { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
0191     { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
0192     { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U */
0193     { USB_DEVICE(0x1199, 0x0301) }, /* Sierra Wireless USB Dongle 250U */
0194     /* Sierra Wireless C597 */
0195     { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0023, 0xFF, 0xFF, 0xFF) },
0196     /* Sierra Wireless T598 */
0197     { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0025, 0xFF, 0xFF, 0xFF) },
0198     { USB_DEVICE(0x1199, 0x0026) }, /* Sierra Wireless T11 */
0199     { USB_DEVICE(0x1199, 0x0027) }, /* Sierra Wireless AC402 */
0200     { USB_DEVICE(0x1199, 0x0028) }, /* Sierra Wireless MC5728 */
0201     { USB_DEVICE(0x1199, 0x0029) }, /* Sierra Wireless Device */
0202 
0203     { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
0204     { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
0205     { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
0206     { USB_DEVICE(0x1199, 0x6805) }, /* Sierra Wireless MC8765 */
0207     { USB_DEVICE(0x1199, 0x6808) }, /* Sierra Wireless MC8755 */
0208     { USB_DEVICE(0x1199, 0x6809) }, /* Sierra Wireless MC8765 */
0209     { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
0210     { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 */
0211     { USB_DEVICE(0x1199, 0x6815) }, /* Sierra Wireless MC8775 */
0212     { USB_DEVICE(0x1199, 0x6816) }, /* Sierra Wireless MC8775 */
0213     { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
0214     { USB_DEVICE(0x1199, 0x6821) }, /* Sierra Wireless AirCard 875U */
0215     { USB_DEVICE(0x1199, 0x6822) }, /* Sierra Wireless AirCard 875E */
0216     { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780 */
0217     { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781 */
0218     { USB_DEVICE(0x1199, 0x6834) }, /* Sierra Wireless MC8780 */
0219     { USB_DEVICE(0x1199, 0x6835) }, /* Sierra Wireless MC8781 */
0220     { USB_DEVICE(0x1199, 0x6838) }, /* Sierra Wireless MC8780 */
0221     { USB_DEVICE(0x1199, 0x6839) }, /* Sierra Wireless MC8781 */
0222     { USB_DEVICE(0x1199, 0x683A) }, /* Sierra Wireless MC8785 */
0223     { USB_DEVICE(0x1199, 0x683B) }, /* Sierra Wireless MC8785 Composite */
0224     /* Sierra Wireless MC8790, MC8791, MC8792 Composite */
0225     { USB_DEVICE(0x1199, 0x683C) },
0226     { USB_DEVICE(0x1199, 0x683D) }, /* Sierra Wireless MC8791 Composite */
0227     /* Sierra Wireless MC8790, MC8791, MC8792 */
0228     { USB_DEVICE(0x1199, 0x683E) },
0229     { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
0230     { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
0231     { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880 E */
0232     { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881 E */
0233     { USB_DEVICE(0x1199, 0x6855) }, /* Sierra Wireless AirCard 880 U */
0234     { USB_DEVICE(0x1199, 0x6856) }, /* Sierra Wireless AirCard 881 U */
0235     { USB_DEVICE(0x1199, 0x6859) }, /* Sierra Wireless AirCard 885 E */
0236     { USB_DEVICE(0x1199, 0x685A) }, /* Sierra Wireless AirCard 885 E */
0237     /* Sierra Wireless C885 */
0238     { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6880, 0xFF, 0xFF, 0xFF)},
0239     /* Sierra Wireless C888, Air Card 501, USB 303, USB 304 */
0240     { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6890, 0xFF, 0xFF, 0xFF)},
0241     /* Sierra Wireless C22/C33 */
0242     { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6891, 0xFF, 0xFF, 0xFF)},
0243     /* Sierra Wireless HSPA Non-Composite Device */
0244     { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6892, 0xFF, 0xFF, 0xFF)},
0245     { USB_DEVICE(0x1199, 0x6893) }, /* Sierra Wireless Device */
0246     /* Sierra Wireless Direct IP modems */
0247     { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x68A3, 0xFF, 0xFF, 0xFF),
0248       .driver_info = (kernel_ulong_t)&direct_ip_interface_ignore
0249     },
0250     { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x68AA, 0xFF, 0xFF, 0xFF),
0251       .driver_info = (kernel_ulong_t)&direct_ip_interface_ignore
0252     },
0253     { USB_DEVICE(0x1199, 0x68AB) }, /* Sierra Wireless AR8550 */
0254     /* AT&T Direct IP LTE modems */
0255     { USB_DEVICE_AND_INTERFACE_INFO(0x0F3D, 0x68AA, 0xFF, 0xFF, 0xFF),
0256       .driver_info = (kernel_ulong_t)&direct_ip_interface_ignore
0257     },
0258     /* Airprime/Sierra Wireless Direct IP modems */
0259     { USB_DEVICE_AND_INTERFACE_INFO(0x0F3D, 0x68A3, 0xFF, 0xFF, 0xFF),
0260       .driver_info = (kernel_ulong_t)&direct_ip_interface_ignore
0261     },
0262 
0263     { }
0264 };
0265 MODULE_DEVICE_TABLE(usb, id_table);
0266 
0267 
0268 struct sierra_port_private {
0269     spinlock_t lock;    /* lock the structure */
0270     int outstanding_urbs;   /* number of out urbs in flight */
0271     struct usb_anchor active;
0272     struct usb_anchor delayed;
0273 
0274     int num_out_urbs;
0275     int num_in_urbs;
0276     /* Input endpoints and buffers for this port */
0277     struct urb *in_urbs[N_IN_URB_HM];
0278 
0279     /* Settings for the port */
0280     int rts_state;  /* Handshaking pins (outputs) */
0281     int dtr_state;
0282     int cts_state;  /* Handshaking pins (inputs) */
0283     int dsr_state;
0284     int dcd_state;
0285     int ri_state;
0286 };
0287 
0288 static int sierra_send_setup(struct usb_serial_port *port)
0289 {
0290     struct usb_serial *serial = port->serial;
0291     struct sierra_port_private *portdata;
0292     __u16 interface = 0;
0293     int val = 0;
0294     int do_send = 0;
0295     int retval;
0296 
0297     portdata = usb_get_serial_port_data(port);
0298 
0299     if (portdata->dtr_state)
0300         val |= 0x01;
0301     if (portdata->rts_state)
0302         val |= 0x02;
0303 
0304     /* If composite device then properly report interface */
0305     if (serial->num_ports == 1) {
0306         interface = sierra_interface_num(serial);
0307         /* Control message is sent only to interfaces with
0308          * interrupt_in endpoints
0309          */
0310         if (port->interrupt_in_urb) {
0311             /* send control message */
0312             do_send = 1;
0313         }
0314     }
0315 
0316     /* Otherwise the need to do non-composite mapping */
0317     else {
0318         if (port->bulk_out_endpointAddress == 2)
0319             interface = 0;
0320         else if (port->bulk_out_endpointAddress == 4)
0321             interface = 1;
0322         else if (port->bulk_out_endpointAddress == 5)
0323             interface = 2;
0324 
0325         do_send = 1;
0326     }
0327     if (!do_send)
0328         return 0;
0329 
0330     retval = usb_autopm_get_interface(serial->interface);
0331     if (retval < 0)
0332         return retval;
0333 
0334     retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
0335         0x22, 0x21, val, interface, NULL, 0, USB_CTRL_SET_TIMEOUT);
0336     usb_autopm_put_interface(serial->interface);
0337 
0338     return retval;
0339 }
0340 
0341 static int sierra_tiocmget(struct tty_struct *tty)
0342 {
0343     struct usb_serial_port *port = tty->driver_data;
0344     unsigned int value;
0345     struct sierra_port_private *portdata;
0346 
0347     portdata = usb_get_serial_port_data(port);
0348 
0349     value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
0350         ((portdata->dtr_state) ? TIOCM_DTR : 0) |
0351         ((portdata->cts_state) ? TIOCM_CTS : 0) |
0352         ((portdata->dsr_state) ? TIOCM_DSR : 0) |
0353         ((portdata->dcd_state) ? TIOCM_CAR : 0) |
0354         ((portdata->ri_state) ? TIOCM_RNG : 0);
0355 
0356     return value;
0357 }
0358 
0359 static int sierra_tiocmset(struct tty_struct *tty,
0360             unsigned int set, unsigned int clear)
0361 {
0362     struct usb_serial_port *port = tty->driver_data;
0363     struct sierra_port_private *portdata;
0364 
0365     portdata = usb_get_serial_port_data(port);
0366 
0367     if (set & TIOCM_RTS)
0368         portdata->rts_state = 1;
0369     if (set & TIOCM_DTR)
0370         portdata->dtr_state = 1;
0371 
0372     if (clear & TIOCM_RTS)
0373         portdata->rts_state = 0;
0374     if (clear & TIOCM_DTR)
0375         portdata->dtr_state = 0;
0376     return sierra_send_setup(port);
0377 }
0378 
0379 static void sierra_release_urb(struct urb *urb)
0380 {
0381     if (urb) {
0382         kfree(urb->transfer_buffer);
0383         usb_free_urb(urb);
0384     }
0385 }
0386 
0387 static void sierra_outdat_callback(struct urb *urb)
0388 {
0389     struct usb_serial_port *port = urb->context;
0390     struct sierra_port_private *portdata = usb_get_serial_port_data(port);
0391     struct sierra_intf_private *intfdata;
0392     int status = urb->status;
0393     unsigned long flags;
0394 
0395     intfdata = usb_get_serial_data(port->serial);
0396 
0397     /* free up the transfer buffer, as usb_free_urb() does not do this */
0398     kfree(urb->transfer_buffer);
0399     usb_autopm_put_interface_async(port->serial->interface);
0400     if (status)
0401         dev_dbg(&port->dev, "%s - nonzero write bulk status "
0402             "received: %d\n", __func__, status);
0403 
0404     spin_lock_irqsave(&portdata->lock, flags);
0405     --portdata->outstanding_urbs;
0406     spin_unlock_irqrestore(&portdata->lock, flags);
0407     spin_lock_irqsave(&intfdata->susp_lock, flags);
0408     --intfdata->in_flight;
0409     spin_unlock_irqrestore(&intfdata->susp_lock, flags);
0410 
0411     usb_serial_port_softint(port);
0412 }
0413 
0414 /* Write */
0415 static int sierra_write(struct tty_struct *tty, struct usb_serial_port *port,
0416                     const unsigned char *buf, int count)
0417 {
0418     struct sierra_port_private *portdata;
0419     struct sierra_intf_private *intfdata;
0420     struct usb_serial *serial = port->serial;
0421     unsigned long flags;
0422     unsigned char *buffer;
0423     struct urb *urb;
0424     size_t writesize = min((size_t)count, (size_t)MAX_TRANSFER);
0425     int retval = 0;
0426 
0427     /* verify that we actually have some data to write */
0428     if (count == 0)
0429         return 0;
0430 
0431     portdata = usb_get_serial_port_data(port);
0432     intfdata = usb_get_serial_data(serial);
0433 
0434     dev_dbg(&port->dev, "%s: write (%zd bytes)\n", __func__, writesize);
0435     spin_lock_irqsave(&portdata->lock, flags);
0436     dev_dbg(&port->dev, "%s - outstanding_urbs: %d\n", __func__,
0437         portdata->outstanding_urbs);
0438     if (portdata->outstanding_urbs > portdata->num_out_urbs) {
0439         spin_unlock_irqrestore(&portdata->lock, flags);
0440         dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
0441         return 0;
0442     }
0443     portdata->outstanding_urbs++;
0444     dev_dbg(&port->dev, "%s - 1, outstanding_urbs: %d\n", __func__,
0445         portdata->outstanding_urbs);
0446     spin_unlock_irqrestore(&portdata->lock, flags);
0447 
0448     retval = usb_autopm_get_interface_async(serial->interface);
0449     if (retval < 0) {
0450         spin_lock_irqsave(&portdata->lock, flags);
0451         portdata->outstanding_urbs--;
0452         spin_unlock_irqrestore(&portdata->lock, flags);
0453         goto error_simple;
0454     }
0455 
0456     buffer = kmemdup(buf, writesize, GFP_ATOMIC);
0457     if (!buffer) {
0458         retval = -ENOMEM;
0459         goto error_no_buffer;
0460     }
0461 
0462     urb = usb_alloc_urb(0, GFP_ATOMIC);
0463     if (!urb) {
0464         retval = -ENOMEM;
0465         goto error_no_urb;
0466     }
0467 
0468     usb_serial_debug_data(&port->dev, __func__, writesize, buffer);
0469 
0470     usb_fill_bulk_urb(urb, serial->dev,
0471               usb_sndbulkpipe(serial->dev,
0472                       port->bulk_out_endpointAddress),
0473               buffer, writesize, sierra_outdat_callback, port);
0474 
0475     /* Handle the need to send a zero length packet */
0476     urb->transfer_flags |= URB_ZERO_PACKET;
0477 
0478     spin_lock_irqsave(&intfdata->susp_lock, flags);
0479 
0480     if (intfdata->suspended) {
0481         usb_anchor_urb(urb, &portdata->delayed);
0482         spin_unlock_irqrestore(&intfdata->susp_lock, flags);
0483         goto skip_power;
0484     } else {
0485         usb_anchor_urb(urb, &portdata->active);
0486     }
0487     /* send it down the pipe */
0488     retval = usb_submit_urb(urb, GFP_ATOMIC);
0489     if (retval) {
0490         usb_unanchor_urb(urb);
0491         spin_unlock_irqrestore(&intfdata->susp_lock, flags);
0492         dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
0493             "with status = %d\n", __func__, retval);
0494         goto error;
0495     } else {
0496         intfdata->in_flight++;
0497         spin_unlock_irqrestore(&intfdata->susp_lock, flags);
0498     }
0499 
0500 skip_power:
0501     /* we are done with this urb, so let the host driver
0502      * really free it when it is finished with it */
0503     usb_free_urb(urb);
0504 
0505     return writesize;
0506 error:
0507     usb_free_urb(urb);
0508 error_no_urb:
0509     kfree(buffer);
0510 error_no_buffer:
0511     spin_lock_irqsave(&portdata->lock, flags);
0512     --portdata->outstanding_urbs;
0513     dev_dbg(&port->dev, "%s - 2. outstanding_urbs: %d\n", __func__,
0514         portdata->outstanding_urbs);
0515     spin_unlock_irqrestore(&portdata->lock, flags);
0516     usb_autopm_put_interface_async(serial->interface);
0517 error_simple:
0518     return retval;
0519 }
0520 
0521 static void sierra_indat_callback(struct urb *urb)
0522 {
0523     int err;
0524     int endpoint;
0525     struct usb_serial_port *port;
0526     unsigned char *data = urb->transfer_buffer;
0527     int status = urb->status;
0528 
0529     endpoint = usb_pipeendpoint(urb->pipe);
0530     port = urb->context;
0531 
0532     if (status) {
0533         dev_dbg(&port->dev, "%s: nonzero status: %d on"
0534             " endpoint %02x\n", __func__, status, endpoint);
0535     } else {
0536         if (urb->actual_length) {
0537             tty_insert_flip_string(&port->port, data,
0538                 urb->actual_length);
0539             tty_flip_buffer_push(&port->port);
0540 
0541             usb_serial_debug_data(&port->dev, __func__,
0542                           urb->actual_length, data);
0543         } else {
0544             dev_dbg(&port->dev, "%s: empty read urb"
0545                 " received\n", __func__);
0546         }
0547     }
0548 
0549     /* Resubmit urb so we continue receiving */
0550     if (status != -ESHUTDOWN && status != -EPERM) {
0551         usb_mark_last_busy(port->serial->dev);
0552         err = usb_submit_urb(urb, GFP_ATOMIC);
0553         if (err && err != -EPERM)
0554             dev_err(&port->dev, "resubmit read urb failed."
0555                 "(%d)\n", err);
0556     }
0557 }
0558 
0559 static void sierra_instat_callback(struct urb *urb)
0560 {
0561     int err;
0562     int status = urb->status;
0563     struct usb_serial_port *port =  urb->context;
0564     struct sierra_port_private *portdata = usb_get_serial_port_data(port);
0565     struct usb_serial *serial = port->serial;
0566 
0567     dev_dbg(&port->dev, "%s: urb %p port %p has data %p\n", __func__,
0568         urb, port, portdata);
0569 
0570     if (status == 0) {
0571         struct usb_ctrlrequest *req_pkt = urb->transfer_buffer;
0572 
0573         if (!req_pkt) {
0574             dev_dbg(&port->dev, "%s: NULL req_pkt\n",
0575                 __func__);
0576             return;
0577         }
0578         if ((req_pkt->bRequestType == 0xA1) &&
0579                 (req_pkt->bRequest == 0x20)) {
0580             int old_dcd_state;
0581             unsigned char signals = *((unsigned char *)
0582                     urb->transfer_buffer +
0583                     sizeof(struct usb_ctrlrequest));
0584 
0585             dev_dbg(&port->dev, "%s: signal x%x\n", __func__,
0586                 signals);
0587 
0588             old_dcd_state = portdata->dcd_state;
0589             portdata->cts_state = 1;
0590             portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
0591             portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
0592             portdata->ri_state = ((signals & 0x08) ? 1 : 0);
0593 
0594             if (old_dcd_state && !portdata->dcd_state)
0595                 tty_port_tty_hangup(&port->port, true);
0596         } else {
0597             dev_dbg(&port->dev, "%s: type %x req %x\n",
0598                 __func__, req_pkt->bRequestType,
0599                 req_pkt->bRequest);
0600         }
0601     } else
0602         dev_dbg(&port->dev, "%s: error %d\n", __func__, status);
0603 
0604     /* Resubmit urb so we continue receiving IRQ data */
0605     if (status != -ESHUTDOWN && status != -ENOENT) {
0606         usb_mark_last_busy(serial->dev);
0607         err = usb_submit_urb(urb, GFP_ATOMIC);
0608         if (err && err != -EPERM)
0609             dev_err(&port->dev, "%s: resubmit intr urb "
0610                 "failed. (%d)\n", __func__, err);
0611     }
0612 }
0613 
0614 static unsigned int sierra_write_room(struct tty_struct *tty)
0615 {
0616     struct usb_serial_port *port = tty->driver_data;
0617     struct sierra_port_private *portdata = usb_get_serial_port_data(port);
0618     unsigned long flags;
0619 
0620     /* try to give a good number back based on if we have any free urbs at
0621      * this point in time */
0622     spin_lock_irqsave(&portdata->lock, flags);
0623     if (portdata->outstanding_urbs > (portdata->num_out_urbs * 2) / 3) {
0624         spin_unlock_irqrestore(&portdata->lock, flags);
0625         dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
0626         return 0;
0627     }
0628     spin_unlock_irqrestore(&portdata->lock, flags);
0629 
0630     return 2048;
0631 }
0632 
0633 static unsigned int sierra_chars_in_buffer(struct tty_struct *tty)
0634 {
0635     struct usb_serial_port *port = tty->driver_data;
0636     struct sierra_port_private *portdata = usb_get_serial_port_data(port);
0637     unsigned long flags;
0638     unsigned int chars;
0639 
0640     /* NOTE: This overcounts somewhat. */
0641     spin_lock_irqsave(&portdata->lock, flags);
0642     chars = portdata->outstanding_urbs * MAX_TRANSFER;
0643     spin_unlock_irqrestore(&portdata->lock, flags);
0644 
0645     dev_dbg(&port->dev, "%s - %u\n", __func__, chars);
0646 
0647     return chars;
0648 }
0649 
0650 static void sierra_stop_rx_urbs(struct usb_serial_port *port)
0651 {
0652     int i;
0653     struct sierra_port_private *portdata = usb_get_serial_port_data(port);
0654 
0655     for (i = 0; i < portdata->num_in_urbs; i++)
0656         usb_kill_urb(portdata->in_urbs[i]);
0657 
0658     usb_kill_urb(port->interrupt_in_urb);
0659 }
0660 
0661 static int sierra_submit_rx_urbs(struct usb_serial_port *port, gfp_t mem_flags)
0662 {
0663     int ok_cnt;
0664     int err = -EINVAL;
0665     int i;
0666     struct urb *urb;
0667     struct sierra_port_private *portdata = usb_get_serial_port_data(port);
0668 
0669     ok_cnt = 0;
0670     for (i = 0; i < portdata->num_in_urbs; i++) {
0671         urb = portdata->in_urbs[i];
0672         if (!urb)
0673             continue;
0674         err = usb_submit_urb(urb, mem_flags);
0675         if (err) {
0676             dev_err(&port->dev, "%s: submit urb failed: %d\n",
0677                 __func__, err);
0678         } else {
0679             ok_cnt++;
0680         }
0681     }
0682 
0683     if (ok_cnt && port->interrupt_in_urb) {
0684         err = usb_submit_urb(port->interrupt_in_urb, mem_flags);
0685         if (err) {
0686             dev_err(&port->dev, "%s: submit intr urb failed: %d\n",
0687                 __func__, err);
0688         }
0689     }
0690 
0691     if (ok_cnt > 0) /* at least one rx urb submitted */
0692         return 0;
0693     else
0694         return err;
0695 }
0696 
0697 static struct urb *sierra_setup_urb(struct usb_serial *serial, int endpoint,
0698                     int dir, void *ctx, int len,
0699                     gfp_t mem_flags,
0700                     usb_complete_t callback)
0701 {
0702     struct urb  *urb;
0703     u8      *buf;
0704 
0705     urb = usb_alloc_urb(0, mem_flags);
0706     if (!urb)
0707         return NULL;
0708 
0709     buf = kmalloc(len, mem_flags);
0710     if (buf) {
0711         /* Fill URB using supplied data */
0712         usb_fill_bulk_urb(urb, serial->dev,
0713             usb_sndbulkpipe(serial->dev, endpoint) | dir,
0714             buf, len, callback, ctx);
0715 
0716         dev_dbg(&serial->dev->dev, "%s %c u : %p d:%p\n", __func__,
0717                 dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
0718     } else {
0719         sierra_release_urb(urb);
0720         urb = NULL;
0721     }
0722 
0723     return urb;
0724 }
0725 
0726 static void sierra_close(struct usb_serial_port *port)
0727 {
0728     int i;
0729     struct usb_serial *serial = port->serial;
0730     struct sierra_port_private *portdata;
0731     struct sierra_intf_private *intfdata = usb_get_serial_data(serial);
0732     struct urb *urb;
0733 
0734     portdata = usb_get_serial_port_data(port);
0735 
0736     /*
0737      * Need to take susp_lock to make sure port is not already being
0738      * resumed, but no need to hold it due to the tty-port initialized
0739      * flag.
0740      */
0741     spin_lock_irq(&intfdata->susp_lock);
0742     if (--intfdata->open_ports == 0)
0743         serial->interface->needs_remote_wakeup = 0;
0744     spin_unlock_irq(&intfdata->susp_lock);
0745 
0746     for (;;) {
0747         urb = usb_get_from_anchor(&portdata->delayed);
0748         if (!urb)
0749             break;
0750         kfree(urb->transfer_buffer);
0751         usb_free_urb(urb);
0752         usb_autopm_put_interface_async(serial->interface);
0753         spin_lock_irq(&portdata->lock);
0754         portdata->outstanding_urbs--;
0755         spin_unlock_irq(&portdata->lock);
0756     }
0757 
0758     sierra_stop_rx_urbs(port);
0759     usb_kill_anchored_urbs(&portdata->active);
0760 
0761     for (i = 0; i < portdata->num_in_urbs; i++) {
0762         sierra_release_urb(portdata->in_urbs[i]);
0763         portdata->in_urbs[i] = NULL;
0764     }
0765 
0766     usb_autopm_get_interface_no_resume(serial->interface);
0767 }
0768 
0769 static int sierra_open(struct tty_struct *tty, struct usb_serial_port *port)
0770 {
0771     struct sierra_port_private *portdata;
0772     struct usb_serial *serial = port->serial;
0773     struct sierra_intf_private *intfdata = usb_get_serial_data(serial);
0774     int i;
0775     int err;
0776     int endpoint;
0777     struct urb *urb;
0778 
0779     portdata = usb_get_serial_port_data(port);
0780 
0781     endpoint = port->bulk_in_endpointAddress;
0782     for (i = 0; i < portdata->num_in_urbs; i++) {
0783         urb = sierra_setup_urb(serial, endpoint, USB_DIR_IN, port,
0784                     IN_BUFLEN, GFP_KERNEL,
0785                     sierra_indat_callback);
0786         portdata->in_urbs[i] = urb;
0787     }
0788     /* clear halt condition */
0789     usb_clear_halt(serial->dev,
0790             usb_sndbulkpipe(serial->dev, endpoint) | USB_DIR_IN);
0791 
0792     err = sierra_submit_rx_urbs(port, GFP_KERNEL);
0793     if (err)
0794         goto err_submit;
0795 
0796     spin_lock_irq(&intfdata->susp_lock);
0797     if (++intfdata->open_ports == 1)
0798         serial->interface->needs_remote_wakeup = 1;
0799     spin_unlock_irq(&intfdata->susp_lock);
0800     usb_autopm_put_interface(serial->interface);
0801 
0802     return 0;
0803 
0804 err_submit:
0805     sierra_stop_rx_urbs(port);
0806 
0807     for (i = 0; i < portdata->num_in_urbs; i++) {
0808         sierra_release_urb(portdata->in_urbs[i]);
0809         portdata->in_urbs[i] = NULL;
0810     }
0811 
0812     return err;
0813 }
0814 
0815 
0816 static void sierra_dtr_rts(struct usb_serial_port *port, int on)
0817 {
0818     struct sierra_port_private *portdata;
0819 
0820     portdata = usb_get_serial_port_data(port);
0821     portdata->rts_state = on;
0822     portdata->dtr_state = on;
0823 
0824     sierra_send_setup(port);
0825 }
0826 
0827 static int sierra_startup(struct usb_serial *serial)
0828 {
0829     struct sierra_intf_private *intfdata;
0830 
0831     intfdata = kzalloc(sizeof(*intfdata), GFP_KERNEL);
0832     if (!intfdata)
0833         return -ENOMEM;
0834 
0835     spin_lock_init(&intfdata->susp_lock);
0836 
0837     usb_set_serial_data(serial, intfdata);
0838 
0839     /* Set Device mode to D0 */
0840     sierra_set_power_state(serial->dev, 0x0000);
0841 
0842     /* Check NMEA and set */
0843     if (nmea)
0844         sierra_vsc_set_nmea(serial->dev, 1);
0845 
0846     return 0;
0847 }
0848 
0849 static void sierra_release(struct usb_serial *serial)
0850 {
0851     struct sierra_intf_private *intfdata;
0852 
0853     intfdata = usb_get_serial_data(serial);
0854     kfree(intfdata);
0855 }
0856 
0857 static int sierra_port_probe(struct usb_serial_port *port)
0858 {
0859     struct usb_serial *serial = port->serial;
0860     struct sierra_port_private *portdata;
0861     const struct sierra_iface_list *himemory_list;
0862     u8 ifnum;
0863 
0864     portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
0865     if (!portdata)
0866         return -ENOMEM;
0867 
0868     spin_lock_init(&portdata->lock);
0869     init_usb_anchor(&portdata->active);
0870     init_usb_anchor(&portdata->delayed);
0871 
0872     /* Assume low memory requirements */
0873     portdata->num_out_urbs = N_OUT_URB;
0874     portdata->num_in_urbs  = N_IN_URB;
0875 
0876     /* Determine actual memory requirements */
0877     if (serial->num_ports == 1) {
0878         /* Get interface number for composite device */
0879         ifnum = sierra_interface_num(serial);
0880         himemory_list = &typeB_interface_list;
0881     } else {
0882         /* This is really the usb-serial port number of the interface
0883          * rather than the interface number.
0884          */
0885         ifnum = port->port_number;
0886         himemory_list = &typeA_interface_list;
0887     }
0888 
0889     if (is_listed(ifnum, himemory_list)) {
0890         portdata->num_out_urbs = N_OUT_URB_HM;
0891         portdata->num_in_urbs  = N_IN_URB_HM;
0892     }
0893 
0894     dev_dbg(&port->dev,
0895             "Memory usage (urbs) interface #%d, in=%d, out=%d\n",
0896             ifnum, portdata->num_in_urbs, portdata->num_out_urbs);
0897 
0898     usb_set_serial_port_data(port, portdata);
0899 
0900     return 0;
0901 }
0902 
0903 static void sierra_port_remove(struct usb_serial_port *port)
0904 {
0905     struct sierra_port_private *portdata;
0906 
0907     portdata = usb_get_serial_port_data(port);
0908     usb_set_serial_port_data(port, NULL);
0909     kfree(portdata);
0910 }
0911 
0912 #ifdef CONFIG_PM
0913 static void stop_read_write_urbs(struct usb_serial *serial)
0914 {
0915     int i;
0916     struct usb_serial_port *port;
0917     struct sierra_port_private *portdata;
0918 
0919     /* Stop reading/writing urbs */
0920     for (i = 0; i < serial->num_ports; ++i) {
0921         port = serial->port[i];
0922         portdata = usb_get_serial_port_data(port);
0923         if (!portdata)
0924             continue;
0925         sierra_stop_rx_urbs(port);
0926         usb_kill_anchored_urbs(&portdata->active);
0927     }
0928 }
0929 
0930 static int sierra_suspend(struct usb_serial *serial, pm_message_t message)
0931 {
0932     struct sierra_intf_private *intfdata = usb_get_serial_data(serial);
0933 
0934     spin_lock_irq(&intfdata->susp_lock);
0935     if (PMSG_IS_AUTO(message)) {
0936         if (intfdata->in_flight) {
0937             spin_unlock_irq(&intfdata->susp_lock);
0938             return -EBUSY;
0939         }
0940     }
0941     intfdata->suspended = 1;
0942     spin_unlock_irq(&intfdata->susp_lock);
0943 
0944     stop_read_write_urbs(serial);
0945 
0946     return 0;
0947 }
0948 
0949 /* Caller must hold susp_lock. */
0950 static int sierra_submit_delayed_urbs(struct usb_serial_port *port)
0951 {
0952     struct sierra_port_private *portdata = usb_get_serial_port_data(port);
0953     struct sierra_intf_private *intfdata;
0954     struct urb *urb;
0955     int ec = 0;
0956     int err;
0957 
0958     intfdata = usb_get_serial_data(port->serial);
0959 
0960     for (;;) {
0961         urb = usb_get_from_anchor(&portdata->delayed);
0962         if (!urb)
0963             break;
0964 
0965         usb_anchor_urb(urb, &portdata->active);
0966         intfdata->in_flight++;
0967         err = usb_submit_urb(urb, GFP_ATOMIC);
0968         if (err) {
0969             dev_err(&port->dev, "%s - submit urb failed: %d",
0970                     __func__, err);
0971             ec++;
0972             intfdata->in_flight--;
0973             usb_unanchor_urb(urb);
0974             kfree(urb->transfer_buffer);
0975             usb_free_urb(urb);
0976 
0977             spin_lock(&portdata->lock);
0978             portdata->outstanding_urbs--;
0979             spin_unlock(&portdata->lock);
0980         }
0981     }
0982 
0983     if (ec)
0984         return -EIO;
0985 
0986     return 0;
0987 }
0988 
0989 static int sierra_resume(struct usb_serial *serial)
0990 {
0991     struct usb_serial_port *port;
0992     struct sierra_intf_private *intfdata = usb_get_serial_data(serial);
0993     int ec = 0;
0994     int i, err;
0995 
0996     spin_lock_irq(&intfdata->susp_lock);
0997     for (i = 0; i < serial->num_ports; i++) {
0998         port = serial->port[i];
0999 
1000         if (!tty_port_initialized(&port->port))
1001             continue;
1002 
1003         err = sierra_submit_delayed_urbs(port);
1004         if (err)
1005             ec++;
1006 
1007         err = sierra_submit_rx_urbs(port, GFP_ATOMIC);
1008         if (err)
1009             ec++;
1010     }
1011     intfdata->suspended = 0;
1012     spin_unlock_irq(&intfdata->susp_lock);
1013 
1014     return ec ? -EIO : 0;
1015 }
1016 
1017 #else
1018 #define sierra_suspend NULL
1019 #define sierra_resume NULL
1020 #endif
1021 
1022 static struct usb_serial_driver sierra_device = {
1023     .driver = {
1024         .owner =    THIS_MODULE,
1025         .name =     "sierra",
1026     },
1027     .description       = "Sierra USB modem",
1028     .id_table          = id_table,
1029     .calc_num_ports    = sierra_calc_num_ports,
1030     .probe         = sierra_probe,
1031     .open              = sierra_open,
1032     .close             = sierra_close,
1033     .dtr_rts       = sierra_dtr_rts,
1034     .write             = sierra_write,
1035     .write_room        = sierra_write_room,
1036     .chars_in_buffer   = sierra_chars_in_buffer,
1037     .tiocmget          = sierra_tiocmget,
1038     .tiocmset          = sierra_tiocmset,
1039     .attach            = sierra_startup,
1040     .release           = sierra_release,
1041     .port_probe        = sierra_port_probe,
1042     .port_remove       = sierra_port_remove,
1043     .suspend       = sierra_suspend,
1044     .resume        = sierra_resume,
1045     .read_int_callback = sierra_instat_callback,
1046 };
1047 
1048 static struct usb_serial_driver * const serial_drivers[] = {
1049     &sierra_device, NULL
1050 };
1051 
1052 module_usb_serial_driver(serial_drivers, id_table);
1053 
1054 MODULE_AUTHOR(DRIVER_AUTHOR);
1055 MODULE_DESCRIPTION(DRIVER_DESC);
1056 MODULE_LICENSE("GPL v2");
1057 
1058 module_param(nmea, bool, 0644);
1059 MODULE_PARM_DESC(nmea, "NMEA streaming");