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