blob: 5083eb5b0d5ed9f7d7dc90ee5309c4ae5e6d437a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/******************************************************************************
2 * speedtch.c - Alcatel SpeedTouch USB xDSL modem driver
3 *
4 * Copyright (C) 2001, Alcatel
5 * Copyright (C) 2003, Duncan Sands
6 * Copyright (C) 2004, David Woodhouse
7 *
Duncan Sands48da7262005-05-11 20:20:40 +02008 * Based on "modem_run.c", copyright (C) 2001, Benoit Papillault
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
13 * any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * more details.
19 *
20 * You should have received a copy of the GNU General Public License along with
21 * this program; if not, write to the Free Software Foundation, Inc., 59
22 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 *
24 ******************************************************************************/
25
Duncan Sands48da7262005-05-11 20:20:40 +020026#include <asm/page.h>
27#include <linux/device.h>
28#include <linux/errno.h>
29#include <linux/firmware.h>
Duncan Sands48da7262005-05-11 20:20:40 +020030#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/module.h>
32#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/slab.h>
Duncan Sands48da7262005-05-11 20:20:40 +020034#include <linux/stat.h>
35#include <linux/timer.h>
Duncan Sands80aae7a2006-01-13 10:59:23 +010036#include <linux/types.h>
David Brownell5f848132006-12-16 15:34:53 -080037#include <linux/usb/ch9.h>
Duncan Sands48da7262005-05-11 20:20:40 +020038#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Duncan Sands48da7262005-05-11 20:20:40 +020040#include "usbatm.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#define DRIVER_AUTHOR "Johan Verrept, Duncan Sands <duncan.sands@free.fr>"
Duncan Sands9b0e54a2006-01-13 11:08:05 +010043#define DRIVER_VERSION "1.10"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define DRIVER_DESC "Alcatel SpeedTouch USB driver version " DRIVER_VERSION
45
46static const char speedtch_driver_name[] = "speedtch";
47
Duncan Sands48da7262005-05-11 20:20:40 +020048#define CTRL_TIMEOUT 2000 /* milliseconds */
49#define DATA_TIMEOUT 2000 /* milliseconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Duncan Sands48da7262005-05-11 20:20:40 +020051#define OFFSET_7 0 /* size 1 */
52#define OFFSET_b 1 /* size 8 */
53#define OFFSET_d 9 /* size 4 */
54#define OFFSET_e 13 /* size 1 */
55#define OFFSET_f 14 /* size 1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Duncan Sands48da7262005-05-11 20:20:40 +020057#define SIZE_7 1
58#define SIZE_b 8
59#define SIZE_d 4
60#define SIZE_e 1
61#define SIZE_f 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Duncan Sands48da7262005-05-11 20:20:40 +020063#define MIN_POLL_DELAY 5000 /* milliseconds */
64#define MAX_POLL_DELAY 60000 /* milliseconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Duncan Sands48da7262005-05-11 20:20:40 +020066#define RESUBMIT_DELAY 1000 /* milliseconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Duncan Sands80aae7a2006-01-13 10:59:23 +010068#define DEFAULT_BULK_ALTSETTING 1
Duncan Sands67c752b2006-04-28 18:44:06 +020069#define DEFAULT_ISOC_ALTSETTING 3
Duncan Sands48da7262005-05-11 20:20:40 +020070#define DEFAULT_DL_512_FIRST 0
Duncan Sands80aae7a2006-01-13 10:59:23 +010071#define DEFAULT_ENABLE_ISOC 0
Duncan Sands48da7262005-05-11 20:20:40 +020072#define DEFAULT_SW_BUFFERING 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Duncan Sands80aae7a2006-01-13 10:59:23 +010074static unsigned int altsetting = 0; /* zero means: use the default */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103075static bool dl_512_first = DEFAULT_DL_512_FIRST;
76static bool enable_isoc = DEFAULT_ENABLE_ISOC;
77static bool sw_buffering = DEFAULT_SW_BUFFERING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Duncan Sands6a4f1b42006-10-05 10:40:55 +020079#define DEFAULT_B_MAX_DSL 8128
80#define DEFAULT_MODEM_MODE 11
81#define MODEM_OPTION_LENGTH 16
82static const unsigned char DEFAULT_MODEM_OPTION[MODEM_OPTION_LENGTH] = {
83 0x10, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
84};
85
86static unsigned int BMaxDSL = DEFAULT_B_MAX_DSL;
87static unsigned char ModemMode = DEFAULT_MODEM_MODE;
88static unsigned char ModemOption[MODEM_OPTION_LENGTH];
Al Viro64a6f952007-10-14 19:35:30 +010089static unsigned int num_ModemOption;
Duncan Sands6a4f1b42006-10-05 10:40:55 +020090
Duncan Sands80aae7a2006-01-13 10:59:23 +010091module_param(altsetting, uint, S_IRUGO | S_IWUSR);
Duncan Sands48da7262005-05-11 20:20:40 +020092MODULE_PARM_DESC(altsetting,
Duncan Sands80aae7a2006-01-13 10:59:23 +010093 "Alternative setting for data interface (bulk_default: "
94 __MODULE_STRING(DEFAULT_BULK_ALTSETTING) "; isoc_default: "
95 __MODULE_STRING(DEFAULT_ISOC_ALTSETTING) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Duncan Sands48da7262005-05-11 20:20:40 +020097module_param(dl_512_first, bool, S_IRUGO | S_IWUSR);
98MODULE_PARM_DESC(dl_512_first,
99 "Read 512 bytes before sending firmware (default: "
100 __MODULE_STRING(DEFAULT_DL_512_FIRST) ")");
101
Duncan Sands80aae7a2006-01-13 10:59:23 +0100102module_param(enable_isoc, bool, S_IRUGO | S_IWUSR);
103MODULE_PARM_DESC(enable_isoc,
104 "Use isochronous transfers if available (default: "
105 __MODULE_STRING(DEFAULT_ENABLE_ISOC) ")");
106
Duncan Sands48da7262005-05-11 20:20:40 +0200107module_param(sw_buffering, bool, S_IRUGO | S_IWUSR);
108MODULE_PARM_DESC(sw_buffering,
109 "Enable software buffering (default: "
110 __MODULE_STRING(DEFAULT_SW_BUFFERING) ")");
111
Duncan Sands6a4f1b42006-10-05 10:40:55 +0200112module_param(BMaxDSL, uint, S_IRUGO | S_IWUSR);
113MODULE_PARM_DESC(BMaxDSL,
114 "default: " __MODULE_STRING(DEFAULT_B_MAX_DSL));
115
116module_param(ModemMode, byte, S_IRUGO | S_IWUSR);
117MODULE_PARM_DESC(ModemMode,
118 "default: " __MODULE_STRING(DEFAULT_MODEM_MODE));
119
120module_param_array(ModemOption, byte, &num_ModemOption, S_IRUGO);
121MODULE_PARM_DESC(ModemOption, "default: 0x10,0x00,0x00,0x00,0x20");
122
Duncan Sands6f749472006-01-13 10:52:38 +0100123#define INTERFACE_DATA 1
Duncan Sands48da7262005-05-11 20:20:40 +0200124#define ENDPOINT_INT 0x81
Duncan Sands80aae7a2006-01-13 10:59:23 +0100125#define ENDPOINT_BULK_DATA 0x07
126#define ENDPOINT_ISOC_DATA 0x07
Duncan Sands48da7262005-05-11 20:20:40 +0200127#define ENDPOINT_FIRMWARE 0x05
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Duncan Sands6a4f1b42006-10-05 10:40:55 +0200129struct speedtch_params {
130 unsigned int altsetting;
131 unsigned int BMaxDSL;
132 unsigned char ModemMode;
133 unsigned char ModemOption[MODEM_OPTION_LENGTH];
134};
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136struct speedtch_instance_data {
Duncan Sands48da7262005-05-11 20:20:40 +0200137 struct usbatm_data *usbatm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Duncan Sands6a4f1b42006-10-05 10:40:55 +0200139 struct speedtch_params params; /* set in probe, constant afterwards */
Duncan Sands6f749472006-01-13 10:52:38 +0100140
Tejun Heo37c95bf2010-12-24 16:14:20 +0100141 struct timer_list status_check_timer;
142 struct work_struct status_check_work;
Duncan Sands48da7262005-05-11 20:20:40 +0200143
Duncan Sands1a7aad12005-06-23 09:37:56 +0200144 unsigned char last_status;
145
Duncan Sands48da7262005-05-11 20:20:40 +0200146 int poll_delay; /* milliseconds */
147
148 struct timer_list resubmit_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 struct urb *int_urb;
150 unsigned char int_data[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Duncan Sands6a4f1b42006-10-05 10:40:55 +0200152 unsigned char scratch_buffer[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153};
154
155/***************
156** firmware **
157***************/
158
Duncan Sands48da7262005-05-11 20:20:40 +0200159static void speedtch_set_swbuff(struct speedtch_instance_data *instance, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Duncan Sands48da7262005-05-11 20:20:40 +0200161 struct usbatm_data *usbatm = instance->usbatm;
162 struct usb_device *usb_dev = usbatm->usb_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 int ret;
164
Duncan Sands48da7262005-05-11 20:20:40 +0200165 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
166 0x32, 0x40, state ? 0x01 : 0x00, 0x00, NULL, 0, CTRL_TIMEOUT);
167 if (ret < 0)
168 usb_warn(usbatm,
169 "%sabling SW buffering: usb_control_msg returned %d\n",
170 state ? "En" : "Dis", ret);
171 else
Greg Kroah-Hartmand8995422012-05-01 21:33:25 -0700172 usb_dbg(usbatm, "speedtch_set_swbuff: %sbled SW buffering\n", state ? "En" : "Dis");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
175static void speedtch_test_sequence(struct speedtch_instance_data *instance)
176{
Duncan Sands48da7262005-05-11 20:20:40 +0200177 struct usbatm_data *usbatm = instance->usbatm;
178 struct usb_device *usb_dev = usbatm->usb_dev;
179 unsigned char *buf = instance->scratch_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 int ret;
181
182 /* URB 147 */
183 buf[0] = 0x1c;
184 buf[1] = 0x50;
Duncan Sands48da7262005-05-11 20:20:40 +0200185 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
186 0x01, 0x40, 0x0b, 0x00, buf, 2, CTRL_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (ret < 0)
Duncan Sands48da7262005-05-11 20:20:40 +0200188 usb_warn(usbatm, "%s failed on URB147: %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 /* URB 148 */
191 buf[0] = 0x32;
192 buf[1] = 0x00;
Duncan Sands48da7262005-05-11 20:20:40 +0200193 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
194 0x01, 0x40, 0x02, 0x00, buf, 2, CTRL_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (ret < 0)
Duncan Sands48da7262005-05-11 20:20:40 +0200196 usb_warn(usbatm, "%s failed on URB148: %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 /* URB 149 */
199 buf[0] = 0x01;
200 buf[1] = 0x00;
201 buf[2] = 0x01;
Duncan Sands48da7262005-05-11 20:20:40 +0200202 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
203 0x01, 0x40, 0x03, 0x00, buf, 3, CTRL_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 if (ret < 0)
Duncan Sands48da7262005-05-11 20:20:40 +0200205 usb_warn(usbatm, "%s failed on URB149: %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 /* URB 150 */
208 buf[0] = 0x01;
209 buf[1] = 0x00;
210 buf[2] = 0x01;
Duncan Sands48da7262005-05-11 20:20:40 +0200211 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
212 0x01, 0x40, 0x04, 0x00, buf, 3, CTRL_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (ret < 0)
Duncan Sands48da7262005-05-11 20:20:40 +0200214 usb_warn(usbatm, "%s failed on URB150: %d\n", __func__, ret);
Duncan Sands6a4f1b42006-10-05 10:40:55 +0200215
216 /* Extra initialisation in recent drivers - gives higher speeds */
217
218 /* URBext1 */
219 buf[0] = instance->params.ModemMode;
220 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
221 0x01, 0x40, 0x11, 0x00, buf, 1, CTRL_TIMEOUT);
222 if (ret < 0)
223 usb_warn(usbatm, "%s failed on URBext1: %d\n", __func__, ret);
224
225 /* URBext2 */
226 /* This seems to be the one which actually triggers the higher sync
227 rate -- it does require the new firmware too, although it works OK
228 with older firmware */
229 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
230 0x01, 0x40, 0x14, 0x00,
231 instance->params.ModemOption,
232 MODEM_OPTION_LENGTH, CTRL_TIMEOUT);
233 if (ret < 0)
234 usb_warn(usbatm, "%s failed on URBext2: %d\n", __func__, ret);
235
236 /* URBext3 */
237 buf[0] = instance->params.BMaxDSL & 0xff;
238 buf[1] = instance->params.BMaxDSL >> 8;
239 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
240 0x01, 0x40, 0x12, 0x00, buf, 2, CTRL_TIMEOUT);
241 if (ret < 0)
242 usb_warn(usbatm, "%s failed on URBext3: %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
Duncan Sands48da7262005-05-11 20:20:40 +0200245static int speedtch_upload_firmware(struct speedtch_instance_data *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 const struct firmware *fw1,
247 const struct firmware *fw2)
248{
249 unsigned char *buffer;
Duncan Sands48da7262005-05-11 20:20:40 +0200250 struct usbatm_data *usbatm = instance->usbatm;
Duncan Sands48da7262005-05-11 20:20:40 +0200251 struct usb_device *usb_dev = usbatm->usb_dev;
252 int actual_length;
253 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 int offset;
255
Duncan Sands48da7262005-05-11 20:20:40 +0200256 usb_dbg(usbatm, "%s entered\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Greg Kroah-Hartman3383ee42015-04-30 11:33:04 +0200258 buffer = (unsigned char *)__get_free_page(GFP_KERNEL);
259 if (!buffer) {
Duncan Sands48da7262005-05-11 20:20:40 +0200260 ret = -ENOMEM;
261 usb_dbg(usbatm, "%s: no memory for buffer!\n", __func__);
262 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264
Micah Gruber011db812007-09-05 13:49:58 +0800265 if (!usb_ifnum_to_if(usb_dev, 2)) {
Duncan Sands48da7262005-05-11 20:20:40 +0200266 ret = -ENODEV;
267 usb_dbg(usbatm, "%s: interface not found!\n", __func__);
268 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270
271 /* URB 7 */
272 if (dl_512_first) { /* some modems need a read before writing the firmware */
Duncan Sands48da7262005-05-11 20:20:40 +0200273 ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 buffer, 0x200, &actual_length, 2000);
275
276 if (ret < 0 && ret != -ETIMEDOUT)
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100277 usb_warn(usbatm, "%s: read BLOCK0 from modem failed (%d)!\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 else
Duncan Sands48da7262005-05-11 20:20:40 +0200279 usb_dbg(usbatm, "%s: BLOCK0 downloaded (%d bytes)\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
281
282 /* URB 8 : both leds are static green */
283 for (offset = 0; offset < fw1->size; offset += PAGE_SIZE) {
284 int thislen = min_t(int, PAGE_SIZE, fw1->size - offset);
285 memcpy(buffer, fw1->data + offset, thislen);
286
Duncan Sands48da7262005-05-11 20:20:40 +0200287 ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 buffer, thislen, &actual_length, DATA_TIMEOUT);
289
290 if (ret < 0) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100291 usb_err(usbatm, "%s: write BLOCK1 to modem failed (%d)!\n", __func__, ret);
Duncan Sands48da7262005-05-11 20:20:40 +0200292 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
Duncan Sands48da7262005-05-11 20:20:40 +0200294 usb_dbg(usbatm, "%s: BLOCK1 uploaded (%zu bytes)\n", __func__, fw1->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296
297 /* USB led blinking green, ADSL led off */
298
299 /* URB 11 */
Duncan Sands48da7262005-05-11 20:20:40 +0200300 ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 buffer, 0x200, &actual_length, DATA_TIMEOUT);
302
303 if (ret < 0) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100304 usb_err(usbatm, "%s: read BLOCK2 from modem failed (%d)!\n", __func__, ret);
Duncan Sands48da7262005-05-11 20:20:40 +0200305 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
Duncan Sands48da7262005-05-11 20:20:40 +0200307 usb_dbg(usbatm, "%s: BLOCK2 downloaded (%d bytes)\n", __func__, actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 /* URBs 12 to 139 - USB led blinking green, ADSL led off */
310 for (offset = 0; offset < fw2->size; offset += PAGE_SIZE) {
311 int thislen = min_t(int, PAGE_SIZE, fw2->size - offset);
312 memcpy(buffer, fw2->data + offset, thislen);
313
Duncan Sands48da7262005-05-11 20:20:40 +0200314 ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 buffer, thislen, &actual_length, DATA_TIMEOUT);
316
317 if (ret < 0) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100318 usb_err(usbatm, "%s: write BLOCK3 to modem failed (%d)!\n", __func__, ret);
Duncan Sands48da7262005-05-11 20:20:40 +0200319 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321 }
Duncan Sands48da7262005-05-11 20:20:40 +0200322 usb_dbg(usbatm, "%s: BLOCK3 uploaded (%zu bytes)\n", __func__, fw2->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 /* USB led static green, ADSL led static red */
325
326 /* URB 142 */
Duncan Sands48da7262005-05-11 20:20:40 +0200327 ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 buffer, 0x200, &actual_length, DATA_TIMEOUT);
329
330 if (ret < 0) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100331 usb_err(usbatm, "%s: read BLOCK4 from modem failed (%d)!\n", __func__, ret);
Duncan Sands48da7262005-05-11 20:20:40 +0200332 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
334
335 /* success */
Duncan Sands48da7262005-05-11 20:20:40 +0200336 usb_dbg(usbatm, "%s: BLOCK4 downloaded (%d bytes)\n", __func__, actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 /* Delay to allow firmware to start up. We can do this here
339 because we're in our own kernel thread anyway. */
Duncan Sands48da7262005-05-11 20:20:40 +0200340 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Duncan Sands6a4f1b42006-10-05 10:40:55 +0200342 if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, instance->params.altsetting)) < 0) {
343 usb_err(usbatm, "%s: setting interface to %d failed (%d)!\n", __func__, instance->params.altsetting, ret);
Duncan Sands6f749472006-01-13 10:52:38 +0100344 goto out_free;
345 }
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 /* Enable software buffering, if requested */
348 if (sw_buffering)
349 speedtch_set_swbuff(instance, 1);
350
351 /* Magic spell; don't ask us what this does */
352 speedtch_test_sequence(instance);
353
Duncan Sands48da7262005-05-11 20:20:40 +0200354 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Duncan Sands48da7262005-05-11 20:20:40 +0200356out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 free_page((unsigned long)buffer);
Duncan Sands48da7262005-05-11 20:20:40 +0200358out:
359 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100362static int speedtch_find_firmware(struct usbatm_data *usbatm, struct usb_interface *intf,
363 int phase, const struct firmware **fw_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Duncan Sands48da7262005-05-11 20:20:40 +0200365 struct device *dev = &intf->dev;
366 const u16 bcdDevice = le16_to_cpu(interface_to_usbdev(intf)->descriptor.bcdDevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 const u8 major_revision = bcdDevice >> 8;
368 const u8 minor_revision = bcdDevice & 0xff;
Duncan Sands48da7262005-05-11 20:20:40 +0200369 char buf[24];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 sprintf(buf, "speedtch-%d.bin.%x.%02x", phase, major_revision, minor_revision);
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100372 usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Duncan Sands48da7262005-05-11 20:20:40 +0200374 if (request_firmware(fw_p, buf, dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 sprintf(buf, "speedtch-%d.bin.%x", phase, major_revision);
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100376 usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Duncan Sands48da7262005-05-11 20:20:40 +0200378 if (request_firmware(fw_p, buf, dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 sprintf(buf, "speedtch-%d.bin", phase);
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100380 usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Duncan Sands48da7262005-05-11 20:20:40 +0200382 if (request_firmware(fw_p, buf, dev)) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100383 usb_err(usbatm, "%s: no stage %d firmware found!\n", __func__, phase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return -ENOENT;
385 }
386 }
387 }
388
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100389 usb_info(usbatm, "found stage %d firmware %s\n", phase, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 return 0;
392}
393
Duncan Sands48da7262005-05-11 20:20:40 +0200394static int speedtch_heavy_init(struct usbatm_data *usbatm, struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
396 const struct firmware *fw1, *fw2;
Duncan Sands48da7262005-05-11 20:20:40 +0200397 struct speedtch_instance_data *instance = usbatm->driver_data;
398 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100400 if ((ret = speedtch_find_firmware(usbatm, intf, 1, &fw1)) < 0)
401 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100403 if ((ret = speedtch_find_firmware(usbatm, intf, 2, &fw2)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 release_firmware(fw1);
Duncan Sands48da7262005-05-11 20:20:40 +0200405 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
407
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100408 if ((ret = speedtch_upload_firmware(instance, fw1, fw2)) < 0)
409 usb_err(usbatm, "%s: firmware upload failed (%d)!\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Duncan Sands48da7262005-05-11 20:20:40 +0200411 release_firmware(fw2);
412 release_firmware(fw1);
413
414 return ret;
415}
416
417
418/**********
419** ATM **
420**********/
421
422static int speedtch_read_status(struct speedtch_instance_data *instance)
423{
424 struct usbatm_data *usbatm = instance->usbatm;
425 struct usb_device *usb_dev = usbatm->usb_dev;
426 unsigned char *buf = instance->scratch_buffer;
427 int ret;
428
Duncan Sands6a4f1b42006-10-05 10:40:55 +0200429 memset(buf, 0, 16);
Duncan Sands48da7262005-05-11 20:20:40 +0200430
431 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
432 0x12, 0xc0, 0x07, 0x00, buf + OFFSET_7, SIZE_7,
433 CTRL_TIMEOUT);
434 if (ret < 0) {
435 atm_dbg(usbatm, "%s: MSG 7 failed\n", __func__);
436 return ret;
437 }
438
439 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
440 0x12, 0xc0, 0x0b, 0x00, buf + OFFSET_b, SIZE_b,
441 CTRL_TIMEOUT);
442 if (ret < 0) {
443 atm_dbg(usbatm, "%s: MSG B failed\n", __func__);
444 return ret;
445 }
446
447 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
448 0x12, 0xc0, 0x0d, 0x00, buf + OFFSET_d, SIZE_d,
449 CTRL_TIMEOUT);
450 if (ret < 0) {
451 atm_dbg(usbatm, "%s: MSG D failed\n", __func__);
452 return ret;
453 }
454
455 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
456 0x01, 0xc0, 0x0e, 0x00, buf + OFFSET_e, SIZE_e,
457 CTRL_TIMEOUT);
458 if (ret < 0) {
459 atm_dbg(usbatm, "%s: MSG E failed\n", __func__);
460 return ret;
461 }
462
463 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
464 0x01, 0xc0, 0x0f, 0x00, buf + OFFSET_f, SIZE_f,
465 CTRL_TIMEOUT);
466 if (ret < 0) {
467 atm_dbg(usbatm, "%s: MSG F failed\n", __func__);
468 return ret;
469 }
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return 0;
472}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Duncan Sands48da7262005-05-11 20:20:40 +0200474static int speedtch_start_synchro(struct speedtch_instance_data *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Duncan Sands48da7262005-05-11 20:20:40 +0200476 struct usbatm_data *usbatm = instance->usbatm;
477 struct usb_device *usb_dev = usbatm->usb_dev;
478 unsigned char *buf = instance->scratch_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Duncan Sands48da7262005-05-11 20:20:40 +0200481 atm_dbg(usbatm, "%s entered\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Duncan Sands48da7262005-05-11 20:20:40 +0200483 memset(buf, 0, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Duncan Sands48da7262005-05-11 20:20:40 +0200485 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
486 0x12, 0xc0, 0x04, 0x00,
487 buf, 2, CTRL_TIMEOUT);
488
489 if (ret < 0)
490 atm_warn(usbatm, "failed to start ADSL synchronisation: %d\n", ret);
491 else
492 atm_dbg(usbatm, "%s: modem prodded. %d bytes returned: %02x %02x\n",
493 __func__, ret, buf[0], buf[1]);
494
495 return ret;
496}
497
David Howellsc4028952006-11-22 14:57:56 +0000498static void speedtch_check_status(struct work_struct *work)
Duncan Sands48da7262005-05-11 20:20:40 +0200499{
David Howellsc4028952006-11-22 14:57:56 +0000500 struct speedtch_instance_data *instance =
501 container_of(work, struct speedtch_instance_data,
Tejun Heo37c95bf2010-12-24 16:14:20 +0100502 status_check_work);
Duncan Sands48da7262005-05-11 20:20:40 +0200503 struct usbatm_data *usbatm = instance->usbatm;
504 struct atm_dev *atm_dev = usbatm->atm_dev;
505 unsigned char *buf = instance->scratch_buffer;
Duncan Sands1a7aad12005-06-23 09:37:56 +0200506 int down_speed, up_speed, ret;
507 unsigned char status;
Duncan Sands48da7262005-05-11 20:20:40 +0200508
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100509#ifdef VERBOSE_DEBUG
Duncan Sands48da7262005-05-11 20:20:40 +0200510 atm_dbg(usbatm, "%s entered\n", __func__);
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100511#endif
Duncan Sands48da7262005-05-11 20:20:40 +0200512
513 ret = speedtch_read_status(instance);
514 if (ret < 0) {
515 atm_warn(usbatm, "error %d fetching device status\n", ret);
Duncan Sandscd5c08f2005-06-23 09:23:10 +0200516 instance->poll_delay = min(2 * instance->poll_delay, MAX_POLL_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return;
518 }
519
Duncan Sandscd5c08f2005-06-23 09:23:10 +0200520 instance->poll_delay = max(instance->poll_delay / 2, MIN_POLL_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Duncan Sands1a7aad12005-06-23 09:37:56 +0200522 status = buf[OFFSET_7];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Duncan Sands1a7aad12005-06-23 09:37:56 +0200524 if ((status != instance->last_status) || !status) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100525 atm_dbg(usbatm, "%s: line state 0x%02x\n", __func__, status);
526
Duncan Sands1a7aad12005-06-23 09:37:56 +0200527 switch (status) {
528 case 0:
Karl Hiramoto23f89f02010-07-08 20:55:37 +0000529 atm_dev_signal_change(atm_dev, ATM_PHY_SIG_LOST);
Duncan Sands1a7aad12005-06-23 09:37:56 +0200530 if (instance->last_status)
David S. Miller52fbae22005-07-25 19:54:35 -0700531 atm_info(usbatm, "ADSL line is down\n");
Duncan Sands1a7aad12005-06-23 09:37:56 +0200532 /* It may never resync again unless we ask it to... */
Duncan Sands48da7262005-05-11 20:20:40 +0200533 ret = speedtch_start_synchro(instance);
Duncan Sands1a7aad12005-06-23 09:37:56 +0200534 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Duncan Sands1a7aad12005-06-23 09:37:56 +0200536 case 0x08:
Karl Hiramoto23f89f02010-07-08 20:55:37 +0000537 atm_dev_signal_change(atm_dev, ATM_PHY_SIG_UNKNOWN);
David S. Miller52fbae22005-07-25 19:54:35 -0700538 atm_info(usbatm, "ADSL line is blocked?\n");
Duncan Sands1a7aad12005-06-23 09:37:56 +0200539 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Duncan Sands1a7aad12005-06-23 09:37:56 +0200541 case 0x10:
Karl Hiramoto23f89f02010-07-08 20:55:37 +0000542 atm_dev_signal_change(atm_dev, ATM_PHY_SIG_LOST);
David S. Miller52fbae22005-07-25 19:54:35 -0700543 atm_info(usbatm, "ADSL line is synchronising\n");
Duncan Sands1a7aad12005-06-23 09:37:56 +0200544 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Duncan Sands1a7aad12005-06-23 09:37:56 +0200546 case 0x20:
547 down_speed = buf[OFFSET_b] | (buf[OFFSET_b + 1] << 8)
Duncan Sands48da7262005-05-11 20:20:40 +0200548 | (buf[OFFSET_b + 2] << 16) | (buf[OFFSET_b + 3] << 24);
Duncan Sands1a7aad12005-06-23 09:37:56 +0200549 up_speed = buf[OFFSET_b + 4] | (buf[OFFSET_b + 5] << 8)
Duncan Sands48da7262005-05-11 20:20:40 +0200550 | (buf[OFFSET_b + 6] << 16) | (buf[OFFSET_b + 7] << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Duncan Sands48da7262005-05-11 20:20:40 +0200552 if (!(down_speed & 0x0000ffff) && !(up_speed & 0x0000ffff)) {
553 down_speed >>= 16;
554 up_speed >>= 16;
555 }
556
557 atm_dev->link_rate = down_speed * 1000 / 424;
Karl Hiramoto23f89f02010-07-08 20:55:37 +0000558 atm_dev_signal_change(atm_dev, ATM_PHY_SIG_FOUND);
Duncan Sands48da7262005-05-11 20:20:40 +0200559
560 atm_info(usbatm,
Duncan Sands322a95b2005-06-23 09:20:50 +0200561 "ADSL line is up (%d kb/s down | %d kb/s up)\n",
Duncan Sands48da7262005-05-11 20:20:40 +0200562 down_speed, up_speed);
Duncan Sands1a7aad12005-06-23 09:37:56 +0200563 break;
Duncan Sands48da7262005-05-11 20:20:40 +0200564
Duncan Sands1a7aad12005-06-23 09:37:56 +0200565 default:
Karl Hiramoto23f89f02010-07-08 20:55:37 +0000566 atm_dev_signal_change(atm_dev, ATM_PHY_SIG_UNKNOWN);
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100567 atm_info(usbatm, "unknown line state %02x\n", status);
Duncan Sands1a7aad12005-06-23 09:37:56 +0200568 break;
Duncan Sands48da7262005-05-11 20:20:40 +0200569 }
Duncan Sands1a7aad12005-06-23 09:37:56 +0200570
571 instance->last_status = status;
Duncan Sands48da7262005-05-11 20:20:40 +0200572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
Duncan Sands48da7262005-05-11 20:20:40 +0200575static void speedtch_status_poll(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Duncan Sands48da7262005-05-11 20:20:40 +0200577 struct speedtch_instance_data *instance = (void *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Tejun Heo37c95bf2010-12-24 16:14:20 +0100579 schedule_work(&instance->status_check_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Duncan Sands48da7262005-05-11 20:20:40 +0200581 /* The following check is racy, but the race is harmless */
582 if (instance->poll_delay < MAX_POLL_DELAY)
Tejun Heo37c95bf2010-12-24 16:14:20 +0100583 mod_timer(&instance->status_check_timer, jiffies + msecs_to_jiffies(instance->poll_delay));
Duncan Sands48da7262005-05-11 20:20:40 +0200584 else
David S. Miller52fbae22005-07-25 19:54:35 -0700585 atm_warn(instance->usbatm, "Too many failures - disabling line status polling\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
Duncan Sands48da7262005-05-11 20:20:40 +0200588static void speedtch_resubmit_int(unsigned long data)
589{
590 struct speedtch_instance_data *instance = (void *)data;
591 struct urb *int_urb = instance->int_urb;
592 int ret;
593
594 atm_dbg(instance->usbatm, "%s entered\n", __func__);
595
596 if (int_urb) {
597 ret = usb_submit_urb(int_urb, GFP_ATOMIC);
598 if (!ret)
Tejun Heo37c95bf2010-12-24 16:14:20 +0100599 schedule_work(&instance->status_check_work);
Duncan Sands48da7262005-05-11 20:20:40 +0200600 else {
601 atm_dbg(instance->usbatm, "%s: usb_submit_urb failed with result %d\n", __func__, ret);
602 mod_timer(&instance->resubmit_timer, jiffies + msecs_to_jiffies(RESUBMIT_DELAY));
603 }
604 }
605}
606
David Howells7d12e782006-10-05 14:55:46 +0100607static void speedtch_handle_int(struct urb *int_urb)
Duncan Sands48da7262005-05-11 20:20:40 +0200608{
609 struct speedtch_instance_data *instance = int_urb->context;
610 struct usbatm_data *usbatm = instance->usbatm;
611 unsigned int count = int_urb->actual_length;
Greg Kroah-Hartman9a5a3e92007-07-18 10:58:02 -0700612 int status = int_urb->status;
613 int ret;
Duncan Sands48da7262005-05-11 20:20:40 +0200614
615 /* The magic interrupt for "up state" */
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800616 static const unsigned char up_int[6] = { 0xa1, 0x00, 0x01, 0x00, 0x00, 0x00 };
Duncan Sands48da7262005-05-11 20:20:40 +0200617 /* The magic interrupt for "down state" */
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800618 static const unsigned char down_int[6] = { 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00 };
Duncan Sands48da7262005-05-11 20:20:40 +0200619
620 atm_dbg(usbatm, "%s entered\n", __func__);
621
Greg Kroah-Hartman9a5a3e92007-07-18 10:58:02 -0700622 if (status < 0) {
623 atm_dbg(usbatm, "%s: nonzero urb status %d!\n", __func__, status);
Duncan Sands48da7262005-05-11 20:20:40 +0200624 goto fail;
625 }
626
627 if ((count == 6) && !memcmp(up_int, instance->int_data, 6)) {
Tejun Heo37c95bf2010-12-24 16:14:20 +0100628 del_timer(&instance->status_check_timer);
David S. Miller52fbae22005-07-25 19:54:35 -0700629 atm_info(usbatm, "DSL line goes up\n");
Duncan Sands48da7262005-05-11 20:20:40 +0200630 } else if ((count == 6) && !memcmp(down_int, instance->int_data, 6)) {
David S. Miller52fbae22005-07-25 19:54:35 -0700631 atm_info(usbatm, "DSL line goes down\n");
Duncan Sands48da7262005-05-11 20:20:40 +0200632 } else {
633 int i;
634
635 atm_dbg(usbatm, "%s: unknown interrupt packet of length %d:", __func__, count);
636 for (i = 0; i < count; i++)
637 printk(" %02x", instance->int_data[i]);
638 printk("\n");
639 goto fail;
640 }
641
Greg Kroah-Hartman3383ee42015-04-30 11:33:04 +0200642 int_urb = instance->int_urb;
643 if (int_urb) {
Duncan Sands48da7262005-05-11 20:20:40 +0200644 ret = usb_submit_urb(int_urb, GFP_ATOMIC);
Tejun Heo37c95bf2010-12-24 16:14:20 +0100645 schedule_work(&instance->status_check_work);
Duncan Sands48da7262005-05-11 20:20:40 +0200646 if (ret < 0) {
647 atm_dbg(usbatm, "%s: usb_submit_urb failed with result %d\n", __func__, ret);
648 goto fail;
649 }
650 }
651
652 return;
653
654fail:
Greg Kroah-Hartman3383ee42015-04-30 11:33:04 +0200655 int_urb = instance->int_urb;
656 if (int_urb)
Duncan Sands48da7262005-05-11 20:20:40 +0200657 mod_timer(&instance->resubmit_timer, jiffies + msecs_to_jiffies(RESUBMIT_DELAY));
658}
659
660static int speedtch_atm_start(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
661{
662 struct usb_device *usb_dev = usbatm->usb_dev;
663 struct speedtch_instance_data *instance = usbatm->driver_data;
664 int i, ret;
665 unsigned char mac_str[13];
666
667 atm_dbg(usbatm, "%s entered\n", __func__);
668
Duncan Sands48da7262005-05-11 20:20:40 +0200669 /* Set MAC address, it is stored in the serial number */
670 memset(atm_dev->esi, 0, sizeof(atm_dev->esi));
671 if (usb_string(usb_dev, usb_dev->descriptor.iSerialNumber, mac_str, sizeof(mac_str)) == 12) {
672 for (i = 0; i < 6; i++)
Andy Shevchenko96b89f32010-05-24 14:33:25 -0700673 atm_dev->esi[i] = (hex_to_bin(mac_str[i * 2]) << 4) +
674 hex_to_bin(mac_str[i * 2 + 1]);
Duncan Sands48da7262005-05-11 20:20:40 +0200675 }
676
677 /* Start modem synchronisation */
678 ret = speedtch_start_synchro(instance);
679
680 /* Set up interrupt endpoint */
681 if (instance->int_urb) {
682 ret = usb_submit_urb(instance->int_urb, GFP_KERNEL);
683 if (ret < 0) {
684 /* Doesn't matter; we'll poll anyway */
685 atm_dbg(usbatm, "%s: submission of interrupt URB failed (%d)!\n", __func__, ret);
686 usb_free_urb(instance->int_urb);
687 instance->int_urb = NULL;
688 }
689 }
690
691 /* Start status polling */
Tejun Heo37c95bf2010-12-24 16:14:20 +0100692 mod_timer(&instance->status_check_timer, jiffies + msecs_to_jiffies(1000));
Duncan Sands48da7262005-05-11 20:20:40 +0200693
694 return 0;
695}
696
697static void speedtch_atm_stop(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
698{
699 struct speedtch_instance_data *instance = usbatm->driver_data;
700 struct urb *int_urb = instance->int_urb;
701
702 atm_dbg(usbatm, "%s entered\n", __func__);
703
Tejun Heo37c95bf2010-12-24 16:14:20 +0100704 del_timer_sync(&instance->status_check_timer);
Duncan Sands48da7262005-05-11 20:20:40 +0200705
706 /*
707 * Since resubmit_timer and int_urb can schedule themselves and
708 * each other, shutting them down correctly takes some care
709 */
710 instance->int_urb = NULL; /* signal shutdown */
711 mb();
712 usb_kill_urb(int_urb);
713 del_timer_sync(&instance->resubmit_timer);
714 /*
715 * At this point, speedtch_handle_int and speedtch_resubmit_int
716 * can run or be running, but instance->int_urb == NULL means that
717 * they will not reschedule
718 */
719 usb_kill_urb(int_urb);
720 del_timer_sync(&instance->resubmit_timer);
721 usb_free_urb(int_urb);
722
Tejun Heo43829732012-08-20 14:51:24 -0700723 flush_work(&instance->status_check_work);
Duncan Sands48da7262005-05-11 20:20:40 +0200724}
725
Alan Stern8fc7aea2008-10-20 09:52:12 -0400726static int speedtch_pre_reset(struct usb_interface *intf)
727{
728 return 0;
729}
730
731static int speedtch_post_reset(struct usb_interface *intf)
732{
733 return 0;
734}
735
Duncan Sands48da7262005-05-11 20:20:40 +0200736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737/**********
738** USB **
739**********/
740
Duncan Sands48da7262005-05-11 20:20:40 +0200741static struct usb_device_id speedtch_usb_ids[] = {
742 {USB_DEVICE(0x06b9, 0x4061)},
743 {}
744};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Duncan Sands48da7262005-05-11 20:20:40 +0200746MODULE_DEVICE_TABLE(usb, speedtch_usb_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Duncan Sands48da7262005-05-11 20:20:40 +0200748static int speedtch_usb_probe(struct usb_interface *, const struct usb_device_id *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Duncan Sands48da7262005-05-11 20:20:40 +0200750static struct usb_driver speedtch_usb_driver = {
Duncan Sands48da7262005-05-11 20:20:40 +0200751 .name = speedtch_driver_name,
752 .probe = speedtch_usb_probe,
753 .disconnect = usbatm_usb_disconnect,
Alan Stern8fc7aea2008-10-20 09:52:12 -0400754 .pre_reset = speedtch_pre_reset,
755 .post_reset = speedtch_post_reset,
Duncan Sands48da7262005-05-11 20:20:40 +0200756 .id_table = speedtch_usb_ids
757};
758
Nicolas Kaiser6c4b7f72010-06-09 20:22:03 +0200759static void speedtch_release_interfaces(struct usb_device *usb_dev,
760 int num_interfaces)
761{
Duncan Sands48da7262005-05-11 20:20:40 +0200762 struct usb_interface *cur_intf;
763 int i;
764
Greg Kroah-Hartman3383ee42015-04-30 11:33:04 +0200765 for (i = 0; i < num_interfaces; i++) {
766 cur_intf = usb_ifnum_to_if(usb_dev, i);
767 if (cur_intf) {
Duncan Sands48da7262005-05-11 20:20:40 +0200768 usb_set_intfdata(cur_intf, NULL);
769 usb_driver_release_interface(&speedtch_usb_driver, cur_intf);
770 }
Greg Kroah-Hartman3383ee42015-04-30 11:33:04 +0200771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
Duncan Sands48da7262005-05-11 20:20:40 +0200774static int speedtch_bind(struct usbatm_data *usbatm,
775 struct usb_interface *intf,
Duncan Sands35644b02006-01-17 11:16:13 +0100776 const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Duncan Sands48da7262005-05-11 20:20:40 +0200778 struct usb_device *usb_dev = interface_to_usbdev(intf);
Duncan Sands80aae7a2006-01-13 10:59:23 +0100779 struct usb_interface *cur_intf, *data_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 struct speedtch_instance_data *instance;
Duncan Sands48da7262005-05-11 20:20:40 +0200781 int ifnum = intf->altsetting->desc.bInterfaceNumber;
782 int num_interfaces = usb_dev->actconfig->desc.bNumInterfaces;
783 int i, ret;
Duncan Sands80aae7a2006-01-13 10:59:23 +0100784 int use_isoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Duncan Sands48da7262005-05-11 20:20:40 +0200786 usb_dbg(usbatm, "%s entered\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100788 /* sanity checks */
789
Duncan Sands48da7262005-05-11 20:20:40 +0200790 if (usb_dev->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100791 usb_err(usbatm, "%s: wrong device class %d\n", __func__, usb_dev->descriptor.bDeviceClass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return -ENODEV;
Duncan Sands48da7262005-05-11 20:20:40 +0200793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Greg Kroah-Hartman3383ee42015-04-30 11:33:04 +0200795 data_intf = usb_ifnum_to_if(usb_dev, INTERFACE_DATA);
796 if (!data_intf) {
Duncan Sands80aae7a2006-01-13 10:59:23 +0100797 usb_err(usbatm, "%s: data interface not found!\n", __func__);
798 return -ENODEV;
799 }
800
Duncan Sands48da7262005-05-11 20:20:40 +0200801 /* claim all interfaces */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Nicolas Kaiser9196cc72010-06-09 20:43:03 +0200803 for (i = 0; i < num_interfaces; i++) {
Duncan Sands48da7262005-05-11 20:20:40 +0200804 cur_intf = usb_ifnum_to_if(usb_dev, i);
805
806 if ((i != ifnum) && cur_intf) {
807 ret = usb_driver_claim_interface(&speedtch_usb_driver, cur_intf, usbatm);
808
809 if (ret < 0) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100810 usb_err(usbatm, "%s: failed to claim interface %2d (%d)!\n", __func__, i, ret);
Duncan Sands48da7262005-05-11 20:20:40 +0200811 speedtch_release_interfaces(usb_dev, i);
812 return ret;
813 }
814 }
815 }
816
Duncan Sands9a734ef2006-01-13 09:38:22 +0100817 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
Duncan Sands48da7262005-05-11 20:20:40 +0200818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (!instance) {
Duncan Sands48da7262005-05-11 20:20:40 +0200820 ret = -ENOMEM;
821 goto fail_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823
Duncan Sands48da7262005-05-11 20:20:40 +0200824 instance->usbatm = usbatm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Duncan Sands6a4f1b42006-10-05 10:40:55 +0200826 /* module parameters may change at any moment, so take a snapshot */
827 instance->params.altsetting = altsetting;
828 instance->params.BMaxDSL = BMaxDSL;
829 instance->params.ModemMode = ModemMode;
830 memcpy(instance->params.ModemOption, DEFAULT_MODEM_OPTION, MODEM_OPTION_LENGTH);
831 memcpy(instance->params.ModemOption, ModemOption, num_ModemOption);
Duncan Sands80aae7a2006-01-13 10:59:23 +0100832 use_isoc = enable_isoc;
Duncan Sands6f749472006-01-13 10:52:38 +0100833
Duncan Sands6a4f1b42006-10-05 10:40:55 +0200834 if (instance->params.altsetting)
835 if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, instance->params.altsetting)) < 0) {
836 usb_err(usbatm, "%s: setting interface to %2d failed (%d)!\n", __func__, instance->params.altsetting, ret);
837 instance->params.altsetting = 0; /* fall back to default */
Duncan Sands6f749472006-01-13 10:52:38 +0100838 }
839
Duncan Sands6a4f1b42006-10-05 10:40:55 +0200840 if (!instance->params.altsetting && use_isoc)
Duncan Sands80aae7a2006-01-13 10:59:23 +0100841 if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, DEFAULT_ISOC_ALTSETTING)) < 0) {
842 usb_dbg(usbatm, "%s: setting interface to %2d failed (%d)!\n", __func__, DEFAULT_ISOC_ALTSETTING, ret);
843 use_isoc = 0; /* fall back to bulk */
844 }
845
846 if (use_isoc) {
847 const struct usb_host_interface *desc = data_intf->cur_altsetting;
848 const __u8 target_address = USB_DIR_IN | usbatm->driver->isoc_in;
Duncan Sands80aae7a2006-01-13 10:59:23 +0100849
850 use_isoc = 0; /* fall back to bulk if endpoint not found */
851
Nicolas Kaiser9196cc72010-06-09 20:43:03 +0200852 for (i = 0; i < desc->desc.bNumEndpoints; i++) {
Duncan Sands80aae7a2006-01-13 10:59:23 +0100853 const struct usb_endpoint_descriptor *endpoint_desc = &desc->endpoint[i].desc;
854
855 if ((endpoint_desc->bEndpointAddress == target_address)) {
Luiz Fernando N. Capitulinoc5dd1f92006-10-26 13:02:58 -0300856 use_isoc =
857 usb_endpoint_xfer_isoc(endpoint_desc);
Duncan Sands80aae7a2006-01-13 10:59:23 +0100858 break;
859 }
860 }
861
862 if (!use_isoc)
863 usb_info(usbatm, "isochronous transfer not supported - using bulk\n");
864 }
865
Duncan Sands6a4f1b42006-10-05 10:40:55 +0200866 if (!use_isoc && !instance->params.altsetting)
Duncan Sands80aae7a2006-01-13 10:59:23 +0100867 if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, DEFAULT_BULK_ALTSETTING)) < 0) {
868 usb_err(usbatm, "%s: setting interface to %2d failed (%d)!\n", __func__, DEFAULT_BULK_ALTSETTING, ret);
Duncan Sands6f749472006-01-13 10:52:38 +0100869 goto fail_free;
870 }
Duncan Sands80aae7a2006-01-13 10:59:23 +0100871
Duncan Sands6a4f1b42006-10-05 10:40:55 +0200872 if (!instance->params.altsetting)
873 instance->params.altsetting = use_isoc ? DEFAULT_ISOC_ALTSETTING : DEFAULT_BULK_ALTSETTING;
Duncan Sands80aae7a2006-01-13 10:59:23 +0100874
875 usbatm->flags |= (use_isoc ? UDSL_USE_ISOC : 0);
Duncan Sands6f749472006-01-13 10:52:38 +0100876
Tejun Heo37c95bf2010-12-24 16:14:20 +0100877 INIT_WORK(&instance->status_check_work, speedtch_check_status);
878 init_timer(&instance->status_check_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Tejun Heo37c95bf2010-12-24 16:14:20 +0100880 instance->status_check_timer.function = speedtch_status_poll;
881 instance->status_check_timer.data = (unsigned long)instance;
Duncan Sands1a7aad12005-06-23 09:37:56 +0200882 instance->last_status = 0xff;
Duncan Sands48da7262005-05-11 20:20:40 +0200883 instance->poll_delay = MIN_POLL_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Duncan Sands48da7262005-05-11 20:20:40 +0200885 init_timer(&instance->resubmit_timer);
886 instance->resubmit_timer.function = speedtch_resubmit_int;
887 instance->resubmit_timer.data = (unsigned long)instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Duncan Sands48da7262005-05-11 20:20:40 +0200889 instance->int_urb = usb_alloc_urb(0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Duncan Sands48da7262005-05-11 20:20:40 +0200891 if (instance->int_urb)
892 usb_fill_int_urb(instance->int_urb, usb_dev,
893 usb_rcvintpipe(usb_dev, ENDPOINT_INT),
894 instance->int_data, sizeof(instance->int_data),
Felipe Balbiafb8aae2013-07-01 11:23:23 +0300895 speedtch_handle_int, instance, 16);
Duncan Sands48da7262005-05-11 20:20:40 +0200896 else
897 usb_dbg(usbatm, "%s: no memory for interrupt urb!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Duncan Sands48da7262005-05-11 20:20:40 +0200899 /* check whether the modem already seems to be alive */
900 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
901 0x12, 0xc0, 0x07, 0x00,
902 instance->scratch_buffer + OFFSET_7, SIZE_7, 500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Duncan Sands80aae7a2006-01-13 10:59:23 +0100904 usbatm->flags |= (ret == SIZE_7 ? UDSL_SKIP_HEAVY_INIT : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Duncan Sands35644b02006-01-17 11:16:13 +0100906 usb_dbg(usbatm, "%s: firmware %s loaded\n", __func__, usbatm->flags & UDSL_SKIP_HEAVY_INIT ? "already" : "not");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Duncan Sands35644b02006-01-17 11:16:13 +0100908 if (!(usbatm->flags & UDSL_SKIP_HEAVY_INIT))
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100909 if ((ret = usb_reset_device(usb_dev)) < 0) {
910 usb_err(usbatm, "%s: device reset failed (%d)!\n", __func__, ret);
Duncan Sands48da7262005-05-11 20:20:40 +0200911 goto fail_free;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100912 }
Duncan Sands48da7262005-05-11 20:20:40 +0200913
914 usbatm->driver_data = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 return 0;
917
Duncan Sands48da7262005-05-11 20:20:40 +0200918fail_free:
919 usb_free_urb(instance->int_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 kfree(instance);
Duncan Sands48da7262005-05-11 20:20:40 +0200921fail_release:
922 speedtch_release_interfaces(usb_dev, num_interfaces);
923 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
Duncan Sands48da7262005-05-11 20:20:40 +0200926static void speedtch_unbind(struct usbatm_data *usbatm, struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Duncan Sands48da7262005-05-11 20:20:40 +0200928 struct usb_device *usb_dev = interface_to_usbdev(intf);
929 struct speedtch_instance_data *instance = usbatm->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Duncan Sands48da7262005-05-11 20:20:40 +0200931 usb_dbg(usbatm, "%s entered\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Duncan Sands48da7262005-05-11 20:20:40 +0200933 speedtch_release_interfaces(usb_dev, usb_dev->actconfig->desc.bNumInterfaces);
934 usb_free_urb(instance->int_urb);
935 kfree(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
Duncan Sands48da7262005-05-11 20:20:40 +0200938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939/***********
940** init **
941***********/
942
Duncan Sands48da7262005-05-11 20:20:40 +0200943static struct usbatm_driver speedtch_usbatm_driver = {
Duncan Sands48da7262005-05-11 20:20:40 +0200944 .driver_name = speedtch_driver_name,
945 .bind = speedtch_bind,
946 .heavy_init = speedtch_heavy_init,
947 .unbind = speedtch_unbind,
948 .atm_start = speedtch_atm_start,
949 .atm_stop = speedtch_atm_stop,
Duncan Sands80aae7a2006-01-13 10:59:23 +0100950 .bulk_in = ENDPOINT_BULK_DATA,
951 .bulk_out = ENDPOINT_BULK_DATA,
952 .isoc_in = ENDPOINT_ISOC_DATA
Duncan Sands48da7262005-05-11 20:20:40 +0200953};
954
955static int speedtch_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
956{
957 return usbatm_usb_probe(intf, id, &speedtch_usbatm_driver);
958}
959
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -0800960module_usb_driver(speedtch_usb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962MODULE_AUTHOR(DRIVER_AUTHOR);
963MODULE_DESCRIPTION(DRIVER_DESC);
964MODULE_LICENSE("GPL");
965MODULE_VERSION(DRIVER_VERSION);