xref: /openbmc/linux/drivers/usb/atm/speedtch.c (revision 7211ec63)
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  *
8  *  Based on "modem_run.c", copyright (C) 2001, Benoit Papillault
9  *
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 
26 #include <asm/page.h>
27 #include <linux/device.h>
28 #include <linux/errno.h>
29 #include <linux/firmware.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/moduleparam.h>
33 #include <linux/slab.h>
34 #include <linux/stat.h>
35 #include <linux/timer.h>
36 #include <linux/types.h>
37 #include <linux/usb/ch9.h>
38 #include <linux/workqueue.h>
39 
40 #include "usbatm.h"
41 
42 #define DRIVER_AUTHOR	"Johan Verrept, Duncan Sands <duncan.sands@free.fr>"
43 #define DRIVER_DESC	"Alcatel SpeedTouch USB driver"
44 
45 static const char speedtch_driver_name[] = "speedtch";
46 
47 #define CTRL_TIMEOUT 2000	/* milliseconds */
48 #define DATA_TIMEOUT 2000	/* milliseconds */
49 
50 #define OFFSET_7	0		/* size 1 */
51 #define OFFSET_b	1		/* size 8 */
52 #define OFFSET_d	9		/* size 4 */
53 #define OFFSET_e	13		/* size 1 */
54 #define OFFSET_f	14		/* size 1 */
55 
56 #define SIZE_7		1
57 #define SIZE_b		8
58 #define SIZE_d		4
59 #define SIZE_e		1
60 #define SIZE_f		1
61 
62 #define MIN_POLL_DELAY		5000	/* milliseconds */
63 #define MAX_POLL_DELAY		60000	/* milliseconds */
64 
65 #define RESUBMIT_DELAY		1000	/* milliseconds */
66 
67 #define DEFAULT_BULK_ALTSETTING	1
68 #define DEFAULT_ISOC_ALTSETTING	3
69 #define DEFAULT_DL_512_FIRST	0
70 #define DEFAULT_ENABLE_ISOC	0
71 #define DEFAULT_SW_BUFFERING	0
72 
73 static unsigned int altsetting = 0; /* zero means: use the default */
74 static bool dl_512_first = DEFAULT_DL_512_FIRST;
75 static bool enable_isoc = DEFAULT_ENABLE_ISOC;
76 static bool sw_buffering = DEFAULT_SW_BUFFERING;
77 
78 #define DEFAULT_B_MAX_DSL	8128
79 #define DEFAULT_MODEM_MODE	11
80 #define MODEM_OPTION_LENGTH	16
81 static const unsigned char DEFAULT_MODEM_OPTION[MODEM_OPTION_LENGTH] = {
82 	0x10, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
83 };
84 
85 static unsigned int BMaxDSL = DEFAULT_B_MAX_DSL;
86 static unsigned char ModemMode = DEFAULT_MODEM_MODE;
87 static unsigned char ModemOption[MODEM_OPTION_LENGTH];
88 static unsigned int num_ModemOption;
89 
90 module_param(altsetting, uint, S_IRUGO | S_IWUSR);
91 MODULE_PARM_DESC(altsetting,
92 		"Alternative setting for data interface (bulk_default: "
93 		__MODULE_STRING(DEFAULT_BULK_ALTSETTING) "; isoc_default: "
94 		__MODULE_STRING(DEFAULT_ISOC_ALTSETTING) ")");
95 
96 module_param(dl_512_first, bool, S_IRUGO | S_IWUSR);
97 MODULE_PARM_DESC(dl_512_first,
98 		 "Read 512 bytes before sending firmware (default: "
99 		 __MODULE_STRING(DEFAULT_DL_512_FIRST) ")");
100 
101 module_param(enable_isoc, bool, S_IRUGO | S_IWUSR);
102 MODULE_PARM_DESC(enable_isoc,
103 		"Use isochronous transfers if available (default: "
104 		__MODULE_STRING(DEFAULT_ENABLE_ISOC) ")");
105 
106 module_param(sw_buffering, bool, S_IRUGO | S_IWUSR);
107 MODULE_PARM_DESC(sw_buffering,
108 		 "Enable software buffering (default: "
109 		 __MODULE_STRING(DEFAULT_SW_BUFFERING) ")");
110 
111 module_param(BMaxDSL, uint, S_IRUGO | S_IWUSR);
112 MODULE_PARM_DESC(BMaxDSL,
113 		"default: " __MODULE_STRING(DEFAULT_B_MAX_DSL));
114 
115 module_param(ModemMode, byte, S_IRUGO | S_IWUSR);
116 MODULE_PARM_DESC(ModemMode,
117 		"default: " __MODULE_STRING(DEFAULT_MODEM_MODE));
118 
119 module_param_array(ModemOption, byte, &num_ModemOption, S_IRUGO);
120 MODULE_PARM_DESC(ModemOption, "default: 0x10,0x00,0x00,0x00,0x20");
121 
122 #define INTERFACE_DATA		1
123 #define ENDPOINT_INT		0x81
124 #define ENDPOINT_BULK_DATA	0x07
125 #define ENDPOINT_ISOC_DATA	0x07
126 #define ENDPOINT_FIRMWARE	0x05
127 
128 struct speedtch_params {
129 	unsigned int altsetting;
130 	unsigned int BMaxDSL;
131 	unsigned char ModemMode;
132 	unsigned char ModemOption[MODEM_OPTION_LENGTH];
133 };
134 
135 struct speedtch_instance_data {
136 	struct usbatm_data *usbatm;
137 
138 	struct speedtch_params params; /* set in probe, constant afterwards */
139 
140 	struct timer_list status_check_timer;
141 	struct work_struct status_check_work;
142 
143 	unsigned char last_status;
144 
145 	int poll_delay; /* milliseconds */
146 
147 	struct timer_list resubmit_timer;
148 	struct urb *int_urb;
149 	unsigned char int_data[16];
150 
151 	unsigned char scratch_buffer[16];
152 };
153 
154 /***************
155 **  firmware  **
156 ***************/
157 
158 static void speedtch_set_swbuff(struct speedtch_instance_data *instance, int state)
159 {
160 	struct usbatm_data *usbatm = instance->usbatm;
161 	struct usb_device *usb_dev = usbatm->usb_dev;
162 	int ret;
163 
164 	ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
165 			      0x32, 0x40, state ? 0x01 : 0x00, 0x00, NULL, 0, CTRL_TIMEOUT);
166 	if (ret < 0)
167 		usb_warn(usbatm,
168 			 "%sabling SW buffering: usb_control_msg returned %d\n",
169 			 state ? "En" : "Dis", ret);
170 	else
171 		usb_dbg(usbatm, "speedtch_set_swbuff: %sbled SW buffering\n", state ? "En" : "Dis");
172 }
173 
174 static void speedtch_test_sequence(struct speedtch_instance_data *instance)
175 {
176 	struct usbatm_data *usbatm = instance->usbatm;
177 	struct usb_device *usb_dev = usbatm->usb_dev;
178 	unsigned char *buf = instance->scratch_buffer;
179 	int ret;
180 
181 	/* URB 147 */
182 	buf[0] = 0x1c;
183 	buf[1] = 0x50;
184 	ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
185 			      0x01, 0x40, 0x0b, 0x00, buf, 2, CTRL_TIMEOUT);
186 	if (ret < 0)
187 		usb_warn(usbatm, "%s failed on URB147: %d\n", __func__, ret);
188 
189 	/* URB 148 */
190 	buf[0] = 0x32;
191 	buf[1] = 0x00;
192 	ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
193 			      0x01, 0x40, 0x02, 0x00, buf, 2, CTRL_TIMEOUT);
194 	if (ret < 0)
195 		usb_warn(usbatm, "%s failed on URB148: %d\n", __func__, ret);
196 
197 	/* URB 149 */
198 	buf[0] = 0x01;
199 	buf[1] = 0x00;
200 	buf[2] = 0x01;
201 	ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
202 			      0x01, 0x40, 0x03, 0x00, buf, 3, CTRL_TIMEOUT);
203 	if (ret < 0)
204 		usb_warn(usbatm, "%s failed on URB149: %d\n", __func__, ret);
205 
206 	/* URB 150 */
207 	buf[0] = 0x01;
208 	buf[1] = 0x00;
209 	buf[2] = 0x01;
210 	ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
211 			      0x01, 0x40, 0x04, 0x00, buf, 3, CTRL_TIMEOUT);
212 	if (ret < 0)
213 		usb_warn(usbatm, "%s failed on URB150: %d\n", __func__, ret);
214 
215 	/* Extra initialisation in recent drivers - gives higher speeds */
216 
217 	/* URBext1 */
218 	buf[0] = instance->params.ModemMode;
219 	ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
220 			      0x01, 0x40, 0x11, 0x00, buf, 1, CTRL_TIMEOUT);
221 	if (ret < 0)
222 		usb_warn(usbatm, "%s failed on URBext1: %d\n", __func__, ret);
223 
224 	/* URBext2 */
225 	/* This seems to be the one which actually triggers the higher sync
226 	   rate -- it does require the new firmware too, although it works OK
227 	   with older firmware */
228 	ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
229 			      0x01, 0x40, 0x14, 0x00,
230 			      instance->params.ModemOption,
231 			      MODEM_OPTION_LENGTH, CTRL_TIMEOUT);
232 	if (ret < 0)
233 		usb_warn(usbatm, "%s failed on URBext2: %d\n", __func__, ret);
234 
235 	/* URBext3 */
236 	buf[0] = instance->params.BMaxDSL & 0xff;
237 	buf[1] = instance->params.BMaxDSL >> 8;
238 	ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
239 			      0x01, 0x40, 0x12, 0x00, buf, 2, CTRL_TIMEOUT);
240 	if (ret < 0)
241 		usb_warn(usbatm, "%s failed on URBext3: %d\n", __func__, ret);
242 }
243 
244 static int speedtch_upload_firmware(struct speedtch_instance_data *instance,
245 				     const struct firmware *fw1,
246 				     const struct firmware *fw2)
247 {
248 	unsigned char *buffer;
249 	struct usbatm_data *usbatm = instance->usbatm;
250 	struct usb_device *usb_dev = usbatm->usb_dev;
251 	int actual_length;
252 	int ret = 0;
253 	int offset;
254 
255 	usb_dbg(usbatm, "%s entered\n", __func__);
256 
257 	buffer = (unsigned char *)__get_free_page(GFP_KERNEL);
258 	if (!buffer) {
259 		ret = -ENOMEM;
260 		usb_dbg(usbatm, "%s: no memory for buffer!\n", __func__);
261 		goto out;
262 	}
263 
264 	if (!usb_ifnum_to_if(usb_dev, 2)) {
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)
276 			usb_warn(usbatm, "%s: read BLOCK0 from modem failed (%d)!\n", __func__, ret);
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) {
290 			usb_err(usbatm, "%s: write BLOCK1 to modem failed (%d)!\n", __func__, ret);
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);
301 
302 	if (ret < 0) {
303 		usb_err(usbatm, "%s: read BLOCK2 from modem failed (%d)!\n", __func__, ret);
304 		goto out_free;
305 	}
306 	usb_dbg(usbatm, "%s: BLOCK2 downloaded (%d bytes)\n", __func__, actual_length);
307 
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) {
317 			usb_err(usbatm, "%s: write BLOCK3 to modem failed (%d)!\n", __func__, ret);
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) {
330 		usb_err(usbatm, "%s: read BLOCK4 from modem failed (%d)!\n", __func__, ret);
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 
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);
343 		goto out_free;
344 	}
345 
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 
355 out_free:
356 	free_page((unsigned long)buffer);
357 out:
358 	return ret;
359 }
360 
361 static int speedtch_find_firmware(struct usbatm_data *usbatm, struct usb_interface *intf,
362 				  int phase, const struct firmware **fw_p)
363 {
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];
369 
370 	sprintf(buf, "speedtch-%d.bin.%x.%02x", phase, major_revision, minor_revision);
371 	usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
372 
373 	if (request_firmware(fw_p, buf, dev)) {
374 		sprintf(buf, "speedtch-%d.bin.%x", phase, major_revision);
375 		usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
376 
377 		if (request_firmware(fw_p, buf, dev)) {
378 			sprintf(buf, "speedtch-%d.bin", phase);
379 			usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
380 
381 			if (request_firmware(fw_p, buf, dev)) {
382 				usb_err(usbatm, "%s: no stage %d firmware found!\n", __func__, phase);
383 				return -ENOENT;
384 			}
385 		}
386 	}
387 
388 	usb_info(usbatm, "found stage %d firmware %s\n", phase, buf);
389 
390 	return 0;
391 }
392 
393 static 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 
399 	if ((ret = speedtch_find_firmware(usbatm, intf, 1, &fw1)) < 0)
400 		return ret;
401 
402 	if ((ret = speedtch_find_firmware(usbatm, intf, 2, &fw2)) < 0) {
403 		release_firmware(fw1);
404 		return ret;
405 	}
406 
407 	if ((ret = speedtch_upload_firmware(instance, fw1, fw2)) < 0)
408 		usb_err(usbatm, "%s: firmware upload failed (%d)!\n", __func__, ret);
409 
410 	release_firmware(fw2);
411 	release_firmware(fw1);
412 
413 	return ret;
414 }
415 
416 
417 /**********
418 **  ATM  **
419 **********/
420 
421 static int speedtch_read_status(struct speedtch_instance_data *instance)
422 {
423 	struct usbatm_data *usbatm = instance->usbatm;
424 	struct usb_device *usb_dev = usbatm->usb_dev;
425 	unsigned char *buf = instance->scratch_buffer;
426 	int ret;
427 
428 	memset(buf, 0, 16);
429 
430 	ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
431 			      0x12, 0xc0, 0x07, 0x00, buf + OFFSET_7, SIZE_7,
432 			      CTRL_TIMEOUT);
433 	if (ret < 0) {
434 		atm_dbg(usbatm, "%s: MSG 7 failed\n", __func__);
435 		return ret;
436 	}
437 
438 	ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
439 			      0x12, 0xc0, 0x0b, 0x00, buf + OFFSET_b, SIZE_b,
440 			      CTRL_TIMEOUT);
441 	if (ret < 0) {
442 		atm_dbg(usbatm, "%s: MSG B failed\n", __func__);
443 		return ret;
444 	}
445 
446 	ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
447 			      0x12, 0xc0, 0x0d, 0x00, buf + OFFSET_d, SIZE_d,
448 			      CTRL_TIMEOUT);
449 	if (ret < 0) {
450 		atm_dbg(usbatm, "%s: MSG D failed\n", __func__);
451 		return ret;
452 	}
453 
454 	ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
455 			      0x01, 0xc0, 0x0e, 0x00, buf + OFFSET_e, SIZE_e,
456 			      CTRL_TIMEOUT);
457 	if (ret < 0) {
458 		atm_dbg(usbatm, "%s: MSG E failed\n", __func__);
459 		return ret;
460 	}
461 
462 	ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
463 			      0x01, 0xc0, 0x0f, 0x00, buf + OFFSET_f, SIZE_f,
464 			      CTRL_TIMEOUT);
465 	if (ret < 0) {
466 		atm_dbg(usbatm, "%s: MSG F failed\n", __func__);
467 		return ret;
468 	}
469 
470 	return 0;
471 }
472 
473 static int speedtch_start_synchro(struct speedtch_instance_data *instance)
474 {
475 	struct usbatm_data *usbatm = instance->usbatm;
476 	struct usb_device *usb_dev = usbatm->usb_dev;
477 	unsigned char *buf = instance->scratch_buffer;
478 	int ret;
479 
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 
497 static void speedtch_check_status(struct work_struct *work)
498 {
499 	struct speedtch_instance_data *instance =
500 		container_of(work, struct speedtch_instance_data,
501 			     status_check_work);
502 	struct usbatm_data *usbatm = instance->usbatm;
503 	struct atm_dev *atm_dev = usbatm->atm_dev;
504 	unsigned char *buf = instance->scratch_buffer;
505 	int down_speed, up_speed, ret;
506 	unsigned char status;
507 
508 #ifdef VERBOSE_DEBUG
509 	atm_dbg(usbatm, "%s entered\n", __func__);
510 #endif
511 
512 	ret = speedtch_read_status(instance);
513 	if (ret < 0) {
514 		atm_warn(usbatm, "error %d fetching device status\n", ret);
515 		instance->poll_delay = min(2 * instance->poll_delay, MAX_POLL_DELAY);
516 		return;
517 	}
518 
519 	instance->poll_delay = max(instance->poll_delay / 2, MIN_POLL_DELAY);
520 
521 	status = buf[OFFSET_7];
522 
523 	if ((status != instance->last_status) || !status) {
524 		atm_dbg(usbatm, "%s: line state 0x%02x\n", __func__, status);
525 
526 		switch (status) {
527 		case 0:
528 			atm_dev_signal_change(atm_dev, ATM_PHY_SIG_LOST);
529 			if (instance->last_status)
530 				atm_info(usbatm, "ADSL line is down\n");
531 			/* It may never resync again unless we ask it to... */
532 			ret = speedtch_start_synchro(instance);
533 			break;
534 
535 		case 0x08:
536 			atm_dev_signal_change(atm_dev, ATM_PHY_SIG_UNKNOWN);
537 			atm_info(usbatm, "ADSL line is blocked?\n");
538 			break;
539 
540 		case 0x10:
541 			atm_dev_signal_change(atm_dev, ATM_PHY_SIG_LOST);
542 			atm_info(usbatm, "ADSL line is synchronising\n");
543 			break;
544 
545 		case 0x20:
546 			down_speed = buf[OFFSET_b] | (buf[OFFSET_b + 1] << 8)
547 				| (buf[OFFSET_b + 2] << 16) | (buf[OFFSET_b + 3] << 24);
548 			up_speed = buf[OFFSET_b + 4] | (buf[OFFSET_b + 5] << 8)
549 				| (buf[OFFSET_b + 6] << 16) | (buf[OFFSET_b + 7] << 24);
550 
551 			if (!(down_speed & 0x0000ffff) && !(up_speed & 0x0000ffff)) {
552 				down_speed >>= 16;
553 				up_speed >>= 16;
554 			}
555 
556 			atm_dev->link_rate = down_speed * 1000 / 424;
557 			atm_dev_signal_change(atm_dev, ATM_PHY_SIG_FOUND);
558 
559 			atm_info(usbatm,
560 				 "ADSL line is up (%d kb/s down | %d kb/s up)\n",
561 				 down_speed, up_speed);
562 			break;
563 
564 		default:
565 			atm_dev_signal_change(atm_dev, ATM_PHY_SIG_UNKNOWN);
566 			atm_info(usbatm, "unknown line state %02x\n", status);
567 			break;
568 		}
569 
570 		instance->last_status = status;
571 	}
572 }
573 
574 static void speedtch_status_poll(unsigned long data)
575 {
576 	struct speedtch_instance_data *instance = (void *)data;
577 
578 	schedule_work(&instance->status_check_work);
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_check_timer, jiffies + msecs_to_jiffies(instance->poll_delay));
583 	else
584 		atm_warn(instance->usbatm, "Too many failures - disabling line status polling\n");
585 }
586 
587 static void speedtch_resubmit_int(unsigned long data)
588 {
589 	struct speedtch_instance_data *instance = (void *)data;
590 	struct urb *int_urb = instance->int_urb;
591 	int ret;
592 
593 	atm_dbg(instance->usbatm, "%s entered\n", __func__);
594 
595 	if (int_urb) {
596 		ret = usb_submit_urb(int_urb, GFP_ATOMIC);
597 		if (!ret)
598 			schedule_work(&instance->status_check_work);
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));
602 		}
603 	}
604 }
605 
606 static void speedtch_handle_int(struct urb *int_urb)
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;
611 	int status = int_urb->status;
612 	int ret;
613 
614 	/* The magic interrupt for "up state" */
615 	static const unsigned char up_int[6]   = { 0xa1, 0x00, 0x01, 0x00, 0x00, 0x00 };
616 	/* The magic interrupt for "down state" */
617 	static const unsigned char down_int[6] = { 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00 };
618 
619 	atm_dbg(usbatm, "%s entered\n", __func__);
620 
621 	if (status < 0) {
622 		atm_dbg(usbatm, "%s: nonzero urb status %d!\n", __func__, status);
623 		goto fail;
624 	}
625 
626 	if ((count == 6) && !memcmp(up_int, instance->int_data, 6)) {
627 		del_timer(&instance->status_check_timer);
628 		atm_info(usbatm, "DSL line goes up\n");
629 	} else if ((count == 6) && !memcmp(down_int, instance->int_data, 6)) {
630 		atm_info(usbatm, "DSL line goes down\n");
631 	} else {
632 		int i;
633 
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 	}
640 
641 	int_urb = instance->int_urb;
642 	if (int_urb) {
643 		ret = usb_submit_urb(int_urb, GFP_ATOMIC);
644 		schedule_work(&instance->status_check_work);
645 		if (ret < 0) {
646 			atm_dbg(usbatm, "%s: usb_submit_urb failed with result %d\n", __func__, ret);
647 			goto fail;
648 		}
649 	}
650 
651 	return;
652 
653 fail:
654 	int_urb = instance->int_urb;
655 	if (int_urb)
656 		mod_timer(&instance->resubmit_timer, jiffies + msecs_to_jiffies(RESUBMIT_DELAY));
657 }
658 
659 static int speedtch_atm_start(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
660 {
661 	struct usb_device *usb_dev = usbatm->usb_dev;
662 	struct speedtch_instance_data *instance = usbatm->driver_data;
663 	int i, ret;
664 	unsigned char mac_str[13];
665 
666 	atm_dbg(usbatm, "%s entered\n", __func__);
667 
668 	/* Set MAC address, it is stored in the serial number */
669 	memset(atm_dev->esi, 0, sizeof(atm_dev->esi));
670 	if (usb_string(usb_dev, usb_dev->descriptor.iSerialNumber, mac_str, sizeof(mac_str)) == 12) {
671 		for (i = 0; i < 6; i++)
672 			atm_dev->esi[i] = (hex_to_bin(mac_str[i * 2]) << 4) +
673 				hex_to_bin(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_check_timer, jiffies + msecs_to_jiffies(1000));
692 
693 	return 0;
694 }
695 
696 static 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_check_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_work(&instance->status_check_work);
723 }
724 
725 static int speedtch_pre_reset(struct usb_interface *intf)
726 {
727 	return 0;
728 }
729 
730 static int speedtch_post_reset(struct usb_interface *intf)
731 {
732 	return 0;
733 }
734 
735 
736 /**********
737 **  USB  **
738 **********/
739 
740 static const struct usb_device_id speedtch_usb_ids[] = {
741 	{USB_DEVICE(0x06b9, 0x4061)},
742 	{}
743 };
744 
745 MODULE_DEVICE_TABLE(usb, speedtch_usb_ids);
746 
747 static int speedtch_usb_probe(struct usb_interface *, const struct usb_device_id *);
748 
749 static struct usb_driver speedtch_usb_driver = {
750 	.name		= speedtch_driver_name,
751 	.probe		= speedtch_usb_probe,
752 	.disconnect	= usbatm_usb_disconnect,
753 	.pre_reset	= speedtch_pre_reset,
754 	.post_reset	= speedtch_post_reset,
755 	.id_table	= speedtch_usb_ids
756 };
757 
758 static void speedtch_release_interfaces(struct usb_device *usb_dev,
759 					int num_interfaces)
760 {
761 	struct usb_interface *cur_intf;
762 	int i;
763 
764 	for (i = 0; i < num_interfaces; i++) {
765 		cur_intf = usb_ifnum_to_if(usb_dev, i);
766 		if (cur_intf) {
767 			usb_set_intfdata(cur_intf, NULL);
768 			usb_driver_release_interface(&speedtch_usb_driver, cur_intf);
769 		}
770 	}
771 }
772 
773 static int speedtch_bind(struct usbatm_data *usbatm,
774 			 struct usb_interface *intf,
775 			 const struct usb_device_id *id)
776 {
777 	struct usb_device *usb_dev = interface_to_usbdev(intf);
778 	struct usb_interface *cur_intf, *data_intf;
779 	struct speedtch_instance_data *instance;
780 	int ifnum = intf->altsetting->desc.bInterfaceNumber;
781 	int num_interfaces = usb_dev->actconfig->desc.bNumInterfaces;
782 	int i, ret;
783 	int use_isoc;
784 
785 	usb_dbg(usbatm, "%s entered\n", __func__);
786 
787 	/* sanity checks */
788 
789 	if (usb_dev->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) {
790 		usb_err(usbatm, "%s: wrong device class %d\n", __func__, usb_dev->descriptor.bDeviceClass);
791 		return -ENODEV;
792 	}
793 
794 	data_intf = usb_ifnum_to_if(usb_dev, INTERFACE_DATA);
795 	if (!data_intf) {
796 		usb_err(usbatm, "%s: data interface not found!\n", __func__);
797 		return -ENODEV;
798 	}
799 
800 	/* claim all interfaces */
801 
802 	for (i = 0; i < num_interfaces; i++) {
803 		cur_intf = usb_ifnum_to_if(usb_dev, i);
804 
805 		if ((i != ifnum) && cur_intf) {
806 			ret = usb_driver_claim_interface(&speedtch_usb_driver, cur_intf, usbatm);
807 
808 			if (ret < 0) {
809 				usb_err(usbatm, "%s: failed to claim interface %2d (%d)!\n", __func__, i, ret);
810 				speedtch_release_interfaces(usb_dev, i);
811 				return ret;
812 			}
813 		}
814 	}
815 
816 	instance = kzalloc(sizeof(*instance), GFP_KERNEL);
817 
818 	if (!instance) {
819 		ret = -ENOMEM;
820 		goto fail_release;
821 	}
822 
823 	instance->usbatm = usbatm;
824 
825 	/* module parameters may change at any moment, so take a snapshot */
826 	instance->params.altsetting = altsetting;
827 	instance->params.BMaxDSL = BMaxDSL;
828 	instance->params.ModemMode = ModemMode;
829 	memcpy(instance->params.ModemOption, DEFAULT_MODEM_OPTION, MODEM_OPTION_LENGTH);
830 	memcpy(instance->params.ModemOption, ModemOption, num_ModemOption);
831 	use_isoc = enable_isoc;
832 
833 	if (instance->params.altsetting)
834 		if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, instance->params.altsetting)) < 0) {
835 			usb_err(usbatm, "%s: setting interface to %2d failed (%d)!\n", __func__, instance->params.altsetting, ret);
836 			instance->params.altsetting = 0; /* fall back to default */
837 		}
838 
839 	if (!instance->params.altsetting && use_isoc)
840 		if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, DEFAULT_ISOC_ALTSETTING)) < 0) {
841 			usb_dbg(usbatm, "%s: setting interface to %2d failed (%d)!\n", __func__, DEFAULT_ISOC_ALTSETTING, ret);
842 			use_isoc = 0; /* fall back to bulk */
843 		}
844 
845 	if (use_isoc) {
846 		const struct usb_host_interface *desc = data_intf->cur_altsetting;
847 		const __u8 target_address = USB_DIR_IN | usbatm->driver->isoc_in;
848 
849 		use_isoc = 0; /* fall back to bulk if endpoint not found */
850 
851 		for (i = 0; i < desc->desc.bNumEndpoints; i++) {
852 			const struct usb_endpoint_descriptor *endpoint_desc = &desc->endpoint[i].desc;
853 
854 			if ((endpoint_desc->bEndpointAddress == target_address)) {
855 				use_isoc =
856 					usb_endpoint_xfer_isoc(endpoint_desc);
857 				break;
858 			}
859 		}
860 
861 		if (!use_isoc)
862 			usb_info(usbatm, "isochronous transfer not supported - using bulk\n");
863 	}
864 
865 	if (!use_isoc && !instance->params.altsetting)
866 		if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, DEFAULT_BULK_ALTSETTING)) < 0) {
867 			usb_err(usbatm, "%s: setting interface to %2d failed (%d)!\n", __func__, DEFAULT_BULK_ALTSETTING, ret);
868 			goto fail_free;
869 		}
870 
871 	if (!instance->params.altsetting)
872 		instance->params.altsetting = use_isoc ? DEFAULT_ISOC_ALTSETTING : DEFAULT_BULK_ALTSETTING;
873 
874 	usbatm->flags |= (use_isoc ? UDSL_USE_ISOC : 0);
875 
876 	INIT_WORK(&instance->status_check_work, speedtch_check_status);
877 	init_timer(&instance->status_check_timer);
878 
879 	instance->status_check_timer.function = speedtch_status_poll;
880 	instance->status_check_timer.data = (unsigned long)instance;
881 	instance->last_status = 0xff;
882 	instance->poll_delay = MIN_POLL_DELAY;
883 
884 	init_timer(&instance->resubmit_timer);
885 	instance->resubmit_timer.function = speedtch_resubmit_int;
886 	instance->resubmit_timer.data = (unsigned long)instance;
887 
888 	instance->int_urb = usb_alloc_urb(0, GFP_KERNEL);
889 
890 	if (instance->int_urb)
891 		usb_fill_int_urb(instance->int_urb, usb_dev,
892 				 usb_rcvintpipe(usb_dev, ENDPOINT_INT),
893 				 instance->int_data, sizeof(instance->int_data),
894 				 speedtch_handle_int, instance, 16);
895 	else
896 		usb_dbg(usbatm, "%s: no memory for interrupt urb!\n", __func__);
897 
898 	/* check whether the modem already seems to be alive */
899 	ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
900 			      0x12, 0xc0, 0x07, 0x00,
901 			      instance->scratch_buffer + OFFSET_7, SIZE_7, 500);
902 
903 	usbatm->flags |= (ret == SIZE_7 ? UDSL_SKIP_HEAVY_INIT : 0);
904 
905 	usb_dbg(usbatm, "%s: firmware %s loaded\n", __func__, usbatm->flags & UDSL_SKIP_HEAVY_INIT ? "already" : "not");
906 
907 	if (!(usbatm->flags & UDSL_SKIP_HEAVY_INIT))
908 		if ((ret = usb_reset_device(usb_dev)) < 0) {
909 			usb_err(usbatm, "%s: device reset failed (%d)!\n", __func__, ret);
910 			goto fail_free;
911 		}
912 
913         usbatm->driver_data = instance;
914 
915 	return 0;
916 
917 fail_free:
918 	usb_free_urb(instance->int_urb);
919 	kfree(instance);
920 fail_release:
921 	speedtch_release_interfaces(usb_dev, num_interfaces);
922 	return ret;
923 }
924 
925 static void speedtch_unbind(struct usbatm_data *usbatm, struct usb_interface *intf)
926 {
927 	struct usb_device *usb_dev = interface_to_usbdev(intf);
928 	struct speedtch_instance_data *instance = usbatm->driver_data;
929 
930 	usb_dbg(usbatm, "%s entered\n", __func__);
931 
932 	speedtch_release_interfaces(usb_dev, usb_dev->actconfig->desc.bNumInterfaces);
933 	usb_free_urb(instance->int_urb);
934 	kfree(instance);
935 }
936 
937 
938 /***********
939 **  init  **
940 ***********/
941 
942 static struct usbatm_driver speedtch_usbatm_driver = {
943 	.driver_name	= speedtch_driver_name,
944 	.bind		= speedtch_bind,
945 	.heavy_init	= speedtch_heavy_init,
946 	.unbind		= speedtch_unbind,
947 	.atm_start	= speedtch_atm_start,
948 	.atm_stop	= speedtch_atm_stop,
949 	.bulk_in	= ENDPOINT_BULK_DATA,
950 	.bulk_out	= ENDPOINT_BULK_DATA,
951 	.isoc_in	= ENDPOINT_ISOC_DATA
952 };
953 
954 static int speedtch_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
955 {
956 	return usbatm_usb_probe(intf, id, &speedtch_usbatm_driver);
957 }
958 
959 module_usb_driver(speedtch_usb_driver);
960 
961 MODULE_AUTHOR(DRIVER_AUTHOR);
962 MODULE_DESCRIPTION(DRIVER_DESC);
963 MODULE_LICENSE("GPL");
964