blob: 3e862401a638e15141460190208270a03aa0a756 [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>
30#include <linux/gfp.h>
31#include <linux/init.h>
32#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/module.h>
34#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/slab.h>
Duncan Sands48da7262005-05-11 20:20:40 +020036#include <linux/stat.h>
37#include <linux/timer.h>
Duncan Sands80aae7a2006-01-13 10:59:23 +010038#include <linux/types.h>
David Brownell5f848132006-12-16 15:34:53 -080039#include <linux/usb/ch9.h>
Duncan Sands48da7262005-05-11 20:20:40 +020040#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Duncan Sands48da7262005-05-11 20:20:40 +020042#include "usbatm.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#define DRIVER_AUTHOR "Johan Verrept, Duncan Sands <duncan.sands@free.fr>"
Duncan Sands9b0e54a2006-01-13 11:08:05 +010045#define DRIVER_VERSION "1.10"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define DRIVER_DESC "Alcatel SpeedTouch USB driver version " DRIVER_VERSION
47
48static const char speedtch_driver_name[] = "speedtch";
49
Duncan Sands48da7262005-05-11 20:20:40 +020050#define CTRL_TIMEOUT 2000 /* milliseconds */
51#define DATA_TIMEOUT 2000 /* milliseconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Duncan Sands48da7262005-05-11 20:20:40 +020053#define OFFSET_7 0 /* size 1 */
54#define OFFSET_b 1 /* size 8 */
55#define OFFSET_d 9 /* size 4 */
56#define OFFSET_e 13 /* size 1 */
57#define OFFSET_f 14 /* size 1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Duncan Sands48da7262005-05-11 20:20:40 +020059#define SIZE_7 1
60#define SIZE_b 8
61#define SIZE_d 4
62#define SIZE_e 1
63#define SIZE_f 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Duncan Sands48da7262005-05-11 20:20:40 +020065#define MIN_POLL_DELAY 5000 /* milliseconds */
66#define MAX_POLL_DELAY 60000 /* milliseconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Duncan Sands48da7262005-05-11 20:20:40 +020068#define RESUBMIT_DELAY 1000 /* milliseconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Duncan Sands80aae7a2006-01-13 10:59:23 +010070#define DEFAULT_BULK_ALTSETTING 1
Duncan Sands67c752b2006-04-28 18:44:06 +020071#define DEFAULT_ISOC_ALTSETTING 3
Duncan Sands48da7262005-05-11 20:20:40 +020072#define DEFAULT_DL_512_FIRST 0
Duncan Sands80aae7a2006-01-13 10:59:23 +010073#define DEFAULT_ENABLE_ISOC 0
Duncan Sands48da7262005-05-11 20:20:40 +020074#define DEFAULT_SW_BUFFERING 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Duncan Sands80aae7a2006-01-13 10:59:23 +010076static unsigned int altsetting = 0; /* zero means: use the default */
Duncan Sands48da7262005-05-11 20:20:40 +020077static int dl_512_first = DEFAULT_DL_512_FIRST;
Duncan Sands80aae7a2006-01-13 10:59:23 +010078static int enable_isoc = DEFAULT_ENABLE_ISOC;
Duncan Sands48da7262005-05-11 20:20:40 +020079static int sw_buffering = DEFAULT_SW_BUFFERING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Duncan Sands6a4f1b42006-10-05 10:40:55 +020081#define DEFAULT_B_MAX_DSL 8128
82#define DEFAULT_MODEM_MODE 11
83#define MODEM_OPTION_LENGTH 16
84static const unsigned char DEFAULT_MODEM_OPTION[MODEM_OPTION_LENGTH] = {
85 0x10, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
86};
87
88static unsigned int BMaxDSL = DEFAULT_B_MAX_DSL;
89static unsigned char ModemMode = DEFAULT_MODEM_MODE;
90static unsigned char ModemOption[MODEM_OPTION_LENGTH];
Al Viro64a6f952007-10-14 19:35:30 +010091static unsigned int num_ModemOption;
Duncan Sands6a4f1b42006-10-05 10:40:55 +020092
Duncan Sands80aae7a2006-01-13 10:59:23 +010093module_param(altsetting, uint, S_IRUGO | S_IWUSR);
Duncan Sands48da7262005-05-11 20:20:40 +020094MODULE_PARM_DESC(altsetting,
Duncan Sands80aae7a2006-01-13 10:59:23 +010095 "Alternative setting for data interface (bulk_default: "
96 __MODULE_STRING(DEFAULT_BULK_ALTSETTING) "; isoc_default: "
97 __MODULE_STRING(DEFAULT_ISOC_ALTSETTING) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Duncan Sands48da7262005-05-11 20:20:40 +020099module_param(dl_512_first, bool, S_IRUGO | S_IWUSR);
100MODULE_PARM_DESC(dl_512_first,
101 "Read 512 bytes before sending firmware (default: "
102 __MODULE_STRING(DEFAULT_DL_512_FIRST) ")");
103
Duncan Sands80aae7a2006-01-13 10:59:23 +0100104module_param(enable_isoc, bool, S_IRUGO | S_IWUSR);
105MODULE_PARM_DESC(enable_isoc,
106 "Use isochronous transfers if available (default: "
107 __MODULE_STRING(DEFAULT_ENABLE_ISOC) ")");
108
Duncan Sands48da7262005-05-11 20:20:40 +0200109module_param(sw_buffering, bool, S_IRUGO | S_IWUSR);
110MODULE_PARM_DESC(sw_buffering,
111 "Enable software buffering (default: "
112 __MODULE_STRING(DEFAULT_SW_BUFFERING) ")");
113
Duncan Sands6a4f1b42006-10-05 10:40:55 +0200114module_param(BMaxDSL, uint, S_IRUGO | S_IWUSR);
115MODULE_PARM_DESC(BMaxDSL,
116 "default: " __MODULE_STRING(DEFAULT_B_MAX_DSL));
117
118module_param(ModemMode, byte, S_IRUGO | S_IWUSR);
119MODULE_PARM_DESC(ModemMode,
120 "default: " __MODULE_STRING(DEFAULT_MODEM_MODE));
121
122module_param_array(ModemOption, byte, &num_ModemOption, S_IRUGO);
123MODULE_PARM_DESC(ModemOption, "default: 0x10,0x00,0x00,0x00,0x20");
124
Duncan Sands6f749472006-01-13 10:52:38 +0100125#define INTERFACE_DATA 1
Duncan Sands48da7262005-05-11 20:20:40 +0200126#define ENDPOINT_INT 0x81
Duncan Sands80aae7a2006-01-13 10:59:23 +0100127#define ENDPOINT_BULK_DATA 0x07
128#define ENDPOINT_ISOC_DATA 0x07
Duncan Sands48da7262005-05-11 20:20:40 +0200129#define ENDPOINT_FIRMWARE 0x05
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131#define hex2int(c) ( (c >= '0') && (c <= '9') ? (c - '0') : ((c & 0xf) + 9) )
132
Duncan Sands6a4f1b42006-10-05 10:40:55 +0200133struct speedtch_params {
134 unsigned int altsetting;
135 unsigned int BMaxDSL;
136 unsigned char ModemMode;
137 unsigned char ModemOption[MODEM_OPTION_LENGTH];
138};
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140struct speedtch_instance_data {
Duncan Sands48da7262005-05-11 20:20:40 +0200141 struct usbatm_data *usbatm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Duncan Sands6a4f1b42006-10-05 10:40:55 +0200143 struct speedtch_params params; /* set in probe, constant afterwards */
Duncan Sands6f749472006-01-13 10:52:38 +0100144
David Howellsc4028952006-11-22 14:57:56 +0000145 struct delayed_work status_checker;
Duncan Sands48da7262005-05-11 20:20:40 +0200146
Duncan Sands1a7aad12005-06-23 09:37:56 +0200147 unsigned char last_status;
148
Duncan Sands48da7262005-05-11 20:20:40 +0200149 int poll_delay; /* milliseconds */
150
151 struct timer_list resubmit_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 struct urb *int_urb;
153 unsigned char int_data[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Duncan Sands6a4f1b42006-10-05 10:40:55 +0200155 unsigned char scratch_buffer[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156};
157
158/***************
159** firmware **
160***************/
161
Duncan Sands48da7262005-05-11 20:20:40 +0200162static void speedtch_set_swbuff(struct speedtch_instance_data *instance, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Duncan Sands48da7262005-05-11 20:20:40 +0200164 struct usbatm_data *usbatm = instance->usbatm;
165 struct usb_device *usb_dev = usbatm->usb_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 int ret;
167
Duncan Sands48da7262005-05-11 20:20:40 +0200168 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
169 0x32, 0x40, state ? 0x01 : 0x00, 0x00, NULL, 0, CTRL_TIMEOUT);
170 if (ret < 0)
171 usb_warn(usbatm,
172 "%sabling SW buffering: usb_control_msg returned %d\n",
173 state ? "En" : "Dis", ret);
174 else
175 dbg("speedtch_set_swbuff: %sbled SW buffering", state ? "En" : "Dis");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178static void speedtch_test_sequence(struct speedtch_instance_data *instance)
179{
Duncan Sands48da7262005-05-11 20:20:40 +0200180 struct usbatm_data *usbatm = instance->usbatm;
181 struct usb_device *usb_dev = usbatm->usb_dev;
182 unsigned char *buf = instance->scratch_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 int ret;
184
185 /* URB 147 */
186 buf[0] = 0x1c;
187 buf[1] = 0x50;
Duncan Sands48da7262005-05-11 20:20:40 +0200188 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
189 0x01, 0x40, 0x0b, 0x00, buf, 2, CTRL_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (ret < 0)
Duncan Sands48da7262005-05-11 20:20:40 +0200191 usb_warn(usbatm, "%s failed on URB147: %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 /* URB 148 */
194 buf[0] = 0x32;
195 buf[1] = 0x00;
Duncan Sands48da7262005-05-11 20:20:40 +0200196 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
197 0x01, 0x40, 0x02, 0x00, buf, 2, CTRL_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (ret < 0)
Duncan Sands48da7262005-05-11 20:20:40 +0200199 usb_warn(usbatm, "%s failed on URB148: %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 /* URB 149 */
202 buf[0] = 0x01;
203 buf[1] = 0x00;
204 buf[2] = 0x01;
Duncan Sands48da7262005-05-11 20:20:40 +0200205 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
206 0x01, 0x40, 0x03, 0x00, buf, 3, CTRL_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (ret < 0)
Duncan Sands48da7262005-05-11 20:20:40 +0200208 usb_warn(usbatm, "%s failed on URB149: %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 /* URB 150 */
211 buf[0] = 0x01;
212 buf[1] = 0x00;
213 buf[2] = 0x01;
Duncan Sands48da7262005-05-11 20:20:40 +0200214 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
215 0x01, 0x40, 0x04, 0x00, buf, 3, CTRL_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (ret < 0)
Duncan Sands48da7262005-05-11 20:20:40 +0200217 usb_warn(usbatm, "%s failed on URB150: %d\n", __func__, ret);
Duncan Sands6a4f1b42006-10-05 10:40:55 +0200218
219 /* Extra initialisation in recent drivers - gives higher speeds */
220
221 /* URBext1 */
222 buf[0] = instance->params.ModemMode;
223 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
224 0x01, 0x40, 0x11, 0x00, buf, 1, CTRL_TIMEOUT);
225 if (ret < 0)
226 usb_warn(usbatm, "%s failed on URBext1: %d\n", __func__, ret);
227
228 /* URBext2 */
229 /* This seems to be the one which actually triggers the higher sync
230 rate -- it does require the new firmware too, although it works OK
231 with older firmware */
232 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
233 0x01, 0x40, 0x14, 0x00,
234 instance->params.ModemOption,
235 MODEM_OPTION_LENGTH, CTRL_TIMEOUT);
236 if (ret < 0)
237 usb_warn(usbatm, "%s failed on URBext2: %d\n", __func__, ret);
238
239 /* URBext3 */
240 buf[0] = instance->params.BMaxDSL & 0xff;
241 buf[1] = instance->params.BMaxDSL >> 8;
242 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
243 0x01, 0x40, 0x12, 0x00, buf, 2, CTRL_TIMEOUT);
244 if (ret < 0)
245 usb_warn(usbatm, "%s failed on URBext3: %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
Duncan Sands48da7262005-05-11 20:20:40 +0200248static int speedtch_upload_firmware(struct speedtch_instance_data *instance,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 const struct firmware *fw1,
250 const struct firmware *fw2)
251{
252 unsigned char *buffer;
Duncan Sands48da7262005-05-11 20:20:40 +0200253 struct usbatm_data *usbatm = instance->usbatm;
Duncan Sands48da7262005-05-11 20:20:40 +0200254 struct usb_device *usb_dev = usbatm->usb_dev;
255 int actual_length;
256 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 int offset;
258
Duncan Sands48da7262005-05-11 20:20:40 +0200259 usb_dbg(usbatm, "%s entered\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 if (!(buffer = (unsigned char *)__get_free_page(GFP_KERNEL))) {
Duncan Sands48da7262005-05-11 20:20:40 +0200262 ret = -ENOMEM;
263 usb_dbg(usbatm, "%s: no memory for buffer!\n", __func__);
264 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266
Micah Gruber011db812007-09-05 13:49:58 +0800267 if (!usb_ifnum_to_if(usb_dev, 2)) {
Duncan Sands48da7262005-05-11 20:20:40 +0200268 ret = -ENODEV;
269 usb_dbg(usbatm, "%s: interface not found!\n", __func__);
270 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272
273 /* URB 7 */
274 if (dl_512_first) { /* some modems need a read before writing the firmware */
Duncan Sands48da7262005-05-11 20:20:40 +0200275 ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 buffer, 0x200, &actual_length, 2000);
277
278 if (ret < 0 && ret != -ETIMEDOUT)
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100279 usb_warn(usbatm, "%s: read BLOCK0 from modem failed (%d)!\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 else
Duncan Sands48da7262005-05-11 20:20:40 +0200281 usb_dbg(usbatm, "%s: BLOCK0 downloaded (%d bytes)\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283
284 /* URB 8 : both leds are static green */
285 for (offset = 0; offset < fw1->size; offset += PAGE_SIZE) {
286 int thislen = min_t(int, PAGE_SIZE, fw1->size - offset);
287 memcpy(buffer, fw1->data + offset, thislen);
288
Duncan Sands48da7262005-05-11 20:20:40 +0200289 ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 buffer, thislen, &actual_length, DATA_TIMEOUT);
291
292 if (ret < 0) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100293 usb_err(usbatm, "%s: write BLOCK1 to modem failed (%d)!\n", __func__, ret);
Duncan Sands48da7262005-05-11 20:20:40 +0200294 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
Duncan Sands48da7262005-05-11 20:20:40 +0200296 usb_dbg(usbatm, "%s: BLOCK1 uploaded (%zu bytes)\n", __func__, fw1->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298
299 /* USB led blinking green, ADSL led off */
300
301 /* URB 11 */
Duncan Sands48da7262005-05-11 20:20:40 +0200302 ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 buffer, 0x200, &actual_length, DATA_TIMEOUT);
304
305 if (ret < 0) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100306 usb_err(usbatm, "%s: read BLOCK2 from modem failed (%d)!\n", __func__, ret);
Duncan Sands48da7262005-05-11 20:20:40 +0200307 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
Duncan Sands48da7262005-05-11 20:20:40 +0200309 usb_dbg(usbatm, "%s: BLOCK2 downloaded (%d bytes)\n", __func__, actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 /* URBs 12 to 139 - USB led blinking green, ADSL led off */
312 for (offset = 0; offset < fw2->size; offset += PAGE_SIZE) {
313 int thislen = min_t(int, PAGE_SIZE, fw2->size - offset);
314 memcpy(buffer, fw2->data + offset, thislen);
315
Duncan Sands48da7262005-05-11 20:20:40 +0200316 ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 buffer, thislen, &actual_length, DATA_TIMEOUT);
318
319 if (ret < 0) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100320 usb_err(usbatm, "%s: write BLOCK3 to modem failed (%d)!\n", __func__, ret);
Duncan Sands48da7262005-05-11 20:20:40 +0200321 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
323 }
Duncan Sands48da7262005-05-11 20:20:40 +0200324 usb_dbg(usbatm, "%s: BLOCK3 uploaded (%zu bytes)\n", __func__, fw2->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 /* USB led static green, ADSL led static red */
327
328 /* URB 142 */
Duncan Sands48da7262005-05-11 20:20:40 +0200329 ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 buffer, 0x200, &actual_length, DATA_TIMEOUT);
331
332 if (ret < 0) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100333 usb_err(usbatm, "%s: read BLOCK4 from modem failed (%d)!\n", __func__, ret);
Duncan Sands48da7262005-05-11 20:20:40 +0200334 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336
337 /* success */
Duncan Sands48da7262005-05-11 20:20:40 +0200338 usb_dbg(usbatm, "%s: BLOCK4 downloaded (%d bytes)\n", __func__, actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 /* Delay to allow firmware to start up. We can do this here
341 because we're in our own kernel thread anyway. */
Duncan Sands48da7262005-05-11 20:20:40 +0200342 msleep_interruptible(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Duncan Sands6a4f1b42006-10-05 10:40:55 +0200344 if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, instance->params.altsetting)) < 0) {
345 usb_err(usbatm, "%s: setting interface to %d failed (%d)!\n", __func__, instance->params.altsetting, ret);
Duncan Sands6f749472006-01-13 10:52:38 +0100346 goto out_free;
347 }
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 /* Enable software buffering, if requested */
350 if (sw_buffering)
351 speedtch_set_swbuff(instance, 1);
352
353 /* Magic spell; don't ask us what this does */
354 speedtch_test_sequence(instance);
355
Duncan Sands48da7262005-05-11 20:20:40 +0200356 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Duncan Sands48da7262005-05-11 20:20:40 +0200358out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 free_page((unsigned long)buffer);
Duncan Sands48da7262005-05-11 20:20:40 +0200360out:
361 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100364static int speedtch_find_firmware(struct usbatm_data *usbatm, struct usb_interface *intf,
365 int phase, const struct firmware **fw_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Duncan Sands48da7262005-05-11 20:20:40 +0200367 struct device *dev = &intf->dev;
368 const u16 bcdDevice = le16_to_cpu(interface_to_usbdev(intf)->descriptor.bcdDevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 const u8 major_revision = bcdDevice >> 8;
370 const u8 minor_revision = bcdDevice & 0xff;
Duncan Sands48da7262005-05-11 20:20:40 +0200371 char buf[24];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 sprintf(buf, "speedtch-%d.bin.%x.%02x", phase, major_revision, minor_revision);
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100374 usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Duncan Sands48da7262005-05-11 20:20:40 +0200376 if (request_firmware(fw_p, buf, dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 sprintf(buf, "speedtch-%d.bin.%x", phase, major_revision);
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100378 usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Duncan Sands48da7262005-05-11 20:20:40 +0200380 if (request_firmware(fw_p, buf, dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 sprintf(buf, "speedtch-%d.bin", phase);
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100382 usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Duncan Sands48da7262005-05-11 20:20:40 +0200384 if (request_firmware(fw_p, buf, dev)) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100385 usb_err(usbatm, "%s: no stage %d firmware found!\n", __func__, phase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return -ENOENT;
387 }
388 }
389 }
390
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100391 usb_info(usbatm, "found stage %d firmware %s\n", phase, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 return 0;
394}
395
Duncan Sands48da7262005-05-11 20:20:40 +0200396static int speedtch_heavy_init(struct usbatm_data *usbatm, struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 const struct firmware *fw1, *fw2;
Duncan Sands48da7262005-05-11 20:20:40 +0200399 struct speedtch_instance_data *instance = usbatm->driver_data;
400 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100402 if ((ret = speedtch_find_firmware(usbatm, intf, 1, &fw1)) < 0)
403 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100405 if ((ret = speedtch_find_firmware(usbatm, intf, 2, &fw2)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 release_firmware(fw1);
Duncan Sands48da7262005-05-11 20:20:40 +0200407 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100410 if ((ret = speedtch_upload_firmware(instance, fw1, fw2)) < 0)
411 usb_err(usbatm, "%s: firmware upload failed (%d)!\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Duncan Sands48da7262005-05-11 20:20:40 +0200413 release_firmware(fw2);
414 release_firmware(fw1);
415
416 return ret;
417}
418
419
420/**********
421** ATM **
422**********/
423
424static int speedtch_read_status(struct speedtch_instance_data *instance)
425{
426 struct usbatm_data *usbatm = instance->usbatm;
427 struct usb_device *usb_dev = usbatm->usb_dev;
428 unsigned char *buf = instance->scratch_buffer;
429 int ret;
430
Duncan Sands6a4f1b42006-10-05 10:40:55 +0200431 memset(buf, 0, 16);
Duncan Sands48da7262005-05-11 20:20:40 +0200432
433 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
434 0x12, 0xc0, 0x07, 0x00, buf + OFFSET_7, SIZE_7,
435 CTRL_TIMEOUT);
436 if (ret < 0) {
437 atm_dbg(usbatm, "%s: MSG 7 failed\n", __func__);
438 return ret;
439 }
440
441 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
442 0x12, 0xc0, 0x0b, 0x00, buf + OFFSET_b, SIZE_b,
443 CTRL_TIMEOUT);
444 if (ret < 0) {
445 atm_dbg(usbatm, "%s: MSG B failed\n", __func__);
446 return ret;
447 }
448
449 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
450 0x12, 0xc0, 0x0d, 0x00, buf + OFFSET_d, SIZE_d,
451 CTRL_TIMEOUT);
452 if (ret < 0) {
453 atm_dbg(usbatm, "%s: MSG D failed\n", __func__);
454 return ret;
455 }
456
457 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
458 0x01, 0xc0, 0x0e, 0x00, buf + OFFSET_e, SIZE_e,
459 CTRL_TIMEOUT);
460 if (ret < 0) {
461 atm_dbg(usbatm, "%s: MSG E failed\n", __func__);
462 return ret;
463 }
464
465 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
466 0x01, 0xc0, 0x0f, 0x00, buf + OFFSET_f, SIZE_f,
467 CTRL_TIMEOUT);
468 if (ret < 0) {
469 atm_dbg(usbatm, "%s: MSG F failed\n", __func__);
470 return ret;
471 }
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return 0;
474}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Duncan Sands48da7262005-05-11 20:20:40 +0200476static int speedtch_start_synchro(struct speedtch_instance_data *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Duncan Sands48da7262005-05-11 20:20:40 +0200478 struct usbatm_data *usbatm = instance->usbatm;
479 struct usb_device *usb_dev = usbatm->usb_dev;
480 unsigned char *buf = instance->scratch_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Duncan Sands48da7262005-05-11 20:20:40 +0200483 atm_dbg(usbatm, "%s entered\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Duncan Sands48da7262005-05-11 20:20:40 +0200485 memset(buf, 0, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Duncan Sands48da7262005-05-11 20:20:40 +0200487 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
488 0x12, 0xc0, 0x04, 0x00,
489 buf, 2, CTRL_TIMEOUT);
490
491 if (ret < 0)
492 atm_warn(usbatm, "failed to start ADSL synchronisation: %d\n", ret);
493 else
494 atm_dbg(usbatm, "%s: modem prodded. %d bytes returned: %02x %02x\n",
495 __func__, ret, buf[0], buf[1]);
496
497 return ret;
498}
499
David Howellsc4028952006-11-22 14:57:56 +0000500static void speedtch_check_status(struct work_struct *work)
Duncan Sands48da7262005-05-11 20:20:40 +0200501{
David Howellsc4028952006-11-22 14:57:56 +0000502 struct speedtch_instance_data *instance =
503 container_of(work, struct speedtch_instance_data,
504 status_checker.work);
Duncan Sands48da7262005-05-11 20:20:40 +0200505 struct usbatm_data *usbatm = instance->usbatm;
506 struct atm_dev *atm_dev = usbatm->atm_dev;
507 unsigned char *buf = instance->scratch_buffer;
Duncan Sands1a7aad12005-06-23 09:37:56 +0200508 int down_speed, up_speed, ret;
509 unsigned char status;
Duncan Sands48da7262005-05-11 20:20:40 +0200510
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100511#ifdef VERBOSE_DEBUG
Duncan Sands48da7262005-05-11 20:20:40 +0200512 atm_dbg(usbatm, "%s entered\n", __func__);
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100513#endif
Duncan Sands48da7262005-05-11 20:20:40 +0200514
515 ret = speedtch_read_status(instance);
516 if (ret < 0) {
517 atm_warn(usbatm, "error %d fetching device status\n", ret);
Duncan Sandscd5c08f2005-06-23 09:23:10 +0200518 instance->poll_delay = min(2 * instance->poll_delay, MAX_POLL_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return;
520 }
521
Duncan Sandscd5c08f2005-06-23 09:23:10 +0200522 instance->poll_delay = max(instance->poll_delay / 2, MIN_POLL_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Duncan Sands1a7aad12005-06-23 09:37:56 +0200524 status = buf[OFFSET_7];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Duncan Sands1a7aad12005-06-23 09:37:56 +0200526 if ((status != instance->last_status) || !status) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100527 atm_dbg(usbatm, "%s: line state 0x%02x\n", __func__, status);
528
Duncan Sands1a7aad12005-06-23 09:37:56 +0200529 switch (status) {
530 case 0:
Duncan Sands48da7262005-05-11 20:20:40 +0200531 atm_dev->signal = ATM_PHY_SIG_LOST;
Duncan Sands1a7aad12005-06-23 09:37:56 +0200532 if (instance->last_status)
David S. Miller52fbae22005-07-25 19:54:35 -0700533 atm_info(usbatm, "ADSL line is down\n");
Duncan Sands1a7aad12005-06-23 09:37:56 +0200534 /* It may never resync again unless we ask it to... */
Duncan Sands48da7262005-05-11 20:20:40 +0200535 ret = speedtch_start_synchro(instance);
Duncan Sands1a7aad12005-06-23 09:37:56 +0200536 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Duncan Sands1a7aad12005-06-23 09:37:56 +0200538 case 0x08:
Duncan Sands48da7262005-05-11 20:20:40 +0200539 atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
David S. Miller52fbae22005-07-25 19:54:35 -0700540 atm_info(usbatm, "ADSL line is blocked?\n");
Duncan Sands1a7aad12005-06-23 09:37:56 +0200541 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Duncan Sands1a7aad12005-06-23 09:37:56 +0200543 case 0x10:
Duncan Sands48da7262005-05-11 20:20:40 +0200544 atm_dev->signal = ATM_PHY_SIG_LOST;
David S. Miller52fbae22005-07-25 19:54:35 -0700545 atm_info(usbatm, "ADSL line is synchronising\n");
Duncan Sands1a7aad12005-06-23 09:37:56 +0200546 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Duncan Sands1a7aad12005-06-23 09:37:56 +0200548 case 0x20:
549 down_speed = buf[OFFSET_b] | (buf[OFFSET_b + 1] << 8)
Duncan Sands48da7262005-05-11 20:20:40 +0200550 | (buf[OFFSET_b + 2] << 16) | (buf[OFFSET_b + 3] << 24);
Duncan Sands1a7aad12005-06-23 09:37:56 +0200551 up_speed = buf[OFFSET_b + 4] | (buf[OFFSET_b + 5] << 8)
Duncan Sands48da7262005-05-11 20:20:40 +0200552 | (buf[OFFSET_b + 6] << 16) | (buf[OFFSET_b + 7] << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Duncan Sands48da7262005-05-11 20:20:40 +0200554 if (!(down_speed & 0x0000ffff) && !(up_speed & 0x0000ffff)) {
555 down_speed >>= 16;
556 up_speed >>= 16;
557 }
558
559 atm_dev->link_rate = down_speed * 1000 / 424;
560 atm_dev->signal = ATM_PHY_SIG_FOUND;
561
562 atm_info(usbatm,
Duncan Sands322a95b2005-06-23 09:20:50 +0200563 "ADSL line is up (%d kb/s down | %d kb/s up)\n",
Duncan Sands48da7262005-05-11 20:20:40 +0200564 down_speed, up_speed);
Duncan Sands1a7aad12005-06-23 09:37:56 +0200565 break;
Duncan Sands48da7262005-05-11 20:20:40 +0200566
Duncan Sands1a7aad12005-06-23 09:37:56 +0200567 default:
Duncan Sands48da7262005-05-11 20:20:40 +0200568 atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100569 atm_info(usbatm, "unknown line state %02x\n", status);
Duncan Sands1a7aad12005-06-23 09:37:56 +0200570 break;
Duncan Sands48da7262005-05-11 20:20:40 +0200571 }
Duncan Sands1a7aad12005-06-23 09:37:56 +0200572
573 instance->last_status = status;
Duncan Sands48da7262005-05-11 20:20:40 +0200574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
Duncan Sands48da7262005-05-11 20:20:40 +0200577static void speedtch_status_poll(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Duncan Sands48da7262005-05-11 20:20:40 +0200579 struct speedtch_instance_data *instance = (void *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
David Howellsc4028952006-11-22 14:57:56 +0000581 schedule_delayed_work(&instance->status_checker, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Duncan Sands48da7262005-05-11 20:20:40 +0200583 /* The following check is racy, but the race is harmless */
584 if (instance->poll_delay < MAX_POLL_DELAY)
585 mod_timer(&instance->status_checker.timer, jiffies + msecs_to_jiffies(instance->poll_delay));
586 else
David S. Miller52fbae22005-07-25 19:54:35 -0700587 atm_warn(instance->usbatm, "Too many failures - disabling line status polling\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
Duncan Sands48da7262005-05-11 20:20:40 +0200590static void speedtch_resubmit_int(unsigned long data)
591{
592 struct speedtch_instance_data *instance = (void *)data;
593 struct urb *int_urb = instance->int_urb;
594 int ret;
595
596 atm_dbg(instance->usbatm, "%s entered\n", __func__);
597
598 if (int_urb) {
599 ret = usb_submit_urb(int_urb, GFP_ATOMIC);
600 if (!ret)
David Howellsc4028952006-11-22 14:57:56 +0000601 schedule_delayed_work(&instance->status_checker, 0);
Duncan Sands48da7262005-05-11 20:20:40 +0200602 else {
603 atm_dbg(instance->usbatm, "%s: usb_submit_urb failed with result %d\n", __func__, ret);
604 mod_timer(&instance->resubmit_timer, jiffies + msecs_to_jiffies(RESUBMIT_DELAY));
605 }
606 }
607}
608
David Howells7d12e782006-10-05 14:55:46 +0100609static void speedtch_handle_int(struct urb *int_urb)
Duncan Sands48da7262005-05-11 20:20:40 +0200610{
611 struct speedtch_instance_data *instance = int_urb->context;
612 struct usbatm_data *usbatm = instance->usbatm;
613 unsigned int count = int_urb->actual_length;
Greg Kroah-Hartman9a5a3e92007-07-18 10:58:02 -0700614 int status = int_urb->status;
615 int ret;
Duncan Sands48da7262005-05-11 20:20:40 +0200616
617 /* The magic interrupt for "up state" */
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800618 static const unsigned char up_int[6] = { 0xa1, 0x00, 0x01, 0x00, 0x00, 0x00 };
Duncan Sands48da7262005-05-11 20:20:40 +0200619 /* The magic interrupt for "down state" */
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800620 static const unsigned char down_int[6] = { 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00 };
Duncan Sands48da7262005-05-11 20:20:40 +0200621
622 atm_dbg(usbatm, "%s entered\n", __func__);
623
Greg Kroah-Hartman9a5a3e92007-07-18 10:58:02 -0700624 if (status < 0) {
625 atm_dbg(usbatm, "%s: nonzero urb status %d!\n", __func__, status);
Duncan Sands48da7262005-05-11 20:20:40 +0200626 goto fail;
627 }
628
629 if ((count == 6) && !memcmp(up_int, instance->int_data, 6)) {
630 del_timer(&instance->status_checker.timer);
David S. Miller52fbae22005-07-25 19:54:35 -0700631 atm_info(usbatm, "DSL line goes up\n");
Duncan Sands48da7262005-05-11 20:20:40 +0200632 } else if ((count == 6) && !memcmp(down_int, instance->int_data, 6)) {
David S. Miller52fbae22005-07-25 19:54:35 -0700633 atm_info(usbatm, "DSL line goes down\n");
Duncan Sands48da7262005-05-11 20:20:40 +0200634 } else {
635 int i;
636
637 atm_dbg(usbatm, "%s: unknown interrupt packet of length %d:", __func__, count);
638 for (i = 0; i < count; i++)
639 printk(" %02x", instance->int_data[i]);
640 printk("\n");
641 goto fail;
642 }
643
644 if ((int_urb = instance->int_urb)) {
645 ret = usb_submit_urb(int_urb, GFP_ATOMIC);
David Howellsc4028952006-11-22 14:57:56 +0000646 schedule_delayed_work(&instance->status_checker, 0);
Duncan Sands48da7262005-05-11 20:20:40 +0200647 if (ret < 0) {
648 atm_dbg(usbatm, "%s: usb_submit_urb failed with result %d\n", __func__, ret);
649 goto fail;
650 }
651 }
652
653 return;
654
655fail:
656 if ((int_urb = instance->int_urb))
657 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++)
673 atm_dev->esi[i] = (hex2int(mac_str[i * 2]) * 16) + (hex2int(mac_str[i * 2 + 1]));
674 }
675
676 /* Start modem synchronisation */
677 ret = speedtch_start_synchro(instance);
678
679 /* Set up interrupt endpoint */
680 if (instance->int_urb) {
681 ret = usb_submit_urb(instance->int_urb, GFP_KERNEL);
682 if (ret < 0) {
683 /* Doesn't matter; we'll poll anyway */
684 atm_dbg(usbatm, "%s: submission of interrupt URB failed (%d)!\n", __func__, ret);
685 usb_free_urb(instance->int_urb);
686 instance->int_urb = NULL;
687 }
688 }
689
690 /* Start status polling */
691 mod_timer(&instance->status_checker.timer, jiffies + msecs_to_jiffies(1000));
692
693 return 0;
694}
695
696static void speedtch_atm_stop(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
697{
698 struct speedtch_instance_data *instance = usbatm->driver_data;
699 struct urb *int_urb = instance->int_urb;
700
701 atm_dbg(usbatm, "%s entered\n", __func__);
702
703 del_timer_sync(&instance->status_checker.timer);
704
705 /*
706 * Since resubmit_timer and int_urb can schedule themselves and
707 * each other, shutting them down correctly takes some care
708 */
709 instance->int_urb = NULL; /* signal shutdown */
710 mb();
711 usb_kill_urb(int_urb);
712 del_timer_sync(&instance->resubmit_timer);
713 /*
714 * At this point, speedtch_handle_int and speedtch_resubmit_int
715 * can run or be running, but instance->int_urb == NULL means that
716 * they will not reschedule
717 */
718 usb_kill_urb(int_urb);
719 del_timer_sync(&instance->resubmit_timer);
720 usb_free_urb(int_urb);
721
722 flush_scheduled_work();
723}
724
Alan Stern8fc7aea2008-10-20 09:52:12 -0400725static int speedtch_pre_reset(struct usb_interface *intf)
726{
727 return 0;
728}
729
730static int speedtch_post_reset(struct usb_interface *intf)
731{
732 return 0;
733}
734
Duncan Sands48da7262005-05-11 20:20:40 +0200735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736/**********
737** USB **
738**********/
739
Duncan Sands48da7262005-05-11 20:20:40 +0200740static struct usb_device_id speedtch_usb_ids[] = {
741 {USB_DEVICE(0x06b9, 0x4061)},
742 {}
743};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Duncan Sands48da7262005-05-11 20:20:40 +0200745MODULE_DEVICE_TABLE(usb, speedtch_usb_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Duncan Sands48da7262005-05-11 20:20:40 +0200747static int speedtch_usb_probe(struct usb_interface *, const struct usb_device_id *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Duncan Sands48da7262005-05-11 20:20:40 +0200749static struct usb_driver speedtch_usb_driver = {
Duncan Sands48da7262005-05-11 20:20:40 +0200750 .name = speedtch_driver_name,
751 .probe = speedtch_usb_probe,
752 .disconnect = usbatm_usb_disconnect,
Alan Stern8fc7aea2008-10-20 09:52:12 -0400753 .pre_reset = speedtch_pre_reset,
754 .post_reset = speedtch_post_reset,
Duncan Sands48da7262005-05-11 20:20:40 +0200755 .id_table = speedtch_usb_ids
756};
757
758static void speedtch_release_interfaces(struct usb_device *usb_dev, int num_interfaces) {
759 struct usb_interface *cur_intf;
760 int i;
761
762 for(i = 0; i < num_interfaces; i++)
763 if ((cur_intf = usb_ifnum_to_if(usb_dev, i))) {
764 usb_set_intfdata(cur_intf, NULL);
765 usb_driver_release_interface(&speedtch_usb_driver, cur_intf);
766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
Duncan Sands48da7262005-05-11 20:20:40 +0200769static int speedtch_bind(struct usbatm_data *usbatm,
770 struct usb_interface *intf,
Duncan Sands35644b02006-01-17 11:16:13 +0100771 const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Duncan Sands48da7262005-05-11 20:20:40 +0200773 struct usb_device *usb_dev = interface_to_usbdev(intf);
Duncan Sands80aae7a2006-01-13 10:59:23 +0100774 struct usb_interface *cur_intf, *data_intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 struct speedtch_instance_data *instance;
Duncan Sands48da7262005-05-11 20:20:40 +0200776 int ifnum = intf->altsetting->desc.bInterfaceNumber;
777 int num_interfaces = usb_dev->actconfig->desc.bNumInterfaces;
778 int i, ret;
Duncan Sands80aae7a2006-01-13 10:59:23 +0100779 int use_isoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Duncan Sands48da7262005-05-11 20:20:40 +0200781 usb_dbg(usbatm, "%s entered\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100783 /* sanity checks */
784
Duncan Sands48da7262005-05-11 20:20:40 +0200785 if (usb_dev->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100786 usb_err(usbatm, "%s: wrong device class %d\n", __func__, usb_dev->descriptor.bDeviceClass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return -ENODEV;
Duncan Sands48da7262005-05-11 20:20:40 +0200788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Duncan Sands80aae7a2006-01-13 10:59:23 +0100790 if (!(data_intf = usb_ifnum_to_if(usb_dev, INTERFACE_DATA))) {
791 usb_err(usbatm, "%s: data interface not found!\n", __func__);
792 return -ENODEV;
793 }
794
Duncan Sands48da7262005-05-11 20:20:40 +0200795 /* claim all interfaces */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Duncan Sands48da7262005-05-11 20:20:40 +0200797 for (i=0; i < num_interfaces; i++) {
798 cur_intf = usb_ifnum_to_if(usb_dev, i);
799
800 if ((i != ifnum) && cur_intf) {
801 ret = usb_driver_claim_interface(&speedtch_usb_driver, cur_intf, usbatm);
802
803 if (ret < 0) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100804 usb_err(usbatm, "%s: failed to claim interface %2d (%d)!\n", __func__, i, ret);
Duncan Sands48da7262005-05-11 20:20:40 +0200805 speedtch_release_interfaces(usb_dev, i);
806 return ret;
807 }
808 }
809 }
810
Duncan Sands9a734ef2006-01-13 09:38:22 +0100811 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
Duncan Sands48da7262005-05-11 20:20:40 +0200812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (!instance) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100814 usb_err(usbatm, "%s: no memory for instance data!\n", __func__);
Duncan Sands48da7262005-05-11 20:20:40 +0200815 ret = -ENOMEM;
816 goto fail_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818
Duncan Sands48da7262005-05-11 20:20:40 +0200819 instance->usbatm = usbatm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Duncan Sands6a4f1b42006-10-05 10:40:55 +0200821 /* module parameters may change at any moment, so take a snapshot */
822 instance->params.altsetting = altsetting;
823 instance->params.BMaxDSL = BMaxDSL;
824 instance->params.ModemMode = ModemMode;
825 memcpy(instance->params.ModemOption, DEFAULT_MODEM_OPTION, MODEM_OPTION_LENGTH);
826 memcpy(instance->params.ModemOption, ModemOption, num_ModemOption);
Duncan Sands80aae7a2006-01-13 10:59:23 +0100827 use_isoc = enable_isoc;
Duncan Sands6f749472006-01-13 10:52:38 +0100828
Duncan Sands6a4f1b42006-10-05 10:40:55 +0200829 if (instance->params.altsetting)
830 if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, instance->params.altsetting)) < 0) {
831 usb_err(usbatm, "%s: setting interface to %2d failed (%d)!\n", __func__, instance->params.altsetting, ret);
832 instance->params.altsetting = 0; /* fall back to default */
Duncan Sands6f749472006-01-13 10:52:38 +0100833 }
834
Duncan Sands6a4f1b42006-10-05 10:40:55 +0200835 if (!instance->params.altsetting && use_isoc)
Duncan Sands80aae7a2006-01-13 10:59:23 +0100836 if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, DEFAULT_ISOC_ALTSETTING)) < 0) {
837 usb_dbg(usbatm, "%s: setting interface to %2d failed (%d)!\n", __func__, DEFAULT_ISOC_ALTSETTING, ret);
838 use_isoc = 0; /* fall back to bulk */
839 }
840
841 if (use_isoc) {
842 const struct usb_host_interface *desc = data_intf->cur_altsetting;
843 const __u8 target_address = USB_DIR_IN | usbatm->driver->isoc_in;
Duncan Sands80aae7a2006-01-13 10:59:23 +0100844
845 use_isoc = 0; /* fall back to bulk if endpoint not found */
846
847 for (i=0; i<desc->desc.bNumEndpoints; i++) {
848 const struct usb_endpoint_descriptor *endpoint_desc = &desc->endpoint[i].desc;
849
850 if ((endpoint_desc->bEndpointAddress == target_address)) {
Luiz Fernando N. Capitulinoc5dd1f92006-10-26 13:02:58 -0300851 use_isoc =
852 usb_endpoint_xfer_isoc(endpoint_desc);
Duncan Sands80aae7a2006-01-13 10:59:23 +0100853 break;
854 }
855 }
856
857 if (!use_isoc)
858 usb_info(usbatm, "isochronous transfer not supported - using bulk\n");
859 }
860
Duncan Sands6a4f1b42006-10-05 10:40:55 +0200861 if (!use_isoc && !instance->params.altsetting)
Duncan Sands80aae7a2006-01-13 10:59:23 +0100862 if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, DEFAULT_BULK_ALTSETTING)) < 0) {
863 usb_err(usbatm, "%s: setting interface to %2d failed (%d)!\n", __func__, DEFAULT_BULK_ALTSETTING, ret);
Duncan Sands6f749472006-01-13 10:52:38 +0100864 goto fail_free;
865 }
Duncan Sands80aae7a2006-01-13 10:59:23 +0100866
Duncan Sands6a4f1b42006-10-05 10:40:55 +0200867 if (!instance->params.altsetting)
868 instance->params.altsetting = use_isoc ? DEFAULT_ISOC_ALTSETTING : DEFAULT_BULK_ALTSETTING;
Duncan Sands80aae7a2006-01-13 10:59:23 +0100869
870 usbatm->flags |= (use_isoc ? UDSL_USE_ISOC : 0);
Duncan Sands6f749472006-01-13 10:52:38 +0100871
David Howellsc4028952006-11-22 14:57:56 +0000872 INIT_DELAYED_WORK(&instance->status_checker, speedtch_check_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Duncan Sands48da7262005-05-11 20:20:40 +0200874 instance->status_checker.timer.function = speedtch_status_poll;
875 instance->status_checker.timer.data = (unsigned long)instance;
Duncan Sands1a7aad12005-06-23 09:37:56 +0200876 instance->last_status = 0xff;
Duncan Sands48da7262005-05-11 20:20:40 +0200877 instance->poll_delay = MIN_POLL_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Duncan Sands48da7262005-05-11 20:20:40 +0200879 init_timer(&instance->resubmit_timer);
880 instance->resubmit_timer.function = speedtch_resubmit_int;
881 instance->resubmit_timer.data = (unsigned long)instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Duncan Sands48da7262005-05-11 20:20:40 +0200883 instance->int_urb = usb_alloc_urb(0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Duncan Sands48da7262005-05-11 20:20:40 +0200885 if (instance->int_urb)
886 usb_fill_int_urb(instance->int_urb, usb_dev,
887 usb_rcvintpipe(usb_dev, ENDPOINT_INT),
888 instance->int_data, sizeof(instance->int_data),
889 speedtch_handle_int, instance, 50);
890 else
891 usb_dbg(usbatm, "%s: no memory for interrupt urb!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Duncan Sands48da7262005-05-11 20:20:40 +0200893 /* check whether the modem already seems to be alive */
894 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
895 0x12, 0xc0, 0x07, 0x00,
896 instance->scratch_buffer + OFFSET_7, SIZE_7, 500);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Duncan Sands80aae7a2006-01-13 10:59:23 +0100898 usbatm->flags |= (ret == SIZE_7 ? UDSL_SKIP_HEAVY_INIT : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Duncan Sands35644b02006-01-17 11:16:13 +0100900 usb_dbg(usbatm, "%s: firmware %s loaded\n", __func__, usbatm->flags & UDSL_SKIP_HEAVY_INIT ? "already" : "not");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Duncan Sands35644b02006-01-17 11:16:13 +0100902 if (!(usbatm->flags & UDSL_SKIP_HEAVY_INIT))
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100903 if ((ret = usb_reset_device(usb_dev)) < 0) {
904 usb_err(usbatm, "%s: device reset failed (%d)!\n", __func__, ret);
Duncan Sands48da7262005-05-11 20:20:40 +0200905 goto fail_free;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100906 }
Duncan Sands48da7262005-05-11 20:20:40 +0200907
908 usbatm->driver_data = instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 return 0;
911
Duncan Sands48da7262005-05-11 20:20:40 +0200912fail_free:
913 usb_free_urb(instance->int_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 kfree(instance);
Duncan Sands48da7262005-05-11 20:20:40 +0200915fail_release:
916 speedtch_release_interfaces(usb_dev, num_interfaces);
917 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
Duncan Sands48da7262005-05-11 20:20:40 +0200920static void speedtch_unbind(struct usbatm_data *usbatm, struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Duncan Sands48da7262005-05-11 20:20:40 +0200922 struct usb_device *usb_dev = interface_to_usbdev(intf);
923 struct speedtch_instance_data *instance = usbatm->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Duncan Sands48da7262005-05-11 20:20:40 +0200925 usb_dbg(usbatm, "%s entered\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Duncan Sands48da7262005-05-11 20:20:40 +0200927 speedtch_release_interfaces(usb_dev, usb_dev->actconfig->desc.bNumInterfaces);
928 usb_free_urb(instance->int_urb);
929 kfree(instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
Duncan Sands48da7262005-05-11 20:20:40 +0200932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933/***********
934** init **
935***********/
936
Duncan Sands48da7262005-05-11 20:20:40 +0200937static struct usbatm_driver speedtch_usbatm_driver = {
Duncan Sands48da7262005-05-11 20:20:40 +0200938 .driver_name = speedtch_driver_name,
939 .bind = speedtch_bind,
940 .heavy_init = speedtch_heavy_init,
941 .unbind = speedtch_unbind,
942 .atm_start = speedtch_atm_start,
943 .atm_stop = speedtch_atm_stop,
Duncan Sands80aae7a2006-01-13 10:59:23 +0100944 .bulk_in = ENDPOINT_BULK_DATA,
945 .bulk_out = ENDPOINT_BULK_DATA,
946 .isoc_in = ENDPOINT_ISOC_DATA
Duncan Sands48da7262005-05-11 20:20:40 +0200947};
948
949static int speedtch_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
950{
951 return usbatm_usb_probe(intf, id, &speedtch_usbatm_driver);
952}
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954static int __init speedtch_usb_init(void)
955{
Duncan Sands48da7262005-05-11 20:20:40 +0200956 dbg("%s: driver version %s", __func__, DRIVER_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 return usb_register(&speedtch_usb_driver);
959}
960
961static void __exit speedtch_usb_cleanup(void)
962{
Duncan Sands48da7262005-05-11 20:20:40 +0200963 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 usb_deregister(&speedtch_usb_driver);
966}
967
968module_init(speedtch_usb_init);
969module_exit(speedtch_usb_cleanup);
970
971MODULE_AUTHOR(DRIVER_AUTHOR);
972MODULE_DESCRIPTION(DRIVER_DESC);
973MODULE_LICENSE("GPL");
974MODULE_VERSION(DRIVER_VERSION);