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