1 // SPDX-License-Identifier: GPL-2.0+
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  *
9  *  Based on "modem_run.c", copyright (C) 2001, Benoit Papillault
10  ******************************************************************************/
11 
12 #include <asm/page.h>
13 #include <linux/device.h>
14 #include <linux/errno.h>
15 #include <linux/firmware.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/slab.h>
20 #include <linux/stat.h>
21 #include <linux/timer.h>
22 #include <linux/types.h>
23 #include <linux/usb/ch9.h>
24 #include <linux/workqueue.h>
25 
26 #include "usbatm.h"
27 
28 #define DRIVER_AUTHOR	"Johan Verrept, Duncan Sands <duncan.sands@free.fr>"
29 #define DRIVER_DESC	"Alcatel SpeedTouch USB driver"
30 
31 static const char speedtch_driver_name[] = "speedtch";
32 
33 #define CTRL_TIMEOUT 2000	/* milliseconds */
34 #define DATA_TIMEOUT 2000	/* milliseconds */
35 
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 */
41 
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
47 
48 #define MIN_POLL_DELAY		5000	/* milliseconds */
49 #define MAX_POLL_DELAY		60000	/* milliseconds */
50 
51 #define RESUBMIT_DELAY		1000	/* milliseconds */
52 
53 #define DEFAULT_BULK_ALTSETTING	1
54 #define DEFAULT_ISOC_ALTSETTING	3
55 #define DEFAULT_DL_512_FIRST	0
56 #define DEFAULT_ENABLE_ISOC	0
57 #define DEFAULT_SW_BUFFERING	0
58 
59 static unsigned int altsetting = 0; /* zero means: use the default */
60 static bool dl_512_first = DEFAULT_DL_512_FIRST;
61 static bool enable_isoc = DEFAULT_ENABLE_ISOC;
62 static bool sw_buffering = DEFAULT_SW_BUFFERING;
63 
64 #define DEFAULT_B_MAX_DSL	8128
65 #define DEFAULT_MODEM_MODE	11
66 #define MODEM_OPTION_LENGTH	16
67 static 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 
71 static unsigned int BMaxDSL = DEFAULT_B_MAX_DSL;
72 static unsigned char ModemMode = DEFAULT_MODEM_MODE;
73 static unsigned char ModemOption[MODEM_OPTION_LENGTH];
74 static unsigned int num_ModemOption;
75 
76 module_param(altsetting, uint, S_IRUGO | S_IWUSR);
77 MODULE_PARM_DESC(altsetting,
78 		"Alternative setting for data interface (bulk_default: "
79 		__MODULE_STRING(DEFAULT_BULK_ALTSETTING) "; isoc_default: "
80 		__MODULE_STRING(DEFAULT_ISOC_ALTSETTING) ")");
81 
82 module_param(dl_512_first, bool, S_IRUGO | S_IWUSR);
83 MODULE_PARM_DESC(dl_512_first,
84 		 "Read 512 bytes before sending firmware (default: "
85 		 __MODULE_STRING(DEFAULT_DL_512_FIRST) ")");
86 
87 module_param(enable_isoc, bool, S_IRUGO | S_IWUSR);
88 MODULE_PARM_DESC(enable_isoc,
89 		"Use isochronous transfers if available (default: "
90 		__MODULE_STRING(DEFAULT_ENABLE_ISOC) ")");
91 
92 module_param(sw_buffering, bool, S_IRUGO | S_IWUSR);
93 MODULE_PARM_DESC(sw_buffering,
94 		 "Enable software buffering (default: "
95 		 __MODULE_STRING(DEFAULT_SW_BUFFERING) ")");
96 
97 module_param(BMaxDSL, uint, S_IRUGO | S_IWUSR);
98 MODULE_PARM_DESC(BMaxDSL,
99 		"default: " __MODULE_STRING(DEFAULT_B_MAX_DSL));
100 
101 module_param(ModemMode, byte, S_IRUGO | S_IWUSR);
102 MODULE_PARM_DESC(ModemMode,
103 		"default: " __MODULE_STRING(DEFAULT_MODEM_MODE));
104 
105 module_param_array(ModemOption, byte, &num_ModemOption, S_IRUGO);
106 MODULE_PARM_DESC(ModemOption, "default: 0x10,0x00,0x00,0x00,0x20");
107 
108 #define INTERFACE_DATA		1
109 #define ENDPOINT_INT		0x81
110 #define ENDPOINT_BULK_DATA	0x07
111 #define ENDPOINT_ISOC_DATA	0x07
112 #define ENDPOINT_FIRMWARE	0x05
113 
114 struct speedtch_params {
115 	unsigned int altsetting;
116 	unsigned int BMaxDSL;
117 	unsigned char ModemMode;
118 	unsigned char ModemOption[MODEM_OPTION_LENGTH];
119 };
120 
121 struct speedtch_instance_data {
122 	struct usbatm_data *usbatm;
123 
124 	struct speedtch_params params; /* set in probe, constant afterwards */
125 
126 	struct timer_list status_check_timer;
127 	struct work_struct status_check_work;
128 
129 	unsigned char last_status;
130 
131 	int poll_delay; /* milliseconds */
132 
133 	struct timer_list resubmit_timer;
134 	struct urb *int_urb;
135 	unsigned char int_data[16];
136 
137 	unsigned char scratch_buffer[16];
138 };
139 
140 /***************
141 **  firmware  **
142 ***************/
143 
speedtch_set_swbuff(struct speedtch_instance_data * instance,int state)144 static void speedtch_set_swbuff(struct speedtch_instance_data *instance, int state)
145 {
146 	struct usbatm_data *usbatm = instance->usbatm;
147 	struct usb_device *usb_dev = usbatm->usb_dev;
148 	int ret;
149 
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
157 		usb_dbg(usbatm, "speedtch_set_swbuff: %sbled SW buffering\n", state ? "En" : "Dis");
158 }
159 
speedtch_test_sequence(struct speedtch_instance_data * instance)160 static void speedtch_test_sequence(struct speedtch_instance_data *instance)
161 {
162 	struct usbatm_data *usbatm = instance->usbatm;
163 	struct usb_device *usb_dev = usbatm->usb_dev;
164 	unsigned char *buf = instance->scratch_buffer;
165 	int ret;
166 
167 	/* URB 147 */
168 	buf[0] = 0x1c;
169 	buf[1] = 0x50;
170 	ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
171 			      0x01, 0x40, 0x0b, 0x00, buf, 2, CTRL_TIMEOUT);
172 	if (ret < 0)
173 		usb_warn(usbatm, "%s failed on URB147: %d\n", __func__, ret);
174 
175 	/* URB 148 */
176 	buf[0] = 0x32;
177 	buf[1] = 0x00;
178 	ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
179 			      0x01, 0x40, 0x02, 0x00, buf, 2, CTRL_TIMEOUT);
180 	if (ret < 0)
181 		usb_warn(usbatm, "%s failed on URB148: %d\n", __func__, ret);
182 
183 	/* URB 149 */
184 	buf[0] = 0x01;
185 	buf[1] = 0x00;
186 	buf[2] = 0x01;
187 	ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
188 			      0x01, 0x40, 0x03, 0x00, buf, 3, CTRL_TIMEOUT);
189 	if (ret < 0)
190 		usb_warn(usbatm, "%s failed on URB149: %d\n", __func__, ret);
191 
192 	/* URB 150 */
193 	buf[0] = 0x01;
194 	buf[1] = 0x00;
195 	buf[2] = 0x01;
196 	ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
197 			      0x01, 0x40, 0x04, 0x00, buf, 3, CTRL_TIMEOUT);
198 	if (ret < 0)
199 		usb_warn(usbatm, "%s failed on URB150: %d\n", __func__, ret);
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);
228 }
229 
speedtch_upload_firmware(struct speedtch_instance_data * instance,const struct firmware * fw1,const struct firmware * fw2)230 static int speedtch_upload_firmware(struct speedtch_instance_data *instance,
231 				     const struct firmware *fw1,
232 				     const struct firmware *fw2)
233 {
234 	unsigned char *buffer;
235 	struct usbatm_data *usbatm = instance->usbatm;
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 
243 	buffer = (unsigned char *)__get_free_page(GFP_KERNEL);
244 	if (!buffer) {
245 		ret = -ENOMEM;
246 		usb_dbg(usbatm, "%s: no memory for buffer!\n", __func__);
247 		goto out;
248 	}
249 
250 	if (!usb_ifnum_to_if(usb_dev, 2)) {
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)
262 			usb_warn(usbatm, "%s: read BLOCK0 from modem failed (%d)!\n", __func__, ret);
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) {
276 			usb_err(usbatm, "%s: write BLOCK1 to modem failed (%d)!\n", __func__, ret);
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);
287 
288 	if (ret < 0) {
289 		usb_err(usbatm, "%s: read BLOCK2 from modem failed (%d)!\n", __func__, ret);
290 		goto out_free;
291 	}
292 	usb_dbg(usbatm, "%s: BLOCK2 downloaded (%d bytes)\n", __func__, actual_length);
293 
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) {
303 			usb_err(usbatm, "%s: write BLOCK3 to modem failed (%d)!\n", __func__, ret);
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) {
316 		usb_err(usbatm, "%s: read BLOCK4 from modem failed (%d)!\n", __func__, ret);
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 
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);
329 		goto out_free;
330 	}
331 
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 
341 out_free:
342 	free_page((unsigned long)buffer);
343 out:
344 	return ret;
345 }
346 
speedtch_find_firmware(struct usbatm_data * usbatm,struct usb_interface * intf,int phase,const struct firmware ** fw_p)347 static int speedtch_find_firmware(struct usbatm_data *usbatm, struct usb_interface *intf,
348 				  int phase, const struct firmware **fw_p)
349 {
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];
355 
356 	sprintf(buf, "speedtch-%d.bin.%x.%02x", phase, major_revision, minor_revision);
357 	usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
358 
359 	if (request_firmware(fw_p, buf, dev)) {
360 		sprintf(buf, "speedtch-%d.bin.%x", phase, major_revision);
361 		usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
362 
363 		if (request_firmware(fw_p, buf, dev)) {
364 			sprintf(buf, "speedtch-%d.bin", phase);
365 			usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
366 
367 			if (request_firmware(fw_p, buf, dev)) {
368 				usb_err(usbatm, "%s: no stage %d firmware found!\n", __func__, phase);
369 				return -ENOENT;
370 			}
371 		}
372 	}
373 
374 	usb_info(usbatm, "found stage %d firmware %s\n", phase, buf);
375 
376 	return 0;
377 }
378 
speedtch_heavy_init(struct usbatm_data * usbatm,struct usb_interface * intf)379 static 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 
385 	if ((ret = speedtch_find_firmware(usbatm, intf, 1, &fw1)) < 0)
386 		return ret;
387 
388 	if ((ret = speedtch_find_firmware(usbatm, intf, 2, &fw2)) < 0) {
389 		release_firmware(fw1);
390 		return ret;
391 	}
392 
393 	if ((ret = speedtch_upload_firmware(instance, fw1, fw2)) < 0)
394 		usb_err(usbatm, "%s: firmware upload failed (%d)!\n", __func__, ret);
395 
396 	release_firmware(fw2);
397 	release_firmware(fw1);
398 
399 	return ret;
400 }
401 
402 
403 /**********
404 **  ATM  **
405 **********/
406 
speedtch_read_status(struct speedtch_instance_data * instance)407 static int speedtch_read_status(struct speedtch_instance_data *instance)
408 {
409 	struct usbatm_data *usbatm = instance->usbatm;
410 	struct usb_device *usb_dev = usbatm->usb_dev;
411 	unsigned char *buf = instance->scratch_buffer;
412 	int ret;
413 
414 	memset(buf, 0, 16);
415 
416 	ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
417 			      0x12, 0xc0, 0x07, 0x00, buf + OFFSET_7, SIZE_7,
418 			      CTRL_TIMEOUT);
419 	if (ret < 0) {
420 		atm_dbg(usbatm, "%s: MSG 7 failed\n", __func__);
421 		return ret;
422 	}
423 
424 	ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
425 			      0x12, 0xc0, 0x0b, 0x00, buf + OFFSET_b, SIZE_b,
426 			      CTRL_TIMEOUT);
427 	if (ret < 0) {
428 		atm_dbg(usbatm, "%s: MSG B failed\n", __func__);
429 		return ret;
430 	}
431 
432 	ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
433 			      0x12, 0xc0, 0x0d, 0x00, buf + OFFSET_d, SIZE_d,
434 			      CTRL_TIMEOUT);
435 	if (ret < 0) {
436 		atm_dbg(usbatm, "%s: MSG D failed\n", __func__);
437 		return ret;
438 	}
439 
440 	ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
441 			      0x01, 0xc0, 0x0e, 0x00, buf + OFFSET_e, SIZE_e,
442 			      CTRL_TIMEOUT);
443 	if (ret < 0) {
444 		atm_dbg(usbatm, "%s: MSG E failed\n", __func__);
445 		return ret;
446 	}
447 
448 	ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
449 			      0x01, 0xc0, 0x0f, 0x00, buf + OFFSET_f, SIZE_f,
450 			      CTRL_TIMEOUT);
451 	if (ret < 0) {
452 		atm_dbg(usbatm, "%s: MSG F failed\n", __func__);
453 		return ret;
454 	}
455 
456 	return 0;
457 }
458 
speedtch_start_synchro(struct speedtch_instance_data * instance)459 static int speedtch_start_synchro(struct speedtch_instance_data *instance)
460 {
461 	struct usbatm_data *usbatm = instance->usbatm;
462 	struct usb_device *usb_dev = usbatm->usb_dev;
463 	unsigned char *buf = instance->scratch_buffer;
464 	int ret;
465 
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 
speedtch_check_status(struct work_struct * work)483 static void speedtch_check_status(struct work_struct *work)
484 {
485 	struct speedtch_instance_data *instance =
486 		container_of(work, struct speedtch_instance_data,
487 			     status_check_work);
488 	struct usbatm_data *usbatm = instance->usbatm;
489 	struct atm_dev *atm_dev = usbatm->atm_dev;
490 	unsigned char *buf = instance->scratch_buffer;
491 	int down_speed, up_speed, ret;
492 	unsigned char status;
493 
494 #ifdef VERBOSE_DEBUG
495 	atm_dbg(usbatm, "%s entered\n", __func__);
496 #endif
497 
498 	ret = speedtch_read_status(instance);
499 	if (ret < 0) {
500 		atm_warn(usbatm, "error %d fetching device status\n", ret);
501 		instance->poll_delay = min(2 * instance->poll_delay, MAX_POLL_DELAY);
502 		return;
503 	}
504 
505 	instance->poll_delay = max(instance->poll_delay / 2, MIN_POLL_DELAY);
506 
507 	status = buf[OFFSET_7];
508 
509 	if ((status != instance->last_status) || !status) {
510 		atm_dbg(usbatm, "%s: line state 0x%02x\n", __func__, status);
511 
512 		switch (status) {
513 		case 0:
514 			atm_dev_signal_change(atm_dev, ATM_PHY_SIG_LOST);
515 			if (instance->last_status)
516 				atm_info(usbatm, "ADSL line is down\n");
517 			/* It may never resync again unless we ask it to... */
518 			ret = speedtch_start_synchro(instance);
519 			break;
520 
521 		case 0x08:
522 			atm_dev_signal_change(atm_dev, ATM_PHY_SIG_UNKNOWN);
523 			atm_info(usbatm, "ADSL line is blocked?\n");
524 			break;
525 
526 		case 0x10:
527 			atm_dev_signal_change(atm_dev, ATM_PHY_SIG_LOST);
528 			atm_info(usbatm, "ADSL line is synchronising\n");
529 			break;
530 
531 		case 0x20:
532 			down_speed = buf[OFFSET_b] | (buf[OFFSET_b + 1] << 8)
533 				| (buf[OFFSET_b + 2] << 16) | (buf[OFFSET_b + 3] << 24);
534 			up_speed = buf[OFFSET_b + 4] | (buf[OFFSET_b + 5] << 8)
535 				| (buf[OFFSET_b + 6] << 16) | (buf[OFFSET_b + 7] << 24);
536 
537 			if (!(down_speed & 0x0000ffff) && !(up_speed & 0x0000ffff)) {
538 				down_speed >>= 16;
539 				up_speed >>= 16;
540 			}
541 
542 			atm_dev->link_rate = down_speed * 1000 / 424;
543 			atm_dev_signal_change(atm_dev, ATM_PHY_SIG_FOUND);
544 
545 			atm_info(usbatm,
546 				 "ADSL line is up (%d kb/s down | %d kb/s up)\n",
547 				 down_speed, up_speed);
548 			break;
549 
550 		default:
551 			atm_dev_signal_change(atm_dev, ATM_PHY_SIG_UNKNOWN);
552 			atm_info(usbatm, "unknown line state %02x\n", status);
553 			break;
554 		}
555 
556 		instance->last_status = status;
557 	}
558 }
559 
speedtch_status_poll(struct timer_list * t)560 static void speedtch_status_poll(struct timer_list *t)
561 {
562 	struct speedtch_instance_data *instance = from_timer(instance, t,
563 						             status_check_timer);
564 
565 	schedule_work(&instance->status_check_work);
566 
567 	/* The following check is racy, but the race is harmless */
568 	if (instance->poll_delay < MAX_POLL_DELAY)
569 		mod_timer(&instance->status_check_timer, jiffies + msecs_to_jiffies(instance->poll_delay));
570 	else
571 		atm_warn(instance->usbatm, "Too many failures - disabling line status polling\n");
572 }
573 
speedtch_resubmit_int(struct timer_list * t)574 static void speedtch_resubmit_int(struct timer_list *t)
575 {
576 	struct speedtch_instance_data *instance = from_timer(instance, t,
577 							     resubmit_timer);
578 	struct urb *int_urb = instance->int_urb;
579 	int ret;
580 
581 	atm_dbg(instance->usbatm, "%s entered\n", __func__);
582 
583 	if (int_urb) {
584 		ret = usb_submit_urb(int_urb, GFP_ATOMIC);
585 		if (!ret)
586 			schedule_work(&instance->status_check_work);
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));
590 		}
591 	}
592 }
593 
speedtch_handle_int(struct urb * int_urb)594 static void speedtch_handle_int(struct urb *int_urb)
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;
599 	int status = int_urb->status;
600 	int ret;
601 
602 	/* The magic interrupt for "up state" */
603 	static const unsigned char up_int[6]   = { 0xa1, 0x00, 0x01, 0x00, 0x00, 0x00 };
604 	/* The magic interrupt for "down state" */
605 	static const unsigned char down_int[6] = { 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00 };
606 
607 	atm_dbg(usbatm, "%s entered\n", __func__);
608 
609 	if (status < 0) {
610 		atm_dbg(usbatm, "%s: nonzero urb status %d!\n", __func__, status);
611 		goto fail;
612 	}
613 
614 	if ((count == 6) && !memcmp(up_int, instance->int_data, 6)) {
615 		del_timer(&instance->status_check_timer);
616 		atm_info(usbatm, "DSL line goes up\n");
617 	} else if ((count == 6) && !memcmp(down_int, instance->int_data, 6)) {
618 		atm_info(usbatm, "DSL line goes down\n");
619 	} else {
620 		int i;
621 
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 	}
628 
629 	int_urb = instance->int_urb;
630 	if (int_urb) {
631 		ret = usb_submit_urb(int_urb, GFP_ATOMIC);
632 		schedule_work(&instance->status_check_work);
633 		if (ret < 0) {
634 			atm_dbg(usbatm, "%s: usb_submit_urb failed with result %d\n", __func__, ret);
635 			goto fail;
636 		}
637 	}
638 
639 	return;
640 
641 fail:
642 	int_urb = instance->int_urb;
643 	if (int_urb)
644 		mod_timer(&instance->resubmit_timer, jiffies + msecs_to_jiffies(RESUBMIT_DELAY));
645 }
646 
speedtch_atm_start(struct usbatm_data * usbatm,struct atm_dev * atm_dev)647 static int speedtch_atm_start(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
648 {
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];
653 
654 	atm_dbg(usbatm, "%s entered\n", __func__);
655 
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++)
660 			atm_dev->esi[i] = (hex_to_bin(mac_str[i * 2]) << 4) +
661 				hex_to_bin(mac_str[i * 2 + 1]);
662 	}
663 
664 	/* Start modem synchronisation */
665 	ret = speedtch_start_synchro(instance);
666 
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;
675 		}
676 	}
677 
678 	/* Start status polling */
679 	mod_timer(&instance->status_check_timer, jiffies + msecs_to_jiffies(1000));
680 
681 	return 0;
682 }
683 
speedtch_atm_stop(struct usbatm_data * usbatm,struct atm_dev * atm_dev)684 static void speedtch_atm_stop(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
685 {
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 
691 	del_timer_sync(&instance->status_check_timer);
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);
709 
710 	flush_work(&instance->status_check_work);
711 }
712 
speedtch_pre_reset(struct usb_interface * intf)713 static int speedtch_pre_reset(struct usb_interface *intf)
714 {
715 	return 0;
716 }
717 
speedtch_post_reset(struct usb_interface * intf)718 static int speedtch_post_reset(struct usb_interface *intf)
719 {
720 	return 0;
721 }
722 
723 
724 /**********
725 **  USB  **
726 **********/
727 
728 static const struct usb_device_id speedtch_usb_ids[] = {
729 	{USB_DEVICE(0x06b9, 0x4061)},
730 	{}
731 };
732 
733 MODULE_DEVICE_TABLE(usb, speedtch_usb_ids);
734 
735 static int speedtch_usb_probe(struct usb_interface *, const struct usb_device_id *);
736 
737 static struct usb_driver speedtch_usb_driver = {
738 	.name		= speedtch_driver_name,
739 	.probe		= speedtch_usb_probe,
740 	.disconnect	= usbatm_usb_disconnect,
741 	.pre_reset	= speedtch_pre_reset,
742 	.post_reset	= speedtch_post_reset,
743 	.id_table	= speedtch_usb_ids
744 };
745 
speedtch_release_interfaces(struct usb_device * usb_dev,int num_interfaces)746 static void speedtch_release_interfaces(struct usb_device *usb_dev,
747 					int num_interfaces)
748 {
749 	struct usb_interface *cur_intf;
750 	int i;
751 
752 	for (i = 0; i < num_interfaces; i++) {
753 		cur_intf = usb_ifnum_to_if(usb_dev, i);
754 		if (cur_intf) {
755 			usb_set_intfdata(cur_intf, NULL);
756 			usb_driver_release_interface(&speedtch_usb_driver, cur_intf);
757 		}
758 	}
759 }
760 
speedtch_bind(struct usbatm_data * usbatm,struct usb_interface * intf,const struct usb_device_id * id)761 static int speedtch_bind(struct usbatm_data *usbatm,
762 			 struct usb_interface *intf,
763 			 const struct usb_device_id *id)
764 {
765 	struct usb_device *usb_dev = interface_to_usbdev(intf);
766 	struct usb_interface *cur_intf, *data_intf;
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;
771 	int use_isoc;
772 
773 	usb_dbg(usbatm, "%s entered\n", __func__);
774 
775 	/* sanity checks */
776 
777 	if (usb_dev->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) {
778 		usb_err(usbatm, "%s: wrong device class %d\n", __func__, usb_dev->descriptor.bDeviceClass);
779 		return -ENODEV;
780 	}
781 
782 	data_intf = usb_ifnum_to_if(usb_dev, INTERFACE_DATA);
783 	if (!data_intf) {
784 		usb_err(usbatm, "%s: data interface not found!\n", __func__);
785 		return -ENODEV;
786 	}
787 
788 	/* claim all interfaces */
789 
790 	for (i = 0; i < num_interfaces; i++) {
791 		cur_intf = usb_ifnum_to_if(usb_dev, i);
792 
793 		if ((i != ifnum) && cur_intf) {
794 			ret = usb_driver_claim_interface(&speedtch_usb_driver, cur_intf, usbatm);
795 
796 			if (ret < 0) {
797 				usb_err(usbatm, "%s: failed to claim interface %2d (%d)!\n", __func__, i, ret);
798 				speedtch_release_interfaces(usb_dev, i);
799 				return ret;
800 			}
801 		}
802 	}
803 
804 	instance = kzalloc(sizeof(*instance), GFP_KERNEL);
805 
806 	if (!instance) {
807 		ret = -ENOMEM;
808 		goto fail_release;
809 	}
810 
811 	instance->usbatm = usbatm;
812 
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);
819 	use_isoc = enable_isoc;
820 
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 */
825 		}
826 
827 	if (!instance->params.altsetting && use_isoc)
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 */
831 		}
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;
836 
837 		use_isoc = 0; /* fall back to bulk if endpoint not found */
838 
839 		for (i = 0; i < desc->desc.bNumEndpoints; i++) {
840 			const struct usb_endpoint_descriptor *endpoint_desc = &desc->endpoint[i].desc;
841 
842 			if ((endpoint_desc->bEndpointAddress == target_address)) {
843 				use_isoc =
844 					usb_endpoint_xfer_isoc(endpoint_desc);
845 				break;
846 			}
847 		}
848 
849 		if (!use_isoc)
850 			usb_info(usbatm, "isochronous transfer not supported - using bulk\n");
851 	}
852 
853 	if (!use_isoc && !instance->params.altsetting)
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 
859 	if (!instance->params.altsetting)
860 		instance->params.altsetting = use_isoc ? DEFAULT_ISOC_ALTSETTING : DEFAULT_BULK_ALTSETTING;
861 
862 	usbatm->flags |= (use_isoc ? UDSL_USE_ISOC : 0);
863 
864 	INIT_WORK(&instance->status_check_work, speedtch_check_status);
865 	timer_setup(&instance->status_check_timer, speedtch_status_poll, 0);
866 	instance->last_status = 0xff;
867 	instance->poll_delay = MIN_POLL_DELAY;
868 
869 	timer_setup(&instance->resubmit_timer, speedtch_resubmit_int, 0);
870 
871 	instance->int_urb = usb_alloc_urb(0, GFP_KERNEL);
872 
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),
877 				 speedtch_handle_int, instance, 16);
878 	else
879 		usb_dbg(usbatm, "%s: no memory for interrupt urb!\n", __func__);
880 
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);
885 
886 	usbatm->flags |= (ret == SIZE_7 ? UDSL_SKIP_HEAVY_INIT : 0);
887 
888 	usb_dbg(usbatm, "%s: firmware %s loaded\n", __func__, usbatm->flags & UDSL_SKIP_HEAVY_INIT ? "already" : "not");
889 
890 	if (!(usbatm->flags & UDSL_SKIP_HEAVY_INIT))
891 		if ((ret = usb_reset_device(usb_dev)) < 0) {
892 			usb_err(usbatm, "%s: device reset failed (%d)!\n", __func__, ret);
893 			goto fail_free;
894 		}
895 
896         usbatm->driver_data = instance;
897 
898 	return 0;
899 
900 fail_free:
901 	usb_free_urb(instance->int_urb);
902 	kfree(instance);
903 fail_release:
904 	speedtch_release_interfaces(usb_dev, num_interfaces);
905 	return ret;
906 }
907 
speedtch_unbind(struct usbatm_data * usbatm,struct usb_interface * intf)908 static void speedtch_unbind(struct usbatm_data *usbatm, struct usb_interface *intf)
909 {
910 	struct usb_device *usb_dev = interface_to_usbdev(intf);
911 	struct speedtch_instance_data *instance = usbatm->driver_data;
912 
913 	usb_dbg(usbatm, "%s entered\n", __func__);
914 
915 	speedtch_release_interfaces(usb_dev, usb_dev->actconfig->desc.bNumInterfaces);
916 	usb_free_urb(instance->int_urb);
917 	kfree(instance);
918 }
919 
920 
921 /***********
922 **  init  **
923 ***********/
924 
925 static struct usbatm_driver speedtch_usbatm_driver = {
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,
932 	.bulk_in	= ENDPOINT_BULK_DATA,
933 	.bulk_out	= ENDPOINT_BULK_DATA,
934 	.isoc_in	= ENDPOINT_ISOC_DATA
935 };
936 
speedtch_usb_probe(struct usb_interface * intf,const struct usb_device_id * id)937 static 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 
942 module_usb_driver(speedtch_usb_driver);
943 
944 MODULE_AUTHOR(DRIVER_AUTHOR);
945 MODULE_DESCRIPTION(DRIVER_DESC);
946 MODULE_LICENSE("GPL");
947