blob: 45883988a005bd660580fb4057d57bb6f12ee7b8 [file] [log] [blame]
Kevin Lloyd69de51f2006-06-30 11:17:55 -07001/*
Kevin Lloyd033a3fb2006-10-13 23:53:21 -07002 USB Driver for Sierra Wireless
3
Elina Pasheva40d2ff32009-04-29 10:26:46 -07004 Copyright (C) 2006, 2007, 2008 Kevin Lloyd <klloyd@sierrawireless.com>,
5
6 Copyright (C) 2008, 2009 Elina Pasheva, Matthew Safar, Rory Filer
7 <linux@sierrawireless.com>
Kevin Lloyd033a3fb2006-10-13 23:53:21 -07008
9 IMPORTANT DISCLAIMER: This driver is not commercially supported by
10 Sierra Wireless. Use at your own risk.
11
12 This driver is free software; you can redistribute it and/or modify
13 it under the terms of Version 2 of the GNU General Public License as
14 published by the Free Software Foundation.
15
16 Portions based on the option driver by Matthias Urlichs <smurf@smurf.noris.de>
17 Whom based his on the Keyspan driver by Hugh Blemings <hugh@blemings.org>
Kevin Lloyd033a3fb2006-10-13 23:53:21 -070018*/
19
Elina Pasheva0f0ba792009-09-17 15:26:20 -070020#define DRIVER_VERSION "v.1.3.8"
Elina Pasheva40d2ff32009-04-29 10:26:46 -070021#define DRIVER_AUTHOR "Kevin Lloyd, Elina Pasheva, Matthew Safar, Rory Filer"
Kevin Lloyd033a3fb2006-10-13 23:53:21 -070022#define DRIVER_DESC "USB Driver for Sierra Wireless USB modems"
Kevin Lloyd69de51f2006-06-30 11:17:55 -070023
24#include <linux/kernel.h>
Kevin Lloyd033a3fb2006-10-13 23:53:21 -070025#include <linux/jiffies.h>
26#include <linux/errno.h>
Kevin Lloyd69de51f2006-06-30 11:17:55 -070027#include <linux/tty.h>
Kevin Lloyd033a3fb2006-10-13 23:53:21 -070028#include <linux/tty_flip.h>
Kevin Lloyd69de51f2006-06-30 11:17:55 -070029#include <linux/module.h>
30#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070031#include <linux/usb/serial.h>
Kevin Lloyd69de51f2006-06-30 11:17:55 -070032
Kevin Lloyd228426e2008-01-10 11:11:04 -080033#define SWIMS_USB_REQUEST_SetPower 0x00
34#define SWIMS_USB_REQUEST_SetNmea 0x07
Kevin Lloyd112225b2007-07-16 13:49:27 -070035
Elina Pashevab748bb72009-04-24 18:41:49 -070036#define N_IN_URB 8
37#define N_OUT_URB 64
Kevin Lloyd112225b2007-07-16 13:49:27 -070038#define IN_BUFLEN 4096
39
Elina Pasheva40d2ff32009-04-29 10:26:46 -070040#define MAX_TRANSFER (PAGE_SIZE - 512)
41/* MAX_TRANSFER is chosen so that the VM is not stressed by
42 allocations > PAGE_SIZE and the number of packets in a page
43 is an integer 512 is the largest possible packet on EHCI */
44
Kevin Lloyd112225b2007-07-16 13:49:27 -070045static int debug;
Kevin Lloyd228426e2008-01-10 11:11:04 -080046static int nmea;
Kevin Lloyd112225b2007-07-16 13:49:27 -070047
Elina Pasheva4db22992009-06-11 14:32:01 +010048/* Used in interface blacklisting */
49struct sierra_iface_info {
50 const u32 infolen; /* number of interface numbers on blacklist */
51 const u8 *ifaceinfo; /* pointer to the array holding the numbers */
52};
53
Oliver Neukume6929a92009-09-04 23:19:53 +020054struct sierra_intf_private {
55 spinlock_t susp_lock;
56 unsigned int suspended:1;
57 int in_flight;
58};
59
Adrian Bunk82a24e62007-07-29 16:59:02 +020060static int sierra_set_power_state(struct usb_device *udev, __u16 swiState)
Kevin Lloyd112225b2007-07-16 13:49:27 -070061{
62 int result;
Elina Pasheva5d44b362009-04-27 18:41:52 -070063 dev_dbg(&udev->dev, "%s\n", __func__);
Kevin Lloyd112225b2007-07-16 13:49:27 -070064 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Kevin Lloyd228426e2008-01-10 11:11:04 -080065 SWIMS_USB_REQUEST_SetPower, /* __u8 request */
Kevin Lloydf3564de2008-03-28 10:05:08 -070066 USB_TYPE_VENDOR, /* __u8 request type */
Kevin Lloyd228426e2008-01-10 11:11:04 -080067 swiState, /* __u16 value */
68 0, /* __u16 index */
69 NULL, /* void *data */
70 0, /* __u16 size */
71 USB_CTRL_SET_TIMEOUT); /* int timeout */
Kevin Lloyd112225b2007-07-16 13:49:27 -070072 return result;
73}
74
Kevin Lloyd228426e2008-01-10 11:11:04 -080075static int sierra_vsc_set_nmea(struct usb_device *udev, __u16 enable)
Kevin Lloyd112225b2007-07-16 13:49:27 -070076{
77 int result;
Elina Pasheva5d44b362009-04-27 18:41:52 -070078 dev_dbg(&udev->dev, "%s\n", __func__);
Kevin Lloyd228426e2008-01-10 11:11:04 -080079 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
80 SWIMS_USB_REQUEST_SetNmea, /* __u8 request */
Kevin Lloydf3564de2008-03-28 10:05:08 -070081 USB_TYPE_VENDOR, /* __u8 request type */
Kevin Lloyd228426e2008-01-10 11:11:04 -080082 enable, /* __u16 value */
83 0x0000, /* __u16 index */
84 NULL, /* void *data */
85 0, /* __u16 size */
86 USB_CTRL_SET_TIMEOUT); /* int timeout */
87 return result;
88}
Kevin Lloyd112225b2007-07-16 13:49:27 -070089
Kevin Lloyd228426e2008-01-10 11:11:04 -080090static int sierra_calc_num_ports(struct usb_serial *serial)
91{
Elina Pasheva9636b682009-05-12 13:12:54 -070092 int num_ports = 0;
93 u8 ifnum, numendpoints;
94
Elina Pasheva5d44b362009-04-27 18:41:52 -070095 dev_dbg(&serial->dev->dev, "%s\n", __func__);
Kevin Lloyd112225b2007-07-16 13:49:27 -070096
Elina Pasheva9636b682009-05-12 13:12:54 -070097 ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
98 numendpoints = serial->interface->cur_altsetting->desc.bNumEndpoints;
Kevin Lloyd228426e2008-01-10 11:11:04 -080099
Elina Pasheva9636b682009-05-12 13:12:54 -0700100 /* Dummy interface present on some SKUs should be ignored */
101 if (ifnum == 0x99)
102 num_ports = 0;
103 else if (numendpoints <= 3)
104 num_ports = 1;
105 else
106 num_ports = (numendpoints-1)/2;
107 return num_ports;
Kevin Lloyd228426e2008-01-10 11:11:04 -0800108}
109
Elina Pasheva4db22992009-06-11 14:32:01 +0100110static int is_blacklisted(const u8 ifnum,
111 const struct sierra_iface_info *blacklist)
112{
113 const u8 *info;
114 int i;
115
116 if (blacklist) {
117 info = blacklist->ifaceinfo;
118
119 for (i = 0; i < blacklist->infolen; i++) {
120 if (info[i] == ifnum)
121 return 1;
122 }
123 }
124 return 0;
125}
126
Kevin Lloyd71069672008-04-02 11:24:56 -0700127static int sierra_calc_interface(struct usb_serial *serial)
128{
Kevin Lloyd4e0fee82008-07-10 14:14:47 -0700129 int interface;
130 struct usb_interface *p_interface;
131 struct usb_host_interface *p_host_interface;
Elina Pasheva5d44b362009-04-27 18:41:52 -0700132 dev_dbg(&serial->dev->dev, "%s\n", __func__);
Kevin Lloyd71069672008-04-02 11:24:56 -0700133
Kevin Lloyd4e0fee82008-07-10 14:14:47 -0700134 /* Get the interface structure pointer from the serial struct */
135 p_interface = serial->interface;
Kevin Lloyd71069672008-04-02 11:24:56 -0700136
Kevin Lloyd4e0fee82008-07-10 14:14:47 -0700137 /* Get a pointer to the host interface structure */
138 p_host_interface = p_interface->cur_altsetting;
Kevin Lloyd71069672008-04-02 11:24:56 -0700139
Kevin Lloyd4e0fee82008-07-10 14:14:47 -0700140 /* read the interface descriptor for this active altsetting
141 * to find out the interface number we are on
142 */
143 interface = p_host_interface->desc.bInterfaceNumber;
Kevin Lloyd71069672008-04-02 11:24:56 -0700144
Kevin Lloyd4e0fee82008-07-10 14:14:47 -0700145 return interface;
Kevin Lloyd71069672008-04-02 11:24:56 -0700146}
147
Kevin Lloyd228426e2008-01-10 11:11:04 -0800148static int sierra_probe(struct usb_serial *serial,
149 const struct usb_device_id *id)
150{
151 int result = 0;
152 struct usb_device *udev;
Oliver Neukume6929a92009-09-04 23:19:53 +0200153 struct sierra_intf_private *data;
Kevin Lloyd228426e2008-01-10 11:11:04 -0800154 u8 ifnum;
Kevin Lloyde3173b22008-07-10 14:14:51 -0700155
Kevin Lloyd228426e2008-01-10 11:11:04 -0800156 udev = serial->dev;
Elina Pasheva9636b682009-05-12 13:12:54 -0700157 dev_dbg(&udev->dev, "%s\n", __func__);
Kevin Lloyd228426e2008-01-10 11:11:04 -0800158
Kevin Lloyde3173b22008-07-10 14:14:51 -0700159 ifnum = sierra_calc_interface(serial);
Kevin Lloyde3173b22008-07-10 14:14:51 -0700160 /*
161 * If this interface supports more than 1 alternate
162 * select the 2nd one
163 */
164 if (serial->interface->num_altsetting == 2) {
165 dev_dbg(&udev->dev, "Selecting alt setting for interface %d\n",
166 ifnum);
167 /* We know the alternate setting is 1 for the MC8785 */
168 usb_set_interface(udev, ifnum, 1);
169 }
Kevin Lloyd71069672008-04-02 11:24:56 -0700170
Elina Pasheva4db22992009-06-11 14:32:01 +0100171 /* ifnum could have changed - by calling usb_set_interface */
172 ifnum = sierra_calc_interface(serial);
173
174 if (is_blacklisted(ifnum,
175 (struct sierra_iface_info *)id->driver_info)) {
176 dev_dbg(&serial->dev->dev,
177 "Ignoring blacklisted interface #%d\n", ifnum);
178 return -ENODEV;
179 }
180
Oliver Neukume6929a92009-09-04 23:19:53 +0200181 data = serial->private = kzalloc(sizeof(struct sierra_intf_private), GFP_KERNEL);
182 if (!data)
183 return -ENOMEM;
184 spin_lock_init(&data->susp_lock);
185
Kevin Lloyd228426e2008-01-10 11:11:04 -0800186 return result;
Kevin Lloyd112225b2007-07-16 13:49:27 -0700187}
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700188
Elina Pasheva4db22992009-06-11 14:32:01 +0100189static const u8 direct_ip_non_serial_ifaces[] = { 7, 8, 9, 10, 11 };
190static const struct sierra_iface_info direct_ip_interface_blacklist = {
191 .infolen = ARRAY_SIZE(direct_ip_non_serial_ifaces),
192 .ifaceinfo = direct_ip_non_serial_ifaces,
193};
194
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700195static struct usb_device_id id_table [] = {
Elina Pashevac5f3d872009-07-09 17:55:18 -0700196 { USB_DEVICE(0x0F3D, 0x0112) }, /* Airprime/Sierra PC 5220 */
197 { USB_DEVICE(0x03F0, 0x1B1D) }, /* HP ev2200 a.k.a MC5720 */
198 { USB_DEVICE(0x03F0, 0x1E1D) }, /* HP hs2300 a.k.a MC8775 */
199
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700200 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800201 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
Greg Kroah-Hartman90ac3c82002-04-09 12:14:34 -0700202 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800203 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
agilmore@wirelessbeehive.comb9e13ac2007-12-04 11:37:12 -0700204 { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
Elina Pashevac5f3d872009-07-09 17:55:18 -0700205 { USB_DEVICE(0x1199, 0x0022) }, /* Sierra Wireless EM5725 */
206 { USB_DEVICE(0x1199, 0x0024) }, /* Sierra Wireless MC5727 */
207 { USB_DEVICE(0x1199, 0x0224) }, /* Sierra Wireless MC5727 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800208 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
209 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
Elina Pashevac5f3d872009-07-09 17:55:18 -0700210 { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700211 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U */
Elina Pashevac5f3d872009-07-09 17:55:18 -0700212 /* Sierra Wireless C597 */
Kevin Lloyd4e0fee82008-07-10 14:14:47 -0700213 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0023, 0xFF, 0xFF, 0xFF) },
Elina Pashevac5f3d872009-07-09 17:55:18 -0700214 /* Sierra Wireless T598 */
Kevin Lloyde3173b22008-07-10 14:14:51 -0700215 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0025, 0xFF, 0xFF, 0xFF) },
Elina Pashevac5f3d872009-07-09 17:55:18 -0700216 { USB_DEVICE(0x1199, 0x0026) }, /* Sierra Wireless T11 */
217 { USB_DEVICE(0x1199, 0x0027) }, /* Sierra Wireless AC402 */
218 { USB_DEVICE(0x1199, 0x0028) }, /* Sierra Wireless MC5728 */
219 { USB_DEVICE(0x1199, 0x0029) }, /* Sierra Wireless Device */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700220
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700221 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
222 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
Elina Pashevac5f3d872009-07-09 17:55:18 -0700223 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
224 { USB_DEVICE(0x1199, 0x6805) }, /* Sierra Wireless MC8765 */
225 { USB_DEVICE(0x1199, 0x6808) }, /* Sierra Wireless MC8755 */
226 { USB_DEVICE(0x1199, 0x6809) }, /* Sierra Wireless MC8765 */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700227 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
Elina Pashevac5f3d872009-07-09 17:55:18 -0700228 { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 */
Kevin Lloyd7f170a62008-03-14 00:53:24 -0700229 { USB_DEVICE(0x1199, 0x6815) }, /* Sierra Wireless MC8775 */
Elina Pashevac5f3d872009-07-09 17:55:18 -0700230 { USB_DEVICE(0x1199, 0x6816) }, /* Sierra Wireless MC8775 */
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700231 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
Kevin Lloyd71069672008-04-02 11:24:56 -0700232 { USB_DEVICE(0x1199, 0x6821) }, /* Sierra Wireless AirCard 875U */
Elina Pashevac5f3d872009-07-09 17:55:18 -0700233 { USB_DEVICE(0x1199, 0x6822) }, /* Sierra Wireless AirCard 875E */
Kevin Lloyd4e0fee82008-07-10 14:14:47 -0700234 { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780 */
235 { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781 */
Elina Pashevac5f3d872009-07-09 17:55:18 -0700236 { USB_DEVICE(0x1199, 0x6834) }, /* Sierra Wireless MC8780 */
237 { USB_DEVICE(0x1199, 0x6835) }, /* Sierra Wireless MC8781 */
238 { USB_DEVICE(0x1199, 0x6838) }, /* Sierra Wireless MC8780 */
239 { USB_DEVICE(0x1199, 0x6839) }, /* Sierra Wireless MC8781 */
Kevin Lloydb77a5c72008-09-17 09:03:38 -0700240 { USB_DEVICE(0x1199, 0x683A) }, /* Sierra Wireless MC8785 */
Kevin Lloyde3173b22008-07-10 14:14:51 -0700241 { USB_DEVICE(0x1199, 0x683B) }, /* Sierra Wireless MC8785 Composite */
Elina Pasheva4db22992009-06-11 14:32:01 +0100242 /* Sierra Wireless MC8790, MC8791, MC8792 Composite */
243 { USB_DEVICE(0x1199, 0x683C) },
244 { USB_DEVICE(0x1199, 0x683D) }, /* Sierra Wireless MC8791 Composite */
245 /* Sierra Wireless MC8790, MC8791, MC8792 */
246 { USB_DEVICE(0x1199, 0x683E) },
Kevin Lloyd9454c462007-07-16 13:49:29 -0700247 { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
248 { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
249 { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880 E */
250 { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881 E */
Kevin Lloyd6835b322008-01-10 11:11:04 -0800251 { USB_DEVICE(0x1199, 0x6855) }, /* Sierra Wireless AirCard 880 U */
Jessica L. Blank62aad3a2007-12-30 20:11:35 -0600252 { USB_DEVICE(0x1199, 0x6856) }, /* Sierra Wireless AirCard 881 U */
Kevin Lloyde3173b22008-07-10 14:14:51 -0700253 { USB_DEVICE(0x1199, 0x6859) }, /* Sierra Wireless AirCard 885 E */
254 { USB_DEVICE(0x1199, 0x685A) }, /* Sierra Wireless AirCard 885 E */
255 /* Sierra Wireless C885 */
256 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6880, 0xFF, 0xFF, 0xFF)},
Elina Pashevac5f3d872009-07-09 17:55:18 -0700257 /* Sierra Wireless C888, Air Card 501, USB 303, USB 304 */
Kevin Lloyde3173b22008-07-10 14:14:51 -0700258 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6890, 0xFF, 0xFF, 0xFF)},
Elina Pashevac5f3d872009-07-09 17:55:18 -0700259 /* Sierra Wireless C22/C33 */
Kevin Lloyd73b2c202008-08-25 19:20:40 -0700260 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6891, 0xFF, 0xFF, 0xFF)},
Elina Pashevac5f3d872009-07-09 17:55:18 -0700261 /* Sierra Wireless HSPA Non-Composite Device */
Kevin Lloyde3173b22008-07-10 14:14:51 -0700262 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6892, 0xFF, 0xFF, 0xFF)},
Elina Pashevac5f3d872009-07-09 17:55:18 -0700263 { USB_DEVICE(0x1199, 0x6893) }, /* Sierra Wireless Device */
Elina Pasheva4db22992009-06-11 14:32:01 +0100264 { USB_DEVICE(0x1199, 0x68A3), /* Sierra Wireless Direct IP modems */
265 .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
266 },
267
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700268 { }
269};
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700270MODULE_DEVICE_TABLE(usb, id_table);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700271
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700272static struct usb_driver sierra_driver = {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700273 .name = "sierra",
Kevin Lloyd228426e2008-01-10 11:11:04 -0800274 .probe = usb_serial_probe,
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700275 .disconnect = usb_serial_disconnect,
Oliver Neukume6929a92009-09-04 23:19:53 +0200276 .suspend = usb_serial_suspend,
277 .resume = usb_serial_resume,
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700278 .id_table = id_table,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700279 .no_dynamic_id = 1,
Oliver Neukume6929a92009-09-04 23:19:53 +0200280 .supports_autosuspend = 1,
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700281};
282
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700283struct sierra_port_private {
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900284 spinlock_t lock; /* lock the structure */
285 int outstanding_urbs; /* number of out urbs in flight */
Oliver Neukume6929a92009-09-04 23:19:53 +0200286 struct usb_anchor active;
287 struct usb_anchor delayed;
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900288
Oliver Neukum4f4f9c52008-03-14 00:53:24 -0700289 /* Input endpoints and buffers for this port */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700290 struct urb *in_urbs[N_IN_URB];
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700291
292 /* Settings for the port */
293 int rts_state; /* Handshaking pins (outputs) */
294 int dtr_state;
295 int cts_state; /* Handshaking pins (inputs) */
296 int dsr_state;
297 int dcd_state;
298 int ri_state;
Oliver Neukume6929a92009-09-04 23:19:53 +0200299
300 unsigned int opened:1;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700301};
302
Alan Cox335f8512009-06-11 12:26:29 +0100303static int sierra_send_setup(struct usb_serial_port *port)
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700304{
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700305 struct usb_serial *serial = port->serial;
306 struct sierra_port_private *portdata;
Kevin Lloyd228426e2008-01-10 11:11:04 -0800307 __u16 interface = 0;
Alan Cox335f8512009-06-11 12:26:29 +0100308 int val = 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700309
Elina Pasheva5d44b362009-04-27 18:41:52 -0700310 dev_dbg(&port->dev, "%s\n", __func__);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700311
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700312 portdata = usb_get_serial_port_data(port);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700313
Alan Cox335f8512009-06-11 12:26:29 +0100314 if (portdata->dtr_state)
315 val |= 0x01;
316 if (portdata->rts_state)
317 val |= 0x02;
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700318
Alan Cox335f8512009-06-11 12:26:29 +0100319 /* If composite device then properly report interface */
Alan Cox00b040d2009-06-11 14:29:29 +0100320 if (serial->num_ports == 1) {
Alan Cox335f8512009-06-11 12:26:29 +0100321 interface = sierra_calc_interface(serial);
Alan Cox00b040d2009-06-11 14:29:29 +0100322 /* Control message is sent only to interfaces with
323 * interrupt_in endpoints
324 */
325 if (port->interrupt_in_urb) {
326 /* send control message */
327 return usb_control_msg(serial->dev,
328 usb_rcvctrlpipe(serial->dev, 0),
329 0x22, 0x21, val, interface,
330 NULL, 0, USB_CTRL_SET_TIMEOUT);
331 }
332 }
Kevin Lloyde3173b22008-07-10 14:14:51 -0700333
Alan Cox335f8512009-06-11 12:26:29 +0100334 /* Otherwise the need to do non-composite mapping */
335 else {
336 if (port->bulk_out_endpointAddress == 2)
337 interface = 0;
338 else if (port->bulk_out_endpointAddress == 4)
339 interface = 1;
340 else if (port->bulk_out_endpointAddress == 5)
341 interface = 2;
Alan Cox00b040d2009-06-11 14:29:29 +0100342 return usb_control_msg(serial->dev,
Alan Cox335f8512009-06-11 12:26:29 +0100343 usb_rcvctrlpipe(serial->dev, 0),
344 0x22, 0x21, val, interface,
345 NULL, 0, USB_CTRL_SET_TIMEOUT);
Alan Cox00b040d2009-06-11 14:29:29 +0100346 }
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700347 return 0;
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700348}
349
Alan Cox95da3102008-07-22 11:09:07 +0100350static void sierra_set_termios(struct tty_struct *tty,
351 struct usb_serial_port *port, struct ktermios *old_termios)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700352{
Elina Pasheva5d44b362009-04-27 18:41:52 -0700353 dev_dbg(&port->dev, "%s\n", __func__);
Alan Cox95da3102008-07-22 11:09:07 +0100354 tty_termios_copy_hw(tty->termios, old_termios);
Alan Cox335f8512009-06-11 12:26:29 +0100355 sierra_send_setup(port);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700356}
357
Alan Cox95da3102008-07-22 11:09:07 +0100358static int sierra_tiocmget(struct tty_struct *tty, struct file *file)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700359{
Alan Cox95da3102008-07-22 11:09:07 +0100360 struct usb_serial_port *port = tty->driver_data;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700361 unsigned int value;
362 struct sierra_port_private *portdata;
363
Elina Pasheva5d44b362009-04-27 18:41:52 -0700364 dev_dbg(&port->dev, "%s\n", __func__);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700365 portdata = usb_get_serial_port_data(port);
366
367 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
368 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
369 ((portdata->cts_state) ? TIOCM_CTS : 0) |
370 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
371 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
372 ((portdata->ri_state) ? TIOCM_RNG : 0);
373
374 return value;
375}
376
Alan Cox95da3102008-07-22 11:09:07 +0100377static int sierra_tiocmset(struct tty_struct *tty, struct file *file,
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700378 unsigned int set, unsigned int clear)
379{
Alan Cox95da3102008-07-22 11:09:07 +0100380 struct usb_serial_port *port = tty->driver_data;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700381 struct sierra_port_private *portdata;
382
383 portdata = usb_get_serial_port_data(port);
384
385 if (set & TIOCM_RTS)
386 portdata->rts_state = 1;
387 if (set & TIOCM_DTR)
388 portdata->dtr_state = 1;
389
390 if (clear & TIOCM_RTS)
391 portdata->rts_state = 0;
392 if (clear & TIOCM_DTR)
393 portdata->dtr_state = 0;
Alan Cox335f8512009-06-11 12:26:29 +0100394 return sierra_send_setup(port);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700395}
396
Elina Pashevab9a44bc12009-06-11 14:30:21 +0100397static void sierra_release_urb(struct urb *urb)
398{
399 struct usb_serial_port *port;
400 if (urb) {
401 port = urb->context;
402 dev_dbg(&port->dev, "%s: %p\n", __func__, urb);
403 kfree(urb->transfer_buffer);
404 usb_free_urb(urb);
405 }
406}
407
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900408static void sierra_outdat_callback(struct urb *urb)
409{
410 struct usb_serial_port *port = urb->context;
411 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
Oliver Neukume6929a92009-09-04 23:19:53 +0200412 struct sierra_intf_private *intfdata;
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900413 int status = urb->status;
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900414
Elina Pasheva5d44b362009-04-27 18:41:52 -0700415 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
Oliver Neukume6929a92009-09-04 23:19:53 +0200416 intfdata = port->serial->private;
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900417
418 /* free up the transfer buffer, as usb_free_urb() does not do this */
419 kfree(urb->transfer_buffer);
Oliver Neukume6929a92009-09-04 23:19:53 +0200420 usb_autopm_put_interface_async(port->serial->interface);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900421 if (status)
Kevin Lloyd7384a922008-09-16 21:05:24 -0700422 dev_dbg(&port->dev, "%s - nonzero write bulk status "
Elina Pasheva5d44b362009-04-27 18:41:52 -0700423 "received: %d\n", __func__, status);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900424
Oliver Neukume6929a92009-09-04 23:19:53 +0200425 spin_lock(&portdata->lock);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900426 --portdata->outstanding_urbs;
Oliver Neukume6929a92009-09-04 23:19:53 +0200427 spin_unlock(&portdata->lock);
428 spin_lock(&intfdata->susp_lock);
429 --intfdata->in_flight;
430 spin_unlock(&intfdata->susp_lock);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900431
432 usb_serial_port_softint(port);
433}
434
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700435/* Write */
Alan Cox95da3102008-07-22 11:09:07 +0100436static int sierra_write(struct tty_struct *tty, struct usb_serial_port *port,
437 const unsigned char *buf, int count)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700438{
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900439 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
Oliver Neukume6929a92009-09-04 23:19:53 +0200440 struct sierra_intf_private *intfdata;
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900441 struct usb_serial *serial = port->serial;
442 unsigned long flags;
443 unsigned char *buffer;
444 struct urb *urb;
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700445 size_t writesize = min((size_t)count, (size_t)MAX_TRANSFER);
446 int retval = 0;
447
448 /* verify that we actually have some data to write */
449 if (count == 0)
450 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700451
452 portdata = usb_get_serial_port_data(port);
Oliver Neukume6929a92009-09-04 23:19:53 +0200453 intfdata = serial->private;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700454
Andrew Morton0b103952009-05-12 15:27:59 -0700455 dev_dbg(&port->dev, "%s: write (%zd bytes)\n", __func__, writesize);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900456 spin_lock_irqsave(&portdata->lock, flags);
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700457 dev_dbg(&port->dev, "%s - outstanding_urbs: %d\n", __func__,
458 portdata->outstanding_urbs);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900459 if (portdata->outstanding_urbs > N_OUT_URB) {
460 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd7384a922008-09-16 21:05:24 -0700461 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900462 return 0;
463 }
464 portdata->outstanding_urbs++;
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700465 dev_dbg(&port->dev, "%s - 1, outstanding_urbs: %d\n", __func__,
466 portdata->outstanding_urbs);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900467 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700468
Oliver Neukume6929a92009-09-04 23:19:53 +0200469 retval = usb_autopm_get_interface_async(serial->interface);
470 if (retval < 0) {
471 spin_lock_irqsave(&portdata->lock, flags);
472 portdata->outstanding_urbs--;
473 spin_unlock_irqrestore(&portdata->lock, flags);
474 goto error_simple;
475 }
476
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700477 buffer = kmalloc(writesize, GFP_ATOMIC);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900478 if (!buffer) {
479 dev_err(&port->dev, "out of memory\n");
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700480 retval = -ENOMEM;
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900481 goto error_no_buffer;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700482 }
483
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900484 urb = usb_alloc_urb(0, GFP_ATOMIC);
485 if (!urb) {
486 dev_err(&port->dev, "no more free urbs\n");
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700487 retval = -ENOMEM;
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900488 goto error_no_urb;
489 }
490
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700491 memcpy(buffer, buf, writesize);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900492
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700493 usb_serial_debug_data(debug, &port->dev, __func__, writesize, buffer);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900494
495 usb_fill_bulk_urb(urb, serial->dev,
496 usb_sndbulkpipe(serial->dev,
497 port->bulk_out_endpointAddress),
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700498 buffer, writesize, sierra_outdat_callback, port);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900499
Elina Pasheva238ebd12009-05-12 13:12:24 -0700500 /* Handle the need to send a zero length packet */
501 urb->transfer_flags |= URB_ZERO_PACKET;
502
Oliver Neukume6929a92009-09-04 23:19:53 +0200503 spin_lock_irqsave(&intfdata->susp_lock, flags);
504
505 if (intfdata->suspended) {
506 usb_anchor_urb(urb, &portdata->delayed);
507 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
508 goto skip_power;
509 } else {
510 usb_anchor_urb(urb, &portdata->active);
511 }
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900512 /* send it down the pipe */
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700513 retval = usb_submit_urb(urb, GFP_ATOMIC);
514 if (retval) {
Oliver Neukume6929a92009-09-04 23:19:53 +0200515 usb_unanchor_urb(urb);
516 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900517 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700518 "with status = %d\n", __func__, retval);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900519 goto error;
Oliver Neukume6929a92009-09-04 23:19:53 +0200520 } else {
521 intfdata->in_flight++;
522 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900523 }
524
Oliver Neukume6929a92009-09-04 23:19:53 +0200525skip_power:
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900526 /* we are done with this urb, so let the host driver
527 * really free it when it is finished with it */
528 usb_free_urb(urb);
529
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700530 return writesize;
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900531error:
532 usb_free_urb(urb);
533error_no_urb:
534 kfree(buffer);
535error_no_buffer:
536 spin_lock_irqsave(&portdata->lock, flags);
537 --portdata->outstanding_urbs;
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700538 dev_dbg(&port->dev, "%s - 2. outstanding_urbs: %d\n", __func__,
539 portdata->outstanding_urbs);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900540 spin_unlock_irqrestore(&portdata->lock, flags);
Oliver Neukume6929a92009-09-04 23:19:53 +0200541 usb_autopm_put_interface_async(serial->interface);
542error_simple:
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700543 return retval;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700544}
545
546static void sierra_indat_callback(struct urb *urb)
547{
548 int err;
549 int endpoint;
550 struct usb_serial_port *port;
551 struct tty_struct *tty;
552 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700553 int status = urb->status;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700554
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700555 endpoint = usb_pipeendpoint(urb->pipe);
Elina Pashevab0cda8c2009-04-29 10:29:21 -0700556 port = urb->context;
557
558 dev_dbg(&port->dev, "%s: %p\n", __func__, urb);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700559
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700560 if (status) {
Kevin Lloyd7384a922008-09-16 21:05:24 -0700561 dev_dbg(&port->dev, "%s: nonzero status: %d on"
Elina Pasheva5d44b362009-04-27 18:41:52 -0700562 " endpoint %02x\n", __func__, status, endpoint);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700563 } else {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700564 if (urb->actual_length) {
Alan Coxd95186d2009-01-02 13:42:56 +0000565 tty = tty_port_tty_get(&port->port);
Elina Pashevab0cda8c2009-04-29 10:29:21 -0700566
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700567 tty_buffer_request_room(tty, urb->actual_length);
568 tty_insert_flip_string(tty, data, urb->actual_length);
569 tty_flip_buffer_push(tty);
Elina Pashevab0cda8c2009-04-29 10:29:21 -0700570
Alan Cox4a90f092008-10-13 10:39:46 +0100571 tty_kref_put(tty);
Elina Pashevab0cda8c2009-04-29 10:29:21 -0700572 usb_serial_debug_data(debug, &port->dev, __func__,
573 urb->actual_length, data);
574 } else {
Kevin Lloyd7384a922008-09-16 21:05:24 -0700575 dev_dbg(&port->dev, "%s: empty read urb"
Elina Pasheva5d44b362009-04-27 18:41:52 -0700576 " received\n", __func__);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700577 }
578 }
Elina Pashevab0cda8c2009-04-29 10:29:21 -0700579
580 /* Resubmit urb so we continue receiving */
581 if (port->port.count && status != -ESHUTDOWN && status != -EPERM) {
Oliver Neukume6929a92009-09-04 23:19:53 +0200582 usb_mark_last_busy(port->serial->dev);
Elina Pashevab0cda8c2009-04-29 10:29:21 -0700583 err = usb_submit_urb(urb, GFP_ATOMIC);
584 if (err)
585 dev_err(&port->dev, "resubmit read urb failed."
586 "(%d)\n", err);
587 }
588
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700589 return;
590}
591
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700592static void sierra_instat_callback(struct urb *urb)
593{
594 int err;
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700595 int status = urb->status;
Ming Leicdc97792008-02-24 18:41:47 +0800596 struct usb_serial_port *port = urb->context;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700597 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
598 struct usb_serial *serial = port->serial;
599
Elina Pasheva5d44b362009-04-27 18:41:52 -0700600 dev_dbg(&port->dev, "%s: urb %p port %p has data %p\n", __func__,
Kevin Lloyd7384a922008-09-16 21:05:24 -0700601 urb, port, portdata);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700602
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700603 if (status == 0) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700604 struct usb_ctrlrequest *req_pkt =
605 (struct usb_ctrlrequest *)urb->transfer_buffer;
606
607 if (!req_pkt) {
Kevin Lloyd7384a922008-09-16 21:05:24 -0700608 dev_dbg(&port->dev, "%s: NULL req_pkt\n",
609 __func__);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700610 return;
611 }
612 if ((req_pkt->bRequestType == 0xA1) &&
613 (req_pkt->bRequest == 0x20)) {
614 int old_dcd_state;
615 unsigned char signals = *((unsigned char *)
616 urb->transfer_buffer +
617 sizeof(struct usb_ctrlrequest));
Alan Cox4a90f092008-10-13 10:39:46 +0100618 struct tty_struct *tty;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700619
Elina Pasheva5d44b362009-04-27 18:41:52 -0700620 dev_dbg(&port->dev, "%s: signal x%x\n", __func__,
Kevin Lloyd7384a922008-09-16 21:05:24 -0700621 signals);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700622
623 old_dcd_state = portdata->dcd_state;
624 portdata->cts_state = 1;
625 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
626 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
627 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
628
Alan Cox4a90f092008-10-13 10:39:46 +0100629 tty = tty_port_tty_get(&port->port);
630 if (tty && !C_CLOCAL(tty) &&
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700631 old_dcd_state && !portdata->dcd_state)
Alan Cox4a90f092008-10-13 10:39:46 +0100632 tty_hangup(tty);
633 tty_kref_put(tty);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700634 } else {
Elina Pasheva5d44b362009-04-27 18:41:52 -0700635 dev_dbg(&port->dev, "%s: type %x req %x\n",
Kevin Lloyd7384a922008-09-16 21:05:24 -0700636 __func__, req_pkt->bRequestType,
637 req_pkt->bRequest);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700638 }
639 } else
Elina Pasheva5d44b362009-04-27 18:41:52 -0700640 dev_dbg(&port->dev, "%s: error %d\n", __func__, status);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700641
642 /* Resubmit urb so we continue receiving IRQ data */
Elina Pashevac76a23d2009-05-12 13:12:37 -0700643 if (port->port.count && status != -ESHUTDOWN && status != -ENOENT) {
Oliver Neukume6929a92009-09-04 23:19:53 +0200644 usb_mark_last_busy(serial->dev);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700645 urb->dev = serial->dev;
646 err = usb_submit_urb(urb, GFP_ATOMIC);
647 if (err)
Elina Pashevac76a23d2009-05-12 13:12:37 -0700648 dev_err(&port->dev, "%s: resubmit intr urb "
649 "failed. (%d)\n", __func__, err);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700650 }
651}
652
Alan Cox95da3102008-07-22 11:09:07 +0100653static int sierra_write_room(struct tty_struct *tty)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700654{
Alan Cox95da3102008-07-22 11:09:07 +0100655 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900656 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
657 unsigned long flags;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700658
Elina Pasheva5d44b362009-04-27 18:41:52 -0700659 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700660
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900661 /* try to give a good number back based on if we have any free urbs at
662 * this point in time */
663 spin_lock_irqsave(&portdata->lock, flags);
664 if (portdata->outstanding_urbs > N_OUT_URB * 2 / 3) {
665 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd7384a922008-09-16 21:05:24 -0700666 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900667 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700668 }
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900669 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700670
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900671 return 2048;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700672}
673
Elina Pashevab9a44bc12009-06-11 14:30:21 +0100674static void sierra_stop_rx_urbs(struct usb_serial_port *port)
675{
676 int i;
677 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
678
679 for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++)
680 usb_kill_urb(portdata->in_urbs[i]);
681
682 usb_kill_urb(port->interrupt_in_urb);
683}
684
685static int sierra_submit_rx_urbs(struct usb_serial_port *port, gfp_t mem_flags)
686{
687 int ok_cnt;
688 int err = -EINVAL;
689 int i;
690 struct urb *urb;
691 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
692
693 ok_cnt = 0;
694 for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++) {
695 urb = portdata->in_urbs[i];
696 if (!urb)
697 continue;
698 err = usb_submit_urb(urb, mem_flags);
699 if (err) {
700 dev_err(&port->dev, "%s: submit urb failed: %d\n",
701 __func__, err);
702 } else {
703 ok_cnt++;
704 }
705 }
706
707 if (ok_cnt && port->interrupt_in_urb) {
708 err = usb_submit_urb(port->interrupt_in_urb, mem_flags);
709 if (err) {
710 dev_err(&port->dev, "%s: submit intr urb failed: %d\n",
711 __func__, err);
712 }
713 }
714
715 if (ok_cnt > 0) /* at least one rx urb submitted */
716 return 0;
717 else
718 return err;
719}
720
721static struct urb *sierra_setup_urb(struct usb_serial *serial, int endpoint,
722 int dir, void *ctx, int len,
723 gfp_t mem_flags,
724 usb_complete_t callback)
725{
726 struct urb *urb;
727 u8 *buf;
728
729 if (endpoint == -1)
730 return NULL;
731
732 urb = usb_alloc_urb(0, mem_flags);
733 if (urb == NULL) {
734 dev_dbg(&serial->dev->dev, "%s: alloc for endpoint %d failed\n",
735 __func__, endpoint);
736 return NULL;
737 }
738
739 buf = kmalloc(len, mem_flags);
740 if (buf) {
741 /* Fill URB using supplied data */
742 usb_fill_bulk_urb(urb, serial->dev,
743 usb_sndbulkpipe(serial->dev, endpoint) | dir,
744 buf, len, callback, ctx);
745
746 /* debug */
747 dev_dbg(&serial->dev->dev, "%s %c u : %p d:%p\n", __func__,
748 dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
749 } else {
750 dev_dbg(&serial->dev->dev, "%s %c u:%p d:%p\n", __func__,
751 dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
752
753 sierra_release_urb(urb);
754 urb = NULL;
755 }
756
757 return urb;
758}
759
760static void sierra_close(struct usb_serial_port *port)
761{
762 int i;
763 struct usb_serial *serial = port->serial;
764 struct sierra_port_private *portdata;
Oliver Neukume6929a92009-09-04 23:19:53 +0200765 struct sierra_intf_private *intfdata = port->serial->private;
766
Elina Pashevab9a44bc12009-06-11 14:30:21 +0100767
768 dev_dbg(&port->dev, "%s\n", __func__);
769 portdata = usb_get_serial_port_data(port);
770
771 portdata->rts_state = 0;
772 portdata->dtr_state = 0;
773
774 if (serial->dev) {
775 mutex_lock(&serial->disc_mutex);
776 if (!serial->disconnected)
777 sierra_send_setup(port);
778 mutex_unlock(&serial->disc_mutex);
Oliver Neukume6929a92009-09-04 23:19:53 +0200779 spin_lock_irq(&intfdata->susp_lock);
780 portdata->opened = 0;
781 spin_unlock_irq(&intfdata->susp_lock);
782
Elina Pashevab9a44bc12009-06-11 14:30:21 +0100783
784 /* Stop reading urbs */
785 sierra_stop_rx_urbs(port);
786 /* .. and release them */
787 for (i = 0; i < N_IN_URB; i++) {
788 sierra_release_urb(portdata->in_urbs[i]);
789 portdata->in_urbs[i] = NULL;
790 }
Oliver Neukume6929a92009-09-04 23:19:53 +0200791 usb_autopm_get_interface(serial->interface);
792 serial->interface->needs_remote_wakeup = 0;
Elina Pashevab9a44bc12009-06-11 14:30:21 +0100793 }
794}
795
Alan Coxa509a7e2009-09-19 13:13:26 -0700796static int sierra_open(struct tty_struct *tty, struct usb_serial_port *port)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700797{
798 struct sierra_port_private *portdata;
799 struct usb_serial *serial = port->serial;
Oliver Neukume6929a92009-09-04 23:19:53 +0200800 struct sierra_intf_private *intfdata = serial->private;
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900801 int i;
Elina Pashevab9a44bc12009-06-11 14:30:21 +0100802 int err;
803 int endpoint;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700804 struct urb *urb;
805
806 portdata = usb_get_serial_port_data(port);
807
Elina Pasheva5d44b362009-04-27 18:41:52 -0700808 dev_dbg(&port->dev, "%s\n", __func__);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700809
810 /* Set some sane defaults */
811 portdata->rts_state = 1;
812 portdata->dtr_state = 1;
813
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700814
Elina Pashevab9a44bc12009-06-11 14:30:21 +0100815 endpoint = port->bulk_in_endpointAddress;
816 for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++) {
817 urb = sierra_setup_urb(serial, endpoint, USB_DIR_IN, port,
818 IN_BUFLEN, GFP_KERNEL,
819 sierra_indat_callback);
820 portdata->in_urbs[i] = urb;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700821 }
Elina Pashevab9a44bc12009-06-11 14:30:21 +0100822 /* clear halt condition */
823 usb_clear_halt(serial->dev,
824 usb_sndbulkpipe(serial->dev, endpoint) | USB_DIR_IN);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700825
Elina Pashevab9a44bc12009-06-11 14:30:21 +0100826 err = sierra_submit_rx_urbs(port, GFP_KERNEL);
827 if (err) {
828 /* get rid of everything as in close */
829 sierra_close(port);
830 return err;
831 }
Alan Cox335f8512009-06-11 12:26:29 +0100832 sierra_send_setup(port);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700833
Oliver Neukume6929a92009-09-04 23:19:53 +0200834 serial->interface->needs_remote_wakeup = 1;
835 spin_lock_irq(&intfdata->susp_lock);
836 portdata->opened = 1;
837 spin_unlock_irq(&intfdata->susp_lock);
838 usb_autopm_put_interface(serial->interface);
839
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900840 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700841}
842
Elina Pashevab9a44bc12009-06-11 14:30:21 +0100843
Alan Cox335f8512009-06-11 12:26:29 +0100844static void sierra_dtr_rts(struct usb_serial_port *port, int on)
845{
846 struct usb_serial *serial = port->serial;
847 struct sierra_port_private *portdata;
848
849 portdata = usb_get_serial_port_data(port);
850 portdata->rts_state = on;
851 portdata->dtr_state = on;
852
853 if (serial->dev) {
854 mutex_lock(&serial->disc_mutex);
855 if (!serial->disconnected)
856 sierra_send_setup(port);
857 mutex_unlock(&serial->disc_mutex);
858 }
859}
860
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700861static int sierra_startup(struct usb_serial *serial)
862{
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700863 struct usb_serial_port *port;
864 struct sierra_port_private *portdata;
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900865 int i;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700866
Elina Pasheva5d44b362009-04-27 18:41:52 -0700867 dev_dbg(&serial->dev->dev, "%s\n", __func__);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700868
Kevin Lloyd228426e2008-01-10 11:11:04 -0800869 /* Set Device mode to D0 */
Kevin Lloyd112225b2007-07-16 13:49:27 -0700870 sierra_set_power_state(serial->dev, 0x0000);
871
Kevin Lloyd228426e2008-01-10 11:11:04 -0800872 /* Check NMEA and set */
873 if (nmea)
874 sierra_vsc_set_nmea(serial->dev, 1);
875
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700876 /* Now setup per port private data */
877 for (i = 0; i < serial->num_ports; i++) {
878 port = serial->port[i];
879 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
880 if (!portdata) {
Kevin Lloyd7384a922008-09-16 21:05:24 -0700881 dev_dbg(&port->dev, "%s: kmalloc for "
Elina Pasheva5d44b362009-04-27 18:41:52 -0700882 "sierra_port_private (%d) failed!.\n",
Kevin Lloyd7384a922008-09-16 21:05:24 -0700883 __func__, i);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900884 return -ENOMEM;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700885 }
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900886 spin_lock_init(&portdata->lock);
Oliver Neukume6929a92009-09-04 23:19:53 +0200887 init_usb_anchor(&portdata->active);
888 init_usb_anchor(&portdata->delayed);
Elina Pashevab9a44bc12009-06-11 14:30:21 +0100889 /* Set the port private data pointer */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700890 usb_set_serial_port_data(port, portdata);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700891 }
892
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900893 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700894}
895
Alan Stern7bae0a02009-07-07 09:50:14 -0400896static void sierra_release(struct usb_serial *serial)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700897{
Elina Pashevab9a44bc12009-06-11 14:30:21 +0100898 int i;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700899 struct usb_serial_port *port;
900 struct sierra_port_private *portdata;
901
Elina Pasheva5d44b362009-04-27 18:41:52 -0700902 dev_dbg(&serial->dev->dev, "%s\n", __func__);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700903
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700904 for (i = 0; i < serial->num_ports; ++i) {
905 port = serial->port[i];
Greg Kroah-Hartmanf094e4f2007-04-26 00:12:01 -0700906 if (!port)
907 continue;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700908 portdata = usb_get_serial_port_data(port);
Greg Kroah-Hartmanf094e4f2007-04-26 00:12:01 -0700909 if (!portdata)
910 continue;
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900911 kfree(portdata);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700912 }
913}
914
Andrew Mortoncd604512009-09-23 15:57:43 -0700915#ifdef CONFIG_PM
Oliver Neukume6929a92009-09-04 23:19:53 +0200916static void stop_read_write_urbs(struct usb_serial *serial)
917{
918 int i, j;
919 struct usb_serial_port *port;
920 struct sierra_port_private *portdata;
921
922 /* Stop reading/writing urbs */
923 for (i = 0; i < serial->num_ports; ++i) {
924 port = serial->port[i];
925 portdata = usb_get_serial_port_data(port);
926 for (j = 0; j < N_IN_URB; j++)
927 usb_kill_urb(portdata->in_urbs[j]);
928 usb_kill_anchored_urbs(&portdata->active);
929 }
930}
931
932static int sierra_suspend(struct usb_serial *serial, pm_message_t message)
933{
934 struct sierra_intf_private *intfdata;
935 int b;
936
937 if (serial->dev->auto_pm) {
938 intfdata = serial->private;
939 spin_lock_irq(&intfdata->susp_lock);
940 b = intfdata->in_flight;
941
942 if (b) {
943 spin_unlock_irq(&intfdata->susp_lock);
944 return -EBUSY;
945 } else {
946 intfdata->suspended = 1;
947 spin_unlock_irq(&intfdata->susp_lock);
948 }
949 }
950 stop_read_write_urbs(serial);
951
952 return 0;
953}
954
955static int sierra_resume(struct usb_serial *serial)
956{
957 struct usb_serial_port *port;
958 struct sierra_intf_private *intfdata = serial->private;
959 struct sierra_port_private *portdata;
960 struct urb *urb;
961 int ec = 0;
962 int i, err;
963
964 spin_lock_irq(&intfdata->susp_lock);
965 for (i = 0; i < serial->num_ports; i++) {
966 port = serial->port[i];
967 portdata = usb_get_serial_port_data(port);
968
969 while ((urb = usb_get_from_anchor(&portdata->delayed))) {
970 usb_anchor_urb(urb, &portdata->active);
971 intfdata->in_flight++;
972 err = usb_submit_urb(urb, GFP_ATOMIC);
973 if (err < 0) {
974 intfdata->in_flight--;
975 usb_unanchor_urb(urb);
976 usb_scuttle_anchored_urbs(&portdata->delayed);
977 break;
978 }
979 }
980
981 if (portdata->opened) {
982 err = sierra_submit_rx_urbs(port, GFP_ATOMIC);
983 if (err)
984 ec++;
985 }
986 }
987 intfdata->suspended = 0;
988 spin_unlock_irq(&intfdata->susp_lock);
989
990 return ec ? -EIO : 0;
991}
Andrew Mortoncd604512009-09-23 15:57:43 -0700992#else
993#define sierra_suspend NULL
994#define sierra_resume NULL
995#endif
Oliver Neukume6929a92009-09-04 23:19:53 +0200996
Kevin Lloyd228426e2008-01-10 11:11:04 -0800997static struct usb_serial_driver sierra_device = {
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700998 .driver = {
999 .owner = THIS_MODULE,
Kevin Lloyd4e0fee82008-07-10 14:14:47 -07001000 .name = "sierra",
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -07001001 },
Kevin Lloyd228426e2008-01-10 11:11:04 -08001002 .description = "Sierra USB modem",
1003 .id_table = id_table,
Johannes Hölzld9b1b782006-12-17 21:50:24 +01001004 .usb_driver = &sierra_driver,
Kevin Lloyd228426e2008-01-10 11:11:04 -08001005 .calc_num_ports = sierra_calc_num_ports,
1006 .probe = sierra_probe,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -07001007 .open = sierra_open,
1008 .close = sierra_close,
Alan Cox335f8512009-06-11 12:26:29 +01001009 .dtr_rts = sierra_dtr_rts,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -07001010 .write = sierra_write,
1011 .write_room = sierra_write_room,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -07001012 .set_termios = sierra_set_termios,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -07001013 .tiocmget = sierra_tiocmget,
1014 .tiocmset = sierra_tiocmset,
1015 .attach = sierra_startup,
Alan Stern7bae0a02009-07-07 09:50:14 -04001016 .release = sierra_release,
Oliver Neukume6929a92009-09-04 23:19:53 +02001017 .suspend = sierra_suspend,
1018 .resume = sierra_resume,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -07001019 .read_int_callback = sierra_instat_callback,
1020};
1021
1022/* Functions used by new usb-serial code. */
1023static int __init sierra_init(void)
1024{
1025 int retval;
Kevin Lloyd228426e2008-01-10 11:11:04 -08001026 retval = usb_serial_register(&sierra_device);
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -07001027 if (retval)
Kevin Lloyd228426e2008-01-10 11:11:04 -08001028 goto failed_device_register;
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -07001029
1030
1031 retval = usb_register(&sierra_driver);
1032 if (retval)
1033 goto failed_driver_register;
1034
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07001035 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1036 DRIVER_DESC "\n");
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -07001037
1038 return 0;
1039
1040failed_driver_register:
Kevin Lloyd228426e2008-01-10 11:11:04 -08001041 usb_serial_deregister(&sierra_device);
1042failed_device_register:
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -07001043 return retval;
1044}
1045
1046static void __exit sierra_exit(void)
1047{
Alan Cox9660ea32008-07-22 11:14:59 +01001048 usb_deregister(&sierra_driver);
Kevin Lloyd228426e2008-01-10 11:11:04 -08001049 usb_serial_deregister(&sierra_device);
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -07001050}
1051
1052module_init(sierra_init);
1053module_exit(sierra_exit);
1054
Kevin Lloyd033a3fb2006-10-13 23:53:21 -07001055MODULE_AUTHOR(DRIVER_AUTHOR);
1056MODULE_DESCRIPTION(DRIVER_DESC);
1057MODULE_VERSION(DRIVER_VERSION);
Kevin Lloyd69de51f2006-06-30 11:17:55 -07001058MODULE_LICENSE("GPL");
Kevin Lloyd033a3fb2006-10-13 23:53:21 -07001059
Kevin Lloyd4e0fee82008-07-10 14:14:47 -07001060module_param(nmea, bool, S_IRUGO | S_IWUSR);
Kevin Lloyd228426e2008-01-10 11:11:04 -08001061MODULE_PARM_DESC(nmea, "NMEA streaming");
1062
Kevin Lloyd033a3fb2006-10-13 23:53:21 -07001063module_param(debug, bool, S_IRUGO | S_IWUSR);
1064MODULE_PARM_DESC(debug, "Debug messages");