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