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