1 /******************************************************************************
2 * speedtch.c - Alcatel SpeedTouch USB xDSL modem driver
4 * Copyright (C) 2001, Alcatel
5 * Copyright (C) 2003, Duncan Sands
6 * Copyright (C) 2004, David Woodhouse
8 * Based on "modem_run.c", copyright (C) 2001, Benoit Papillault
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)
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
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.
24 ******************************************************************************/
27 #include <linux/device.h>
28 #include <linux/errno.h>
29 #include <linux/firmware.h>
30 #include <linux/gfp.h>
31 #include <linux/init.h>
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/moduleparam.h>
35 #include <linux/slab.h>
36 #include <linux/stat.h>
37 #include <linux/timer.h>
38 #include <linux/types.h>
39 #include <linux/usb_ch9.h>
40 #include <linux/workqueue.h>
44 #define DRIVER_AUTHOR "Johan Verrept, Duncan Sands <duncan.sands@free.fr>"
45 #define DRIVER_VERSION "1.10"
46 #define DRIVER_DESC "Alcatel SpeedTouch USB driver version " DRIVER_VERSION
48 static const char speedtch_driver_name[] = "speedtch";
50 #define CTRL_TIMEOUT 2000 /* milliseconds */
51 #define DATA_TIMEOUT 2000 /* milliseconds */
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 */
65 #define MIN_POLL_DELAY 5000 /* milliseconds */
66 #define MAX_POLL_DELAY 60000 /* milliseconds */
68 #define RESUBMIT_DELAY 1000 /* milliseconds */
70 #define DEFAULT_BULK_ALTSETTING 1
71 #define DEFAULT_ISOC_ALTSETTING 3
72 #define DEFAULT_DL_512_FIRST 0
73 #define DEFAULT_ENABLE_ISOC 0
74 #define DEFAULT_SW_BUFFERING 0
76 static unsigned int altsetting = 0; /* zero means: use the default */
77 static int dl_512_first = DEFAULT_DL_512_FIRST;
78 static int enable_isoc = DEFAULT_ENABLE_ISOC;
79 static int sw_buffering = DEFAULT_SW_BUFFERING;
81 #define DEFAULT_B_MAX_DSL 8128
82 #define DEFAULT_MODEM_MODE 11
83 #define MODEM_OPTION_LENGTH 16
84 static const unsigned char DEFAULT_MODEM_OPTION[MODEM_OPTION_LENGTH] = {
85 0x10, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
88 static unsigned int BMaxDSL = DEFAULT_B_MAX_DSL;
89 static unsigned char ModemMode = DEFAULT_MODEM_MODE;
90 static unsigned char ModemOption[MODEM_OPTION_LENGTH];
91 static int num_ModemOption;
93 module_param(altsetting, uint, S_IRUGO | S_IWUSR);
94 MODULE_PARM_DESC(altsetting,
95 "Alternative setting for data interface (bulk_default: "
96 __MODULE_STRING(DEFAULT_BULK_ALTSETTING) "; isoc_default: "
97 __MODULE_STRING(DEFAULT_ISOC_ALTSETTING) ")");
99 module_param(dl_512_first, bool, S_IRUGO | S_IWUSR);
100 MODULE_PARM_DESC(dl_512_first,
101 "Read 512 bytes before sending firmware (default: "
102 __MODULE_STRING(DEFAULT_DL_512_FIRST) ")");
104 module_param(enable_isoc, bool, S_IRUGO | S_IWUSR);
105 MODULE_PARM_DESC(enable_isoc,
106 "Use isochronous transfers if available (default: "
107 __MODULE_STRING(DEFAULT_ENABLE_ISOC) ")");
109 module_param(sw_buffering, bool, S_IRUGO | S_IWUSR);
110 MODULE_PARM_DESC(sw_buffering,
111 "Enable software buffering (default: "
112 __MODULE_STRING(DEFAULT_SW_BUFFERING) ")");
114 module_param(BMaxDSL, uint, S_IRUGO | S_IWUSR);
115 MODULE_PARM_DESC(BMaxDSL,
116 "default: " __MODULE_STRING(DEFAULT_B_MAX_DSL));
118 module_param(ModemMode, byte, S_IRUGO | S_IWUSR);
119 MODULE_PARM_DESC(ModemMode,
120 "default: " __MODULE_STRING(DEFAULT_MODEM_MODE));
122 module_param_array(ModemOption, byte, &num_ModemOption, S_IRUGO);
123 MODULE_PARM_DESC(ModemOption, "default: 0x10,0x00,0x00,0x00,0x20");
125 #define INTERFACE_DATA 1
126 #define ENDPOINT_INT 0x81
127 #define ENDPOINT_BULK_DATA 0x07
128 #define ENDPOINT_ISOC_DATA 0x07
129 #define ENDPOINT_FIRMWARE 0x05
131 #define hex2int(c) ( (c >= '0') && (c <= '9') ? (c - '0') : ((c & 0xf) + 9) )
133 struct speedtch_params {
134 unsigned int altsetting;
135 unsigned int BMaxDSL;
136 unsigned char ModemMode;
137 unsigned char ModemOption[MODEM_OPTION_LENGTH];
140 struct speedtch_instance_data {
141 struct usbatm_data *usbatm;
143 struct speedtch_params params; /* set in probe, constant afterwards */
145 struct work_struct status_checker;
147 unsigned char last_status;
149 int poll_delay; /* milliseconds */
151 struct timer_list resubmit_timer;
153 unsigned char int_data[16];
155 unsigned char scratch_buffer[16];
162 static void speedtch_set_swbuff(struct speedtch_instance_data *instance, int state)
164 struct usbatm_data *usbatm = instance->usbatm;
165 struct usb_device *usb_dev = usbatm->usb_dev;
168 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
169 0x32, 0x40, state ? 0x01 : 0x00, 0x00, NULL, 0, CTRL_TIMEOUT);
172 "%sabling SW buffering: usb_control_msg returned %d\n",
173 state ? "En" : "Dis", ret);
175 dbg("speedtch_set_swbuff: %sbled SW buffering", state ? "En" : "Dis");
178 static void speedtch_test_sequence(struct speedtch_instance_data *instance)
180 struct usbatm_data *usbatm = instance->usbatm;
181 struct usb_device *usb_dev = usbatm->usb_dev;
182 unsigned char *buf = instance->scratch_buffer;
188 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
189 0x01, 0x40, 0x0b, 0x00, buf, 2, CTRL_TIMEOUT);
191 usb_warn(usbatm, "%s failed on URB147: %d\n", __func__, ret);
196 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
197 0x01, 0x40, 0x02, 0x00, buf, 2, CTRL_TIMEOUT);
199 usb_warn(usbatm, "%s failed on URB148: %d\n", __func__, ret);
205 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
206 0x01, 0x40, 0x03, 0x00, buf, 3, CTRL_TIMEOUT);
208 usb_warn(usbatm, "%s failed on URB149: %d\n", __func__, ret);
214 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
215 0x01, 0x40, 0x04, 0x00, buf, 3, CTRL_TIMEOUT);
217 usb_warn(usbatm, "%s failed on URB150: %d\n", __func__, ret);
219 /* Extra initialisation in recent drivers - gives higher speeds */
222 buf[0] = instance->params.ModemMode;
223 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
224 0x01, 0x40, 0x11, 0x00, buf, 1, CTRL_TIMEOUT);
226 usb_warn(usbatm, "%s failed on URBext1: %d\n", __func__, ret);
229 /* This seems to be the one which actually triggers the higher sync
230 rate -- it does require the new firmware too, although it works OK
231 with older firmware */
232 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
233 0x01, 0x40, 0x14, 0x00,
234 instance->params.ModemOption,
235 MODEM_OPTION_LENGTH, CTRL_TIMEOUT);
237 usb_warn(usbatm, "%s failed on URBext2: %d\n", __func__, ret);
240 buf[0] = instance->params.BMaxDSL & 0xff;
241 buf[1] = instance->params.BMaxDSL >> 8;
242 ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
243 0x01, 0x40, 0x12, 0x00, buf, 2, CTRL_TIMEOUT);
245 usb_warn(usbatm, "%s failed on URBext3: %d\n", __func__, ret);
248 static int speedtch_upload_firmware(struct speedtch_instance_data *instance,
249 const struct firmware *fw1,
250 const struct firmware *fw2)
252 unsigned char *buffer;
253 struct usbatm_data *usbatm = instance->usbatm;
254 struct usb_interface *intf;
255 struct usb_device *usb_dev = usbatm->usb_dev;
260 usb_dbg(usbatm, "%s entered\n", __func__);
262 if (!(buffer = (unsigned char *)__get_free_page(GFP_KERNEL))) {
264 usb_dbg(usbatm, "%s: no memory for buffer!\n", __func__);
268 if (!(intf = usb_ifnum_to_if(usb_dev, 2))) {
270 usb_dbg(usbatm, "%s: interface not found!\n", __func__);
275 if (dl_512_first) { /* some modems need a read before writing the firmware */
276 ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
277 buffer, 0x200, &actual_length, 2000);
279 if (ret < 0 && ret != -ETIMEDOUT)
280 usb_warn(usbatm, "%s: read BLOCK0 from modem failed (%d)!\n", __func__, ret);
282 usb_dbg(usbatm, "%s: BLOCK0 downloaded (%d bytes)\n", __func__, ret);
285 /* URB 8 : both leds are static green */
286 for (offset = 0; offset < fw1->size; offset += PAGE_SIZE) {
287 int thislen = min_t(int, PAGE_SIZE, fw1->size - offset);
288 memcpy(buffer, fw1->data + offset, thislen);
290 ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
291 buffer, thislen, &actual_length, DATA_TIMEOUT);
294 usb_err(usbatm, "%s: write BLOCK1 to modem failed (%d)!\n", __func__, ret);
297 usb_dbg(usbatm, "%s: BLOCK1 uploaded (%zu bytes)\n", __func__, fw1->size);
300 /* USB led blinking green, ADSL led off */
303 ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
304 buffer, 0x200, &actual_length, DATA_TIMEOUT);
307 usb_err(usbatm, "%s: read BLOCK2 from modem failed (%d)!\n", __func__, ret);
310 usb_dbg(usbatm, "%s: BLOCK2 downloaded (%d bytes)\n", __func__, actual_length);
312 /* URBs 12 to 139 - USB led blinking green, ADSL led off */
313 for (offset = 0; offset < fw2->size; offset += PAGE_SIZE) {
314 int thislen = min_t(int, PAGE_SIZE, fw2->size - offset);
315 memcpy(buffer, fw2->data + offset, thislen);
317 ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
318 buffer, thislen, &actual_length, DATA_TIMEOUT);
321 usb_err(usbatm, "%s: write BLOCK3 to modem failed (%d)!\n", __func__, ret);
325 usb_dbg(usbatm, "%s: BLOCK3 uploaded (%zu bytes)\n", __func__, fw2->size);
327 /* USB led static green, ADSL led static red */
330 ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
331 buffer, 0x200, &actual_length, DATA_TIMEOUT);
334 usb_err(usbatm, "%s: read BLOCK4 from modem failed (%d)!\n", __func__, ret);
339 usb_dbg(usbatm, "%s: BLOCK4 downloaded (%d bytes)\n", __func__, actual_length);
341 /* Delay to allow firmware to start up. We can do this here
342 because we're in our own kernel thread anyway. */
343 msleep_interruptible(1000);
345 if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, instance->params.altsetting)) < 0) {
346 usb_err(usbatm, "%s: setting interface to %d failed (%d)!\n", __func__, instance->params.altsetting, ret);
350 /* Enable software buffering, if requested */
352 speedtch_set_swbuff(instance, 1);
354 /* Magic spell; don't ask us what this does */
355 speedtch_test_sequence(instance);
360 free_page((unsigned long)buffer);
365 static int speedtch_find_firmware(struct usbatm_data *usbatm, struct usb_interface *intf,
366 int phase, const struct firmware **fw_p)
368 struct device *dev = &intf->dev;
369 const u16 bcdDevice = le16_to_cpu(interface_to_usbdev(intf)->descriptor.bcdDevice);
370 const u8 major_revision = bcdDevice >> 8;
371 const u8 minor_revision = bcdDevice & 0xff;
374 sprintf(buf, "speedtch-%d.bin.%x.%02x", phase, major_revision, minor_revision);
375 usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
377 if (request_firmware(fw_p, buf, dev)) {
378 sprintf(buf, "speedtch-%d.bin.%x", phase, major_revision);
379 usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
381 if (request_firmware(fw_p, buf, dev)) {
382 sprintf(buf, "speedtch-%d.bin", phase);
383 usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
385 if (request_firmware(fw_p, buf, dev)) {
386 usb_err(usbatm, "%s: no stage %d firmware found!\n", __func__, phase);
392 usb_info(usbatm, "found stage %d firmware %s\n", phase, buf);
397 static int speedtch_heavy_init(struct usbatm_data *usbatm, struct usb_interface *intf)
399 const struct firmware *fw1, *fw2;
400 struct speedtch_instance_data *instance = usbatm->driver_data;
403 if ((ret = speedtch_find_firmware(usbatm, intf, 1, &fw1)) < 0)
406 if ((ret = speedtch_find_firmware(usbatm, intf, 2, &fw2)) < 0) {
407 release_firmware(fw1);
411 if ((ret = speedtch_upload_firmware(instance, fw1, fw2)) < 0)
412 usb_err(usbatm, "%s: firmware upload failed (%d)!\n", __func__, ret);
414 release_firmware(fw2);
415 release_firmware(fw1);
425 static int speedtch_read_status(struct speedtch_instance_data *instance)
427 struct usbatm_data *usbatm = instance->usbatm;
428 struct usb_device *usb_dev = usbatm->usb_dev;
429 unsigned char *buf = instance->scratch_buffer;
434 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
435 0x12, 0xc0, 0x07, 0x00, buf + OFFSET_7, SIZE_7,
438 atm_dbg(usbatm, "%s: MSG 7 failed\n", __func__);
442 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
443 0x12, 0xc0, 0x0b, 0x00, buf + OFFSET_b, SIZE_b,
446 atm_dbg(usbatm, "%s: MSG B failed\n", __func__);
450 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
451 0x12, 0xc0, 0x0d, 0x00, buf + OFFSET_d, SIZE_d,
454 atm_dbg(usbatm, "%s: MSG D failed\n", __func__);
458 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
459 0x01, 0xc0, 0x0e, 0x00, buf + OFFSET_e, SIZE_e,
462 atm_dbg(usbatm, "%s: MSG E failed\n", __func__);
466 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
467 0x01, 0xc0, 0x0f, 0x00, buf + OFFSET_f, SIZE_f,
470 atm_dbg(usbatm, "%s: MSG F failed\n", __func__);
477 static int speedtch_start_synchro(struct speedtch_instance_data *instance)
479 struct usbatm_data *usbatm = instance->usbatm;
480 struct usb_device *usb_dev = usbatm->usb_dev;
481 unsigned char *buf = instance->scratch_buffer;
484 atm_dbg(usbatm, "%s entered\n", __func__);
488 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
489 0x12, 0xc0, 0x04, 0x00,
490 buf, 2, CTRL_TIMEOUT);
493 atm_warn(usbatm, "failed to start ADSL synchronisation: %d\n", ret);
495 atm_dbg(usbatm, "%s: modem prodded. %d bytes returned: %02x %02x\n",
496 __func__, ret, buf[0], buf[1]);
501 static void speedtch_check_status(struct speedtch_instance_data *instance)
503 struct usbatm_data *usbatm = instance->usbatm;
504 struct atm_dev *atm_dev = usbatm->atm_dev;
505 unsigned char *buf = instance->scratch_buffer;
506 int down_speed, up_speed, ret;
507 unsigned char status;
510 atm_dbg(usbatm, "%s entered\n", __func__);
513 ret = speedtch_read_status(instance);
515 atm_warn(usbatm, "error %d fetching device status\n", ret);
516 instance->poll_delay = min(2 * instance->poll_delay, MAX_POLL_DELAY);
520 instance->poll_delay = max(instance->poll_delay / 2, MIN_POLL_DELAY);
522 status = buf[OFFSET_7];
524 if ((status != instance->last_status) || !status) {
525 atm_dbg(usbatm, "%s: line state 0x%02x\n", __func__, status);
529 atm_dev->signal = ATM_PHY_SIG_LOST;
530 if (instance->last_status)
531 atm_info(usbatm, "ADSL line is down\n");
532 /* It may never resync again unless we ask it to... */
533 ret = speedtch_start_synchro(instance);
537 atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
538 atm_info(usbatm, "ADSL line is blocked?\n");
542 atm_dev->signal = ATM_PHY_SIG_LOST;
543 atm_info(usbatm, "ADSL line is synchronising\n");
547 down_speed = buf[OFFSET_b] | (buf[OFFSET_b + 1] << 8)
548 | (buf[OFFSET_b + 2] << 16) | (buf[OFFSET_b + 3] << 24);
549 up_speed = buf[OFFSET_b + 4] | (buf[OFFSET_b + 5] << 8)
550 | (buf[OFFSET_b + 6] << 16) | (buf[OFFSET_b + 7] << 24);
552 if (!(down_speed & 0x0000ffff) && !(up_speed & 0x0000ffff)) {
557 atm_dev->link_rate = down_speed * 1000 / 424;
558 atm_dev->signal = ATM_PHY_SIG_FOUND;
561 "ADSL line is up (%d kb/s down | %d kb/s up)\n",
562 down_speed, up_speed);
566 atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
567 atm_info(usbatm, "unknown line state %02x\n", status);
571 instance->last_status = status;
575 static void speedtch_status_poll(unsigned long data)
577 struct speedtch_instance_data *instance = (void *)data;
579 schedule_work(&instance->status_checker);
581 /* The following check is racy, but the race is harmless */
582 if (instance->poll_delay < MAX_POLL_DELAY)
583 mod_timer(&instance->status_checker.timer, jiffies + msecs_to_jiffies(instance->poll_delay));
585 atm_warn(instance->usbatm, "Too many failures - disabling line status polling\n");
588 static void speedtch_resubmit_int(unsigned long data)
590 struct speedtch_instance_data *instance = (void *)data;
591 struct urb *int_urb = instance->int_urb;
594 atm_dbg(instance->usbatm, "%s entered\n", __func__);
597 ret = usb_submit_urb(int_urb, GFP_ATOMIC);
599 schedule_work(&instance->status_checker);
601 atm_dbg(instance->usbatm, "%s: usb_submit_urb failed with result %d\n", __func__, ret);
602 mod_timer(&instance->resubmit_timer, jiffies + msecs_to_jiffies(RESUBMIT_DELAY));
607 static void speedtch_handle_int(struct urb *int_urb)
609 struct speedtch_instance_data *instance = int_urb->context;
610 struct usbatm_data *usbatm = instance->usbatm;
611 unsigned int count = int_urb->actual_length;
612 int ret = int_urb->status;
614 /* The magic interrupt for "up state" */
615 static const unsigned char up_int[6] = { 0xa1, 0x00, 0x01, 0x00, 0x00, 0x00 };
616 /* The magic interrupt for "down state" */
617 static const unsigned char down_int[6] = { 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00 };
619 atm_dbg(usbatm, "%s entered\n", __func__);
622 atm_dbg(usbatm, "%s: nonzero urb status %d!\n", __func__, ret);
626 if ((count == 6) && !memcmp(up_int, instance->int_data, 6)) {
627 del_timer(&instance->status_checker.timer);
628 atm_info(usbatm, "DSL line goes up\n");
629 } else if ((count == 6) && !memcmp(down_int, instance->int_data, 6)) {
630 atm_info(usbatm, "DSL line goes down\n");
634 atm_dbg(usbatm, "%s: unknown interrupt packet of length %d:", __func__, count);
635 for (i = 0; i < count; i++)
636 printk(" %02x", instance->int_data[i]);
641 if ((int_urb = instance->int_urb)) {
642 ret = usb_submit_urb(int_urb, GFP_ATOMIC);
643 schedule_work(&instance->status_checker);
645 atm_dbg(usbatm, "%s: usb_submit_urb failed with result %d\n", __func__, ret);
653 if ((int_urb = instance->int_urb))
654 mod_timer(&instance->resubmit_timer, jiffies + msecs_to_jiffies(RESUBMIT_DELAY));
657 static int speedtch_atm_start(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
659 struct usb_device *usb_dev = usbatm->usb_dev;
660 struct speedtch_instance_data *instance = usbatm->driver_data;
662 unsigned char mac_str[13];
664 atm_dbg(usbatm, "%s entered\n", __func__);
666 /* Set MAC address, it is stored in the serial number */
667 memset(atm_dev->esi, 0, sizeof(atm_dev->esi));
668 if (usb_string(usb_dev, usb_dev->descriptor.iSerialNumber, mac_str, sizeof(mac_str)) == 12) {
669 for (i = 0; i < 6; i++)
670 atm_dev->esi[i] = (hex2int(mac_str[i * 2]) * 16) + (hex2int(mac_str[i * 2 + 1]));
673 /* Start modem synchronisation */
674 ret = speedtch_start_synchro(instance);
676 /* Set up interrupt endpoint */
677 if (instance->int_urb) {
678 ret = usb_submit_urb(instance->int_urb, GFP_KERNEL);
680 /* Doesn't matter; we'll poll anyway */
681 atm_dbg(usbatm, "%s: submission of interrupt URB failed (%d)!\n", __func__, ret);
682 usb_free_urb(instance->int_urb);
683 instance->int_urb = NULL;
687 /* Start status polling */
688 mod_timer(&instance->status_checker.timer, jiffies + msecs_to_jiffies(1000));
693 static void speedtch_atm_stop(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
695 struct speedtch_instance_data *instance = usbatm->driver_data;
696 struct urb *int_urb = instance->int_urb;
698 atm_dbg(usbatm, "%s entered\n", __func__);
700 del_timer_sync(&instance->status_checker.timer);
703 * Since resubmit_timer and int_urb can schedule themselves and
704 * each other, shutting them down correctly takes some care
706 instance->int_urb = NULL; /* signal shutdown */
708 usb_kill_urb(int_urb);
709 del_timer_sync(&instance->resubmit_timer);
711 * At this point, speedtch_handle_int and speedtch_resubmit_int
712 * can run or be running, but instance->int_urb == NULL means that
713 * they will not reschedule
715 usb_kill_urb(int_urb);
716 del_timer_sync(&instance->resubmit_timer);
717 usb_free_urb(int_urb);
719 flush_scheduled_work();
727 static struct usb_device_id speedtch_usb_ids[] = {
728 {USB_DEVICE(0x06b9, 0x4061)},
732 MODULE_DEVICE_TABLE(usb, speedtch_usb_ids);
734 static int speedtch_usb_probe(struct usb_interface *, const struct usb_device_id *);
736 static struct usb_driver speedtch_usb_driver = {
737 .name = speedtch_driver_name,
738 .probe = speedtch_usb_probe,
739 .disconnect = usbatm_usb_disconnect,
740 .id_table = speedtch_usb_ids
743 static void speedtch_release_interfaces(struct usb_device *usb_dev, int num_interfaces) {
744 struct usb_interface *cur_intf;
747 for(i = 0; i < num_interfaces; i++)
748 if ((cur_intf = usb_ifnum_to_if(usb_dev, i))) {
749 usb_set_intfdata(cur_intf, NULL);
750 usb_driver_release_interface(&speedtch_usb_driver, cur_intf);
754 static int speedtch_bind(struct usbatm_data *usbatm,
755 struct usb_interface *intf,
756 const struct usb_device_id *id)
758 struct usb_device *usb_dev = interface_to_usbdev(intf);
759 struct usb_interface *cur_intf, *data_intf;
760 struct speedtch_instance_data *instance;
761 int ifnum = intf->altsetting->desc.bInterfaceNumber;
762 int num_interfaces = usb_dev->actconfig->desc.bNumInterfaces;
766 usb_dbg(usbatm, "%s entered\n", __func__);
770 if (usb_dev->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) {
771 usb_err(usbatm, "%s: wrong device class %d\n", __func__, usb_dev->descriptor.bDeviceClass);
775 if (!(data_intf = usb_ifnum_to_if(usb_dev, INTERFACE_DATA))) {
776 usb_err(usbatm, "%s: data interface not found!\n", __func__);
780 /* claim all interfaces */
782 for (i=0; i < num_interfaces; i++) {
783 cur_intf = usb_ifnum_to_if(usb_dev, i);
785 if ((i != ifnum) && cur_intf) {
786 ret = usb_driver_claim_interface(&speedtch_usb_driver, cur_intf, usbatm);
789 usb_err(usbatm, "%s: failed to claim interface %2d (%d)!\n", __func__, i, ret);
790 speedtch_release_interfaces(usb_dev, i);
796 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
799 usb_err(usbatm, "%s: no memory for instance data!\n", __func__);
804 instance->usbatm = usbatm;
806 /* module parameters may change at any moment, so take a snapshot */
807 instance->params.altsetting = altsetting;
808 instance->params.BMaxDSL = BMaxDSL;
809 instance->params.ModemMode = ModemMode;
810 memcpy(instance->params.ModemOption, DEFAULT_MODEM_OPTION, MODEM_OPTION_LENGTH);
811 memcpy(instance->params.ModemOption, ModemOption, num_ModemOption);
812 use_isoc = enable_isoc;
814 if (instance->params.altsetting)
815 if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, instance->params.altsetting)) < 0) {
816 usb_err(usbatm, "%s: setting interface to %2d failed (%d)!\n", __func__, instance->params.altsetting, ret);
817 instance->params.altsetting = 0; /* fall back to default */
820 if (!instance->params.altsetting && use_isoc)
821 if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, DEFAULT_ISOC_ALTSETTING)) < 0) {
822 usb_dbg(usbatm, "%s: setting interface to %2d failed (%d)!\n", __func__, DEFAULT_ISOC_ALTSETTING, ret);
823 use_isoc = 0; /* fall back to bulk */
827 const struct usb_host_interface *desc = data_intf->cur_altsetting;
828 const __u8 target_address = USB_DIR_IN | usbatm->driver->isoc_in;
831 use_isoc = 0; /* fall back to bulk if endpoint not found */
833 for (i=0; i<desc->desc.bNumEndpoints; i++) {
834 const struct usb_endpoint_descriptor *endpoint_desc = &desc->endpoint[i].desc;
836 if ((endpoint_desc->bEndpointAddress == target_address)) {
837 use_isoc = (endpoint_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
838 USB_ENDPOINT_XFER_ISOC;
844 usb_info(usbatm, "isochronous transfer not supported - using bulk\n");
847 if (!use_isoc && !instance->params.altsetting)
848 if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, DEFAULT_BULK_ALTSETTING)) < 0) {
849 usb_err(usbatm, "%s: setting interface to %2d failed (%d)!\n", __func__, DEFAULT_BULK_ALTSETTING, ret);
853 if (!instance->params.altsetting)
854 instance->params.altsetting = use_isoc ? DEFAULT_ISOC_ALTSETTING : DEFAULT_BULK_ALTSETTING;
856 usbatm->flags |= (use_isoc ? UDSL_USE_ISOC : 0);
858 INIT_WORK(&instance->status_checker, (void *)speedtch_check_status, instance);
860 instance->status_checker.timer.function = speedtch_status_poll;
861 instance->status_checker.timer.data = (unsigned long)instance;
862 instance->last_status = 0xff;
863 instance->poll_delay = MIN_POLL_DELAY;
865 init_timer(&instance->resubmit_timer);
866 instance->resubmit_timer.function = speedtch_resubmit_int;
867 instance->resubmit_timer.data = (unsigned long)instance;
869 instance->int_urb = usb_alloc_urb(0, GFP_KERNEL);
871 if (instance->int_urb)
872 usb_fill_int_urb(instance->int_urb, usb_dev,
873 usb_rcvintpipe(usb_dev, ENDPOINT_INT),
874 instance->int_data, sizeof(instance->int_data),
875 speedtch_handle_int, instance, 50);
877 usb_dbg(usbatm, "%s: no memory for interrupt urb!\n", __func__);
879 /* check whether the modem already seems to be alive */
880 ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
881 0x12, 0xc0, 0x07, 0x00,
882 instance->scratch_buffer + OFFSET_7, SIZE_7, 500);
884 usbatm->flags |= (ret == SIZE_7 ? UDSL_SKIP_HEAVY_INIT : 0);
886 usb_dbg(usbatm, "%s: firmware %s loaded\n", __func__, usbatm->flags & UDSL_SKIP_HEAVY_INIT ? "already" : "not");
888 if (!(usbatm->flags & UDSL_SKIP_HEAVY_INIT))
889 if ((ret = usb_reset_device(usb_dev)) < 0) {
890 usb_err(usbatm, "%s: device reset failed (%d)!\n", __func__, ret);
894 usbatm->driver_data = instance;
899 usb_free_urb(instance->int_urb);
902 speedtch_release_interfaces(usb_dev, num_interfaces);
906 static void speedtch_unbind(struct usbatm_data *usbatm, struct usb_interface *intf)
908 struct usb_device *usb_dev = interface_to_usbdev(intf);
909 struct speedtch_instance_data *instance = usbatm->driver_data;
911 usb_dbg(usbatm, "%s entered\n", __func__);
913 speedtch_release_interfaces(usb_dev, usb_dev->actconfig->desc.bNumInterfaces);
914 usb_free_urb(instance->int_urb);
923 static struct usbatm_driver speedtch_usbatm_driver = {
924 .driver_name = speedtch_driver_name,
925 .bind = speedtch_bind,
926 .heavy_init = speedtch_heavy_init,
927 .unbind = speedtch_unbind,
928 .atm_start = speedtch_atm_start,
929 .atm_stop = speedtch_atm_stop,
930 .bulk_in = ENDPOINT_BULK_DATA,
931 .bulk_out = ENDPOINT_BULK_DATA,
932 .isoc_in = ENDPOINT_ISOC_DATA
935 static int speedtch_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
937 return usbatm_usb_probe(intf, id, &speedtch_usbatm_driver);
940 static int __init speedtch_usb_init(void)
942 dbg("%s: driver version %s", __func__, DRIVER_VERSION);
944 return usb_register(&speedtch_usb_driver);
947 static void __exit speedtch_usb_cleanup(void)
951 usb_deregister(&speedtch_usb_driver);
954 module_init(speedtch_usb_init);
955 module_exit(speedtch_usb_cleanup);
957 MODULE_AUTHOR(DRIVER_AUTHOR);
958 MODULE_DESCRIPTION(DRIVER_DESC);
959 MODULE_LICENSE("GPL");
960 MODULE_VERSION(DRIVER_VERSION);