]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/usb/atm/speedtch.c
USB: speedtouch: fixed more brace and spacing coding style issues
[net-next-2.6.git] / drivers / usb / atm / speedtch.c
CommitLineData
1da177e4
LT
1/******************************************************************************
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 *
48da7267
DS
8 * Based on "modem_run.c", copyright (C) 2001, Benoit Papillault
9 *
1da177e4
LT
10 * 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
48da7267
DS
26#include <asm/page.h>
27#include <linux/device.h>
28#include <linux/errno.h>
29#include <linux/firmware.h>
48da7267 30#include <linux/init.h>
1da177e4 31#include <linux/kernel.h>
48da7267
DS
32#include <linux/module.h>
33#include <linux/moduleparam.h>
1da177e4 34#include <linux/slab.h>
48da7267
DS
35#include <linux/stat.h>
36#include <linux/timer.h>
80aae7a1 37#include <linux/types.h>
5f848137 38#include <linux/usb/ch9.h>
48da7267 39#include <linux/workqueue.h>
1da177e4 40
48da7267 41#include "usbatm.h"
1da177e4
LT
42
43#define DRIVER_AUTHOR "Johan Verrept, Duncan Sands <duncan.sands@free.fr>"
9b0e54ad 44#define DRIVER_VERSION "1.10"
1da177e4
LT
45#define DRIVER_DESC "Alcatel SpeedTouch USB driver version " DRIVER_VERSION
46
47static const char speedtch_driver_name[] = "speedtch";
48
48da7267
DS
49#define CTRL_TIMEOUT 2000 /* milliseconds */
50#define DATA_TIMEOUT 2000 /* milliseconds */
1da177e4 51
48da7267
DS
52#define OFFSET_7 0 /* size 1 */
53#define OFFSET_b 1 /* size 8 */
54#define OFFSET_d 9 /* size 4 */
55#define OFFSET_e 13 /* size 1 */
56#define OFFSET_f 14 /* size 1 */
1da177e4 57
48da7267
DS
58#define SIZE_7 1
59#define SIZE_b 8
60#define SIZE_d 4
61#define SIZE_e 1
62#define SIZE_f 1
1da177e4 63
48da7267
DS
64#define MIN_POLL_DELAY 5000 /* milliseconds */
65#define MAX_POLL_DELAY 60000 /* milliseconds */
1da177e4 66
48da7267 67#define RESUBMIT_DELAY 1000 /* milliseconds */
1da177e4 68
80aae7a1 69#define DEFAULT_BULK_ALTSETTING 1
67c752b4 70#define DEFAULT_ISOC_ALTSETTING 3
48da7267 71#define DEFAULT_DL_512_FIRST 0
80aae7a1 72#define DEFAULT_ENABLE_ISOC 0
48da7267 73#define DEFAULT_SW_BUFFERING 0
1da177e4 74
80aae7a1 75static unsigned int altsetting = 0; /* zero means: use the default */
48da7267 76static int dl_512_first = DEFAULT_DL_512_FIRST;
80aae7a1 77static int enable_isoc = DEFAULT_ENABLE_ISOC;
48da7267 78static int sw_buffering = DEFAULT_SW_BUFFERING;
1da177e4 79
6a4f1b41
DS
80#define DEFAULT_B_MAX_DSL 8128
81#define DEFAULT_MODEM_MODE 11
82#define MODEM_OPTION_LENGTH 16
83static const unsigned char DEFAULT_MODEM_OPTION[MODEM_OPTION_LENGTH] = {
84 0x10, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
85};
86
87static unsigned int BMaxDSL = DEFAULT_B_MAX_DSL;
88static unsigned char ModemMode = DEFAULT_MODEM_MODE;
89static unsigned char ModemOption[MODEM_OPTION_LENGTH];
64a6f950 90static unsigned int num_ModemOption;
6a4f1b41 91
80aae7a1 92module_param(altsetting, uint, S_IRUGO | S_IWUSR);
48da7267 93MODULE_PARM_DESC(altsetting,
80aae7a1
DS
94 "Alternative setting for data interface (bulk_default: "
95 __MODULE_STRING(DEFAULT_BULK_ALTSETTING) "; isoc_default: "
96 __MODULE_STRING(DEFAULT_ISOC_ALTSETTING) ")");
1da177e4 97
48da7267
DS
98module_param(dl_512_first, bool, S_IRUGO | S_IWUSR);
99MODULE_PARM_DESC(dl_512_first,
100 "Read 512 bytes before sending firmware (default: "
101 __MODULE_STRING(DEFAULT_DL_512_FIRST) ")");
1da177e4 102
80aae7a1
DS
103module_param(enable_isoc, bool, S_IRUGO | S_IWUSR);
104MODULE_PARM_DESC(enable_isoc,
105 "Use isochronous transfers if available (default: "
106 __MODULE_STRING(DEFAULT_ENABLE_ISOC) ")");
107
48da7267
DS
108module_param(sw_buffering, bool, S_IRUGO | S_IWUSR);
109MODULE_PARM_DESC(sw_buffering,
110 "Enable software buffering (default: "
111 __MODULE_STRING(DEFAULT_SW_BUFFERING) ")");
1da177e4 112
6a4f1b41
DS
113module_param(BMaxDSL, uint, S_IRUGO | S_IWUSR);
114MODULE_PARM_DESC(BMaxDSL,
115 "default: " __MODULE_STRING(DEFAULT_B_MAX_DSL));
116
117module_param(ModemMode, byte, S_IRUGO | S_IWUSR);
118MODULE_PARM_DESC(ModemMode,
119 "default: " __MODULE_STRING(DEFAULT_MODEM_MODE));
120
121module_param_array(ModemOption, byte, &num_ModemOption, S_IRUGO);
122MODULE_PARM_DESC(ModemOption, "default: 0x10,0x00,0x00,0x00,0x20");
123
6f749475 124#define INTERFACE_DATA 1
48da7267 125#define ENDPOINT_INT 0x81
80aae7a1
DS
126#define ENDPOINT_BULK_DATA 0x07
127#define ENDPOINT_ISOC_DATA 0x07
48da7267 128#define ENDPOINT_FIRMWARE 0x05
1da177e4 129
6a4f1b41
DS
130struct speedtch_params {
131 unsigned int altsetting;
132 unsigned int BMaxDSL;
133 unsigned char ModemMode;
134 unsigned char ModemOption[MODEM_OPTION_LENGTH];
135};
136
1da177e4 137struct speedtch_instance_data {
48da7267
DS
138 struct usbatm_data *usbatm;
139
6a4f1b41 140 struct speedtch_params params; /* set in probe, constant afterwards */
6f749475 141
c4028958 142 struct delayed_work status_checker;
1da177e4 143
1a7aad15
DS
144 unsigned char last_status;
145
48da7267
DS
146 int poll_delay; /* milliseconds */
147
148 struct timer_list resubmit_timer;
1da177e4
LT
149 struct urb *int_urb;
150 unsigned char int_data[16];
1da177e4 151
6a4f1b41 152 unsigned char scratch_buffer[16];
1da177e4
LT
153};
154
155/***************
156** firmware **
157***************/
158
48da7267 159static void speedtch_set_swbuff(struct speedtch_instance_data *instance, int state)
1da177e4 160{
48da7267
DS
161 struct usbatm_data *usbatm = instance->usbatm;
162 struct usb_device *usb_dev = usbatm->usb_dev;
1da177e4
LT
163 int ret;
164
48da7267
DS
165 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
172 dbg("speedtch_set_swbuff: %sbled SW buffering", state ? "En" : "Dis");
1da177e4
LT
173}
174
175static void speedtch_test_sequence(struct speedtch_instance_data *instance)
176{
48da7267
DS
177 struct usbatm_data *usbatm = instance->usbatm;
178 struct usb_device *usb_dev = usbatm->usb_dev;
179 unsigned char *buf = instance->scratch_buffer;
1da177e4
LT
180 int ret;
181
182 /* URB 147 */
183 buf[0] = 0x1c;
184 buf[1] = 0x50;
48da7267
DS
185 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
186 0x01, 0x40, 0x0b, 0x00, buf, 2, CTRL_TIMEOUT);
1da177e4 187 if (ret < 0)
48da7267 188 usb_warn(usbatm, "%s failed on URB147: %d\n", __func__, ret);
1da177e4
LT
189
190 /* URB 148 */
191 buf[0] = 0x32;
192 buf[1] = 0x00;
48da7267
DS
193 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
194 0x01, 0x40, 0x02, 0x00, buf, 2, CTRL_TIMEOUT);
1da177e4 195 if (ret < 0)
48da7267 196 usb_warn(usbatm, "%s failed on URB148: %d\n", __func__, ret);
1da177e4
LT
197
198 /* URB 149 */
199 buf[0] = 0x01;
200 buf[1] = 0x00;
201 buf[2] = 0x01;
48da7267
DS
202 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
203 0x01, 0x40, 0x03, 0x00, buf, 3, CTRL_TIMEOUT);
1da177e4 204 if (ret < 0)
48da7267 205 usb_warn(usbatm, "%s failed on URB149: %d\n", __func__, ret);
1da177e4
LT
206
207 /* URB 150 */
208 buf[0] = 0x01;
209 buf[1] = 0x00;
210 buf[2] = 0x01;
48da7267
DS
211 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
212 0x01, 0x40, 0x04, 0x00, buf, 3, CTRL_TIMEOUT);
1da177e4 213 if (ret < 0)
48da7267 214 usb_warn(usbatm, "%s failed on URB150: %d\n", __func__, ret);
6a4f1b41
DS
215
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);
1da177e4
LT
243}
244
48da7267
DS
245static int speedtch_upload_firmware(struct speedtch_instance_data *instance,
246 const struct firmware *fw1,
247 const struct firmware *fw2)
1da177e4 248{
48da7267
DS
249 unsigned char *buffer;
250 struct usbatm_data *usbatm = instance->usbatm;
48da7267
DS
251 struct usb_device *usb_dev = usbatm->usb_dev;
252 int actual_length;
253 int ret = 0;
254 int offset;
255
256 usb_dbg(usbatm, "%s entered\n", __func__);
257
258 if (!(buffer = (unsigned char *)__get_free_page(GFP_KERNEL))) {
259 ret = -ENOMEM;
260 usb_dbg(usbatm, "%s: no memory for buffer!\n", __func__);
261 goto out;
262 }
263
011db815 264 if (!usb_ifnum_to_if(usb_dev, 2)) {
48da7267
DS
265 ret = -ENODEV;
266 usb_dbg(usbatm, "%s: interface not found!\n", __func__);
267 goto out_free;
268 }
269
270 /* URB 7 */
271 if (dl_512_first) { /* some modems need a read before writing the firmware */
272 ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
273 buffer, 0x200, &actual_length, 2000);
274
275 if (ret < 0 && ret != -ETIMEDOUT)
0ec3c7e8 276 usb_warn(usbatm, "%s: read BLOCK0 from modem failed (%d)!\n", __func__, ret);
48da7267
DS
277 else
278 usb_dbg(usbatm, "%s: BLOCK0 downloaded (%d bytes)\n", __func__, ret);
279 }
280
281 /* URB 8 : both leds are static green */
282 for (offset = 0; offset < fw1->size; offset += PAGE_SIZE) {
283 int thislen = min_t(int, PAGE_SIZE, fw1->size - offset);
284 memcpy(buffer, fw1->data + offset, thislen);
285
286 ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
287 buffer, thislen, &actual_length, DATA_TIMEOUT);
288
289 if (ret < 0) {
0ec3c7e8 290 usb_err(usbatm, "%s: write BLOCK1 to modem failed (%d)!\n", __func__, ret);
48da7267
DS
291 goto out_free;
292 }
293 usb_dbg(usbatm, "%s: BLOCK1 uploaded (%zu bytes)\n", __func__, fw1->size);
294 }
295
296 /* USB led blinking green, ADSL led off */
297
298 /* URB 11 */
299 ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
300 buffer, 0x200, &actual_length, DATA_TIMEOUT);
1da177e4 301
1da177e4 302 if (ret < 0) {
0ec3c7e8 303 usb_err(usbatm, "%s: read BLOCK2 from modem failed (%d)!\n", __func__, ret);
48da7267 304 goto out_free;
1da177e4 305 }
48da7267 306 usb_dbg(usbatm, "%s: BLOCK2 downloaded (%d bytes)\n", __func__, actual_length);
1da177e4 307
48da7267
DS
308 /* URBs 12 to 139 - USB led blinking green, ADSL led off */
309 for (offset = 0; offset < fw2->size; offset += PAGE_SIZE) {
310 int thislen = min_t(int, PAGE_SIZE, fw2->size - offset);
311 memcpy(buffer, fw2->data + offset, thislen);
312
313 ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
314 buffer, thislen, &actual_length, DATA_TIMEOUT);
315
316 if (ret < 0) {
0ec3c7e8 317 usb_err(usbatm, "%s: write BLOCK3 to modem failed (%d)!\n", __func__, ret);
48da7267
DS
318 goto out_free;
319 }
320 }
321 usb_dbg(usbatm, "%s: BLOCK3 uploaded (%zu bytes)\n", __func__, fw2->size);
322
323 /* USB led static green, ADSL led static red */
324
325 /* URB 142 */
326 ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
327 buffer, 0x200, &actual_length, DATA_TIMEOUT);
328
329 if (ret < 0) {
0ec3c7e8 330 usb_err(usbatm, "%s: read BLOCK4 from modem failed (%d)!\n", __func__, ret);
48da7267
DS
331 goto out_free;
332 }
333
334 /* success */
335 usb_dbg(usbatm, "%s: BLOCK4 downloaded (%d bytes)\n", __func__, actual_length);
336
337 /* Delay to allow firmware to start up. We can do this here
338 because we're in our own kernel thread anyway. */
339 msleep_interruptible(1000);
340
6a4f1b41
DS
341 if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, instance->params.altsetting)) < 0) {
342 usb_err(usbatm, "%s: setting interface to %d failed (%d)!\n", __func__, instance->params.altsetting, ret);
6f749475
DS
343 goto out_free;
344 }
345
48da7267
DS
346 /* Enable software buffering, if requested */
347 if (sw_buffering)
348 speedtch_set_swbuff(instance, 1);
349
350 /* Magic spell; don't ask us what this does */
351 speedtch_test_sequence(instance);
352
353 ret = 0;
354
355out_free:
356 free_page((unsigned long)buffer);
357out:
358 return ret;
1da177e4
LT
359}
360
0ec3c7e8
DS
361static int speedtch_find_firmware(struct usbatm_data *usbatm, struct usb_interface *intf,
362 int phase, const struct firmware **fw_p)
1da177e4 363{
48da7267
DS
364 struct device *dev = &intf->dev;
365 const u16 bcdDevice = le16_to_cpu(interface_to_usbdev(intf)->descriptor.bcdDevice);
366 const u8 major_revision = bcdDevice >> 8;
367 const u8 minor_revision = bcdDevice & 0xff;
368 char buf[24];
1da177e4 369
48da7267 370 sprintf(buf, "speedtch-%d.bin.%x.%02x", phase, major_revision, minor_revision);
0ec3c7e8 371 usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
1da177e4 372
48da7267
DS
373 if (request_firmware(fw_p, buf, dev)) {
374 sprintf(buf, "speedtch-%d.bin.%x", phase, major_revision);
0ec3c7e8 375 usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
1da177e4 376
48da7267
DS
377 if (request_firmware(fw_p, buf, dev)) {
378 sprintf(buf, "speedtch-%d.bin", phase);
0ec3c7e8 379 usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
48da7267
DS
380
381 if (request_firmware(fw_p, buf, dev)) {
0ec3c7e8 382 usb_err(usbatm, "%s: no stage %d firmware found!\n", __func__, phase);
48da7267
DS
383 return -ENOENT;
384 }
385 }
1da177e4
LT
386 }
387
0ec3c7e8 388 usb_info(usbatm, "found stage %d firmware %s\n", phase, buf);
1da177e4 389
48da7267
DS
390 return 0;
391}
392
393static int speedtch_heavy_init(struct usbatm_data *usbatm, struct usb_interface *intf)
394{
395 const struct firmware *fw1, *fw2;
396 struct speedtch_instance_data *instance = usbatm->driver_data;
397 int ret;
398
0ec3c7e8
DS
399 if ((ret = speedtch_find_firmware(usbatm, intf, 1, &fw1)) < 0)
400 return ret;
48da7267 401
0ec3c7e8 402 if ((ret = speedtch_find_firmware(usbatm, intf, 2, &fw2)) < 0) {
48da7267
DS
403 release_firmware(fw1);
404 return ret;
1da177e4 405 }
1da177e4 406
0ec3c7e8
DS
407 if ((ret = speedtch_upload_firmware(instance, fw1, fw2)) < 0)
408 usb_err(usbatm, "%s: firmware upload failed (%d)!\n", __func__, ret);
48da7267
DS
409
410 release_firmware(fw2);
411 release_firmware(fw1);
1da177e4 412
48da7267 413 return ret;
1da177e4
LT
414}
415
48da7267
DS
416
417/**********
418** ATM **
419**********/
420
421static int speedtch_read_status(struct speedtch_instance_data *instance)
1da177e4 422{
48da7267
DS
423 struct usbatm_data *usbatm = instance->usbatm;
424 struct usb_device *usb_dev = usbatm->usb_dev;
425 unsigned char *buf = instance->scratch_buffer;
1da177e4
LT
426 int ret;
427
6a4f1b41 428 memset(buf, 0, 16);
1da177e4 429
48da7267 430 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
1da177e4
LT
431 0x12, 0xc0, 0x07, 0x00, buf + OFFSET_7, SIZE_7,
432 CTRL_TIMEOUT);
433 if (ret < 0) {
48da7267 434 atm_dbg(usbatm, "%s: MSG 7 failed\n", __func__);
1da177e4
LT
435 return ret;
436 }
437
48da7267 438 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
1da177e4
LT
439 0x12, 0xc0, 0x0b, 0x00, buf + OFFSET_b, SIZE_b,
440 CTRL_TIMEOUT);
441 if (ret < 0) {
48da7267 442 atm_dbg(usbatm, "%s: MSG B failed\n", __func__);
1da177e4
LT
443 return ret;
444 }
445
48da7267 446 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
1da177e4
LT
447 0x12, 0xc0, 0x0d, 0x00, buf + OFFSET_d, SIZE_d,
448 CTRL_TIMEOUT);
449 if (ret < 0) {
48da7267 450 atm_dbg(usbatm, "%s: MSG D failed\n", __func__);
1da177e4
LT
451 return ret;
452 }
453
48da7267 454 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
1da177e4
LT
455 0x01, 0xc0, 0x0e, 0x00, buf + OFFSET_e, SIZE_e,
456 CTRL_TIMEOUT);
457 if (ret < 0) {
48da7267 458 atm_dbg(usbatm, "%s: MSG E failed\n", __func__);
1da177e4
LT
459 return ret;
460 }
461
48da7267 462 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
1da177e4
LT
463 0x01, 0xc0, 0x0f, 0x00, buf + OFFSET_f, SIZE_f,
464 CTRL_TIMEOUT);
465 if (ret < 0) {
48da7267 466 atm_dbg(usbatm, "%s: MSG F failed\n", __func__);
1da177e4
LT
467 return ret;
468 }
469
470 return 0;
471}
472
48da7267 473static int speedtch_start_synchro(struct speedtch_instance_data *instance)
1da177e4 474{
48da7267
DS
475 struct usbatm_data *usbatm = instance->usbatm;
476 struct usb_device *usb_dev = usbatm->usb_dev;
477 unsigned char *buf = instance->scratch_buffer;
1da177e4
LT
478 int ret;
479
48da7267
DS
480 atm_dbg(usbatm, "%s entered\n", __func__);
481
482 memset(buf, 0, 2);
483
484 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
485 0x12, 0xc0, 0x04, 0x00,
486 buf, 2, CTRL_TIMEOUT);
487
488 if (ret < 0)
489 atm_warn(usbatm, "failed to start ADSL synchronisation: %d\n", ret);
490 else
491 atm_dbg(usbatm, "%s: modem prodded. %d bytes returned: %02x %02x\n",
492 __func__, ret, buf[0], buf[1]);
493
494 return ret;
495}
496
c4028958 497static void speedtch_check_status(struct work_struct *work)
48da7267 498{
c4028958
DH
499 struct speedtch_instance_data *instance =
500 container_of(work, struct speedtch_instance_data,
501 status_checker.work);
48da7267
DS
502 struct usbatm_data *usbatm = instance->usbatm;
503 struct atm_dev *atm_dev = usbatm->atm_dev;
504 unsigned char *buf = instance->scratch_buffer;
1a7aad15
DS
505 int down_speed, up_speed, ret;
506 unsigned char status;
48da7267 507
0ec3c7e8 508#ifdef VERBOSE_DEBUG
48da7267 509 atm_dbg(usbatm, "%s entered\n", __func__);
0ec3c7e8 510#endif
48da7267
DS
511
512 ret = speedtch_read_status(instance);
513 if (ret < 0) {
514 atm_warn(usbatm, "error %d fetching device status\n", ret);
cd5c08fb 515 instance->poll_delay = min(2 * instance->poll_delay, MAX_POLL_DELAY);
1da177e4
LT
516 return;
517 }
518
cd5c08fb 519 instance->poll_delay = max(instance->poll_delay / 2, MIN_POLL_DELAY);
48da7267 520
1a7aad15 521 status = buf[OFFSET_7];
1da177e4 522
1a7aad15 523 if ((status != instance->last_status) || !status) {
0ec3c7e8
DS
524 atm_dbg(usbatm, "%s: line state 0x%02x\n", __func__, status);
525
1a7aad15
DS
526 switch (status) {
527 case 0:
23f89f04 528 atm_dev_signal_change(atm_dev, ATM_PHY_SIG_LOST);
1a7aad15 529 if (instance->last_status)
52fbae2a 530 atm_info(usbatm, "ADSL line is down\n");
1a7aad15 531 /* It may never resync again unless we ask it to... */
48da7267 532 ret = speedtch_start_synchro(instance);
1a7aad15 533 break;
1da177e4 534
1a7aad15 535 case 0x08:
23f89f04 536 atm_dev_signal_change(atm_dev, ATM_PHY_SIG_UNKNOWN);
52fbae2a 537 atm_info(usbatm, "ADSL line is blocked?\n");
1a7aad15 538 break;
1da177e4 539
1a7aad15 540 case 0x10:
23f89f04 541 atm_dev_signal_change(atm_dev, ATM_PHY_SIG_LOST);
52fbae2a 542 atm_info(usbatm, "ADSL line is synchronising\n");
1a7aad15 543 break;
1da177e4 544
1a7aad15
DS
545 case 0x20:
546 down_speed = buf[OFFSET_b] | (buf[OFFSET_b + 1] << 8)
1da177e4 547 | (buf[OFFSET_b + 2] << 16) | (buf[OFFSET_b + 3] << 24);
1a7aad15 548 up_speed = buf[OFFSET_b + 4] | (buf[OFFSET_b + 5] << 8)
1da177e4
LT
549 | (buf[OFFSET_b + 6] << 16) | (buf[OFFSET_b + 7] << 24);
550
48da7267 551 if (!(down_speed & 0x0000ffff) && !(up_speed & 0x0000ffff)) {
1da177e4
LT
552 down_speed >>= 16;
553 up_speed >>= 16;
554 }
1da177e4 555
48da7267 556 atm_dev->link_rate = down_speed * 1000 / 424;
23f89f04 557 atm_dev_signal_change(atm_dev, ATM_PHY_SIG_FOUND);
48da7267
DS
558
559 atm_info(usbatm,
322a95bc 560 "ADSL line is up (%d kb/s down | %d kb/s up)\n",
48da7267 561 down_speed, up_speed);
1a7aad15 562 break;
1da177e4 563
1a7aad15 564 default:
23f89f04 565 atm_dev_signal_change(atm_dev, ATM_PHY_SIG_UNKNOWN);
0ec3c7e8 566 atm_info(usbatm, "unknown line state %02x\n", status);
1a7aad15 567 break;
1da177e4 568 }
1a7aad15
DS
569
570 instance->last_status = status;
1da177e4
LT
571 }
572}
573
48da7267 574static void speedtch_status_poll(unsigned long data)
1da177e4
LT
575{
576 struct speedtch_instance_data *instance = (void *)data;
577
c4028958 578 schedule_delayed_work(&instance->status_checker, 0);
48da7267
DS
579
580 /* The following check is racy, but the race is harmless */
581 if (instance->poll_delay < MAX_POLL_DELAY)
582 mod_timer(&instance->status_checker.timer, jiffies + msecs_to_jiffies(instance->poll_delay));
583 else
52fbae2a 584 atm_warn(instance->usbatm, "Too many failures - disabling line status polling\n");
1da177e4
LT
585}
586
48da7267 587static void speedtch_resubmit_int(unsigned long data)
1da177e4 588{
48da7267
DS
589 struct speedtch_instance_data *instance = (void *)data;
590 struct urb *int_urb = instance->int_urb;
591 int ret;
1da177e4 592
48da7267 593 atm_dbg(instance->usbatm, "%s entered\n", __func__);
1da177e4 594
48da7267
DS
595 if (int_urb) {
596 ret = usb_submit_urb(int_urb, GFP_ATOMIC);
597 if (!ret)
c4028958 598 schedule_delayed_work(&instance->status_checker, 0);
48da7267
DS
599 else {
600 atm_dbg(instance->usbatm, "%s: usb_submit_urb failed with result %d\n", __func__, ret);
601 mod_timer(&instance->resubmit_timer, jiffies + msecs_to_jiffies(RESUBMIT_DELAY));
1da177e4 602 }
1da177e4 603 }
48da7267 604}
1da177e4 605
7d12e780 606static void speedtch_handle_int(struct urb *int_urb)
48da7267
DS
607{
608 struct speedtch_instance_data *instance = int_urb->context;
609 struct usbatm_data *usbatm = instance->usbatm;
610 unsigned int count = int_urb->actual_length;
9a5a3e95
GKH
611 int status = int_urb->status;
612 int ret;
1da177e4 613
48da7267 614 /* The magic interrupt for "up state" */
3c6bee1d 615 static const unsigned char up_int[6] = { 0xa1, 0x00, 0x01, 0x00, 0x00, 0x00 };
48da7267 616 /* The magic interrupt for "down state" */
3c6bee1d 617 static const unsigned char down_int[6] = { 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00 };
48da7267
DS
618
619 atm_dbg(usbatm, "%s entered\n", __func__);
1da177e4 620
9a5a3e95
GKH
621 if (status < 0) {
622 atm_dbg(usbatm, "%s: nonzero urb status %d!\n", __func__, status);
48da7267 623 goto fail;
1da177e4 624 }
1da177e4 625
48da7267
DS
626 if ((count == 6) && !memcmp(up_int, instance->int_data, 6)) {
627 del_timer(&instance->status_checker.timer);
52fbae2a 628 atm_info(usbatm, "DSL line goes up\n");
48da7267 629 } else if ((count == 6) && !memcmp(down_int, instance->int_data, 6)) {
52fbae2a 630 atm_info(usbatm, "DSL line goes down\n");
48da7267
DS
631 } else {
632 int i;
1da177e4 633
48da7267
DS
634 atm_dbg(usbatm, "%s: unknown interrupt packet of length %d:", __func__, count);
635 for (i = 0; i < count; i++)
636 printk(" %02x", instance->int_data[i]);
637 printk("\n");
638 goto fail;
639 }
1da177e4 640
48da7267
DS
641 if ((int_urb = instance->int_urb)) {
642 ret = usb_submit_urb(int_urb, GFP_ATOMIC);
c4028958 643 schedule_delayed_work(&instance->status_checker, 0);
1da177e4 644 if (ret < 0) {
48da7267
DS
645 atm_dbg(usbatm, "%s: usb_submit_urb failed with result %d\n", __func__, ret);
646 goto fail;
1da177e4
LT
647 }
648 }
1da177e4 649
1da177e4
LT
650 return;
651
48da7267
DS
652fail:
653 if ((int_urb = instance->int_urb))
654 mod_timer(&instance->resubmit_timer, jiffies + msecs_to_jiffies(RESUBMIT_DELAY));
1da177e4
LT
655}
656
48da7267 657static int speedtch_atm_start(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
1da177e4 658{
48da7267
DS
659 struct usb_device *usb_dev = usbatm->usb_dev;
660 struct speedtch_instance_data *instance = usbatm->driver_data;
661 int i, ret;
662 unsigned char mac_str[13];
1da177e4 663
48da7267 664 atm_dbg(usbatm, "%s entered\n", __func__);
1da177e4 665
48da7267
DS
666 /* Set MAC address, it is stored in the serial number */
667 memset(atm_dev->esi, 0, sizeof(atm_dev->esi));
668 if (usb_string(usb_dev, usb_dev->descriptor.iSerialNumber, mac_str, sizeof(mac_str)) == 12) {
669 for (i = 0; i < 6; i++)
96b89f32
AS
670 atm_dev->esi[i] = (hex_to_bin(mac_str[i * 2]) << 4) +
671 hex_to_bin(mac_str[i * 2 + 1]);
48da7267 672 }
1da177e4 673
48da7267
DS
674 /* Start modem synchronisation */
675 ret = speedtch_start_synchro(instance);
1da177e4 676
48da7267
DS
677 /* Set up interrupt endpoint */
678 if (instance->int_urb) {
679 ret = usb_submit_urb(instance->int_urb, GFP_KERNEL);
680 if (ret < 0) {
681 /* Doesn't matter; we'll poll anyway */
682 atm_dbg(usbatm, "%s: submission of interrupt URB failed (%d)!\n", __func__, ret);
683 usb_free_urb(instance->int_urb);
684 instance->int_urb = NULL;
1da177e4 685 }
1da177e4
LT
686 }
687
48da7267
DS
688 /* Start status polling */
689 mod_timer(&instance->status_checker.timer, jiffies + msecs_to_jiffies(1000));
1da177e4 690
1da177e4
LT
691 return 0;
692}
1da177e4 693
48da7267 694static void speedtch_atm_stop(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
1da177e4 695{
48da7267
DS
696 struct speedtch_instance_data *instance = usbatm->driver_data;
697 struct urb *int_urb = instance->int_urb;
698
699 atm_dbg(usbatm, "%s entered\n", __func__);
700
701 del_timer_sync(&instance->status_checker.timer);
702
703 /*
704 * Since resubmit_timer and int_urb can schedule themselves and
705 * each other, shutting them down correctly takes some care
706 */
707 instance->int_urb = NULL; /* signal shutdown */
708 mb();
709 usb_kill_urb(int_urb);
710 del_timer_sync(&instance->resubmit_timer);
711 /*
712 * At this point, speedtch_handle_int and speedtch_resubmit_int
713 * can run or be running, but instance->int_urb == NULL means that
714 * they will not reschedule
715 */
716 usb_kill_urb(int_urb);
717 del_timer_sync(&instance->resubmit_timer);
718 usb_free_urb(int_urb);
1da177e4 719
48da7267
DS
720 flush_scheduled_work();
721}
1da177e4 722
8fc7aeab
AS
723static int speedtch_pre_reset(struct usb_interface *intf)
724{
725 return 0;
726}
727
728static int speedtch_post_reset(struct usb_interface *intf)
729{
730 return 0;
731}
732
1da177e4 733
48da7267
DS
734/**********
735** USB **
736**********/
1da177e4 737
48da7267
DS
738static struct usb_device_id speedtch_usb_ids[] = {
739 {USB_DEVICE(0x06b9, 0x4061)},
740 {}
741};
1da177e4 742
48da7267 743MODULE_DEVICE_TABLE(usb, speedtch_usb_ids);
1da177e4 744
48da7267 745static int speedtch_usb_probe(struct usb_interface *, const struct usb_device_id *);
1da177e4 746
48da7267 747static struct usb_driver speedtch_usb_driver = {
48da7267
DS
748 .name = speedtch_driver_name,
749 .probe = speedtch_usb_probe,
750 .disconnect = usbatm_usb_disconnect,
8fc7aeab
AS
751 .pre_reset = speedtch_pre_reset,
752 .post_reset = speedtch_post_reset,
48da7267
DS
753 .id_table = speedtch_usb_ids
754};
1da177e4 755
6c4b7f70
NK
756static void speedtch_release_interfaces(struct usb_device *usb_dev,
757 int num_interfaces)
758{
48da7267
DS
759 struct usb_interface *cur_intf;
760 int i;
1da177e4 761
9196cc7b 762 for (i = 0; i < num_interfaces; i++)
48da7267
DS
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 }
1da177e4
LT
767}
768
48da7267
DS
769static int speedtch_bind(struct usbatm_data *usbatm,
770 struct usb_interface *intf,
35644b0c 771 const struct usb_device_id *id)
1da177e4 772{
48da7267 773 struct usb_device *usb_dev = interface_to_usbdev(intf);
80aae7a1 774 struct usb_interface *cur_intf, *data_intf;
48da7267
DS
775 struct speedtch_instance_data *instance;
776 int ifnum = intf->altsetting->desc.bInterfaceNumber;
777 int num_interfaces = usb_dev->actconfig->desc.bNumInterfaces;
778 int i, ret;
80aae7a1 779 int use_isoc;
1da177e4 780
48da7267 781 usb_dbg(usbatm, "%s entered\n", __func__);
1da177e4 782
0ec3c7e8
DS
783 /* sanity checks */
784
48da7267 785 if (usb_dev->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) {
0ec3c7e8 786 usb_err(usbatm, "%s: wrong device class %d\n", __func__, usb_dev->descriptor.bDeviceClass);
1da177e4
LT
787 return -ENODEV;
788 }
789
80aae7a1
DS
790 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
48da7267 795 /* claim all interfaces */
1da177e4 796
9196cc7b 797 for (i = 0; i < num_interfaces; i++) {
48da7267 798 cur_intf = usb_ifnum_to_if(usb_dev, i);
1da177e4 799
48da7267
DS
800 if ((i != ifnum) && cur_intf) {
801 ret = usb_driver_claim_interface(&speedtch_usb_driver, cur_intf, usbatm);
1da177e4 802
48da7267 803 if (ret < 0) {
0ec3c7e8 804 usb_err(usbatm, "%s: failed to claim interface %2d (%d)!\n", __func__, i, ret);
48da7267
DS
805 speedtch_release_interfaces(usb_dev, i);
806 return ret;
807 }
808 }
809 }
1da177e4 810
9a734efe 811 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
48da7267 812
1da177e4 813 if (!instance) {
0ec3c7e8 814 usb_err(usbatm, "%s: no memory for instance data!\n", __func__);
48da7267
DS
815 ret = -ENOMEM;
816 goto fail_release;
1da177e4
LT
817 }
818
48da7267 819 instance->usbatm = usbatm;
1da177e4 820
6a4f1b41
DS
821 /* 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);
80aae7a1 827 use_isoc = enable_isoc;
6f749475 828
6a4f1b41
DS
829 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 */
6f749475
DS
833 }
834
6a4f1b41 835 if (!instance->params.altsetting && use_isoc)
80aae7a1
DS
836 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 */
6f749475 839 }
80aae7a1
DS
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;
80aae7a1
DS
844
845 use_isoc = 0; /* fall back to bulk if endpoint not found */
846
9196cc7b 847 for (i = 0; i < desc->desc.bNumEndpoints; i++) {
80aae7a1
DS
848 const struct usb_endpoint_descriptor *endpoint_desc = &desc->endpoint[i].desc;
849
850 if ((endpoint_desc->bEndpointAddress == target_address)) {
c5dd1f94
LFC
851 use_isoc =
852 usb_endpoint_xfer_isoc(endpoint_desc);
80aae7a1
DS
853 break;
854 }
855 }
856
857 if (!use_isoc)
858 usb_info(usbatm, "isochronous transfer not supported - using bulk\n");
6f749475
DS
859 }
860
6a4f1b41 861 if (!use_isoc && !instance->params.altsetting)
80aae7a1
DS
862 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);
864 goto fail_free;
865 }
866
6a4f1b41
DS
867 if (!instance->params.altsetting)
868 instance->params.altsetting = use_isoc ? DEFAULT_ISOC_ALTSETTING : DEFAULT_BULK_ALTSETTING;
80aae7a1
DS
869
870 usbatm->flags |= (use_isoc ? UDSL_USE_ISOC : 0);
871
c4028958 872 INIT_DELAYED_WORK(&instance->status_checker, speedtch_check_status);
1da177e4 873
48da7267
DS
874 instance->status_checker.timer.function = speedtch_status_poll;
875 instance->status_checker.timer.data = (unsigned long)instance;
1a7aad15 876 instance->last_status = 0xff;
48da7267 877 instance->poll_delay = MIN_POLL_DELAY;
1da177e4 878
48da7267
DS
879 init_timer(&instance->resubmit_timer);
880 instance->resubmit_timer.function = speedtch_resubmit_int;
881 instance->resubmit_timer.data = (unsigned long)instance;
1da177e4 882
48da7267 883 instance->int_urb = usb_alloc_urb(0, GFP_KERNEL);
1da177e4 884
48da7267
DS
885 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__);
1da177e4 892
48da7267
DS
893 /* 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);
1da177e4 897
80aae7a1 898 usbatm->flags |= (ret == SIZE_7 ? UDSL_SKIP_HEAVY_INIT : 0);
1da177e4 899
35644b0c 900 usb_dbg(usbatm, "%s: firmware %s loaded\n", __func__, usbatm->flags & UDSL_SKIP_HEAVY_INIT ? "already" : "not");
48da7267 901
35644b0c 902 if (!(usbatm->flags & UDSL_SKIP_HEAVY_INIT))
0ec3c7e8
DS
903 if ((ret = usb_reset_device(usb_dev)) < 0) {
904 usb_err(usbatm, "%s: device reset failed (%d)!\n", __func__, ret);
48da7267 905 goto fail_free;
0ec3c7e8 906 }
1da177e4 907
48da7267 908 usbatm->driver_data = instance;
1da177e4
LT
909
910 return 0;
911
48da7267
DS
912fail_free:
913 usb_free_urb(instance->int_urb);
1da177e4 914 kfree(instance);
48da7267
DS
915fail_release:
916 speedtch_release_interfaces(usb_dev, num_interfaces);
917 return ret;
1da177e4
LT
918}
919
48da7267 920static void speedtch_unbind(struct usbatm_data *usbatm, struct usb_interface *intf)
1da177e4 921{
48da7267
DS
922 struct usb_device *usb_dev = interface_to_usbdev(intf);
923 struct speedtch_instance_data *instance = usbatm->driver_data;
1da177e4 924
48da7267 925 usb_dbg(usbatm, "%s entered\n", __func__);
1da177e4 926
48da7267
DS
927 speedtch_release_interfaces(usb_dev, usb_dev->actconfig->desc.bNumInterfaces);
928 usb_free_urb(instance->int_urb);
929 kfree(instance);
1da177e4
LT
930}
931
48da7267 932
1da177e4
LT
933/***********
934** init **
935***********/
936
48da7267 937static struct usbatm_driver speedtch_usbatm_driver = {
48da7267
DS
938 .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,
80aae7a1
DS
944 .bulk_in = ENDPOINT_BULK_DATA,
945 .bulk_out = ENDPOINT_BULK_DATA,
946 .isoc_in = ENDPOINT_ISOC_DATA
48da7267
DS
947};
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
1da177e4
LT
954static int __init speedtch_usb_init(void)
955{
48da7267 956 dbg("%s: driver version %s", __func__, DRIVER_VERSION);
1da177e4
LT
957
958 return usb_register(&speedtch_usb_driver);
959}
960
961static void __exit speedtch_usb_cleanup(void)
962{
48da7267 963 dbg("%s", __func__);
1da177e4
LT
964
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);