blob: 55391bbe12304c508f9275602b2fe46abe566953 [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 Pasheva238ebd12009-05-12 13:12:24 -070020#define DRIVER_VERSION "v.1.3.7"
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
Adrian Bunk82a24e62007-07-29 16:59:02 +020054static int sierra_set_power_state(struct usb_device *udev, __u16 swiState)
Kevin Lloyd112225b2007-07-16 13:49:27 -070055{
56 int result;
Elina Pasheva5d44b362009-04-27 18:41:52 -070057 dev_dbg(&udev->dev, "%s\n", __func__);
Kevin Lloyd112225b2007-07-16 13:49:27 -070058 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Kevin Lloyd228426e2008-01-10 11:11:04 -080059 SWIMS_USB_REQUEST_SetPower, /* __u8 request */
Kevin Lloydf3564de2008-03-28 10:05:08 -070060 USB_TYPE_VENDOR, /* __u8 request type */
Kevin Lloyd228426e2008-01-10 11:11:04 -080061 swiState, /* __u16 value */
62 0, /* __u16 index */
63 NULL, /* void *data */
64 0, /* __u16 size */
65 USB_CTRL_SET_TIMEOUT); /* int timeout */
Kevin Lloyd112225b2007-07-16 13:49:27 -070066 return result;
67}
68
Kevin Lloyd228426e2008-01-10 11:11:04 -080069static int sierra_vsc_set_nmea(struct usb_device *udev, __u16 enable)
Kevin Lloyd112225b2007-07-16 13:49:27 -070070{
71 int result;
Elina Pasheva5d44b362009-04-27 18:41:52 -070072 dev_dbg(&udev->dev, "%s\n", __func__);
Kevin Lloyd228426e2008-01-10 11:11:04 -080073 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
74 SWIMS_USB_REQUEST_SetNmea, /* __u8 request */
Kevin Lloydf3564de2008-03-28 10:05:08 -070075 USB_TYPE_VENDOR, /* __u8 request type */
Kevin Lloyd228426e2008-01-10 11:11:04 -080076 enable, /* __u16 value */
77 0x0000, /* __u16 index */
78 NULL, /* void *data */
79 0, /* __u16 size */
80 USB_CTRL_SET_TIMEOUT); /* int timeout */
81 return result;
82}
Kevin Lloyd112225b2007-07-16 13:49:27 -070083
Kevin Lloyd228426e2008-01-10 11:11:04 -080084static int sierra_calc_num_ports(struct usb_serial *serial)
85{
Elina Pasheva9636b682009-05-12 13:12:54 -070086 int num_ports = 0;
87 u8 ifnum, numendpoints;
88
Elina Pasheva5d44b362009-04-27 18:41:52 -070089 dev_dbg(&serial->dev->dev, "%s\n", __func__);
Kevin Lloyd112225b2007-07-16 13:49:27 -070090
Elina Pasheva9636b682009-05-12 13:12:54 -070091 ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
92 numendpoints = serial->interface->cur_altsetting->desc.bNumEndpoints;
Kevin Lloyd228426e2008-01-10 11:11:04 -080093
Elina Pasheva9636b682009-05-12 13:12:54 -070094 /* Dummy interface present on some SKUs should be ignored */
95 if (ifnum == 0x99)
96 num_ports = 0;
97 else if (numendpoints <= 3)
98 num_ports = 1;
99 else
100 num_ports = (numendpoints-1)/2;
101 return num_ports;
Kevin Lloyd228426e2008-01-10 11:11:04 -0800102}
103
Elina Pasheva4db22992009-06-11 14:32:01 +0100104static int is_blacklisted(const u8 ifnum,
105 const struct sierra_iface_info *blacklist)
106{
107 const u8 *info;
108 int i;
109
110 if (blacklist) {
111 info = blacklist->ifaceinfo;
112
113 for (i = 0; i < blacklist->infolen; i++) {
114 if (info[i] == ifnum)
115 return 1;
116 }
117 }
118 return 0;
119}
120
Kevin Lloyd71069672008-04-02 11:24:56 -0700121static int sierra_calc_interface(struct usb_serial *serial)
122{
Kevin Lloyd4e0fee82008-07-10 14:14:47 -0700123 int interface;
124 struct usb_interface *p_interface;
125 struct usb_host_interface *p_host_interface;
Elina Pasheva5d44b362009-04-27 18:41:52 -0700126 dev_dbg(&serial->dev->dev, "%s\n", __func__);
Kevin Lloyd71069672008-04-02 11:24:56 -0700127
Kevin Lloyd4e0fee82008-07-10 14:14:47 -0700128 /* Get the interface structure pointer from the serial struct */
129 p_interface = serial->interface;
Kevin Lloyd71069672008-04-02 11:24:56 -0700130
Kevin Lloyd4e0fee82008-07-10 14:14:47 -0700131 /* Get a pointer to the host interface structure */
132 p_host_interface = p_interface->cur_altsetting;
Kevin Lloyd71069672008-04-02 11:24:56 -0700133
Kevin Lloyd4e0fee82008-07-10 14:14:47 -0700134 /* read the interface descriptor for this active altsetting
135 * to find out the interface number we are on
136 */
137 interface = p_host_interface->desc.bInterfaceNumber;
Kevin Lloyd71069672008-04-02 11:24:56 -0700138
Kevin Lloyd4e0fee82008-07-10 14:14:47 -0700139 return interface;
Kevin Lloyd71069672008-04-02 11:24:56 -0700140}
141
Kevin Lloyd228426e2008-01-10 11:11:04 -0800142static int sierra_probe(struct usb_serial *serial,
143 const struct usb_device_id *id)
144{
145 int result = 0;
146 struct usb_device *udev;
Kevin Lloyd228426e2008-01-10 11:11:04 -0800147 u8 ifnum;
Kevin Lloyde3173b22008-07-10 14:14:51 -0700148
Kevin Lloyd228426e2008-01-10 11:11:04 -0800149 udev = serial->dev;
Elina Pasheva9636b682009-05-12 13:12:54 -0700150 dev_dbg(&udev->dev, "%s\n", __func__);
Kevin Lloyd228426e2008-01-10 11:11:04 -0800151
Kevin Lloyde3173b22008-07-10 14:14:51 -0700152 ifnum = sierra_calc_interface(serial);
Kevin Lloyde3173b22008-07-10 14:14:51 -0700153 /*
154 * If this interface supports more than 1 alternate
155 * select the 2nd one
156 */
157 if (serial->interface->num_altsetting == 2) {
158 dev_dbg(&udev->dev, "Selecting alt setting for interface %d\n",
159 ifnum);
160 /* We know the alternate setting is 1 for the MC8785 */
161 usb_set_interface(udev, ifnum, 1);
162 }
Kevin Lloyd71069672008-04-02 11:24:56 -0700163
Elina Pasheva4db22992009-06-11 14:32:01 +0100164 /* ifnum could have changed - by calling usb_set_interface */
165 ifnum = sierra_calc_interface(serial);
166
167 if (is_blacklisted(ifnum,
168 (struct sierra_iface_info *)id->driver_info)) {
169 dev_dbg(&serial->dev->dev,
170 "Ignoring blacklisted interface #%d\n", ifnum);
171 return -ENODEV;
172 }
173
Kevin Lloyd228426e2008-01-10 11:11:04 -0800174 return result;
Kevin Lloyd112225b2007-07-16 13:49:27 -0700175}
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700176
Elina Pasheva4db22992009-06-11 14:32:01 +0100177static const u8 direct_ip_non_serial_ifaces[] = { 7, 8, 9, 10, 11 };
178static const struct sierra_iface_info direct_ip_interface_blacklist = {
179 .infolen = ARRAY_SIZE(direct_ip_non_serial_ifaces),
180 .ifaceinfo = direct_ip_non_serial_ifaces,
181};
182
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700183static struct usb_device_id id_table [] = {
Elina Pashevac5f3d872009-07-09 17:55:18 -0700184 { USB_DEVICE(0x0F3D, 0x0112) }, /* Airprime/Sierra PC 5220 */
185 { USB_DEVICE(0x03F0, 0x1B1D) }, /* HP ev2200 a.k.a MC5720 */
186 { USB_DEVICE(0x03F0, 0x1E1D) }, /* HP hs2300 a.k.a MC8775 */
187
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700188 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800189 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
Greg Kroah-Hartman90ac3c82002-04-09 12:14:34 -0700190 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800191 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
agilmore@wirelessbeehive.comb9e13ac2007-12-04 11:37:12 -0700192 { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
Elina Pashevac5f3d872009-07-09 17:55:18 -0700193 { USB_DEVICE(0x1199, 0x0022) }, /* Sierra Wireless EM5725 */
194 { USB_DEVICE(0x1199, 0x0024) }, /* Sierra Wireless MC5727 */
195 { USB_DEVICE(0x1199, 0x0224) }, /* Sierra Wireless MC5727 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800196 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
197 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
Elina Pashevac5f3d872009-07-09 17:55:18 -0700198 { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700199 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U */
Elina Pashevac5f3d872009-07-09 17:55:18 -0700200 /* Sierra Wireless C597 */
Kevin Lloyd4e0fee82008-07-10 14:14:47 -0700201 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0023, 0xFF, 0xFF, 0xFF) },
Elina Pashevac5f3d872009-07-09 17:55:18 -0700202 /* Sierra Wireless T598 */
Kevin Lloyde3173b22008-07-10 14:14:51 -0700203 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0025, 0xFF, 0xFF, 0xFF) },
Elina Pashevac5f3d872009-07-09 17:55:18 -0700204 { USB_DEVICE(0x1199, 0x0026) }, /* Sierra Wireless T11 */
205 { USB_DEVICE(0x1199, 0x0027) }, /* Sierra Wireless AC402 */
206 { USB_DEVICE(0x1199, 0x0028) }, /* Sierra Wireless MC5728 */
207 { USB_DEVICE(0x1199, 0x0029) }, /* Sierra Wireless Device */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700208
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700209 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
210 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
Elina Pashevac5f3d872009-07-09 17:55:18 -0700211 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
212 { USB_DEVICE(0x1199, 0x6805) }, /* Sierra Wireless MC8765 */
213 { USB_DEVICE(0x1199, 0x6808) }, /* Sierra Wireless MC8755 */
214 { USB_DEVICE(0x1199, 0x6809) }, /* Sierra Wireless MC8765 */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700215 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
Elina Pashevac5f3d872009-07-09 17:55:18 -0700216 { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 */
Kevin Lloyd7f170a62008-03-14 00:53:24 -0700217 { USB_DEVICE(0x1199, 0x6815) }, /* Sierra Wireless MC8775 */
Elina Pashevac5f3d872009-07-09 17:55:18 -0700218 { USB_DEVICE(0x1199, 0x6816) }, /* Sierra Wireless MC8775 */
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700219 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
Kevin Lloyd71069672008-04-02 11:24:56 -0700220 { USB_DEVICE(0x1199, 0x6821) }, /* Sierra Wireless AirCard 875U */
Elina Pashevac5f3d872009-07-09 17:55:18 -0700221 { USB_DEVICE(0x1199, 0x6822) }, /* Sierra Wireless AirCard 875E */
Kevin Lloyd4e0fee82008-07-10 14:14:47 -0700222 { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780 */
223 { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781 */
Elina Pashevac5f3d872009-07-09 17:55:18 -0700224 { USB_DEVICE(0x1199, 0x6834) }, /* Sierra Wireless MC8780 */
225 { USB_DEVICE(0x1199, 0x6835) }, /* Sierra Wireless MC8781 */
226 { USB_DEVICE(0x1199, 0x6838) }, /* Sierra Wireless MC8780 */
227 { USB_DEVICE(0x1199, 0x6839) }, /* Sierra Wireless MC8781 */
Kevin Lloydb77a5c72008-09-17 09:03:38 -0700228 { USB_DEVICE(0x1199, 0x683A) }, /* Sierra Wireless MC8785 */
Kevin Lloyde3173b22008-07-10 14:14:51 -0700229 { USB_DEVICE(0x1199, 0x683B) }, /* Sierra Wireless MC8785 Composite */
Elina Pasheva4db22992009-06-11 14:32:01 +0100230 /* Sierra Wireless MC8790, MC8791, MC8792 Composite */
231 { USB_DEVICE(0x1199, 0x683C) },
232 { USB_DEVICE(0x1199, 0x683D) }, /* Sierra Wireless MC8791 Composite */
233 /* Sierra Wireless MC8790, MC8791, MC8792 */
234 { USB_DEVICE(0x1199, 0x683E) },
Kevin Lloyd9454c462007-07-16 13:49:29 -0700235 { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
236 { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
237 { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880 E */
238 { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881 E */
Kevin Lloyd6835b322008-01-10 11:11:04 -0800239 { USB_DEVICE(0x1199, 0x6855) }, /* Sierra Wireless AirCard 880 U */
Jessica L. Blank62aad3a2007-12-30 20:11:35 -0600240 { USB_DEVICE(0x1199, 0x6856) }, /* Sierra Wireless AirCard 881 U */
Kevin Lloyde3173b22008-07-10 14:14:51 -0700241 { USB_DEVICE(0x1199, 0x6859) }, /* Sierra Wireless AirCard 885 E */
242 { USB_DEVICE(0x1199, 0x685A) }, /* Sierra Wireless AirCard 885 E */
243 /* Sierra Wireless C885 */
244 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6880, 0xFF, 0xFF, 0xFF)},
Elina Pashevac5f3d872009-07-09 17:55:18 -0700245 /* Sierra Wireless C888, Air Card 501, USB 303, USB 304 */
Kevin Lloyde3173b22008-07-10 14:14:51 -0700246 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6890, 0xFF, 0xFF, 0xFF)},
Elina Pashevac5f3d872009-07-09 17:55:18 -0700247 /* Sierra Wireless C22/C33 */
Kevin Lloyd73b2c202008-08-25 19:20:40 -0700248 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6891, 0xFF, 0xFF, 0xFF)},
Elina Pashevac5f3d872009-07-09 17:55:18 -0700249 /* Sierra Wireless HSPA Non-Composite Device */
Kevin Lloyde3173b22008-07-10 14:14:51 -0700250 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6892, 0xFF, 0xFF, 0xFF)},
Elina Pashevac5f3d872009-07-09 17:55:18 -0700251 { USB_DEVICE(0x1199, 0x6893) }, /* Sierra Wireless Device */
Elina Pasheva4db22992009-06-11 14:32:01 +0100252 { USB_DEVICE(0x1199, 0x68A3), /* Sierra Wireless Direct IP modems */
253 .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
254 },
255
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700256 { }
257};
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700258MODULE_DEVICE_TABLE(usb, id_table);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700259
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700260static struct usb_driver sierra_driver = {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700261 .name = "sierra",
Kevin Lloyd228426e2008-01-10 11:11:04 -0800262 .probe = usb_serial_probe,
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700263 .disconnect = usb_serial_disconnect,
264 .id_table = id_table,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700265 .no_dynamic_id = 1,
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700266};
267
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700268struct sierra_port_private {
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900269 spinlock_t lock; /* lock the structure */
270 int outstanding_urbs; /* number of out urbs in flight */
271
Oliver Neukum4f4f9c52008-03-14 00:53:24 -0700272 /* Input endpoints and buffers for this port */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700273 struct urb *in_urbs[N_IN_URB];
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700274
275 /* Settings for the port */
276 int rts_state; /* Handshaking pins (outputs) */
277 int dtr_state;
278 int cts_state; /* Handshaking pins (inputs) */
279 int dsr_state;
280 int dcd_state;
281 int ri_state;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700282};
283
Alan Cox335f8512009-06-11 12:26:29 +0100284static int sierra_send_setup(struct usb_serial_port *port)
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700285{
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700286 struct usb_serial *serial = port->serial;
287 struct sierra_port_private *portdata;
Kevin Lloyd228426e2008-01-10 11:11:04 -0800288 __u16 interface = 0;
Alan Cox335f8512009-06-11 12:26:29 +0100289 int val = 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700290
Elina Pasheva5d44b362009-04-27 18:41:52 -0700291 dev_dbg(&port->dev, "%s\n", __func__);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700292
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700293 portdata = usb_get_serial_port_data(port);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700294
Alan Cox335f8512009-06-11 12:26:29 +0100295 if (portdata->dtr_state)
296 val |= 0x01;
297 if (portdata->rts_state)
298 val |= 0x02;
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700299
Alan Cox335f8512009-06-11 12:26:29 +0100300 /* If composite device then properly report interface */
Alan Cox00b040d2009-06-11 14:29:29 +0100301 if (serial->num_ports == 1) {
Alan Cox335f8512009-06-11 12:26:29 +0100302 interface = sierra_calc_interface(serial);
Alan Cox00b040d2009-06-11 14:29:29 +0100303 /* Control message is sent only to interfaces with
304 * interrupt_in endpoints
305 */
306 if (port->interrupt_in_urb) {
307 /* send control message */
308 return usb_control_msg(serial->dev,
309 usb_rcvctrlpipe(serial->dev, 0),
310 0x22, 0x21, val, interface,
311 NULL, 0, USB_CTRL_SET_TIMEOUT);
312 }
313 }
Kevin Lloyde3173b22008-07-10 14:14:51 -0700314
Alan Cox335f8512009-06-11 12:26:29 +0100315 /* Otherwise the need to do non-composite mapping */
316 else {
317 if (port->bulk_out_endpointAddress == 2)
318 interface = 0;
319 else if (port->bulk_out_endpointAddress == 4)
320 interface = 1;
321 else if (port->bulk_out_endpointAddress == 5)
322 interface = 2;
Alan Cox00b040d2009-06-11 14:29:29 +0100323 return usb_control_msg(serial->dev,
Alan Cox335f8512009-06-11 12:26:29 +0100324 usb_rcvctrlpipe(serial->dev, 0),
325 0x22, 0x21, val, interface,
326 NULL, 0, USB_CTRL_SET_TIMEOUT);
Alan Cox00b040d2009-06-11 14:29:29 +0100327 }
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700328 return 0;
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700329}
330
Alan Cox95da3102008-07-22 11:09:07 +0100331static void sierra_set_termios(struct tty_struct *tty,
332 struct usb_serial_port *port, struct ktermios *old_termios)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700333{
Elina Pasheva5d44b362009-04-27 18:41:52 -0700334 dev_dbg(&port->dev, "%s\n", __func__);
Alan Cox95da3102008-07-22 11:09:07 +0100335 tty_termios_copy_hw(tty->termios, old_termios);
Alan Cox335f8512009-06-11 12:26:29 +0100336 sierra_send_setup(port);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700337}
338
Alan Cox95da3102008-07-22 11:09:07 +0100339static int sierra_tiocmget(struct tty_struct *tty, struct file *file)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700340{
Alan Cox95da3102008-07-22 11:09:07 +0100341 struct usb_serial_port *port = tty->driver_data;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700342 unsigned int value;
343 struct sierra_port_private *portdata;
344
Elina Pasheva5d44b362009-04-27 18:41:52 -0700345 dev_dbg(&port->dev, "%s\n", __func__);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700346 portdata = usb_get_serial_port_data(port);
347
348 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
349 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
350 ((portdata->cts_state) ? TIOCM_CTS : 0) |
351 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
352 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
353 ((portdata->ri_state) ? TIOCM_RNG : 0);
354
355 return value;
356}
357
Alan Cox95da3102008-07-22 11:09:07 +0100358static int sierra_tiocmset(struct tty_struct *tty, struct file *file,
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700359 unsigned int set, unsigned int clear)
360{
Alan Cox95da3102008-07-22 11:09:07 +0100361 struct usb_serial_port *port = tty->driver_data;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700362 struct sierra_port_private *portdata;
363
364 portdata = usb_get_serial_port_data(port);
365
366 if (set & TIOCM_RTS)
367 portdata->rts_state = 1;
368 if (set & TIOCM_DTR)
369 portdata->dtr_state = 1;
370
371 if (clear & TIOCM_RTS)
372 portdata->rts_state = 0;
373 if (clear & TIOCM_DTR)
374 portdata->dtr_state = 0;
Alan Cox335f8512009-06-11 12:26:29 +0100375 return sierra_send_setup(port);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700376}
377
Elina Pashevab9a44bc2009-06-11 14:30:21 +0100378static void sierra_release_urb(struct urb *urb)
379{
380 struct usb_serial_port *port;
381 if (urb) {
382 port = urb->context;
383 dev_dbg(&port->dev, "%s: %p\n", __func__, urb);
384 kfree(urb->transfer_buffer);
385 usb_free_urb(urb);
386 }
387}
388
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900389static void sierra_outdat_callback(struct urb *urb)
390{
391 struct usb_serial_port *port = urb->context;
392 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
393 int status = urb->status;
394 unsigned long flags;
395
Elina Pasheva5d44b362009-04-27 18:41:52 -0700396 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900397
398 /* free up the transfer buffer, as usb_free_urb() does not do this */
399 kfree(urb->transfer_buffer);
400
401 if (status)
Kevin Lloyd7384a922008-09-16 21:05:24 -0700402 dev_dbg(&port->dev, "%s - nonzero write bulk status "
Elina Pasheva5d44b362009-04-27 18:41:52 -0700403 "received: %d\n", __func__, status);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900404
405 spin_lock_irqsave(&portdata->lock, flags);
406 --portdata->outstanding_urbs;
407 spin_unlock_irqrestore(&portdata->lock, flags);
408
409 usb_serial_port_softint(port);
410}
411
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700412/* Write */
Alan Cox95da3102008-07-22 11:09:07 +0100413static int sierra_write(struct tty_struct *tty, struct usb_serial_port *port,
414 const unsigned char *buf, int count)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700415{
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900416 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
417 struct usb_serial *serial = port->serial;
418 unsigned long flags;
419 unsigned char *buffer;
420 struct urb *urb;
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700421 size_t writesize = min((size_t)count, (size_t)MAX_TRANSFER);
422 int retval = 0;
423
424 /* verify that we actually have some data to write */
425 if (count == 0)
426 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700427
428 portdata = usb_get_serial_port_data(port);
429
Andrew Morton0b103952009-05-12 15:27:59 -0700430 dev_dbg(&port->dev, "%s: write (%zd bytes)\n", __func__, writesize);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700431
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900432 spin_lock_irqsave(&portdata->lock, flags);
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700433 dev_dbg(&port->dev, "%s - outstanding_urbs: %d\n", __func__,
434 portdata->outstanding_urbs);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900435 if (portdata->outstanding_urbs > N_OUT_URB) {
436 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd7384a922008-09-16 21:05:24 -0700437 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900438 return 0;
439 }
440 portdata->outstanding_urbs++;
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700441 dev_dbg(&port->dev, "%s - 1, outstanding_urbs: %d\n", __func__,
442 portdata->outstanding_urbs);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900443 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700444
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700445 buffer = kmalloc(writesize, GFP_ATOMIC);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900446 if (!buffer) {
447 dev_err(&port->dev, "out of memory\n");
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700448 retval = -ENOMEM;
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900449 goto error_no_buffer;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700450 }
451
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900452 urb = usb_alloc_urb(0, GFP_ATOMIC);
453 if (!urb) {
454 dev_err(&port->dev, "no more free urbs\n");
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700455 retval = -ENOMEM;
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900456 goto error_no_urb;
457 }
458
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700459 memcpy(buffer, buf, writesize);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900460
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700461 usb_serial_debug_data(debug, &port->dev, __func__, writesize, buffer);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900462
463 usb_fill_bulk_urb(urb, serial->dev,
464 usb_sndbulkpipe(serial->dev,
465 port->bulk_out_endpointAddress),
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700466 buffer, writesize, sierra_outdat_callback, port);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900467
Elina Pasheva238ebd12009-05-12 13:12:24 -0700468 /* Handle the need to send a zero length packet */
469 urb->transfer_flags |= URB_ZERO_PACKET;
470
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900471 /* send it down the pipe */
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700472 retval = usb_submit_urb(urb, GFP_ATOMIC);
473 if (retval) {
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900474 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700475 "with status = %d\n", __func__, retval);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900476 goto error;
477 }
478
479 /* we are done with this urb, so let the host driver
480 * really free it when it is finished with it */
481 usb_free_urb(urb);
482
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700483 return writesize;
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900484error:
485 usb_free_urb(urb);
486error_no_urb:
487 kfree(buffer);
488error_no_buffer:
489 spin_lock_irqsave(&portdata->lock, flags);
490 --portdata->outstanding_urbs;
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700491 dev_dbg(&port->dev, "%s - 2. outstanding_urbs: %d\n", __func__,
492 portdata->outstanding_urbs);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900493 spin_unlock_irqrestore(&portdata->lock, flags);
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700494 return retval;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700495}
496
497static void sierra_indat_callback(struct urb *urb)
498{
499 int err;
500 int endpoint;
501 struct usb_serial_port *port;
502 struct tty_struct *tty;
503 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700504 int status = urb->status;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700505
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700506 endpoint = usb_pipeendpoint(urb->pipe);
Elina Pashevab0cda8c2009-04-29 10:29:21 -0700507 port = urb->context;
508
509 dev_dbg(&port->dev, "%s: %p\n", __func__, urb);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700510
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700511 if (status) {
Kevin Lloyd7384a922008-09-16 21:05:24 -0700512 dev_dbg(&port->dev, "%s: nonzero status: %d on"
Elina Pasheva5d44b362009-04-27 18:41:52 -0700513 " endpoint %02x\n", __func__, status, endpoint);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700514 } else {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700515 if (urb->actual_length) {
Alan Coxd95186d2009-01-02 13:42:56 +0000516 tty = tty_port_tty_get(&port->port);
Elina Pashevab0cda8c2009-04-29 10:29:21 -0700517
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700518 tty_buffer_request_room(tty, urb->actual_length);
519 tty_insert_flip_string(tty, data, urb->actual_length);
520 tty_flip_buffer_push(tty);
Elina Pashevab0cda8c2009-04-29 10:29:21 -0700521
Alan Cox4a90f092008-10-13 10:39:46 +0100522 tty_kref_put(tty);
Elina Pashevab0cda8c2009-04-29 10:29:21 -0700523 usb_serial_debug_data(debug, &port->dev, __func__,
524 urb->actual_length, data);
525 } else {
Kevin Lloyd7384a922008-09-16 21:05:24 -0700526 dev_dbg(&port->dev, "%s: empty read urb"
Elina Pasheva5d44b362009-04-27 18:41:52 -0700527 " received\n", __func__);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700528 }
529 }
Elina Pashevab0cda8c2009-04-29 10:29:21 -0700530
531 /* Resubmit urb so we continue receiving */
532 if (port->port.count && status != -ESHUTDOWN && status != -EPERM) {
533 err = usb_submit_urb(urb, GFP_ATOMIC);
534 if (err)
535 dev_err(&port->dev, "resubmit read urb failed."
536 "(%d)\n", err);
537 }
538
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700539 return;
540}
541
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700542static void sierra_instat_callback(struct urb *urb)
543{
544 int err;
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700545 int status = urb->status;
Ming Leicdc97792008-02-24 18:41:47 +0800546 struct usb_serial_port *port = urb->context;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700547 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
548 struct usb_serial *serial = port->serial;
549
Elina Pasheva5d44b362009-04-27 18:41:52 -0700550 dev_dbg(&port->dev, "%s: urb %p port %p has data %p\n", __func__,
Kevin Lloyd7384a922008-09-16 21:05:24 -0700551 urb, port, portdata);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700552
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700553 if (status == 0) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700554 struct usb_ctrlrequest *req_pkt =
555 (struct usb_ctrlrequest *)urb->transfer_buffer;
556
557 if (!req_pkt) {
Kevin Lloyd7384a922008-09-16 21:05:24 -0700558 dev_dbg(&port->dev, "%s: NULL req_pkt\n",
559 __func__);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700560 return;
561 }
562 if ((req_pkt->bRequestType == 0xA1) &&
563 (req_pkt->bRequest == 0x20)) {
564 int old_dcd_state;
565 unsigned char signals = *((unsigned char *)
566 urb->transfer_buffer +
567 sizeof(struct usb_ctrlrequest));
Alan Cox4a90f092008-10-13 10:39:46 +0100568 struct tty_struct *tty;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700569
Elina Pasheva5d44b362009-04-27 18:41:52 -0700570 dev_dbg(&port->dev, "%s: signal x%x\n", __func__,
Kevin Lloyd7384a922008-09-16 21:05:24 -0700571 signals);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700572
573 old_dcd_state = portdata->dcd_state;
574 portdata->cts_state = 1;
575 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
576 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
577 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
578
Alan Cox4a90f092008-10-13 10:39:46 +0100579 tty = tty_port_tty_get(&port->port);
580 if (tty && !C_CLOCAL(tty) &&
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700581 old_dcd_state && !portdata->dcd_state)
Alan Cox4a90f092008-10-13 10:39:46 +0100582 tty_hangup(tty);
583 tty_kref_put(tty);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700584 } else {
Elina Pasheva5d44b362009-04-27 18:41:52 -0700585 dev_dbg(&port->dev, "%s: type %x req %x\n",
Kevin Lloyd7384a922008-09-16 21:05:24 -0700586 __func__, req_pkt->bRequestType,
587 req_pkt->bRequest);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700588 }
589 } else
Elina Pasheva5d44b362009-04-27 18:41:52 -0700590 dev_dbg(&port->dev, "%s: error %d\n", __func__, status);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700591
592 /* Resubmit urb so we continue receiving IRQ data */
Elina Pashevac76a23d2009-05-12 13:12:37 -0700593 if (port->port.count && status != -ESHUTDOWN && status != -ENOENT) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700594 urb->dev = serial->dev;
595 err = usb_submit_urb(urb, GFP_ATOMIC);
596 if (err)
Elina Pashevac76a23d2009-05-12 13:12:37 -0700597 dev_err(&port->dev, "%s: resubmit intr urb "
598 "failed. (%d)\n", __func__, err);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700599 }
600}
601
Alan Cox95da3102008-07-22 11:09:07 +0100602static int sierra_write_room(struct tty_struct *tty)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700603{
Alan Cox95da3102008-07-22 11:09:07 +0100604 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900605 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
606 unsigned long flags;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700607
Elina Pasheva5d44b362009-04-27 18:41:52 -0700608 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700609
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900610 /* try to give a good number back based on if we have any free urbs at
611 * this point in time */
612 spin_lock_irqsave(&portdata->lock, flags);
613 if (portdata->outstanding_urbs > N_OUT_URB * 2 / 3) {
614 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd7384a922008-09-16 21:05:24 -0700615 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900616 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700617 }
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900618 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700619
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900620 return 2048;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700621}
622
Elina Pashevab9a44bc2009-06-11 14:30:21 +0100623static void sierra_stop_rx_urbs(struct usb_serial_port *port)
624{
625 int i;
626 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
627
628 for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++)
629 usb_kill_urb(portdata->in_urbs[i]);
630
631 usb_kill_urb(port->interrupt_in_urb);
632}
633
634static int sierra_submit_rx_urbs(struct usb_serial_port *port, gfp_t mem_flags)
635{
636 int ok_cnt;
637 int err = -EINVAL;
638 int i;
639 struct urb *urb;
640 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
641
642 ok_cnt = 0;
643 for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++) {
644 urb = portdata->in_urbs[i];
645 if (!urb)
646 continue;
647 err = usb_submit_urb(urb, mem_flags);
648 if (err) {
649 dev_err(&port->dev, "%s: submit urb failed: %d\n",
650 __func__, err);
651 } else {
652 ok_cnt++;
653 }
654 }
655
656 if (ok_cnt && port->interrupt_in_urb) {
657 err = usb_submit_urb(port->interrupt_in_urb, mem_flags);
658 if (err) {
659 dev_err(&port->dev, "%s: submit intr urb failed: %d\n",
660 __func__, err);
661 }
662 }
663
664 if (ok_cnt > 0) /* at least one rx urb submitted */
665 return 0;
666 else
667 return err;
668}
669
670static struct urb *sierra_setup_urb(struct usb_serial *serial, int endpoint,
671 int dir, void *ctx, int len,
672 gfp_t mem_flags,
673 usb_complete_t callback)
674{
675 struct urb *urb;
676 u8 *buf;
677
678 if (endpoint == -1)
679 return NULL;
680
681 urb = usb_alloc_urb(0, mem_flags);
682 if (urb == NULL) {
683 dev_dbg(&serial->dev->dev, "%s: alloc for endpoint %d failed\n",
684 __func__, endpoint);
685 return NULL;
686 }
687
688 buf = kmalloc(len, mem_flags);
689 if (buf) {
690 /* Fill URB using supplied data */
691 usb_fill_bulk_urb(urb, serial->dev,
692 usb_sndbulkpipe(serial->dev, endpoint) | dir,
693 buf, len, callback, ctx);
694
695 /* debug */
696 dev_dbg(&serial->dev->dev, "%s %c u : %p d:%p\n", __func__,
697 dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
698 } else {
699 dev_dbg(&serial->dev->dev, "%s %c u:%p d:%p\n", __func__,
700 dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
701
702 sierra_release_urb(urb);
703 urb = NULL;
704 }
705
706 return urb;
707}
708
709static void sierra_close(struct usb_serial_port *port)
710{
711 int i;
712 struct usb_serial *serial = port->serial;
713 struct sierra_port_private *portdata;
714
715 dev_dbg(&port->dev, "%s\n", __func__);
716 portdata = usb_get_serial_port_data(port);
717
718 portdata->rts_state = 0;
719 portdata->dtr_state = 0;
720
721 if (serial->dev) {
722 mutex_lock(&serial->disc_mutex);
723 if (!serial->disconnected)
724 sierra_send_setup(port);
725 mutex_unlock(&serial->disc_mutex);
726
727 /* Stop reading urbs */
728 sierra_stop_rx_urbs(port);
729 /* .. and release them */
730 for (i = 0; i < N_IN_URB; i++) {
731 sierra_release_urb(portdata->in_urbs[i]);
732 portdata->in_urbs[i] = NULL;
733 }
734 }
735}
736
Alan Coxa509a7e2009-09-19 13:13:26 -0700737static int sierra_open(struct tty_struct *tty, struct usb_serial_port *port)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700738{
739 struct sierra_port_private *portdata;
740 struct usb_serial *serial = port->serial;
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900741 int i;
Elina Pashevab9a44bc2009-06-11 14:30:21 +0100742 int err;
743 int endpoint;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700744 struct urb *urb;
745
746 portdata = usb_get_serial_port_data(port);
747
Elina Pasheva5d44b362009-04-27 18:41:52 -0700748 dev_dbg(&port->dev, "%s\n", __func__);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700749
750 /* Set some sane defaults */
751 portdata->rts_state = 1;
752 portdata->dtr_state = 1;
753
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700754
Elina Pashevab9a44bc2009-06-11 14:30:21 +0100755 endpoint = port->bulk_in_endpointAddress;
756 for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++) {
757 urb = sierra_setup_urb(serial, endpoint, USB_DIR_IN, port,
758 IN_BUFLEN, GFP_KERNEL,
759 sierra_indat_callback);
760 portdata->in_urbs[i] = urb;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700761 }
Elina Pashevab9a44bc2009-06-11 14:30:21 +0100762 /* clear halt condition */
763 usb_clear_halt(serial->dev,
764 usb_sndbulkpipe(serial->dev, endpoint) | USB_DIR_IN);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700765
Elina Pashevab9a44bc2009-06-11 14:30:21 +0100766 err = sierra_submit_rx_urbs(port, GFP_KERNEL);
767 if (err) {
768 /* get rid of everything as in close */
769 sierra_close(port);
770 return err;
771 }
Alan Cox335f8512009-06-11 12:26:29 +0100772 sierra_send_setup(port);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700773
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900774 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700775}
776
Elina Pashevab9a44bc2009-06-11 14:30:21 +0100777
Alan Cox335f8512009-06-11 12:26:29 +0100778static void sierra_dtr_rts(struct usb_serial_port *port, int on)
779{
780 struct usb_serial *serial = port->serial;
781 struct sierra_port_private *portdata;
782
783 portdata = usb_get_serial_port_data(port);
784 portdata->rts_state = on;
785 portdata->dtr_state = on;
786
787 if (serial->dev) {
788 mutex_lock(&serial->disc_mutex);
789 if (!serial->disconnected)
790 sierra_send_setup(port);
791 mutex_unlock(&serial->disc_mutex);
792 }
793}
794
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700795static int sierra_startup(struct usb_serial *serial)
796{
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700797 struct usb_serial_port *port;
798 struct sierra_port_private *portdata;
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900799 int i;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700800
Elina Pasheva5d44b362009-04-27 18:41:52 -0700801 dev_dbg(&serial->dev->dev, "%s\n", __func__);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700802
Kevin Lloyd228426e2008-01-10 11:11:04 -0800803 /* Set Device mode to D0 */
Kevin Lloyd112225b2007-07-16 13:49:27 -0700804 sierra_set_power_state(serial->dev, 0x0000);
805
Kevin Lloyd228426e2008-01-10 11:11:04 -0800806 /* Check NMEA and set */
807 if (nmea)
808 sierra_vsc_set_nmea(serial->dev, 1);
809
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700810 /* Now setup per port private data */
811 for (i = 0; i < serial->num_ports; i++) {
812 port = serial->port[i];
813 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
814 if (!portdata) {
Kevin Lloyd7384a922008-09-16 21:05:24 -0700815 dev_dbg(&port->dev, "%s: kmalloc for "
Elina Pasheva5d44b362009-04-27 18:41:52 -0700816 "sierra_port_private (%d) failed!.\n",
Kevin Lloyd7384a922008-09-16 21:05:24 -0700817 __func__, i);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900818 return -ENOMEM;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700819 }
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900820 spin_lock_init(&portdata->lock);
Elina Pashevab9a44bc2009-06-11 14:30:21 +0100821 /* Set the port private data pointer */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700822 usb_set_serial_port_data(port, portdata);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700823 }
824
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900825 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700826}
827
Alan Stern7bae0a02009-07-07 09:50:14 -0400828static void sierra_release(struct usb_serial *serial)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700829{
Elina Pashevab9a44bc2009-06-11 14:30:21 +0100830 int i;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700831 struct usb_serial_port *port;
832 struct sierra_port_private *portdata;
833
Elina Pasheva5d44b362009-04-27 18:41:52 -0700834 dev_dbg(&serial->dev->dev, "%s\n", __func__);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700835
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700836 for (i = 0; i < serial->num_ports; ++i) {
837 port = serial->port[i];
Greg Kroah-Hartmanf094e4f2007-04-26 00:12:01 -0700838 if (!port)
839 continue;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700840 portdata = usb_get_serial_port_data(port);
Greg Kroah-Hartmanf094e4f2007-04-26 00:12:01 -0700841 if (!portdata)
842 continue;
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900843 kfree(portdata);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700844 }
845}
846
Kevin Lloyd228426e2008-01-10 11:11:04 -0800847static struct usb_serial_driver sierra_device = {
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700848 .driver = {
849 .owner = THIS_MODULE,
Kevin Lloyd4e0fee82008-07-10 14:14:47 -0700850 .name = "sierra",
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700851 },
Kevin Lloyd228426e2008-01-10 11:11:04 -0800852 .description = "Sierra USB modem",
853 .id_table = id_table,
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100854 .usb_driver = &sierra_driver,
Kevin Lloyd228426e2008-01-10 11:11:04 -0800855 .calc_num_ports = sierra_calc_num_ports,
856 .probe = sierra_probe,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700857 .open = sierra_open,
858 .close = sierra_close,
Alan Cox335f8512009-06-11 12:26:29 +0100859 .dtr_rts = sierra_dtr_rts,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700860 .write = sierra_write,
861 .write_room = sierra_write_room,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700862 .set_termios = sierra_set_termios,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700863 .tiocmget = sierra_tiocmget,
864 .tiocmset = sierra_tiocmset,
865 .attach = sierra_startup,
Alan Stern7bae0a02009-07-07 09:50:14 -0400866 .release = sierra_release,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700867 .read_int_callback = sierra_instat_callback,
868};
869
870/* Functions used by new usb-serial code. */
871static int __init sierra_init(void)
872{
873 int retval;
Kevin Lloyd228426e2008-01-10 11:11:04 -0800874 retval = usb_serial_register(&sierra_device);
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700875 if (retval)
Kevin Lloyd228426e2008-01-10 11:11:04 -0800876 goto failed_device_register;
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700877
878
879 retval = usb_register(&sierra_driver);
880 if (retval)
881 goto failed_driver_register;
882
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700883 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
884 DRIVER_DESC "\n");
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700885
886 return 0;
887
888failed_driver_register:
Kevin Lloyd228426e2008-01-10 11:11:04 -0800889 usb_serial_deregister(&sierra_device);
890failed_device_register:
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700891 return retval;
892}
893
894static void __exit sierra_exit(void)
895{
Alan Cox9660ea32008-07-22 11:14:59 +0100896 usb_deregister(&sierra_driver);
Kevin Lloyd228426e2008-01-10 11:11:04 -0800897 usb_serial_deregister(&sierra_device);
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700898}
899
900module_init(sierra_init);
901module_exit(sierra_exit);
902
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700903MODULE_AUTHOR(DRIVER_AUTHOR);
904MODULE_DESCRIPTION(DRIVER_DESC);
905MODULE_VERSION(DRIVER_VERSION);
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700906MODULE_LICENSE("GPL");
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700907
Kevin Lloyd4e0fee82008-07-10 14:14:47 -0700908module_param(nmea, bool, S_IRUGO | S_IWUSR);
Kevin Lloyd228426e2008-01-10 11:11:04 -0800909MODULE_PARM_DESC(nmea, "NMEA streaming");
910
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700911module_param(debug, bool, S_IRUGO | S_IWUSR);
912MODULE_PARM_DESC(debug, "Debug messages");