xref: /openbmc/linux/drivers/usb/atm/speedtch.c (revision 664b0bae0b87f69bc9deb098f5e0158b9cf18e04)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
21da177e4SLinus Torvalds /******************************************************************************
31da177e4SLinus Torvalds  *  speedtch.c  -  Alcatel SpeedTouch USB xDSL modem driver
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 2001, Alcatel
61da177e4SLinus Torvalds  *  Copyright (C) 2003, Duncan Sands
71da177e4SLinus Torvalds  *  Copyright (C) 2004, David Woodhouse
81da177e4SLinus Torvalds  *
948da7267SDuncan Sands  *  Based on "modem_run.c", copyright (C) 2001, Benoit Papillault
101da177e4SLinus Torvalds  ******************************************************************************/
111da177e4SLinus Torvalds 
1248da7267SDuncan Sands #include <asm/page.h>
1348da7267SDuncan Sands #include <linux/device.h>
1448da7267SDuncan Sands #include <linux/errno.h>
1548da7267SDuncan Sands #include <linux/firmware.h>
1648da7267SDuncan Sands #include <linux/kernel.h>
171da177e4SLinus Torvalds #include <linux/module.h>
181da177e4SLinus Torvalds #include <linux/moduleparam.h>
191da177e4SLinus Torvalds #include <linux/slab.h>
2048da7267SDuncan Sands #include <linux/stat.h>
2148da7267SDuncan Sands #include <linux/timer.h>
2280aae7a1SDuncan Sands #include <linux/types.h>
235f848137SDavid Brownell #include <linux/usb/ch9.h>
2448da7267SDuncan Sands #include <linux/workqueue.h>
251da177e4SLinus Torvalds 
2648da7267SDuncan Sands #include "usbatm.h"
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds #define DRIVER_AUTHOR	"Johan Verrept, Duncan Sands <duncan.sands@free.fr>"
299acd6b2aSGreg Kroah-Hartman #define DRIVER_DESC	"Alcatel SpeedTouch USB driver"
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds static const char speedtch_driver_name[] = "speedtch";
321da177e4SLinus Torvalds 
3348da7267SDuncan Sands #define CTRL_TIMEOUT 2000	/* milliseconds */
3448da7267SDuncan Sands #define DATA_TIMEOUT 2000	/* milliseconds */
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds #define OFFSET_7	0		/* size 1 */
371da177e4SLinus Torvalds #define OFFSET_b	1		/* size 8 */
381da177e4SLinus Torvalds #define OFFSET_d	9		/* size 4 */
391da177e4SLinus Torvalds #define OFFSET_e	13		/* size 1 */
401da177e4SLinus Torvalds #define OFFSET_f	14		/* size 1 */
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #define SIZE_7		1
431da177e4SLinus Torvalds #define SIZE_b		8
441da177e4SLinus Torvalds #define SIZE_d		4
451da177e4SLinus Torvalds #define SIZE_e		1
461da177e4SLinus Torvalds #define SIZE_f		1
471da177e4SLinus Torvalds 
4848da7267SDuncan Sands #define MIN_POLL_DELAY		5000	/* milliseconds */
4948da7267SDuncan Sands #define MAX_POLL_DELAY		60000	/* milliseconds */
501da177e4SLinus Torvalds 
5148da7267SDuncan Sands #define RESUBMIT_DELAY		1000	/* milliseconds */
521da177e4SLinus Torvalds 
5380aae7a1SDuncan Sands #define DEFAULT_BULK_ALTSETTING	1
5467c752b4SDuncan Sands #define DEFAULT_ISOC_ALTSETTING	3
5548da7267SDuncan Sands #define DEFAULT_DL_512_FIRST	0
5680aae7a1SDuncan Sands #define DEFAULT_ENABLE_ISOC	0
5748da7267SDuncan Sands #define DEFAULT_SW_BUFFERING	0
581da177e4SLinus Torvalds 
5980aae7a1SDuncan Sands static unsigned int altsetting = 0; /* zero means: use the default */
6090ab5ee9SRusty Russell static bool dl_512_first = DEFAULT_DL_512_FIRST;
6190ab5ee9SRusty Russell static bool enable_isoc = DEFAULT_ENABLE_ISOC;
6290ab5ee9SRusty Russell static bool sw_buffering = DEFAULT_SW_BUFFERING;
631da177e4SLinus Torvalds 
646a4f1b41SDuncan Sands #define DEFAULT_B_MAX_DSL	8128
656a4f1b41SDuncan Sands #define DEFAULT_MODEM_MODE	11
666a4f1b41SDuncan Sands #define MODEM_OPTION_LENGTH	16
676a4f1b41SDuncan Sands static const unsigned char DEFAULT_MODEM_OPTION[MODEM_OPTION_LENGTH] = {
686a4f1b41SDuncan Sands 	0x10, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
696a4f1b41SDuncan Sands };
706a4f1b41SDuncan Sands 
716a4f1b41SDuncan Sands static unsigned int BMaxDSL = DEFAULT_B_MAX_DSL;
726a4f1b41SDuncan Sands static unsigned char ModemMode = DEFAULT_MODEM_MODE;
736a4f1b41SDuncan Sands static unsigned char ModemOption[MODEM_OPTION_LENGTH];
7464a6f950SAl Viro static unsigned int num_ModemOption;
756a4f1b41SDuncan Sands 
7680aae7a1SDuncan Sands module_param(altsetting, uint, S_IRUGO | S_IWUSR);
7748da7267SDuncan Sands MODULE_PARM_DESC(altsetting,
7880aae7a1SDuncan Sands 		"Alternative setting for data interface (bulk_default: "
7980aae7a1SDuncan Sands 		__MODULE_STRING(DEFAULT_BULK_ALTSETTING) "; isoc_default: "
8080aae7a1SDuncan Sands 		__MODULE_STRING(DEFAULT_ISOC_ALTSETTING) ")");
8148da7267SDuncan Sands 
8248da7267SDuncan Sands module_param(dl_512_first, bool, S_IRUGO | S_IWUSR);
8348da7267SDuncan Sands MODULE_PARM_DESC(dl_512_first,
8448da7267SDuncan Sands 		 "Read 512 bytes before sending firmware (default: "
8548da7267SDuncan Sands 		 __MODULE_STRING(DEFAULT_DL_512_FIRST) ")");
8648da7267SDuncan Sands 
8780aae7a1SDuncan Sands module_param(enable_isoc, bool, S_IRUGO | S_IWUSR);
8880aae7a1SDuncan Sands MODULE_PARM_DESC(enable_isoc,
8980aae7a1SDuncan Sands 		"Use isochronous transfers if available (default: "
9080aae7a1SDuncan Sands 		__MODULE_STRING(DEFAULT_ENABLE_ISOC) ")");
9180aae7a1SDuncan Sands 
9248da7267SDuncan Sands module_param(sw_buffering, bool, S_IRUGO | S_IWUSR);
9348da7267SDuncan Sands MODULE_PARM_DESC(sw_buffering,
9448da7267SDuncan Sands 		 "Enable software buffering (default: "
9548da7267SDuncan Sands 		 __MODULE_STRING(DEFAULT_SW_BUFFERING) ")");
9648da7267SDuncan Sands 
976a4f1b41SDuncan Sands module_param(BMaxDSL, uint, S_IRUGO | S_IWUSR);
986a4f1b41SDuncan Sands MODULE_PARM_DESC(BMaxDSL,
996a4f1b41SDuncan Sands 		"default: " __MODULE_STRING(DEFAULT_B_MAX_DSL));
1006a4f1b41SDuncan Sands 
1016a4f1b41SDuncan Sands module_param(ModemMode, byte, S_IRUGO | S_IWUSR);
1026a4f1b41SDuncan Sands MODULE_PARM_DESC(ModemMode,
1036a4f1b41SDuncan Sands 		"default: " __MODULE_STRING(DEFAULT_MODEM_MODE));
1046a4f1b41SDuncan Sands 
1056a4f1b41SDuncan Sands module_param_array(ModemOption, byte, &num_ModemOption, S_IRUGO);
1066a4f1b41SDuncan Sands MODULE_PARM_DESC(ModemOption, "default: 0x10,0x00,0x00,0x00,0x20");
1076a4f1b41SDuncan Sands 
1086f749475SDuncan Sands #define INTERFACE_DATA		1
10948da7267SDuncan Sands #define ENDPOINT_INT		0x81
11080aae7a1SDuncan Sands #define ENDPOINT_BULK_DATA	0x07
11180aae7a1SDuncan Sands #define ENDPOINT_ISOC_DATA	0x07
11248da7267SDuncan Sands #define ENDPOINT_FIRMWARE	0x05
1131da177e4SLinus Torvalds 
1146a4f1b41SDuncan Sands struct speedtch_params {
1156a4f1b41SDuncan Sands 	unsigned int altsetting;
1166a4f1b41SDuncan Sands 	unsigned int BMaxDSL;
1176a4f1b41SDuncan Sands 	unsigned char ModemMode;
1186a4f1b41SDuncan Sands 	unsigned char ModemOption[MODEM_OPTION_LENGTH];
1196a4f1b41SDuncan Sands };
1206a4f1b41SDuncan Sands 
1211da177e4SLinus Torvalds struct speedtch_instance_data {
12248da7267SDuncan Sands 	struct usbatm_data *usbatm;
1231da177e4SLinus Torvalds 
1246a4f1b41SDuncan Sands 	struct speedtch_params params; /* set in probe, constant afterwards */
1256f749475SDuncan Sands 
12637c95bfeSTejun Heo 	struct timer_list status_check_timer;
12737c95bfeSTejun Heo 	struct work_struct status_check_work;
12848da7267SDuncan Sands 
1291a7aad15SDuncan Sands 	unsigned char last_status;
1301a7aad15SDuncan Sands 
13148da7267SDuncan Sands 	int poll_delay; /* milliseconds */
13248da7267SDuncan Sands 
13348da7267SDuncan Sands 	struct timer_list resubmit_timer;
1341da177e4SLinus Torvalds 	struct urb *int_urb;
1351da177e4SLinus Torvalds 	unsigned char int_data[16];
1361da177e4SLinus Torvalds 
1376a4f1b41SDuncan Sands 	unsigned char scratch_buffer[16];
1381da177e4SLinus Torvalds };
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds /***************
1411da177e4SLinus Torvalds **  firmware  **
1421da177e4SLinus Torvalds ***************/
1431da177e4SLinus Torvalds 
speedtch_set_swbuff(struct speedtch_instance_data * instance,int state)14448da7267SDuncan Sands static void speedtch_set_swbuff(struct speedtch_instance_data *instance, int state)
1451da177e4SLinus Torvalds {
14648da7267SDuncan Sands 	struct usbatm_data *usbatm = instance->usbatm;
14748da7267SDuncan Sands 	struct usb_device *usb_dev = usbatm->usb_dev;
1481da177e4SLinus Torvalds 	int ret;
1491da177e4SLinus Torvalds 
15048da7267SDuncan Sands 	ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
15148da7267SDuncan Sands 			      0x32, 0x40, state ? 0x01 : 0x00, 0x00, NULL, 0, CTRL_TIMEOUT);
15248da7267SDuncan Sands 	if (ret < 0)
15348da7267SDuncan Sands 		usb_warn(usbatm,
15448da7267SDuncan Sands 			 "%sabling SW buffering: usb_control_msg returned %d\n",
1551da177e4SLinus Torvalds 			 state ? "En" : "Dis", ret);
15648da7267SDuncan Sands 	else
157d8995425SGreg Kroah-Hartman 		usb_dbg(usbatm, "speedtch_set_swbuff: %sbled SW buffering\n", state ? "En" : "Dis");
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds 
speedtch_test_sequence(struct speedtch_instance_data * instance)1601da177e4SLinus Torvalds static void speedtch_test_sequence(struct speedtch_instance_data *instance)
1611da177e4SLinus Torvalds {
16248da7267SDuncan Sands 	struct usbatm_data *usbatm = instance->usbatm;
16348da7267SDuncan Sands 	struct usb_device *usb_dev = usbatm->usb_dev;
16448da7267SDuncan Sands 	unsigned char *buf = instance->scratch_buffer;
1651da177e4SLinus Torvalds 	int ret;
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	/* URB 147 */
1681da177e4SLinus Torvalds 	buf[0] = 0x1c;
1691da177e4SLinus Torvalds 	buf[1] = 0x50;
17048da7267SDuncan Sands 	ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
17148da7267SDuncan Sands 			      0x01, 0x40, 0x0b, 0x00, buf, 2, CTRL_TIMEOUT);
1721da177e4SLinus Torvalds 	if (ret < 0)
17348da7267SDuncan Sands 		usb_warn(usbatm, "%s failed on URB147: %d\n", __func__, ret);
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	/* URB 148 */
1761da177e4SLinus Torvalds 	buf[0] = 0x32;
1771da177e4SLinus Torvalds 	buf[1] = 0x00;
17848da7267SDuncan Sands 	ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
17948da7267SDuncan Sands 			      0x01, 0x40, 0x02, 0x00, buf, 2, CTRL_TIMEOUT);
1801da177e4SLinus Torvalds 	if (ret < 0)
18148da7267SDuncan Sands 		usb_warn(usbatm, "%s failed on URB148: %d\n", __func__, ret);
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	/* URB 149 */
1841da177e4SLinus Torvalds 	buf[0] = 0x01;
1851da177e4SLinus Torvalds 	buf[1] = 0x00;
1861da177e4SLinus Torvalds 	buf[2] = 0x01;
18748da7267SDuncan Sands 	ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
18848da7267SDuncan Sands 			      0x01, 0x40, 0x03, 0x00, buf, 3, CTRL_TIMEOUT);
1891da177e4SLinus Torvalds 	if (ret < 0)
19048da7267SDuncan Sands 		usb_warn(usbatm, "%s failed on URB149: %d\n", __func__, ret);
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	/* URB 150 */
1931da177e4SLinus Torvalds 	buf[0] = 0x01;
1941da177e4SLinus Torvalds 	buf[1] = 0x00;
1951da177e4SLinus Torvalds 	buf[2] = 0x01;
19648da7267SDuncan Sands 	ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
19748da7267SDuncan Sands 			      0x01, 0x40, 0x04, 0x00, buf, 3, CTRL_TIMEOUT);
1981da177e4SLinus Torvalds 	if (ret < 0)
19948da7267SDuncan Sands 		usb_warn(usbatm, "%s failed on URB150: %d\n", __func__, ret);
2006a4f1b41SDuncan Sands 
2016a4f1b41SDuncan Sands 	/* Extra initialisation in recent drivers - gives higher speeds */
2026a4f1b41SDuncan Sands 
2036a4f1b41SDuncan Sands 	/* URBext1 */
2046a4f1b41SDuncan Sands 	buf[0] = instance->params.ModemMode;
2056a4f1b41SDuncan Sands 	ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
2066a4f1b41SDuncan Sands 			      0x01, 0x40, 0x11, 0x00, buf, 1, CTRL_TIMEOUT);
2076a4f1b41SDuncan Sands 	if (ret < 0)
2086a4f1b41SDuncan Sands 		usb_warn(usbatm, "%s failed on URBext1: %d\n", __func__, ret);
2096a4f1b41SDuncan Sands 
2106a4f1b41SDuncan Sands 	/* URBext2 */
2116a4f1b41SDuncan Sands 	/* This seems to be the one which actually triggers the higher sync
2126a4f1b41SDuncan Sands 	   rate -- it does require the new firmware too, although it works OK
2136a4f1b41SDuncan Sands 	   with older firmware */
2146a4f1b41SDuncan Sands 	ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
2156a4f1b41SDuncan Sands 			      0x01, 0x40, 0x14, 0x00,
2166a4f1b41SDuncan Sands 			      instance->params.ModemOption,
2176a4f1b41SDuncan Sands 			      MODEM_OPTION_LENGTH, CTRL_TIMEOUT);
2186a4f1b41SDuncan Sands 	if (ret < 0)
2196a4f1b41SDuncan Sands 		usb_warn(usbatm, "%s failed on URBext2: %d\n", __func__, ret);
2206a4f1b41SDuncan Sands 
2216a4f1b41SDuncan Sands 	/* URBext3 */
2226a4f1b41SDuncan Sands 	buf[0] = instance->params.BMaxDSL & 0xff;
2236a4f1b41SDuncan Sands 	buf[1] = instance->params.BMaxDSL >> 8;
2246a4f1b41SDuncan Sands 	ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
2256a4f1b41SDuncan Sands 			      0x01, 0x40, 0x12, 0x00, buf, 2, CTRL_TIMEOUT);
2266a4f1b41SDuncan Sands 	if (ret < 0)
2276a4f1b41SDuncan Sands 		usb_warn(usbatm, "%s failed on URBext3: %d\n", __func__, ret);
2281da177e4SLinus Torvalds }
2291da177e4SLinus Torvalds 
speedtch_upload_firmware(struct speedtch_instance_data * instance,const struct firmware * fw1,const struct firmware * fw2)23048da7267SDuncan Sands static int speedtch_upload_firmware(struct speedtch_instance_data *instance,
2311da177e4SLinus Torvalds 				     const struct firmware *fw1,
2321da177e4SLinus Torvalds 				     const struct firmware *fw2)
2331da177e4SLinus Torvalds {
2341da177e4SLinus Torvalds 	unsigned char *buffer;
23548da7267SDuncan Sands 	struct usbatm_data *usbatm = instance->usbatm;
23648da7267SDuncan Sands 	struct usb_device *usb_dev = usbatm->usb_dev;
23748da7267SDuncan Sands 	int actual_length;
23848da7267SDuncan Sands 	int ret = 0;
2391da177e4SLinus Torvalds 	int offset;
2401da177e4SLinus Torvalds 
24148da7267SDuncan Sands 	usb_dbg(usbatm, "%s entered\n", __func__);
2421da177e4SLinus Torvalds 
2433383ee4cSGreg Kroah-Hartman 	buffer = (unsigned char *)__get_free_page(GFP_KERNEL);
2443383ee4cSGreg Kroah-Hartman 	if (!buffer) {
24548da7267SDuncan Sands 		ret = -ENOMEM;
24648da7267SDuncan Sands 		usb_dbg(usbatm, "%s: no memory for buffer!\n", __func__);
24748da7267SDuncan Sands 		goto out;
2481da177e4SLinus Torvalds 	}
2491da177e4SLinus Torvalds 
250011db815SMicah Gruber 	if (!usb_ifnum_to_if(usb_dev, 2)) {
25148da7267SDuncan Sands 		ret = -ENODEV;
25248da7267SDuncan Sands 		usb_dbg(usbatm, "%s: interface not found!\n", __func__);
25348da7267SDuncan Sands 		goto out_free;
2541da177e4SLinus Torvalds 	}
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	/* URB 7 */
2571da177e4SLinus Torvalds 	if (dl_512_first) {	/* some modems need a read before writing the firmware */
25848da7267SDuncan Sands 		ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
2591da177e4SLinus Torvalds 				   buffer, 0x200, &actual_length, 2000);
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 		if (ret < 0 && ret != -ETIMEDOUT)
2620ec3c7e8SDuncan Sands 			usb_warn(usbatm, "%s: read BLOCK0 from modem failed (%d)!\n", __func__, ret);
2631da177e4SLinus Torvalds 		else
26448da7267SDuncan Sands 			usb_dbg(usbatm, "%s: BLOCK0 downloaded (%d bytes)\n", __func__, ret);
2651da177e4SLinus Torvalds 	}
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 	/* URB 8 : both leds are static green */
2681da177e4SLinus Torvalds 	for (offset = 0; offset < fw1->size; offset += PAGE_SIZE) {
2691da177e4SLinus Torvalds 		int thislen = min_t(int, PAGE_SIZE, fw1->size - offset);
2701da177e4SLinus Torvalds 		memcpy(buffer, fw1->data + offset, thislen);
2711da177e4SLinus Torvalds 
27248da7267SDuncan Sands 		ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
2731da177e4SLinus Torvalds 				   buffer, thislen, &actual_length, DATA_TIMEOUT);
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 		if (ret < 0) {
2760ec3c7e8SDuncan Sands 			usb_err(usbatm, "%s: write BLOCK1 to modem failed (%d)!\n", __func__, ret);
27748da7267SDuncan Sands 			goto out_free;
2781da177e4SLinus Torvalds 		}
27948da7267SDuncan Sands 		usb_dbg(usbatm, "%s: BLOCK1 uploaded (%zu bytes)\n", __func__, fw1->size);
2801da177e4SLinus Torvalds 	}
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	/* USB led blinking green, ADSL led off */
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	/* URB 11 */
28548da7267SDuncan Sands 	ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
2861da177e4SLinus Torvalds 			   buffer, 0x200, &actual_length, DATA_TIMEOUT);
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	if (ret < 0) {
2890ec3c7e8SDuncan Sands 		usb_err(usbatm, "%s: read BLOCK2 from modem failed (%d)!\n", __func__, ret);
29048da7267SDuncan Sands 		goto out_free;
2911da177e4SLinus Torvalds 	}
29248da7267SDuncan Sands 	usb_dbg(usbatm, "%s: BLOCK2 downloaded (%d bytes)\n", __func__, actual_length);
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	/* URBs 12 to 139 - USB led blinking green, ADSL led off */
2951da177e4SLinus Torvalds 	for (offset = 0; offset < fw2->size; offset += PAGE_SIZE) {
2961da177e4SLinus Torvalds 		int thislen = min_t(int, PAGE_SIZE, fw2->size - offset);
2971da177e4SLinus Torvalds 		memcpy(buffer, fw2->data + offset, thislen);
2981da177e4SLinus Torvalds 
29948da7267SDuncan Sands 		ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
3001da177e4SLinus Torvalds 				   buffer, thislen, &actual_length, DATA_TIMEOUT);
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 		if (ret < 0) {
3030ec3c7e8SDuncan Sands 			usb_err(usbatm, "%s: write BLOCK3 to modem failed (%d)!\n", __func__, ret);
30448da7267SDuncan Sands 			goto out_free;
3051da177e4SLinus Torvalds 		}
3061da177e4SLinus Torvalds 	}
30748da7267SDuncan Sands 	usb_dbg(usbatm, "%s: BLOCK3 uploaded (%zu bytes)\n", __func__, fw2->size);
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	/* USB led static green, ADSL led static red */
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	/* URB 142 */
31248da7267SDuncan Sands 	ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
3131da177e4SLinus Torvalds 			   buffer, 0x200, &actual_length, DATA_TIMEOUT);
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	if (ret < 0) {
3160ec3c7e8SDuncan Sands 		usb_err(usbatm, "%s: read BLOCK4 from modem failed (%d)!\n", __func__, ret);
31748da7267SDuncan Sands 		goto out_free;
3181da177e4SLinus Torvalds 	}
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	/* success */
32148da7267SDuncan Sands 	usb_dbg(usbatm, "%s: BLOCK4 downloaded (%d bytes)\n", __func__, actual_length);
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds 	/* Delay to allow firmware to start up. We can do this here
3241da177e4SLinus Torvalds 	   because we're in our own kernel thread anyway. */
32548da7267SDuncan Sands 	msleep_interruptible(1000);
3261da177e4SLinus Torvalds 
3276a4f1b41SDuncan Sands 	if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, instance->params.altsetting)) < 0) {
3286a4f1b41SDuncan Sands 		usb_err(usbatm, "%s: setting interface to %d failed (%d)!\n", __func__, instance->params.altsetting, ret);
3296f749475SDuncan Sands 		goto out_free;
3306f749475SDuncan Sands 	}
3316f749475SDuncan Sands 
3321da177e4SLinus Torvalds 	/* Enable software buffering, if requested */
3331da177e4SLinus Torvalds 	if (sw_buffering)
3341da177e4SLinus Torvalds 		speedtch_set_swbuff(instance, 1);
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	/* Magic spell; don't ask us what this does */
3371da177e4SLinus Torvalds 	speedtch_test_sequence(instance);
3381da177e4SLinus Torvalds 
33948da7267SDuncan Sands 	ret = 0;
3401da177e4SLinus Torvalds 
34148da7267SDuncan Sands out_free:
3421da177e4SLinus Torvalds 	free_page((unsigned long)buffer);
34348da7267SDuncan Sands out:
34448da7267SDuncan Sands 	return ret;
3451da177e4SLinus Torvalds }
3461da177e4SLinus Torvalds 
speedtch_find_firmware(struct usbatm_data * usbatm,struct usb_interface * intf,int phase,const struct firmware ** fw_p)3470ec3c7e8SDuncan Sands static int speedtch_find_firmware(struct usbatm_data *usbatm, struct usb_interface *intf,
3480ec3c7e8SDuncan Sands 				  int phase, const struct firmware **fw_p)
3491da177e4SLinus Torvalds {
35048da7267SDuncan Sands 	struct device *dev = &intf->dev;
35148da7267SDuncan Sands 	const u16 bcdDevice = le16_to_cpu(interface_to_usbdev(intf)->descriptor.bcdDevice);
3521da177e4SLinus Torvalds 	const u8 major_revision = bcdDevice >> 8;
3531da177e4SLinus Torvalds 	const u8 minor_revision = bcdDevice & 0xff;
35448da7267SDuncan Sands 	char buf[24];
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 	sprintf(buf, "speedtch-%d.bin.%x.%02x", phase, major_revision, minor_revision);
3570ec3c7e8SDuncan Sands 	usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
3581da177e4SLinus Torvalds 
35948da7267SDuncan Sands 	if (request_firmware(fw_p, buf, dev)) {
3601da177e4SLinus Torvalds 		sprintf(buf, "speedtch-%d.bin.%x", phase, major_revision);
3610ec3c7e8SDuncan Sands 		usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
3621da177e4SLinus Torvalds 
36348da7267SDuncan Sands 		if (request_firmware(fw_p, buf, dev)) {
3641da177e4SLinus Torvalds 			sprintf(buf, "speedtch-%d.bin", phase);
3650ec3c7e8SDuncan Sands 			usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
3661da177e4SLinus Torvalds 
36748da7267SDuncan Sands 			if (request_firmware(fw_p, buf, dev)) {
3680ec3c7e8SDuncan Sands 				usb_err(usbatm, "%s: no stage %d firmware found!\n", __func__, phase);
3691da177e4SLinus Torvalds 				return -ENOENT;
3701da177e4SLinus Torvalds 			}
3711da177e4SLinus Torvalds 		}
3721da177e4SLinus Torvalds 	}
3731da177e4SLinus Torvalds 
3740ec3c7e8SDuncan Sands 	usb_info(usbatm, "found stage %d firmware %s\n", phase, buf);
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 	return 0;
3771da177e4SLinus Torvalds }
3781da177e4SLinus Torvalds 
speedtch_heavy_init(struct usbatm_data * usbatm,struct usb_interface * intf)37948da7267SDuncan Sands static int speedtch_heavy_init(struct usbatm_data *usbatm, struct usb_interface *intf)
3801da177e4SLinus Torvalds {
3811da177e4SLinus Torvalds 	const struct firmware *fw1, *fw2;
38248da7267SDuncan Sands 	struct speedtch_instance_data *instance = usbatm->driver_data;
38348da7267SDuncan Sands 	int ret;
3841da177e4SLinus Torvalds 
3850ec3c7e8SDuncan Sands 	if ((ret = speedtch_find_firmware(usbatm, intf, 1, &fw1)) < 0)
38648da7267SDuncan Sands 		return ret;
3871da177e4SLinus Torvalds 
3880ec3c7e8SDuncan Sands 	if ((ret = speedtch_find_firmware(usbatm, intf, 2, &fw2)) < 0) {
3891da177e4SLinus Torvalds 		release_firmware(fw1);
39048da7267SDuncan Sands 		return ret;
3911da177e4SLinus Torvalds 	}
3921da177e4SLinus Torvalds 
3930ec3c7e8SDuncan Sands 	if ((ret = speedtch_upload_firmware(instance, fw1, fw2)) < 0)
3940ec3c7e8SDuncan Sands 		usb_err(usbatm, "%s: firmware upload failed (%d)!\n", __func__, ret);
3951da177e4SLinus Torvalds 
39648da7267SDuncan Sands 	release_firmware(fw2);
39748da7267SDuncan Sands 	release_firmware(fw1);
39848da7267SDuncan Sands 
39948da7267SDuncan Sands 	return ret;
40048da7267SDuncan Sands }
40148da7267SDuncan Sands 
40248da7267SDuncan Sands 
40348da7267SDuncan Sands /**********
40448da7267SDuncan Sands **  ATM  **
40548da7267SDuncan Sands **********/
40648da7267SDuncan Sands 
speedtch_read_status(struct speedtch_instance_data * instance)40748da7267SDuncan Sands static int speedtch_read_status(struct speedtch_instance_data *instance)
40848da7267SDuncan Sands {
40948da7267SDuncan Sands 	struct usbatm_data *usbatm = instance->usbatm;
41048da7267SDuncan Sands 	struct usb_device *usb_dev = usbatm->usb_dev;
41148da7267SDuncan Sands 	unsigned char *buf = instance->scratch_buffer;
41248da7267SDuncan Sands 	int ret;
41348da7267SDuncan Sands 
4146a4f1b41SDuncan Sands 	memset(buf, 0, 16);
41548da7267SDuncan Sands 
41648da7267SDuncan Sands 	ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
41748da7267SDuncan Sands 			      0x12, 0xc0, 0x07, 0x00, buf + OFFSET_7, SIZE_7,
41848da7267SDuncan Sands 			      CTRL_TIMEOUT);
41948da7267SDuncan Sands 	if (ret < 0) {
42048da7267SDuncan Sands 		atm_dbg(usbatm, "%s: MSG 7 failed\n", __func__);
42148da7267SDuncan Sands 		return ret;
42248da7267SDuncan Sands 	}
42348da7267SDuncan Sands 
42448da7267SDuncan Sands 	ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
42548da7267SDuncan Sands 			      0x12, 0xc0, 0x0b, 0x00, buf + OFFSET_b, SIZE_b,
42648da7267SDuncan Sands 			      CTRL_TIMEOUT);
42748da7267SDuncan Sands 	if (ret < 0) {
42848da7267SDuncan Sands 		atm_dbg(usbatm, "%s: MSG B failed\n", __func__);
42948da7267SDuncan Sands 		return ret;
43048da7267SDuncan Sands 	}
43148da7267SDuncan Sands 
43248da7267SDuncan Sands 	ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
43348da7267SDuncan Sands 			      0x12, 0xc0, 0x0d, 0x00, buf + OFFSET_d, SIZE_d,
43448da7267SDuncan Sands 			      CTRL_TIMEOUT);
43548da7267SDuncan Sands 	if (ret < 0) {
43648da7267SDuncan Sands 		atm_dbg(usbatm, "%s: MSG D failed\n", __func__);
43748da7267SDuncan Sands 		return ret;
43848da7267SDuncan Sands 	}
43948da7267SDuncan Sands 
44048da7267SDuncan Sands 	ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
44148da7267SDuncan Sands 			      0x01, 0xc0, 0x0e, 0x00, buf + OFFSET_e, SIZE_e,
44248da7267SDuncan Sands 			      CTRL_TIMEOUT);
44348da7267SDuncan Sands 	if (ret < 0) {
44448da7267SDuncan Sands 		atm_dbg(usbatm, "%s: MSG E failed\n", __func__);
44548da7267SDuncan Sands 		return ret;
44648da7267SDuncan Sands 	}
44748da7267SDuncan Sands 
44848da7267SDuncan Sands 	ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
44948da7267SDuncan Sands 			      0x01, 0xc0, 0x0f, 0x00, buf + OFFSET_f, SIZE_f,
45048da7267SDuncan Sands 			      CTRL_TIMEOUT);
45148da7267SDuncan Sands 	if (ret < 0) {
45248da7267SDuncan Sands 		atm_dbg(usbatm, "%s: MSG F failed\n", __func__);
45348da7267SDuncan Sands 		return ret;
45448da7267SDuncan Sands 	}
45548da7267SDuncan Sands 
4561da177e4SLinus Torvalds 	return 0;
4571da177e4SLinus Torvalds }
4581da177e4SLinus Torvalds 
speedtch_start_synchro(struct speedtch_instance_data * instance)45948da7267SDuncan Sands static int speedtch_start_synchro(struct speedtch_instance_data *instance)
4601da177e4SLinus Torvalds {
46148da7267SDuncan Sands 	struct usbatm_data *usbatm = instance->usbatm;
46248da7267SDuncan Sands 	struct usb_device *usb_dev = usbatm->usb_dev;
46348da7267SDuncan Sands 	unsigned char *buf = instance->scratch_buffer;
4641da177e4SLinus Torvalds 	int ret;
4651da177e4SLinus Torvalds 
46648da7267SDuncan Sands 	atm_dbg(usbatm, "%s entered\n", __func__);
4671da177e4SLinus Torvalds 
46848da7267SDuncan Sands 	memset(buf, 0, 2);
4691da177e4SLinus Torvalds 
47048da7267SDuncan Sands 	ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
47148da7267SDuncan Sands 			      0x12, 0xc0, 0x04, 0x00,
47248da7267SDuncan Sands 			      buf, 2, CTRL_TIMEOUT);
47348da7267SDuncan Sands 
47448da7267SDuncan Sands 	if (ret < 0)
47548da7267SDuncan Sands 		atm_warn(usbatm, "failed to start ADSL synchronisation: %d\n", ret);
47648da7267SDuncan Sands 	else
47748da7267SDuncan Sands 		atm_dbg(usbatm, "%s: modem prodded. %d bytes returned: %02x %02x\n",
47848da7267SDuncan Sands 			__func__, ret, buf[0], buf[1]);
47948da7267SDuncan Sands 
48048da7267SDuncan Sands 	return ret;
48148da7267SDuncan Sands }
48248da7267SDuncan Sands 
speedtch_check_status(struct work_struct * work)483c4028958SDavid Howells static void speedtch_check_status(struct work_struct *work)
48448da7267SDuncan Sands {
485c4028958SDavid Howells 	struct speedtch_instance_data *instance =
486c4028958SDavid Howells 		container_of(work, struct speedtch_instance_data,
48737c95bfeSTejun Heo 			     status_check_work);
48848da7267SDuncan Sands 	struct usbatm_data *usbatm = instance->usbatm;
48948da7267SDuncan Sands 	struct atm_dev *atm_dev = usbatm->atm_dev;
49048da7267SDuncan Sands 	unsigned char *buf = instance->scratch_buffer;
4911a7aad15SDuncan Sands 	int down_speed, up_speed, ret;
4921a7aad15SDuncan Sands 	unsigned char status;
49348da7267SDuncan Sands 
4940ec3c7e8SDuncan Sands #ifdef VERBOSE_DEBUG
49548da7267SDuncan Sands 	atm_dbg(usbatm, "%s entered\n", __func__);
4960ec3c7e8SDuncan Sands #endif
49748da7267SDuncan Sands 
49848da7267SDuncan Sands 	ret = speedtch_read_status(instance);
49948da7267SDuncan Sands 	if (ret < 0) {
50048da7267SDuncan Sands 		atm_warn(usbatm, "error %d fetching device status\n", ret);
501cd5c08fbSDuncan Sands 		instance->poll_delay = min(2 * instance->poll_delay, MAX_POLL_DELAY);
5021da177e4SLinus Torvalds 		return;
5031da177e4SLinus Torvalds 	}
5041da177e4SLinus Torvalds 
505cd5c08fbSDuncan Sands 	instance->poll_delay = max(instance->poll_delay / 2, MIN_POLL_DELAY);
5061da177e4SLinus Torvalds 
5071a7aad15SDuncan Sands 	status = buf[OFFSET_7];
5081da177e4SLinus Torvalds 
5091a7aad15SDuncan Sands 	if ((status != instance->last_status) || !status) {
5100ec3c7e8SDuncan Sands 		atm_dbg(usbatm, "%s: line state 0x%02x\n", __func__, status);
5110ec3c7e8SDuncan Sands 
5121a7aad15SDuncan Sands 		switch (status) {
51348da7267SDuncan Sands 		case 0:
51423f89f04SKarl Hiramoto 			atm_dev_signal_change(atm_dev, ATM_PHY_SIG_LOST);
5151a7aad15SDuncan Sands 			if (instance->last_status)
51652fbae2aSDavid S. Miller 				atm_info(usbatm, "ADSL line is down\n");
5171a7aad15SDuncan Sands 			/* It may never resync again unless we ask it to... */
51848da7267SDuncan Sands 			ret = speedtch_start_synchro(instance);
51948da7267SDuncan Sands 			break;
5201da177e4SLinus Torvalds 
52148da7267SDuncan Sands 		case 0x08:
52223f89f04SKarl Hiramoto 			atm_dev_signal_change(atm_dev, ATM_PHY_SIG_UNKNOWN);
52352fbae2aSDavid S. Miller 			atm_info(usbatm, "ADSL line is blocked?\n");
52448da7267SDuncan Sands 			break;
5251da177e4SLinus Torvalds 
52648da7267SDuncan Sands 		case 0x10:
52723f89f04SKarl Hiramoto 			atm_dev_signal_change(atm_dev, ATM_PHY_SIG_LOST);
52852fbae2aSDavid S. Miller 			atm_info(usbatm, "ADSL line is synchronising\n");
52948da7267SDuncan Sands 			break;
5301da177e4SLinus Torvalds 
53148da7267SDuncan Sands 		case 0x20:
5321a7aad15SDuncan Sands 			down_speed = buf[OFFSET_b] | (buf[OFFSET_b + 1] << 8)
53348da7267SDuncan Sands 				| (buf[OFFSET_b + 2] << 16) | (buf[OFFSET_b + 3] << 24);
5341a7aad15SDuncan Sands 			up_speed = buf[OFFSET_b + 4] | (buf[OFFSET_b + 5] << 8)
53548da7267SDuncan Sands 				| (buf[OFFSET_b + 6] << 16) | (buf[OFFSET_b + 7] << 24);
5361da177e4SLinus Torvalds 
53748da7267SDuncan Sands 			if (!(down_speed & 0x0000ffff) && !(up_speed & 0x0000ffff)) {
53848da7267SDuncan Sands 				down_speed >>= 16;
53948da7267SDuncan Sands 				up_speed >>= 16;
5401da177e4SLinus Torvalds 			}
5411da177e4SLinus Torvalds 
54248da7267SDuncan Sands 			atm_dev->link_rate = down_speed * 1000 / 424;
54323f89f04SKarl Hiramoto 			atm_dev_signal_change(atm_dev, ATM_PHY_SIG_FOUND);
54448da7267SDuncan Sands 
54548da7267SDuncan Sands 			atm_info(usbatm,
546322a95bcSDuncan Sands 				 "ADSL line is up (%d kb/s down | %d kb/s up)\n",
54748da7267SDuncan Sands 				 down_speed, up_speed);
54848da7267SDuncan Sands 			break;
54948da7267SDuncan Sands 
55048da7267SDuncan Sands 		default:
55123f89f04SKarl Hiramoto 			atm_dev_signal_change(atm_dev, ATM_PHY_SIG_UNKNOWN);
5520ec3c7e8SDuncan Sands 			atm_info(usbatm, "unknown line state %02x\n", status);
55348da7267SDuncan Sands 			break;
55448da7267SDuncan Sands 		}
5551a7aad15SDuncan Sands 
5561a7aad15SDuncan Sands 		instance->last_status = status;
5571a7aad15SDuncan Sands 	}
55848da7267SDuncan Sands }
55948da7267SDuncan Sands 
speedtch_status_poll(struct timer_list * t)560*72a9f9a4SKees Cook static void speedtch_status_poll(struct timer_list *t)
5611da177e4SLinus Torvalds {
562*72a9f9a4SKees Cook 	struct speedtch_instance_data *instance = from_timer(instance, t,
563*72a9f9a4SKees Cook 						             status_check_timer);
5641da177e4SLinus Torvalds 
56537c95bfeSTejun Heo 	schedule_work(&instance->status_check_work);
5661da177e4SLinus Torvalds 
56748da7267SDuncan Sands 	/* The following check is racy, but the race is harmless */
56848da7267SDuncan Sands 	if (instance->poll_delay < MAX_POLL_DELAY)
56937c95bfeSTejun Heo 		mod_timer(&instance->status_check_timer, jiffies + msecs_to_jiffies(instance->poll_delay));
57048da7267SDuncan Sands 	else
57152fbae2aSDavid S. Miller 		atm_warn(instance->usbatm, "Too many failures - disabling line status polling\n");
5721da177e4SLinus Torvalds }
5731da177e4SLinus Torvalds 
speedtch_resubmit_int(struct timer_list * t)574*72a9f9a4SKees Cook static void speedtch_resubmit_int(struct timer_list *t)
57548da7267SDuncan Sands {
576*72a9f9a4SKees Cook 	struct speedtch_instance_data *instance = from_timer(instance, t,
577*72a9f9a4SKees Cook 							     resubmit_timer);
57848da7267SDuncan Sands 	struct urb *int_urb = instance->int_urb;
57948da7267SDuncan Sands 	int ret;
58048da7267SDuncan Sands 
58148da7267SDuncan Sands 	atm_dbg(instance->usbatm, "%s entered\n", __func__);
58248da7267SDuncan Sands 
58348da7267SDuncan Sands 	if (int_urb) {
58448da7267SDuncan Sands 		ret = usb_submit_urb(int_urb, GFP_ATOMIC);
58548da7267SDuncan Sands 		if (!ret)
58637c95bfeSTejun Heo 			schedule_work(&instance->status_check_work);
58748da7267SDuncan Sands 		else {
58848da7267SDuncan Sands 			atm_dbg(instance->usbatm, "%s: usb_submit_urb failed with result %d\n", __func__, ret);
58948da7267SDuncan Sands 			mod_timer(&instance->resubmit_timer, jiffies + msecs_to_jiffies(RESUBMIT_DELAY));
59048da7267SDuncan Sands 		}
59148da7267SDuncan Sands 	}
59248da7267SDuncan Sands }
59348da7267SDuncan Sands 
speedtch_handle_int(struct urb * int_urb)5947d12e780SDavid Howells static void speedtch_handle_int(struct urb *int_urb)
59548da7267SDuncan Sands {
59648da7267SDuncan Sands 	struct speedtch_instance_data *instance = int_urb->context;
59748da7267SDuncan Sands 	struct usbatm_data *usbatm = instance->usbatm;
59848da7267SDuncan Sands 	unsigned int count = int_urb->actual_length;
5999a5a3e95SGreg Kroah-Hartman 	int status = int_urb->status;
6009a5a3e95SGreg Kroah-Hartman 	int ret;
60148da7267SDuncan Sands 
60248da7267SDuncan Sands 	/* The magic interrupt for "up state" */
6033c6bee1dSJesper Juhl 	static const unsigned char up_int[6]   = { 0xa1, 0x00, 0x01, 0x00, 0x00, 0x00 };
60448da7267SDuncan Sands 	/* The magic interrupt for "down state" */
6053c6bee1dSJesper Juhl 	static const unsigned char down_int[6] = { 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00 };
60648da7267SDuncan Sands 
60748da7267SDuncan Sands 	atm_dbg(usbatm, "%s entered\n", __func__);
60848da7267SDuncan Sands 
6099a5a3e95SGreg Kroah-Hartman 	if (status < 0) {
6109a5a3e95SGreg Kroah-Hartman 		atm_dbg(usbatm, "%s: nonzero urb status %d!\n", __func__, status);
61148da7267SDuncan Sands 		goto fail;
61248da7267SDuncan Sands 	}
61348da7267SDuncan Sands 
61448da7267SDuncan Sands 	if ((count == 6) && !memcmp(up_int, instance->int_data, 6)) {
61537c95bfeSTejun Heo 		del_timer(&instance->status_check_timer);
61652fbae2aSDavid S. Miller 		atm_info(usbatm, "DSL line goes up\n");
61748da7267SDuncan Sands 	} else if ((count == 6) && !memcmp(down_int, instance->int_data, 6)) {
61852fbae2aSDavid S. Miller 		atm_info(usbatm, "DSL line goes down\n");
61948da7267SDuncan Sands 	} else {
62048da7267SDuncan Sands 		int i;
62148da7267SDuncan Sands 
62248da7267SDuncan Sands 		atm_dbg(usbatm, "%s: unknown interrupt packet of length %d:", __func__, count);
62348da7267SDuncan Sands 		for (i = 0; i < count; i++)
62448da7267SDuncan Sands 			printk(" %02x", instance->int_data[i]);
62548da7267SDuncan Sands 		printk("\n");
62648da7267SDuncan Sands 		goto fail;
62748da7267SDuncan Sands 	}
62848da7267SDuncan Sands 
6293383ee4cSGreg Kroah-Hartman 	int_urb = instance->int_urb;
6303383ee4cSGreg Kroah-Hartman 	if (int_urb) {
63148da7267SDuncan Sands 		ret = usb_submit_urb(int_urb, GFP_ATOMIC);
63237c95bfeSTejun Heo 		schedule_work(&instance->status_check_work);
63348da7267SDuncan Sands 		if (ret < 0) {
63448da7267SDuncan Sands 			atm_dbg(usbatm, "%s: usb_submit_urb failed with result %d\n", __func__, ret);
63548da7267SDuncan Sands 			goto fail;
63648da7267SDuncan Sands 		}
63748da7267SDuncan Sands 	}
63848da7267SDuncan Sands 
63948da7267SDuncan Sands 	return;
64048da7267SDuncan Sands 
64148da7267SDuncan Sands fail:
6423383ee4cSGreg Kroah-Hartman 	int_urb = instance->int_urb;
6433383ee4cSGreg Kroah-Hartman 	if (int_urb)
64448da7267SDuncan Sands 		mod_timer(&instance->resubmit_timer, jiffies + msecs_to_jiffies(RESUBMIT_DELAY));
64548da7267SDuncan Sands }
64648da7267SDuncan Sands 
speedtch_atm_start(struct usbatm_data * usbatm,struct atm_dev * atm_dev)64748da7267SDuncan Sands static int speedtch_atm_start(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
64848da7267SDuncan Sands {
64948da7267SDuncan Sands 	struct usb_device *usb_dev = usbatm->usb_dev;
65048da7267SDuncan Sands 	struct speedtch_instance_data *instance = usbatm->driver_data;
65148da7267SDuncan Sands 	int i, ret;
65248da7267SDuncan Sands 	unsigned char mac_str[13];
65348da7267SDuncan Sands 
65448da7267SDuncan Sands 	atm_dbg(usbatm, "%s entered\n", __func__);
65548da7267SDuncan Sands 
65648da7267SDuncan Sands 	/* Set MAC address, it is stored in the serial number */
65748da7267SDuncan Sands 	memset(atm_dev->esi, 0, sizeof(atm_dev->esi));
65848da7267SDuncan Sands 	if (usb_string(usb_dev, usb_dev->descriptor.iSerialNumber, mac_str, sizeof(mac_str)) == 12) {
65948da7267SDuncan Sands 		for (i = 0; i < 6; i++)
66096b89f32SAndy Shevchenko 			atm_dev->esi[i] = (hex_to_bin(mac_str[i * 2]) << 4) +
66196b89f32SAndy Shevchenko 				hex_to_bin(mac_str[i * 2 + 1]);
66248da7267SDuncan Sands 	}
66348da7267SDuncan Sands 
66448da7267SDuncan Sands 	/* Start modem synchronisation */
66548da7267SDuncan Sands 	ret = speedtch_start_synchro(instance);
66648da7267SDuncan Sands 
66748da7267SDuncan Sands 	/* Set up interrupt endpoint */
66848da7267SDuncan Sands 	if (instance->int_urb) {
66948da7267SDuncan Sands 		ret = usb_submit_urb(instance->int_urb, GFP_KERNEL);
67048da7267SDuncan Sands 		if (ret < 0) {
67148da7267SDuncan Sands 			/* Doesn't matter; we'll poll anyway */
67248da7267SDuncan Sands 			atm_dbg(usbatm, "%s: submission of interrupt URB failed (%d)!\n", __func__, ret);
67348da7267SDuncan Sands 			usb_free_urb(instance->int_urb);
67448da7267SDuncan Sands 			instance->int_urb = NULL;
67548da7267SDuncan Sands 		}
67648da7267SDuncan Sands 	}
67748da7267SDuncan Sands 
67848da7267SDuncan Sands 	/* Start status polling */
67937c95bfeSTejun Heo 	mod_timer(&instance->status_check_timer, jiffies + msecs_to_jiffies(1000));
68048da7267SDuncan Sands 
68148da7267SDuncan Sands 	return 0;
68248da7267SDuncan Sands }
68348da7267SDuncan Sands 
speedtch_atm_stop(struct usbatm_data * usbatm,struct atm_dev * atm_dev)68448da7267SDuncan Sands static void speedtch_atm_stop(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
68548da7267SDuncan Sands {
68648da7267SDuncan Sands 	struct speedtch_instance_data *instance = usbatm->driver_data;
68748da7267SDuncan Sands 	struct urb *int_urb = instance->int_urb;
68848da7267SDuncan Sands 
68948da7267SDuncan Sands 	atm_dbg(usbatm, "%s entered\n", __func__);
69048da7267SDuncan Sands 
69137c95bfeSTejun Heo 	del_timer_sync(&instance->status_check_timer);
69248da7267SDuncan Sands 
69348da7267SDuncan Sands 	/*
69448da7267SDuncan Sands 	 * Since resubmit_timer and int_urb can schedule themselves and
69548da7267SDuncan Sands 	 * each other, shutting them down correctly takes some care
69648da7267SDuncan Sands 	 */
69748da7267SDuncan Sands 	instance->int_urb = NULL; /* signal shutdown */
69848da7267SDuncan Sands 	mb();
69948da7267SDuncan Sands 	usb_kill_urb(int_urb);
70048da7267SDuncan Sands 	del_timer_sync(&instance->resubmit_timer);
70148da7267SDuncan Sands 	/*
70248da7267SDuncan Sands 	 * At this point, speedtch_handle_int and speedtch_resubmit_int
70348da7267SDuncan Sands 	 * can run or be running, but instance->int_urb == NULL means that
70448da7267SDuncan Sands 	 * they will not reschedule
70548da7267SDuncan Sands 	 */
70648da7267SDuncan Sands 	usb_kill_urb(int_urb);
70748da7267SDuncan Sands 	del_timer_sync(&instance->resubmit_timer);
70848da7267SDuncan Sands 	usb_free_urb(int_urb);
70948da7267SDuncan Sands 
71043829731STejun Heo 	flush_work(&instance->status_check_work);
71148da7267SDuncan Sands }
71248da7267SDuncan Sands 
speedtch_pre_reset(struct usb_interface * intf)7138fc7aeabSAlan Stern static int speedtch_pre_reset(struct usb_interface *intf)
7148fc7aeabSAlan Stern {
7158fc7aeabSAlan Stern 	return 0;
7168fc7aeabSAlan Stern }
7178fc7aeabSAlan Stern 
speedtch_post_reset(struct usb_interface * intf)7188fc7aeabSAlan Stern static int speedtch_post_reset(struct usb_interface *intf)
7198fc7aeabSAlan Stern {
7208fc7aeabSAlan Stern 	return 0;
7218fc7aeabSAlan Stern }
7228fc7aeabSAlan Stern 
72348da7267SDuncan Sands 
7241da177e4SLinus Torvalds /**********
7251da177e4SLinus Torvalds **  USB  **
7261da177e4SLinus Torvalds **********/
7271da177e4SLinus Torvalds 
7281e44f54bSArvind Yadav static const struct usb_device_id speedtch_usb_ids[] = {
72948da7267SDuncan Sands 	{USB_DEVICE(0x06b9, 0x4061)},
73048da7267SDuncan Sands 	{}
73148da7267SDuncan Sands };
73248da7267SDuncan Sands 
73348da7267SDuncan Sands MODULE_DEVICE_TABLE(usb, speedtch_usb_ids);
73448da7267SDuncan Sands 
73548da7267SDuncan Sands static int speedtch_usb_probe(struct usb_interface *, const struct usb_device_id *);
73648da7267SDuncan Sands 
73748da7267SDuncan Sands static struct usb_driver speedtch_usb_driver = {
73848da7267SDuncan Sands 	.name		= speedtch_driver_name,
73948da7267SDuncan Sands 	.probe		= speedtch_usb_probe,
74048da7267SDuncan Sands 	.disconnect	= usbatm_usb_disconnect,
7418fc7aeabSAlan Stern 	.pre_reset	= speedtch_pre_reset,
7428fc7aeabSAlan Stern 	.post_reset	= speedtch_post_reset,
74348da7267SDuncan Sands 	.id_table	= speedtch_usb_ids
74448da7267SDuncan Sands };
74548da7267SDuncan Sands 
speedtch_release_interfaces(struct usb_device * usb_dev,int num_interfaces)7466c4b7f70SNicolas Kaiser static void speedtch_release_interfaces(struct usb_device *usb_dev,
7476c4b7f70SNicolas Kaiser 					int num_interfaces)
7486c4b7f70SNicolas Kaiser {
74948da7267SDuncan Sands 	struct usb_interface *cur_intf;
75048da7267SDuncan Sands 	int i;
75148da7267SDuncan Sands 
7523383ee4cSGreg Kroah-Hartman 	for (i = 0; i < num_interfaces; i++) {
7533383ee4cSGreg Kroah-Hartman 		cur_intf = usb_ifnum_to_if(usb_dev, i);
7543383ee4cSGreg Kroah-Hartman 		if (cur_intf) {
75548da7267SDuncan Sands 			usb_set_intfdata(cur_intf, NULL);
75648da7267SDuncan Sands 			usb_driver_release_interface(&speedtch_usb_driver, cur_intf);
75748da7267SDuncan Sands 		}
75848da7267SDuncan Sands 	}
7593383ee4cSGreg Kroah-Hartman }
76048da7267SDuncan Sands 
speedtch_bind(struct usbatm_data * usbatm,struct usb_interface * intf,const struct usb_device_id * id)76148da7267SDuncan Sands static int speedtch_bind(struct usbatm_data *usbatm,
76248da7267SDuncan Sands 			 struct usb_interface *intf,
76335644b0cSDuncan Sands 			 const struct usb_device_id *id)
7641da177e4SLinus Torvalds {
76548da7267SDuncan Sands 	struct usb_device *usb_dev = interface_to_usbdev(intf);
76680aae7a1SDuncan Sands 	struct usb_interface *cur_intf, *data_intf;
7671da177e4SLinus Torvalds 	struct speedtch_instance_data *instance;
76848da7267SDuncan Sands 	int ifnum = intf->altsetting->desc.bInterfaceNumber;
76948da7267SDuncan Sands 	int num_interfaces = usb_dev->actconfig->desc.bNumInterfaces;
77048da7267SDuncan Sands 	int i, ret;
77180aae7a1SDuncan Sands 	int use_isoc;
7721da177e4SLinus Torvalds 
77348da7267SDuncan Sands 	usb_dbg(usbatm, "%s entered\n", __func__);
7741da177e4SLinus Torvalds 
7750ec3c7e8SDuncan Sands 	/* sanity checks */
7760ec3c7e8SDuncan Sands 
77748da7267SDuncan Sands 	if (usb_dev->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) {
7780ec3c7e8SDuncan Sands 		usb_err(usbatm, "%s: wrong device class %d\n", __func__, usb_dev->descriptor.bDeviceClass);
7791da177e4SLinus Torvalds 		return -ENODEV;
78048da7267SDuncan Sands 	}
7811da177e4SLinus Torvalds 
7823383ee4cSGreg Kroah-Hartman 	data_intf = usb_ifnum_to_if(usb_dev, INTERFACE_DATA);
7833383ee4cSGreg Kroah-Hartman 	if (!data_intf) {
78480aae7a1SDuncan Sands 		usb_err(usbatm, "%s: data interface not found!\n", __func__);
78580aae7a1SDuncan Sands 		return -ENODEV;
78680aae7a1SDuncan Sands 	}
78780aae7a1SDuncan Sands 
78848da7267SDuncan Sands 	/* claim all interfaces */
7891da177e4SLinus Torvalds 
79048da7267SDuncan Sands 	for (i = 0; i < num_interfaces; i++) {
79148da7267SDuncan Sands 		cur_intf = usb_ifnum_to_if(usb_dev, i);
79248da7267SDuncan Sands 
79348da7267SDuncan Sands 		if ((i != ifnum) && cur_intf) {
79448da7267SDuncan Sands 			ret = usb_driver_claim_interface(&speedtch_usb_driver, cur_intf, usbatm);
79548da7267SDuncan Sands 
79648da7267SDuncan Sands 			if (ret < 0) {
7970ec3c7e8SDuncan Sands 				usb_err(usbatm, "%s: failed to claim interface %2d (%d)!\n", __func__, i, ret);
79848da7267SDuncan Sands 				speedtch_release_interfaces(usb_dev, i);
79948da7267SDuncan Sands 				return ret;
80048da7267SDuncan Sands 			}
80148da7267SDuncan Sands 		}
80248da7267SDuncan Sands 	}
80348da7267SDuncan Sands 
8049a734efeSDuncan Sands 	instance = kzalloc(sizeof(*instance), GFP_KERNEL);
80548da7267SDuncan Sands 
8061da177e4SLinus Torvalds 	if (!instance) {
80748da7267SDuncan Sands 		ret = -ENOMEM;
80848da7267SDuncan Sands 		goto fail_release;
8091da177e4SLinus Torvalds 	}
8101da177e4SLinus Torvalds 
81148da7267SDuncan Sands 	instance->usbatm = usbatm;
8121da177e4SLinus Torvalds 
8136a4f1b41SDuncan Sands 	/* module parameters may change at any moment, so take a snapshot */
8146a4f1b41SDuncan Sands 	instance->params.altsetting = altsetting;
8156a4f1b41SDuncan Sands 	instance->params.BMaxDSL = BMaxDSL;
8166a4f1b41SDuncan Sands 	instance->params.ModemMode = ModemMode;
8176a4f1b41SDuncan Sands 	memcpy(instance->params.ModemOption, DEFAULT_MODEM_OPTION, MODEM_OPTION_LENGTH);
8186a4f1b41SDuncan Sands 	memcpy(instance->params.ModemOption, ModemOption, num_ModemOption);
81980aae7a1SDuncan Sands 	use_isoc = enable_isoc;
8206f749475SDuncan Sands 
8216a4f1b41SDuncan Sands 	if (instance->params.altsetting)
8226a4f1b41SDuncan Sands 		if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, instance->params.altsetting)) < 0) {
8236a4f1b41SDuncan Sands 			usb_err(usbatm, "%s: setting interface to %2d failed (%d)!\n", __func__, instance->params.altsetting, ret);
8246a4f1b41SDuncan Sands 			instance->params.altsetting = 0; /* fall back to default */
8256f749475SDuncan Sands 		}
8266f749475SDuncan Sands 
8276a4f1b41SDuncan Sands 	if (!instance->params.altsetting && use_isoc)
82880aae7a1SDuncan Sands 		if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, DEFAULT_ISOC_ALTSETTING)) < 0) {
82980aae7a1SDuncan Sands 			usb_dbg(usbatm, "%s: setting interface to %2d failed (%d)!\n", __func__, DEFAULT_ISOC_ALTSETTING, ret);
83080aae7a1SDuncan Sands 			use_isoc = 0; /* fall back to bulk */
83180aae7a1SDuncan Sands 		}
83280aae7a1SDuncan Sands 
83380aae7a1SDuncan Sands 	if (use_isoc) {
83480aae7a1SDuncan Sands 		const struct usb_host_interface *desc = data_intf->cur_altsetting;
83580aae7a1SDuncan Sands 		const __u8 target_address = USB_DIR_IN | usbatm->driver->isoc_in;
83680aae7a1SDuncan Sands 
83780aae7a1SDuncan Sands 		use_isoc = 0; /* fall back to bulk if endpoint not found */
83880aae7a1SDuncan Sands 
83980aae7a1SDuncan Sands 		for (i = 0; i < desc->desc.bNumEndpoints; i++) {
84080aae7a1SDuncan Sands 			const struct usb_endpoint_descriptor *endpoint_desc = &desc->endpoint[i].desc;
84180aae7a1SDuncan Sands 
84280aae7a1SDuncan Sands 			if ((endpoint_desc->bEndpointAddress == target_address)) {
843c5dd1f94SLuiz Fernando N. Capitulino 				use_isoc =
844c5dd1f94SLuiz Fernando N. Capitulino 					usb_endpoint_xfer_isoc(endpoint_desc);
84580aae7a1SDuncan Sands 				break;
84680aae7a1SDuncan Sands 			}
84780aae7a1SDuncan Sands 		}
84880aae7a1SDuncan Sands 
84980aae7a1SDuncan Sands 		if (!use_isoc)
85080aae7a1SDuncan Sands 			usb_info(usbatm, "isochronous transfer not supported - using bulk\n");
85180aae7a1SDuncan Sands 	}
85280aae7a1SDuncan Sands 
8536a4f1b41SDuncan Sands 	if (!use_isoc && !instance->params.altsetting)
85480aae7a1SDuncan Sands 		if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, DEFAULT_BULK_ALTSETTING)) < 0) {
85580aae7a1SDuncan Sands 			usb_err(usbatm, "%s: setting interface to %2d failed (%d)!\n", __func__, DEFAULT_BULK_ALTSETTING, ret);
8566f749475SDuncan Sands 			goto fail_free;
8576f749475SDuncan Sands 		}
85880aae7a1SDuncan Sands 
8596a4f1b41SDuncan Sands 	if (!instance->params.altsetting)
8606a4f1b41SDuncan Sands 		instance->params.altsetting = use_isoc ? DEFAULT_ISOC_ALTSETTING : DEFAULT_BULK_ALTSETTING;
86180aae7a1SDuncan Sands 
86280aae7a1SDuncan Sands 	usbatm->flags |= (use_isoc ? UDSL_USE_ISOC : 0);
8636f749475SDuncan Sands 
86437c95bfeSTejun Heo 	INIT_WORK(&instance->status_check_work, speedtch_check_status);
865*72a9f9a4SKees Cook 	timer_setup(&instance->status_check_timer, speedtch_status_poll, 0);
8661a7aad15SDuncan Sands 	instance->last_status = 0xff;
86748da7267SDuncan Sands 	instance->poll_delay = MIN_POLL_DELAY;
8681da177e4SLinus Torvalds 
869*72a9f9a4SKees Cook 	timer_setup(&instance->resubmit_timer, speedtch_resubmit_int, 0);
8701da177e4SLinus Torvalds 
87148da7267SDuncan Sands 	instance->int_urb = usb_alloc_urb(0, GFP_KERNEL);
8721da177e4SLinus Torvalds 
87348da7267SDuncan Sands 	if (instance->int_urb)
87448da7267SDuncan Sands 		usb_fill_int_urb(instance->int_urb, usb_dev,
87548da7267SDuncan Sands 				 usb_rcvintpipe(usb_dev, ENDPOINT_INT),
87648da7267SDuncan Sands 				 instance->int_data, sizeof(instance->int_data),
877afb8aae8SFelipe Balbi 				 speedtch_handle_int, instance, 16);
87848da7267SDuncan Sands 	else
87948da7267SDuncan Sands 		usb_dbg(usbatm, "%s: no memory for interrupt urb!\n", __func__);
8801da177e4SLinus Torvalds 
88148da7267SDuncan Sands 	/* check whether the modem already seems to be alive */
88248da7267SDuncan Sands 	ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
88348da7267SDuncan Sands 			      0x12, 0xc0, 0x07, 0x00,
88448da7267SDuncan Sands 			      instance->scratch_buffer + OFFSET_7, SIZE_7, 500);
8851da177e4SLinus Torvalds 
88680aae7a1SDuncan Sands 	usbatm->flags |= (ret == SIZE_7 ? UDSL_SKIP_HEAVY_INIT : 0);
8871da177e4SLinus Torvalds 
88835644b0cSDuncan Sands 	usb_dbg(usbatm, "%s: firmware %s loaded\n", __func__, usbatm->flags & UDSL_SKIP_HEAVY_INIT ? "already" : "not");
8891da177e4SLinus Torvalds 
89035644b0cSDuncan Sands 	if (!(usbatm->flags & UDSL_SKIP_HEAVY_INIT))
8910ec3c7e8SDuncan Sands 		if ((ret = usb_reset_device(usb_dev)) < 0) {
8920ec3c7e8SDuncan Sands 			usb_err(usbatm, "%s: device reset failed (%d)!\n", __func__, ret);
89348da7267SDuncan Sands 			goto fail_free;
8940ec3c7e8SDuncan Sands 		}
89548da7267SDuncan Sands 
89648da7267SDuncan Sands         usbatm->driver_data = instance;
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 	return 0;
8991da177e4SLinus Torvalds 
90048da7267SDuncan Sands fail_free:
90148da7267SDuncan Sands 	usb_free_urb(instance->int_urb);
9021da177e4SLinus Torvalds 	kfree(instance);
90348da7267SDuncan Sands fail_release:
90448da7267SDuncan Sands 	speedtch_release_interfaces(usb_dev, num_interfaces);
90548da7267SDuncan Sands 	return ret;
9061da177e4SLinus Torvalds }
9071da177e4SLinus Torvalds 
speedtch_unbind(struct usbatm_data * usbatm,struct usb_interface * intf)90848da7267SDuncan Sands static void speedtch_unbind(struct usbatm_data *usbatm, struct usb_interface *intf)
9091da177e4SLinus Torvalds {
91048da7267SDuncan Sands 	struct usb_device *usb_dev = interface_to_usbdev(intf);
91148da7267SDuncan Sands 	struct speedtch_instance_data *instance = usbatm->driver_data;
9121da177e4SLinus Torvalds 
91348da7267SDuncan Sands 	usb_dbg(usbatm, "%s entered\n", __func__);
9141da177e4SLinus Torvalds 
91548da7267SDuncan Sands 	speedtch_release_interfaces(usb_dev, usb_dev->actconfig->desc.bNumInterfaces);
91648da7267SDuncan Sands 	usb_free_urb(instance->int_urb);
91748da7267SDuncan Sands 	kfree(instance);
9181da177e4SLinus Torvalds }
9191da177e4SLinus Torvalds 
9201da177e4SLinus Torvalds 
9211da177e4SLinus Torvalds /***********
9221da177e4SLinus Torvalds **  init  **
9231da177e4SLinus Torvalds ***********/
9241da177e4SLinus Torvalds 
92548da7267SDuncan Sands static struct usbatm_driver speedtch_usbatm_driver = {
92648da7267SDuncan Sands 	.driver_name	= speedtch_driver_name,
92748da7267SDuncan Sands 	.bind		= speedtch_bind,
92848da7267SDuncan Sands 	.heavy_init	= speedtch_heavy_init,
92948da7267SDuncan Sands 	.unbind		= speedtch_unbind,
93048da7267SDuncan Sands 	.atm_start	= speedtch_atm_start,
93148da7267SDuncan Sands 	.atm_stop	= speedtch_atm_stop,
93280aae7a1SDuncan Sands 	.bulk_in	= ENDPOINT_BULK_DATA,
93380aae7a1SDuncan Sands 	.bulk_out	= ENDPOINT_BULK_DATA,
93480aae7a1SDuncan Sands 	.isoc_in	= ENDPOINT_ISOC_DATA
93548da7267SDuncan Sands };
93648da7267SDuncan Sands 
speedtch_usb_probe(struct usb_interface * intf,const struct usb_device_id * id)93748da7267SDuncan Sands static int speedtch_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
93848da7267SDuncan Sands {
93948da7267SDuncan Sands 	return usbatm_usb_probe(intf, id, &speedtch_usbatm_driver);
94048da7267SDuncan Sands }
94148da7267SDuncan Sands 
94265db4305SGreg Kroah-Hartman module_usb_driver(speedtch_usb_driver);
9431da177e4SLinus Torvalds 
9441da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR);
9451da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
9461da177e4SLinus Torvalds MODULE_LICENSE("GPL");
947