blob: bcfe0ac1e2211921c2c6cf240a6eba11cf7fc011 [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{
86 int result;
87 int *num_ports = usb_get_serial_data(serial);
Elina Pasheva5d44b362009-04-27 18:41:52 -070088 dev_dbg(&serial->dev->dev, "%s\n", __func__);
Kevin Lloyd112225b2007-07-16 13:49:27 -070089
Kevin Lloyd228426e2008-01-10 11:11:04 -080090 result = *num_ports;
91
92 if (result) {
93 kfree(num_ports);
94 usb_set_serial_data(serial, NULL);
Kevin Lloyd112225b2007-07-16 13:49:27 -070095 }
96
Kevin Lloyd228426e2008-01-10 11:11:04 -080097 return result;
98}
99
Elina Pasheva4db22992009-06-11 14:32:01 +0100100static int is_blacklisted(const u8 ifnum,
101 const struct sierra_iface_info *blacklist)
102{
103 const u8 *info;
104 int i;
105
106 if (blacklist) {
107 info = blacklist->ifaceinfo;
108
109 for (i = 0; i < blacklist->infolen; i++) {
110 if (info[i] == ifnum)
111 return 1;
112 }
113 }
114 return 0;
115}
116
Kevin Lloyd71069672008-04-02 11:24:56 -0700117static int sierra_calc_interface(struct usb_serial *serial)
118{
Kevin Lloyd4e0fee82008-07-10 14:14:47 -0700119 int interface;
120 struct usb_interface *p_interface;
121 struct usb_host_interface *p_host_interface;
Elina Pasheva5d44b362009-04-27 18:41:52 -0700122 dev_dbg(&serial->dev->dev, "%s\n", __func__);
Kevin Lloyd71069672008-04-02 11:24:56 -0700123
Kevin Lloyd4e0fee82008-07-10 14:14:47 -0700124 /* Get the interface structure pointer from the serial struct */
125 p_interface = serial->interface;
Kevin Lloyd71069672008-04-02 11:24:56 -0700126
Kevin Lloyd4e0fee82008-07-10 14:14:47 -0700127 /* Get a pointer to the host interface structure */
128 p_host_interface = p_interface->cur_altsetting;
Kevin Lloyd71069672008-04-02 11:24:56 -0700129
Kevin Lloyd4e0fee82008-07-10 14:14:47 -0700130 /* read the interface descriptor for this active altsetting
131 * to find out the interface number we are on
132 */
133 interface = p_host_interface->desc.bInterfaceNumber;
Kevin Lloyd71069672008-04-02 11:24:56 -0700134
Kevin Lloyd4e0fee82008-07-10 14:14:47 -0700135 return interface;
Kevin Lloyd71069672008-04-02 11:24:56 -0700136}
137
Kevin Lloyd228426e2008-01-10 11:11:04 -0800138static int sierra_probe(struct usb_serial *serial,
139 const struct usb_device_id *id)
140{
141 int result = 0;
142 struct usb_device *udev;
143 int *num_ports;
144 u8 ifnum;
Kevin Lloyde3173b22008-07-10 14:14:51 -0700145 u8 numendpoints;
146
Elina Pasheva5d44b362009-04-27 18:41:52 -0700147 dev_dbg(&serial->dev->dev, "%s\n", __func__);
Kevin Lloyd228426e2008-01-10 11:11:04 -0800148
149 num_ports = kmalloc(sizeof(*num_ports), GFP_KERNEL);
150 if (!num_ports)
151 return -ENOMEM;
152
153 ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
Kevin Lloyde3173b22008-07-10 14:14:51 -0700154 numendpoints = serial->interface->cur_altsetting->desc.bNumEndpoints;
Kevin Lloyd228426e2008-01-10 11:11:04 -0800155 udev = serial->dev;
156
Kevin Lloyde3173b22008-07-10 14:14:51 -0700157 /* Figure out the interface number from the serial structure */
158 ifnum = sierra_calc_interface(serial);
Kevin Lloyd71069672008-04-02 11:24:56 -0700159
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
Kevin Lloyde3173b22008-07-10 14:14:51 -0700171 /* Dummy interface present on some SKUs should be ignored */
Kevin Lloyd0585e4d2008-07-10 14:14:54 -0700172 if (ifnum == 0x99)
Kevin Lloyd228426e2008-01-10 11:11:04 -0800173 *num_ports = 0;
Kevin Lloyde3173b22008-07-10 14:14:51 -0700174 else if (numendpoints <= 3)
175 *num_ports = 1;
Kevin Lloyd228426e2008-01-10 11:11:04 -0800176 else
Kevin Lloyde3173b22008-07-10 14:14:51 -0700177 *num_ports = (numendpoints-1)/2;
178
Kevin Lloyd228426e2008-01-10 11:11:04 -0800179 /*
180 * save off our num_ports info so that we can use it in the
181 * calc_num_ports callback
182 */
183 usb_set_serial_data(serial, (void *)num_ports);
184
Elina Pasheva4db22992009-06-11 14:32:01 +0100185 /* ifnum could have changed - by calling usb_set_interface */
186 ifnum = sierra_calc_interface(serial);
187
188 if (is_blacklisted(ifnum,
189 (struct sierra_iface_info *)id->driver_info)) {
190 dev_dbg(&serial->dev->dev,
191 "Ignoring blacklisted interface #%d\n", ifnum);
192 return -ENODEV;
193 }
194
Kevin Lloyd228426e2008-01-10 11:11:04 -0800195 return result;
Kevin Lloyd112225b2007-07-16 13:49:27 -0700196}
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700197
Elina Pasheva4db22992009-06-11 14:32:01 +0100198static const u8 direct_ip_non_serial_ifaces[] = { 7, 8, 9, 10, 11 };
199static const struct sierra_iface_info direct_ip_interface_blacklist = {
200 .infolen = ARRAY_SIZE(direct_ip_non_serial_ifaces),
201 .ifaceinfo = direct_ip_non_serial_ifaces,
202};
203
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700204static struct usb_device_id id_table [] = {
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700205 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800206 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
Greg Kroah-Hartman90ac3c82002-04-09 12:14:34 -0700207 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
Tony Murrayf8834f12008-08-22 17:30:44 -0500208 { USB_DEVICE(0x03f0, 0x1b1d) }, /* HP ev2200 a.k.a MC5720 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800209 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
Kevin Lloyd4e0fee82008-07-10 14:14:47 -0700210 { USB_DEVICE(0x1199, 0x0024) }, /* Sierra Wireless MC5727 */
agilmore@wirelessbeehive.comb9e13ac2007-12-04 11:37:12 -0700211 { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800212 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
213 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700214 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U */
Kevin Lloyd4e0fee82008-07-10 14:14:47 -0700215 /* Sierra Wireless C597 */
216 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0023, 0xFF, 0xFF, 0xFF) },
Kevin Lloyde3173b22008-07-10 14:14:51 -0700217 /* Sierra Wireless Device */
218 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0025, 0xFF, 0xFF, 0xFF) },
219 { USB_DEVICE(0x1199, 0x0026) }, /* Sierra Wireless Device */
Kevin Lloyd73b2c202008-08-25 19:20:40 -0700220 { USB_DEVICE(0x1199, 0x0027) }, /* Sierra Wireless Device */
221 { USB_DEVICE(0x1199, 0x0028) }, /* Sierra Wireless Device */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700222
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700223 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
Kevin Lloyde43062d2007-01-17 16:04:18 -0800224 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700225 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
Kevin Lloyd9454c462007-07-16 13:49:29 -0700226 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
Kevin Lloyd4e0fee82008-07-10 14:14:47 -0700227 { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 (Lenovo) */
Kevin Lloyd7f170a62008-03-14 00:53:24 -0700228 { USB_DEVICE(0x1199, 0x6815) }, /* Sierra Wireless MC8775 */
Stefan Seyfried8f7f85e2008-04-17 07:47:34 +0200229 { USB_DEVICE(0x03f0, 0x1e1d) }, /* HP hs2300 a.k.a MC8775 */
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700230 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
Kevin Lloyd71069672008-04-02 11:24:56 -0700231 { USB_DEVICE(0x1199, 0x6821) }, /* Sierra Wireless AirCard 875U */
Kevin Lloyd4e0fee82008-07-10 14:14:47 -0700232 { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780 */
233 { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781 */
Kevin Lloydb77a5c72008-09-17 09:03:38 -0700234 { USB_DEVICE(0x1199, 0x683A) }, /* Sierra Wireless MC8785 */
Kevin Lloyde3173b22008-07-10 14:14:51 -0700235 { USB_DEVICE(0x1199, 0x683B) }, /* Sierra Wireless MC8785 Composite */
Elina Pasheva4db22992009-06-11 14:32:01 +0100236 /* Sierra Wireless MC8790, MC8791, MC8792 Composite */
237 { USB_DEVICE(0x1199, 0x683C) },
238 { USB_DEVICE(0x1199, 0x683D) }, /* Sierra Wireless MC8791 Composite */
239 /* Sierra Wireless MC8790, MC8791, MC8792 */
240 { USB_DEVICE(0x1199, 0x683E) },
Kevin Lloyd9454c462007-07-16 13:49:29 -0700241 { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
242 { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
243 { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880 E */
244 { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881 E */
Kevin Lloyd6835b322008-01-10 11:11:04 -0800245 { USB_DEVICE(0x1199, 0x6855) }, /* Sierra Wireless AirCard 880 U */
Jessica L. Blank62aad3a2007-12-30 20:11:35 -0600246 { USB_DEVICE(0x1199, 0x6856) }, /* Sierra Wireless AirCard 881 U */
Kevin Lloyde3173b22008-07-10 14:14:51 -0700247 { USB_DEVICE(0x1199, 0x6859) }, /* Sierra Wireless AirCard 885 E */
248 { USB_DEVICE(0x1199, 0x685A) }, /* Sierra Wireless AirCard 885 E */
249 /* Sierra Wireless C885 */
250 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6880, 0xFF, 0xFF, 0xFF)},
251 /* Sierra Wireless Device */
252 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6890, 0xFF, 0xFF, 0xFF)},
253 /* Sierra Wireless Device */
Kevin Lloyd73b2c202008-08-25 19:20:40 -0700254 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6891, 0xFF, 0xFF, 0xFF)},
255 /* Sierra Wireless Device */
Kevin Lloyde3173b22008-07-10 14:14:51 -0700256 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6892, 0xFF, 0xFF, 0xFF)},
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700257
Kevin Lloyde3173b22008-07-10 14:14:51 -0700258 { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
259 { USB_DEVICE(0x0F3D, 0x0112) }, /* Airprime/Sierra PC 5220 */
Kevin Lloyd112225b2007-07-16 13:49:27 -0700260
Elina Pasheva4db22992009-06-11 14:32:01 +0100261 { USB_DEVICE(0x1199, 0x68A3), /* Sierra Wireless Direct IP modems */
262 .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
263 },
264
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700265 { }
266};
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700267MODULE_DEVICE_TABLE(usb, id_table);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700268
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700269static struct usb_driver sierra_driver = {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700270 .name = "sierra",
Kevin Lloyd228426e2008-01-10 11:11:04 -0800271 .probe = usb_serial_probe,
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700272 .disconnect = usb_serial_disconnect,
273 .id_table = id_table,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700274 .no_dynamic_id = 1,
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700275};
276
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700277struct sierra_port_private {
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900278 spinlock_t lock; /* lock the structure */
279 int outstanding_urbs; /* number of out urbs in flight */
280
Oliver Neukum4f4f9c52008-03-14 00:53:24 -0700281 /* Input endpoints and buffers for this port */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700282 struct urb *in_urbs[N_IN_URB];
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700283
284 /* Settings for the port */
285 int rts_state; /* Handshaking pins (outputs) */
286 int dtr_state;
287 int cts_state; /* Handshaking pins (inputs) */
288 int dsr_state;
289 int dcd_state;
290 int ri_state;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700291};
292
Alan Cox335f8512009-06-11 12:26:29 +0100293static int sierra_send_setup(struct usb_serial_port *port)
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700294{
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700295 struct usb_serial *serial = port->serial;
296 struct sierra_port_private *portdata;
Kevin Lloyd228426e2008-01-10 11:11:04 -0800297 __u16 interface = 0;
Alan Cox335f8512009-06-11 12:26:29 +0100298 int val = 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700299
Elina Pasheva5d44b362009-04-27 18:41:52 -0700300 dev_dbg(&port->dev, "%s\n", __func__);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700301
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700302 portdata = usb_get_serial_port_data(port);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700303
Alan Cox335f8512009-06-11 12:26:29 +0100304 if (portdata->dtr_state)
305 val |= 0x01;
306 if (portdata->rts_state)
307 val |= 0x02;
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700308
Alan Cox335f8512009-06-11 12:26:29 +0100309 /* If composite device then properly report interface */
Alan Cox00b040d2009-06-11 14:29:29 +0100310 if (serial->num_ports == 1) {
Alan Cox335f8512009-06-11 12:26:29 +0100311 interface = sierra_calc_interface(serial);
Alan Cox00b040d2009-06-11 14:29:29 +0100312 /* Control message is sent only to interfaces with
313 * interrupt_in endpoints
314 */
315 if (port->interrupt_in_urb) {
316 /* send control message */
317 return usb_control_msg(serial->dev,
318 usb_rcvctrlpipe(serial->dev, 0),
319 0x22, 0x21, val, interface,
320 NULL, 0, USB_CTRL_SET_TIMEOUT);
321 }
322 }
Kevin Lloyde3173b22008-07-10 14:14:51 -0700323
Alan Cox335f8512009-06-11 12:26:29 +0100324 /* Otherwise the need to do non-composite mapping */
325 else {
326 if (port->bulk_out_endpointAddress == 2)
327 interface = 0;
328 else if (port->bulk_out_endpointAddress == 4)
329 interface = 1;
330 else if (port->bulk_out_endpointAddress == 5)
331 interface = 2;
Alan Cox00b040d2009-06-11 14:29:29 +0100332 return usb_control_msg(serial->dev,
Alan Cox335f8512009-06-11 12:26:29 +0100333 usb_rcvctrlpipe(serial->dev, 0),
334 0x22, 0x21, val, interface,
335 NULL, 0, USB_CTRL_SET_TIMEOUT);
Alan Cox00b040d2009-06-11 14:29:29 +0100336 }
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700337 return 0;
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700338}
339
Alan Cox95da3102008-07-22 11:09:07 +0100340static void sierra_set_termios(struct tty_struct *tty,
341 struct usb_serial_port *port, struct ktermios *old_termios)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700342{
Elina Pasheva5d44b362009-04-27 18:41:52 -0700343 dev_dbg(&port->dev, "%s\n", __func__);
Alan Cox95da3102008-07-22 11:09:07 +0100344 tty_termios_copy_hw(tty->termios, old_termios);
Alan Cox335f8512009-06-11 12:26:29 +0100345 sierra_send_setup(port);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700346}
347
Alan Cox95da3102008-07-22 11:09:07 +0100348static int sierra_tiocmget(struct tty_struct *tty, struct file *file)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700349{
Alan Cox95da3102008-07-22 11:09:07 +0100350 struct usb_serial_port *port = tty->driver_data;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700351 unsigned int value;
352 struct sierra_port_private *portdata;
353
Elina Pasheva5d44b362009-04-27 18:41:52 -0700354 dev_dbg(&port->dev, "%s\n", __func__);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700355 portdata = usb_get_serial_port_data(port);
356
357 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
358 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
359 ((portdata->cts_state) ? TIOCM_CTS : 0) |
360 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
361 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
362 ((portdata->ri_state) ? TIOCM_RNG : 0);
363
364 return value;
365}
366
Alan Cox95da3102008-07-22 11:09:07 +0100367static int sierra_tiocmset(struct tty_struct *tty, struct file *file,
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700368 unsigned int set, unsigned int clear)
369{
Alan Cox95da3102008-07-22 11:09:07 +0100370 struct usb_serial_port *port = tty->driver_data;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700371 struct sierra_port_private *portdata;
372
373 portdata = usb_get_serial_port_data(port);
374
375 if (set & TIOCM_RTS)
376 portdata->rts_state = 1;
377 if (set & TIOCM_DTR)
378 portdata->dtr_state = 1;
379
380 if (clear & TIOCM_RTS)
381 portdata->rts_state = 0;
382 if (clear & TIOCM_DTR)
383 portdata->dtr_state = 0;
Alan Cox335f8512009-06-11 12:26:29 +0100384 return sierra_send_setup(port);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700385}
386
Elina Pashevab9a44bc2009-06-11 14:30:21 +0100387static void sierra_release_urb(struct urb *urb)
388{
389 struct usb_serial_port *port;
390 if (urb) {
391 port = urb->context;
392 dev_dbg(&port->dev, "%s: %p\n", __func__, urb);
393 kfree(urb->transfer_buffer);
394 usb_free_urb(urb);
395 }
396}
397
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900398static void sierra_outdat_callback(struct urb *urb)
399{
400 struct usb_serial_port *port = urb->context;
401 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
402 int status = urb->status;
403 unsigned long flags;
404
Elina Pasheva5d44b362009-04-27 18:41:52 -0700405 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900406
407 /* free up the transfer buffer, as usb_free_urb() does not do this */
408 kfree(urb->transfer_buffer);
409
410 if (status)
Kevin Lloyd7384a922008-09-16 21:05:24 -0700411 dev_dbg(&port->dev, "%s - nonzero write bulk status "
Elina Pasheva5d44b362009-04-27 18:41:52 -0700412 "received: %d\n", __func__, status);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900413
414 spin_lock_irqsave(&portdata->lock, flags);
415 --portdata->outstanding_urbs;
416 spin_unlock_irqrestore(&portdata->lock, flags);
417
418 usb_serial_port_softint(port);
419}
420
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700421/* Write */
Alan Cox95da3102008-07-22 11:09:07 +0100422static int sierra_write(struct tty_struct *tty, struct usb_serial_port *port,
423 const unsigned char *buf, int count)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700424{
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900425 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
426 struct usb_serial *serial = port->serial;
427 unsigned long flags;
428 unsigned char *buffer;
429 struct urb *urb;
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700430 size_t writesize = min((size_t)count, (size_t)MAX_TRANSFER);
431 int retval = 0;
432
433 /* verify that we actually have some data to write */
434 if (count == 0)
435 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700436
437 portdata = usb_get_serial_port_data(port);
438
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700439 dev_dbg(&port->dev, "%s: write (%d bytes)\n", __func__, writesize);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700440
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900441 spin_lock_irqsave(&portdata->lock, flags);
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700442 dev_dbg(&port->dev, "%s - outstanding_urbs: %d\n", __func__,
443 portdata->outstanding_urbs);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900444 if (portdata->outstanding_urbs > N_OUT_URB) {
445 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd7384a922008-09-16 21:05:24 -0700446 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900447 return 0;
448 }
449 portdata->outstanding_urbs++;
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700450 dev_dbg(&port->dev, "%s - 1, outstanding_urbs: %d\n", __func__,
451 portdata->outstanding_urbs);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900452 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700453
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700454 buffer = kmalloc(writesize, GFP_ATOMIC);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900455 if (!buffer) {
456 dev_err(&port->dev, "out of memory\n");
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700457 retval = -ENOMEM;
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900458 goto error_no_buffer;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700459 }
460
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900461 urb = usb_alloc_urb(0, GFP_ATOMIC);
462 if (!urb) {
463 dev_err(&port->dev, "no more free urbs\n");
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700464 retval = -ENOMEM;
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900465 goto error_no_urb;
466 }
467
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700468 memcpy(buffer, buf, writesize);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900469
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700470 usb_serial_debug_data(debug, &port->dev, __func__, writesize, buffer);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900471
472 usb_fill_bulk_urb(urb, serial->dev,
473 usb_sndbulkpipe(serial->dev,
474 port->bulk_out_endpointAddress),
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700475 buffer, writesize, sierra_outdat_callback, port);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900476
Elina Pasheva238ebd12009-05-12 13:12:24 -0700477 /* Handle the need to send a zero length packet */
478 urb->transfer_flags |= URB_ZERO_PACKET;
479
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900480 /* send it down the pipe */
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700481 retval = usb_submit_urb(urb, GFP_ATOMIC);
482 if (retval) {
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900483 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700484 "with status = %d\n", __func__, retval);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900485 goto error;
486 }
487
488 /* we are done with this urb, so let the host driver
489 * really free it when it is finished with it */
490 usb_free_urb(urb);
491
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700492 return writesize;
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900493error:
494 usb_free_urb(urb);
495error_no_urb:
496 kfree(buffer);
497error_no_buffer:
498 spin_lock_irqsave(&portdata->lock, flags);
499 --portdata->outstanding_urbs;
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700500 dev_dbg(&port->dev, "%s - 2. outstanding_urbs: %d\n", __func__,
501 portdata->outstanding_urbs);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900502 spin_unlock_irqrestore(&portdata->lock, flags);
Elina Pasheva40d2ff32009-04-29 10:26:46 -0700503 return retval;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700504}
505
506static void sierra_indat_callback(struct urb *urb)
507{
508 int err;
509 int endpoint;
510 struct usb_serial_port *port;
511 struct tty_struct *tty;
512 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700513 int status = urb->status;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700514
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700515 endpoint = usb_pipeendpoint(urb->pipe);
Elina Pashevab0cda8c2009-04-29 10:29:21 -0700516 port = urb->context;
517
518 dev_dbg(&port->dev, "%s: %p\n", __func__, urb);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700519
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700520 if (status) {
Kevin Lloyd7384a922008-09-16 21:05:24 -0700521 dev_dbg(&port->dev, "%s: nonzero status: %d on"
Elina Pasheva5d44b362009-04-27 18:41:52 -0700522 " endpoint %02x\n", __func__, status, endpoint);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700523 } else {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700524 if (urb->actual_length) {
Alan Coxd95186d2009-01-02 13:42:56 +0000525 tty = tty_port_tty_get(&port->port);
Elina Pashevab0cda8c2009-04-29 10:29:21 -0700526
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700527 tty_buffer_request_room(tty, urb->actual_length);
528 tty_insert_flip_string(tty, data, urb->actual_length);
529 tty_flip_buffer_push(tty);
Elina Pashevab0cda8c2009-04-29 10:29:21 -0700530
Alan Cox4a90f092008-10-13 10:39:46 +0100531 tty_kref_put(tty);
Elina Pashevab0cda8c2009-04-29 10:29:21 -0700532 usb_serial_debug_data(debug, &port->dev, __func__,
533 urb->actual_length, data);
534 } else {
Kevin Lloyd7384a922008-09-16 21:05:24 -0700535 dev_dbg(&port->dev, "%s: empty read urb"
Elina Pasheva5d44b362009-04-27 18:41:52 -0700536 " received\n", __func__);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700537 }
538 }
Elina Pashevab0cda8c2009-04-29 10:29:21 -0700539
540 /* Resubmit urb so we continue receiving */
541 if (port->port.count && status != -ESHUTDOWN && status != -EPERM) {
542 err = usb_submit_urb(urb, GFP_ATOMIC);
543 if (err)
544 dev_err(&port->dev, "resubmit read urb failed."
545 "(%d)\n", err);
546 }
547
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700548 return;
549}
550
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700551static void sierra_instat_callback(struct urb *urb)
552{
553 int err;
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700554 int status = urb->status;
Ming Leicdc97792008-02-24 18:41:47 +0800555 struct usb_serial_port *port = urb->context;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700556 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
557 struct usb_serial *serial = port->serial;
558
Elina Pasheva5d44b362009-04-27 18:41:52 -0700559 dev_dbg(&port->dev, "%s: urb %p port %p has data %p\n", __func__,
Kevin Lloyd7384a922008-09-16 21:05:24 -0700560 urb, port, portdata);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700561
Greg Kroah-Hartman17dd2212007-06-15 15:44:13 -0700562 if (status == 0) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700563 struct usb_ctrlrequest *req_pkt =
564 (struct usb_ctrlrequest *)urb->transfer_buffer;
565
566 if (!req_pkt) {
Kevin Lloyd7384a922008-09-16 21:05:24 -0700567 dev_dbg(&port->dev, "%s: NULL req_pkt\n",
568 __func__);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700569 return;
570 }
571 if ((req_pkt->bRequestType == 0xA1) &&
572 (req_pkt->bRequest == 0x20)) {
573 int old_dcd_state;
574 unsigned char signals = *((unsigned char *)
575 urb->transfer_buffer +
576 sizeof(struct usb_ctrlrequest));
Alan Cox4a90f092008-10-13 10:39:46 +0100577 struct tty_struct *tty;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700578
Elina Pasheva5d44b362009-04-27 18:41:52 -0700579 dev_dbg(&port->dev, "%s: signal x%x\n", __func__,
Kevin Lloyd7384a922008-09-16 21:05:24 -0700580 signals);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700581
582 old_dcd_state = portdata->dcd_state;
583 portdata->cts_state = 1;
584 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
585 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
586 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
587
Alan Cox4a90f092008-10-13 10:39:46 +0100588 tty = tty_port_tty_get(&port->port);
589 if (tty && !C_CLOCAL(tty) &&
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700590 old_dcd_state && !portdata->dcd_state)
Alan Cox4a90f092008-10-13 10:39:46 +0100591 tty_hangup(tty);
592 tty_kref_put(tty);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700593 } else {
Elina Pasheva5d44b362009-04-27 18:41:52 -0700594 dev_dbg(&port->dev, "%s: type %x req %x\n",
Kevin Lloyd7384a922008-09-16 21:05:24 -0700595 __func__, req_pkt->bRequestType,
596 req_pkt->bRequest);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700597 }
598 } else
Elina Pasheva5d44b362009-04-27 18:41:52 -0700599 dev_dbg(&port->dev, "%s: error %d\n", __func__, status);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700600
601 /* Resubmit urb so we continue receiving IRQ data */
Elina Pashevac76a23d2009-05-12 13:12:37 -0700602 if (port->port.count && status != -ESHUTDOWN && status != -ENOENT) {
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700603 urb->dev = serial->dev;
604 err = usb_submit_urb(urb, GFP_ATOMIC);
605 if (err)
Elina Pashevac76a23d2009-05-12 13:12:37 -0700606 dev_err(&port->dev, "%s: resubmit intr urb "
607 "failed. (%d)\n", __func__, err);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700608 }
609}
610
Alan Cox95da3102008-07-22 11:09:07 +0100611static int sierra_write_room(struct tty_struct *tty)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700612{
Alan Cox95da3102008-07-22 11:09:07 +0100613 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900614 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
615 unsigned long flags;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700616
Elina Pasheva5d44b362009-04-27 18:41:52 -0700617 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700618
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900619 /* try to give a good number back based on if we have any free urbs at
620 * this point in time */
621 spin_lock_irqsave(&portdata->lock, flags);
622 if (portdata->outstanding_urbs > N_OUT_URB * 2 / 3) {
623 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd7384a922008-09-16 21:05:24 -0700624 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900625 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700626 }
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900627 spin_unlock_irqrestore(&portdata->lock, flags);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700628
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900629 return 2048;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700630}
631
Elina Pashevab9a44bc2009-06-11 14:30:21 +0100632static void sierra_stop_rx_urbs(struct usb_serial_port *port)
633{
634 int i;
635 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
636
637 for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++)
638 usb_kill_urb(portdata->in_urbs[i]);
639
640 usb_kill_urb(port->interrupt_in_urb);
641}
642
643static int sierra_submit_rx_urbs(struct usb_serial_port *port, gfp_t mem_flags)
644{
645 int ok_cnt;
646 int err = -EINVAL;
647 int i;
648 struct urb *urb;
649 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
650
651 ok_cnt = 0;
652 for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++) {
653 urb = portdata->in_urbs[i];
654 if (!urb)
655 continue;
656 err = usb_submit_urb(urb, mem_flags);
657 if (err) {
658 dev_err(&port->dev, "%s: submit urb failed: %d\n",
659 __func__, err);
660 } else {
661 ok_cnt++;
662 }
663 }
664
665 if (ok_cnt && port->interrupt_in_urb) {
666 err = usb_submit_urb(port->interrupt_in_urb, mem_flags);
667 if (err) {
668 dev_err(&port->dev, "%s: submit intr urb failed: %d\n",
669 __func__, err);
670 }
671 }
672
673 if (ok_cnt > 0) /* at least one rx urb submitted */
674 return 0;
675 else
676 return err;
677}
678
679static struct urb *sierra_setup_urb(struct usb_serial *serial, int endpoint,
680 int dir, void *ctx, int len,
681 gfp_t mem_flags,
682 usb_complete_t callback)
683{
684 struct urb *urb;
685 u8 *buf;
686
687 if (endpoint == -1)
688 return NULL;
689
690 urb = usb_alloc_urb(0, mem_flags);
691 if (urb == NULL) {
692 dev_dbg(&serial->dev->dev, "%s: alloc for endpoint %d failed\n",
693 __func__, endpoint);
694 return NULL;
695 }
696
697 buf = kmalloc(len, mem_flags);
698 if (buf) {
699 /* Fill URB using supplied data */
700 usb_fill_bulk_urb(urb, serial->dev,
701 usb_sndbulkpipe(serial->dev, endpoint) | dir,
702 buf, len, callback, ctx);
703
704 /* debug */
705 dev_dbg(&serial->dev->dev, "%s %c u : %p d:%p\n", __func__,
706 dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
707 } else {
708 dev_dbg(&serial->dev->dev, "%s %c u:%p d:%p\n", __func__,
709 dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
710
711 sierra_release_urb(urb);
712 urb = NULL;
713 }
714
715 return urb;
716}
717
718static void sierra_close(struct usb_serial_port *port)
719{
720 int i;
721 struct usb_serial *serial = port->serial;
722 struct sierra_port_private *portdata;
723
724 dev_dbg(&port->dev, "%s\n", __func__);
725 portdata = usb_get_serial_port_data(port);
726
727 portdata->rts_state = 0;
728 portdata->dtr_state = 0;
729
730 if (serial->dev) {
731 mutex_lock(&serial->disc_mutex);
732 if (!serial->disconnected)
733 sierra_send_setup(port);
734 mutex_unlock(&serial->disc_mutex);
735
736 /* Stop reading urbs */
737 sierra_stop_rx_urbs(port);
738 /* .. and release them */
739 for (i = 0; i < N_IN_URB; i++) {
740 sierra_release_urb(portdata->in_urbs[i]);
741 portdata->in_urbs[i] = NULL;
742 }
743 }
744}
745
Alan Cox95da3102008-07-22 11:09:07 +0100746static int sierra_open(struct tty_struct *tty,
747 struct usb_serial_port *port, struct file *filp)
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700748{
749 struct sierra_port_private *portdata;
750 struct usb_serial *serial = port->serial;
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900751 int i;
Elina Pashevab9a44bc2009-06-11 14:30:21 +0100752 int err;
753 int endpoint;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700754 struct urb *urb;
755
756 portdata = usb_get_serial_port_data(port);
757
Elina Pasheva5d44b362009-04-27 18:41:52 -0700758 dev_dbg(&port->dev, "%s\n", __func__);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700759
760 /* Set some sane defaults */
761 portdata->rts_state = 1;
762 portdata->dtr_state = 1;
763
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700764
Elina Pashevab9a44bc2009-06-11 14:30:21 +0100765 endpoint = port->bulk_in_endpointAddress;
766 for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++) {
767 urb = sierra_setup_urb(serial, endpoint, USB_DIR_IN, port,
768 IN_BUFLEN, GFP_KERNEL,
769 sierra_indat_callback);
770 portdata->in_urbs[i] = urb;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700771 }
Elina Pashevab9a44bc2009-06-11 14:30:21 +0100772 /* clear halt condition */
773 usb_clear_halt(serial->dev,
774 usb_sndbulkpipe(serial->dev, endpoint) | USB_DIR_IN);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700775
Elina Pashevab9a44bc2009-06-11 14:30:21 +0100776 err = sierra_submit_rx_urbs(port, GFP_KERNEL);
777 if (err) {
778 /* get rid of everything as in close */
779 sierra_close(port);
780 return err;
781 }
Alan Cox335f8512009-06-11 12:26:29 +0100782 sierra_send_setup(port);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700783
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900784 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700785}
786
Elina Pashevab9a44bc2009-06-11 14:30:21 +0100787
Alan Cox335f8512009-06-11 12:26:29 +0100788static void sierra_dtr_rts(struct usb_serial_port *port, int on)
789{
790 struct usb_serial *serial = port->serial;
791 struct sierra_port_private *portdata;
792
793 portdata = usb_get_serial_port_data(port);
794 portdata->rts_state = on;
795 portdata->dtr_state = on;
796
797 if (serial->dev) {
798 mutex_lock(&serial->disc_mutex);
799 if (!serial->disconnected)
800 sierra_send_setup(port);
801 mutex_unlock(&serial->disc_mutex);
802 }
803}
804
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700805static int sierra_startup(struct usb_serial *serial)
806{
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700807 struct usb_serial_port *port;
808 struct sierra_port_private *portdata;
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900809 int i;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700810
Elina Pasheva5d44b362009-04-27 18:41:52 -0700811 dev_dbg(&serial->dev->dev, "%s\n", __func__);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700812
Kevin Lloyd228426e2008-01-10 11:11:04 -0800813 /* Set Device mode to D0 */
Kevin Lloyd112225b2007-07-16 13:49:27 -0700814 sierra_set_power_state(serial->dev, 0x0000);
815
Kevin Lloyd228426e2008-01-10 11:11:04 -0800816 /* Check NMEA and set */
817 if (nmea)
818 sierra_vsc_set_nmea(serial->dev, 1);
819
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700820 /* Now setup per port private data */
821 for (i = 0; i < serial->num_ports; i++) {
822 port = serial->port[i];
823 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
824 if (!portdata) {
Kevin Lloyd7384a922008-09-16 21:05:24 -0700825 dev_dbg(&port->dev, "%s: kmalloc for "
Elina Pasheva5d44b362009-04-27 18:41:52 -0700826 "sierra_port_private (%d) failed!.\n",
Kevin Lloyd7384a922008-09-16 21:05:24 -0700827 __func__, i);
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900828 return -ENOMEM;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700829 }
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900830 spin_lock_init(&portdata->lock);
Elina Pashevab9a44bc2009-06-11 14:30:21 +0100831 /* Set the port private data pointer */
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700832 usb_set_serial_port_data(port, portdata);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700833 }
834
Greg Kroah-Hartman17c23272007-06-20 14:22:23 +0900835 return 0;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700836}
837
838static void sierra_shutdown(struct usb_serial *serial)
839{
Elina Pashevab9a44bc2009-06-11 14:30:21 +0100840 int i;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700841 struct usb_serial_port *port;
842 struct sierra_port_private *portdata;
843
Elina Pasheva5d44b362009-04-27 18:41:52 -0700844 dev_dbg(&serial->dev->dev, "%s\n", __func__);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700845
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700846 for (i = 0; i < serial->num_ports; ++i) {
847 port = serial->port[i];
Greg Kroah-Hartmanf094e4f2007-04-26 00:12:01 -0700848 if (!port)
849 continue;
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700850 portdata = usb_get_serial_port_data(port);
Greg Kroah-Hartmanf094e4f2007-04-26 00:12:01 -0700851 if (!portdata)
852 continue;
Greg Kroah-Hartman9e85c5f2007-06-20 14:22:23 +0900853 kfree(portdata);
854 usb_set_serial_port_data(port, NULL);
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700855 }
856}
857
Kevin Lloyd228426e2008-01-10 11:11:04 -0800858static struct usb_serial_driver sierra_device = {
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700859 .driver = {
860 .owner = THIS_MODULE,
Kevin Lloyd4e0fee82008-07-10 14:14:47 -0700861 .name = "sierra",
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700862 },
Kevin Lloyd228426e2008-01-10 11:11:04 -0800863 .description = "Sierra USB modem",
864 .id_table = id_table,
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100865 .usb_driver = &sierra_driver,
Kevin Lloyd228426e2008-01-10 11:11:04 -0800866 .calc_num_ports = sierra_calc_num_ports,
867 .probe = sierra_probe,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700868 .open = sierra_open,
869 .close = sierra_close,
Alan Cox335f8512009-06-11 12:26:29 +0100870 .dtr_rts = sierra_dtr_rts,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700871 .write = sierra_write,
872 .write_room = sierra_write_room,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700873 .set_termios = sierra_set_termios,
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700874 .tiocmget = sierra_tiocmget,
875 .tiocmset = sierra_tiocmset,
876 .attach = sierra_startup,
877 .shutdown = sierra_shutdown,
878 .read_int_callback = sierra_instat_callback,
879};
880
881/* Functions used by new usb-serial code. */
882static int __init sierra_init(void)
883{
884 int retval;
Kevin Lloyd228426e2008-01-10 11:11:04 -0800885 retval = usb_serial_register(&sierra_device);
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700886 if (retval)
Kevin Lloyd228426e2008-01-10 11:11:04 -0800887 goto failed_device_register;
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700888
889
890 retval = usb_register(&sierra_driver);
891 if (retval)
892 goto failed_driver_register;
893
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700894 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
895 DRIVER_DESC "\n");
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700896
897 return 0;
898
899failed_driver_register:
Kevin Lloyd228426e2008-01-10 11:11:04 -0800900 usb_serial_deregister(&sierra_device);
901failed_device_register:
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700902 return retval;
903}
904
905static void __exit sierra_exit(void)
906{
Alan Cox9660ea32008-07-22 11:14:59 +0100907 usb_deregister(&sierra_driver);
Kevin Lloyd228426e2008-01-10 11:11:04 -0800908 usb_serial_deregister(&sierra_device);
Greg Kroah-Hartman964ee1d2006-10-17 10:17:58 -0700909}
910
911module_init(sierra_init);
912module_exit(sierra_exit);
913
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700914MODULE_AUTHOR(DRIVER_AUTHOR);
915MODULE_DESCRIPTION(DRIVER_DESC);
916MODULE_VERSION(DRIVER_VERSION);
Kevin Lloyd69de51f2006-06-30 11:17:55 -0700917MODULE_LICENSE("GPL");
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700918
Kevin Lloyd4e0fee82008-07-10 14:14:47 -0700919module_param(nmea, bool, S_IRUGO | S_IWUSR);
Kevin Lloyd228426e2008-01-10 11:11:04 -0800920MODULE_PARM_DESC(nmea, "NMEA streaming");
921
Kevin Lloyd033a3fb2006-10-13 23:53:21 -0700922module_param(debug, bool, S_IRUGO | S_IWUSR);
923MODULE_PARM_DESC(debug, "Debug messages");