xref: /openbmc/linux/drivers/usb/atm/cxacru.c (revision 9b93eb47)
1 // SPDX-License-Identifier: GPL-2.0+
2 /******************************************************************************
3  *  cxacru.c  -  driver for USB ADSL modems based on
4  *               Conexant AccessRunner chipset
5  *
6  *  Copyright (C) 2004 David Woodhouse, Duncan Sands, Roman Kagan
7  *  Copyright (C) 2005 Duncan Sands, Roman Kagan (rkagan % mail ! ru)
8  *  Copyright (C) 2007 Simon Arlott
9  *  Copyright (C) 2009 Simon Arlott
10  ******************************************************************************/
11 
12 /*
13  *  Credit is due for Josep Comas, who created the original patch to speedtch.c
14  *  to support the different padding used by the AccessRunner (now generalized
15  *  into usbatm), and the userspace firmware loading utility.
16  */
17 
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/kernel.h>
21 #include <linux/timer.h>
22 #include <linux/errno.h>
23 #include <linux/slab.h>
24 #include <linux/device.h>
25 #include <linux/firmware.h>
26 #include <linux/mutex.h>
27 #include <asm/unaligned.h>
28 
29 #include "usbatm.h"
30 
31 #define DRIVER_AUTHOR	"Roman Kagan, David Woodhouse, Duncan Sands, Simon Arlott"
32 #define DRIVER_DESC	"Conexant AccessRunner ADSL USB modem driver"
33 
34 static const char cxacru_driver_name[] = "cxacru";
35 
36 #define CXACRU_EP_CMD		0x01	/* Bulk/interrupt in/out */
37 #define CXACRU_EP_DATA		0x02	/* Bulk in/out */
38 
39 #define CMD_PACKET_SIZE		64	/* Should be maxpacket(ep)? */
40 #define CMD_MAX_CONFIG		((CMD_PACKET_SIZE / 4 - 1) / 2)
41 
42 /* Addresses */
43 #define PLLFCLK_ADDR	0x00350068
44 #define PLLBCLK_ADDR	0x0035006c
45 #define SDRAMEN_ADDR	0x00350010
46 #define FW_ADDR		0x00801000
47 #define BR_ADDR		0x00180600
48 #define SIG_ADDR	0x00180500
49 #define BR_STACK_ADDR	0x00187f10
50 
51 /* Values */
52 #define SDRAM_ENA	0x1
53 
54 #define CMD_TIMEOUT	2000	/* msecs */
55 #define POLL_INTERVAL	1	/* secs */
56 
57 /* commands for interaction with the modem through the control channel before
58  * firmware is loaded  */
59 enum cxacru_fw_request {
60 	FW_CMD_ERR,
61 	FW_GET_VER,
62 	FW_READ_MEM,
63 	FW_WRITE_MEM,
64 	FW_RMW_MEM,
65 	FW_CHECKSUM_MEM,
66 	FW_GOTO_MEM,
67 };
68 
69 /* commands for interaction with the modem through the control channel once
70  * firmware is loaded  */
71 enum cxacru_cm_request {
72 	CM_REQUEST_UNDEFINED = 0x80,
73 	CM_REQUEST_TEST,
74 	CM_REQUEST_CHIP_GET_MAC_ADDRESS,
75 	CM_REQUEST_CHIP_GET_DP_VERSIONS,
76 	CM_REQUEST_CHIP_ADSL_LINE_START,
77 	CM_REQUEST_CHIP_ADSL_LINE_STOP,
78 	CM_REQUEST_CHIP_ADSL_LINE_GET_STATUS,
79 	CM_REQUEST_CHIP_ADSL_LINE_GET_SPEED,
80 	CM_REQUEST_CARD_INFO_GET,
81 	CM_REQUEST_CARD_DATA_GET,
82 	CM_REQUEST_CARD_DATA_SET,
83 	CM_REQUEST_COMMAND_HW_IO,
84 	CM_REQUEST_INTERFACE_HW_IO,
85 	CM_REQUEST_CARD_SERIAL_DATA_PATH_GET,
86 	CM_REQUEST_CARD_SERIAL_DATA_PATH_SET,
87 	CM_REQUEST_CARD_CONTROLLER_VERSION_GET,
88 	CM_REQUEST_CARD_GET_STATUS,
89 	CM_REQUEST_CARD_GET_MAC_ADDRESS,
90 	CM_REQUEST_CARD_GET_DATA_LINK_STATUS,
91 	CM_REQUEST_MAX,
92 };
93 
94 /* commands for interaction with the flash memory
95  *
96  * read:  response is the contents of the first 60 bytes of flash memory
97  * write: request contains the 60 bytes of data to write to flash memory
98  *        response is the contents of the first 60 bytes of flash memory
99  *
100  * layout: PP PP VV VV  MM MM MM MM  MM MM ?? ??  SS SS SS SS  SS SS SS SS
101  *         SS SS SS SS  SS SS SS SS  00 00 00 00  00 00 00 00  00 00 00 00
102  *         00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00
103  *
104  *   P: le16  USB Product ID
105  *   V: le16  USB Vendor ID
106  *   M: be48  MAC Address
107  *   S: le16  ASCII Serial Number
108  */
109 enum cxacru_cm_flash {
110 	CM_FLASH_READ = 0xa1,
111 	CM_FLASH_WRITE = 0xa2
112 };
113 
114 /* reply codes to the commands above */
115 enum cxacru_cm_status {
116 	CM_STATUS_UNDEFINED,
117 	CM_STATUS_SUCCESS,
118 	CM_STATUS_ERROR,
119 	CM_STATUS_UNSUPPORTED,
120 	CM_STATUS_UNIMPLEMENTED,
121 	CM_STATUS_PARAMETER_ERROR,
122 	CM_STATUS_DBG_LOOPBACK,
123 	CM_STATUS_MAX,
124 };
125 
126 /* indices into CARD_INFO_GET return array */
127 enum cxacru_info_idx {
128 	CXINF_DOWNSTREAM_RATE,
129 	CXINF_UPSTREAM_RATE,
130 	CXINF_LINK_STATUS,
131 	CXINF_LINE_STATUS,
132 	CXINF_MAC_ADDRESS_HIGH,
133 	CXINF_MAC_ADDRESS_LOW,
134 	CXINF_UPSTREAM_SNR_MARGIN,
135 	CXINF_DOWNSTREAM_SNR_MARGIN,
136 	CXINF_UPSTREAM_ATTENUATION,
137 	CXINF_DOWNSTREAM_ATTENUATION,
138 	CXINF_TRANSMITTER_POWER,
139 	CXINF_UPSTREAM_BITS_PER_FRAME,
140 	CXINF_DOWNSTREAM_BITS_PER_FRAME,
141 	CXINF_STARTUP_ATTEMPTS,
142 	CXINF_UPSTREAM_CRC_ERRORS,
143 	CXINF_DOWNSTREAM_CRC_ERRORS,
144 	CXINF_UPSTREAM_FEC_ERRORS,
145 	CXINF_DOWNSTREAM_FEC_ERRORS,
146 	CXINF_UPSTREAM_HEC_ERRORS,
147 	CXINF_DOWNSTREAM_HEC_ERRORS,
148 	CXINF_LINE_STARTABLE,
149 	CXINF_MODULATION,
150 	CXINF_ADSL_HEADEND,
151 	CXINF_ADSL_HEADEND_ENVIRONMENT,
152 	CXINF_CONTROLLER_VERSION,
153 	/* dunno what the missing two mean */
154 	CXINF_MAX = 0x1c,
155 };
156 
157 enum cxacru_poll_state {
158 	CXPOLL_STOPPING,
159 	CXPOLL_STOPPED,
160 	CXPOLL_POLLING,
161 	CXPOLL_SHUTDOWN
162 };
163 
164 struct cxacru_modem_type {
165 	u32 pll_f_clk;
166 	u32 pll_b_clk;
167 	int boot_rom_patch;
168 };
169 
170 struct cxacru_data {
171 	struct usbatm_data *usbatm;
172 
173 	const struct cxacru_modem_type *modem_type;
174 
175 	int line_status;
176 	struct mutex adsl_state_serialize;
177 	int adsl_status;
178 	struct delayed_work poll_work;
179 	u32 card_info[CXINF_MAX];
180 	struct mutex poll_state_serialize;
181 	enum cxacru_poll_state poll_state;
182 
183 	/* contol handles */
184 	struct mutex cm_serialize;
185 	u8 *rcv_buf;
186 	u8 *snd_buf;
187 	struct urb *rcv_urb;
188 	struct urb *snd_urb;
189 	struct completion rcv_done;
190 	struct completion snd_done;
191 };
192 
193 static int cxacru_cm(struct cxacru_data *instance, enum cxacru_cm_request cm,
194 	u8 *wdata, int wsize, u8 *rdata, int rsize);
195 static void cxacru_poll_status(struct work_struct *work);
196 
197 /* Card info exported through sysfs */
198 #define CXACRU__ATTR_INIT(_name) \
199 static DEVICE_ATTR_RO(_name)
200 
201 #define CXACRU_CMD_INIT(_name) \
202 static DEVICE_ATTR_RW(_name)
203 
204 #define CXACRU_SET_INIT(_name) \
205 static DEVICE_ATTR_WO(_name)
206 
207 #define CXACRU_ATTR_INIT(_value, _type, _name) \
208 static ssize_t _name##_show(struct device *dev, \
209 	struct device_attribute *attr, char *buf) \
210 { \
211 	struct cxacru_data *instance = to_usbatm_driver_data(\
212 		to_usb_interface(dev)); \
213 \
214 	if (instance == NULL) \
215 		return -ENODEV; \
216 \
217 	return cxacru_sysfs_showattr_##_type(instance->card_info[_value], buf); \
218 } \
219 CXACRU__ATTR_INIT(_name)
220 
221 #define CXACRU_ATTR_CREATE(_v, _t, _name) CXACRU_DEVICE_CREATE_FILE(_name)
222 #define CXACRU_CMD_CREATE(_name)          CXACRU_DEVICE_CREATE_FILE(_name)
223 #define CXACRU_SET_CREATE(_name)          CXACRU_DEVICE_CREATE_FILE(_name)
224 #define CXACRU__ATTR_CREATE(_name)        CXACRU_DEVICE_CREATE_FILE(_name)
225 
226 #define CXACRU_ATTR_REMOVE(_v, _t, _name) CXACRU_DEVICE_REMOVE_FILE(_name)
227 #define CXACRU_CMD_REMOVE(_name)          CXACRU_DEVICE_REMOVE_FILE(_name)
228 #define CXACRU_SET_REMOVE(_name)          CXACRU_DEVICE_REMOVE_FILE(_name)
229 #define CXACRU__ATTR_REMOVE(_name)        CXACRU_DEVICE_REMOVE_FILE(_name)
230 
231 static ssize_t cxacru_sysfs_showattr_u32(u32 value, char *buf)
232 {
233 	return snprintf(buf, PAGE_SIZE, "%u\n", value);
234 }
235 
236 static ssize_t cxacru_sysfs_showattr_s8(s8 value, char *buf)
237 {
238 	return snprintf(buf, PAGE_SIZE, "%d\n", value);
239 }
240 
241 static ssize_t cxacru_sysfs_showattr_dB(s16 value, char *buf)
242 {
243 	if (likely(value >= 0)) {
244 		return snprintf(buf, PAGE_SIZE, "%u.%02u\n",
245 					value / 100, value % 100);
246 	} else {
247 		value = -value;
248 		return snprintf(buf, PAGE_SIZE, "-%u.%02u\n",
249 					value / 100, value % 100);
250 	}
251 }
252 
253 static ssize_t cxacru_sysfs_showattr_bool(u32 value, char *buf)
254 {
255 	static char *str[] = { "no", "yes" };
256 
257 	if (unlikely(value >= ARRAY_SIZE(str)))
258 		return snprintf(buf, PAGE_SIZE, "%u\n", value);
259 	return snprintf(buf, PAGE_SIZE, "%s\n", str[value]);
260 }
261 
262 static ssize_t cxacru_sysfs_showattr_LINK(u32 value, char *buf)
263 {
264 	static char *str[] = { NULL, "not connected", "connected", "lost" };
265 
266 	if (unlikely(value >= ARRAY_SIZE(str) || str[value] == NULL))
267 		return snprintf(buf, PAGE_SIZE, "%u\n", value);
268 	return snprintf(buf, PAGE_SIZE, "%s\n", str[value]);
269 }
270 
271 static ssize_t cxacru_sysfs_showattr_LINE(u32 value, char *buf)
272 {
273 	static char *str[] = { "down", "attempting to activate",
274 		"training", "channel analysis", "exchange", "up",
275 		"waiting", "initialising"
276 	};
277 	if (unlikely(value >= ARRAY_SIZE(str)))
278 		return snprintf(buf, PAGE_SIZE, "%u\n", value);
279 	return snprintf(buf, PAGE_SIZE, "%s\n", str[value]);
280 }
281 
282 static ssize_t cxacru_sysfs_showattr_MODU(u32 value, char *buf)
283 {
284 	static char *str[] = {
285 			"",
286 			"ANSI T1.413",
287 			"ITU-T G.992.1 (G.DMT)",
288 			"ITU-T G.992.2 (G.LITE)"
289 	};
290 	if (unlikely(value >= ARRAY_SIZE(str)))
291 		return snprintf(buf, PAGE_SIZE, "%u\n", value);
292 	return snprintf(buf, PAGE_SIZE, "%s\n", str[value]);
293 }
294 
295 /*
296  * This could use MAC_ADDRESS_HIGH and MAC_ADDRESS_LOW, but since
297  * this data is already in atm_dev there's no point.
298  *
299  * MAC_ADDRESS_HIGH = 0x????5544
300  * MAC_ADDRESS_LOW  = 0x33221100
301  * Where 00-55 are bytes 0-5 of the MAC.
302  */
303 static ssize_t mac_address_show(struct device *dev,
304 	struct device_attribute *attr, char *buf)
305 {
306 	struct cxacru_data *instance = to_usbatm_driver_data(
307 			to_usb_interface(dev));
308 
309 	if (instance == NULL || instance->usbatm->atm_dev == NULL)
310 		return -ENODEV;
311 
312 	return snprintf(buf, PAGE_SIZE, "%pM\n",
313 		instance->usbatm->atm_dev->esi);
314 }
315 
316 static ssize_t adsl_state_show(struct device *dev,
317 	struct device_attribute *attr, char *buf)
318 {
319 	static char *str[] = { "running", "stopped" };
320 	struct cxacru_data *instance = to_usbatm_driver_data(
321 			to_usb_interface(dev));
322 	u32 value;
323 
324 	if (instance == NULL)
325 		return -ENODEV;
326 
327 	value = instance->card_info[CXINF_LINE_STARTABLE];
328 	if (unlikely(value >= ARRAY_SIZE(str)))
329 		return snprintf(buf, PAGE_SIZE, "%u\n", value);
330 	return snprintf(buf, PAGE_SIZE, "%s\n", str[value]);
331 }
332 
333 static ssize_t adsl_state_store(struct device *dev,
334 	struct device_attribute *attr, const char *buf, size_t count)
335 {
336 	struct cxacru_data *instance = to_usbatm_driver_data(
337 			to_usb_interface(dev));
338 	int ret;
339 	int poll = -1;
340 	char str_cmd[8];
341 	int len = strlen(buf);
342 
343 	if (!capable(CAP_NET_ADMIN))
344 		return -EACCES;
345 
346 	ret = sscanf(buf, "%7s", str_cmd);
347 	if (ret != 1)
348 		return -EINVAL;
349 	ret = 0;
350 
351 	if (instance == NULL)
352 		return -ENODEV;
353 
354 	if (mutex_lock_interruptible(&instance->adsl_state_serialize))
355 		return -ERESTARTSYS;
356 
357 	if (!strcmp(str_cmd, "stop") || !strcmp(str_cmd, "restart")) {
358 		ret = cxacru_cm(instance, CM_REQUEST_CHIP_ADSL_LINE_STOP, NULL, 0, NULL, 0);
359 		if (ret < 0) {
360 			atm_err(instance->usbatm, "change adsl state:"
361 				" CHIP_ADSL_LINE_STOP returned %d\n", ret);
362 
363 			ret = -EIO;
364 		} else {
365 			ret = len;
366 			poll = CXPOLL_STOPPED;
367 		}
368 	}
369 
370 	/* Line status is only updated every second
371 	 * and the device appears to only react to
372 	 * START/STOP every second too. Wait 1.5s to
373 	 * be sure that restart will have an effect. */
374 	if (!strcmp(str_cmd, "restart"))
375 		msleep(1500);
376 
377 	if (!strcmp(str_cmd, "start") || !strcmp(str_cmd, "restart")) {
378 		ret = cxacru_cm(instance, CM_REQUEST_CHIP_ADSL_LINE_START, NULL, 0, NULL, 0);
379 		if (ret < 0) {
380 			atm_err(instance->usbatm, "change adsl state:"
381 				" CHIP_ADSL_LINE_START returned %d\n", ret);
382 
383 			ret = -EIO;
384 		} else {
385 			ret = len;
386 			poll = CXPOLL_POLLING;
387 		}
388 	}
389 
390 	if (!strcmp(str_cmd, "poll")) {
391 		ret = len;
392 		poll = CXPOLL_POLLING;
393 	}
394 
395 	if (ret == 0) {
396 		ret = -EINVAL;
397 		poll = -1;
398 	}
399 
400 	if (poll == CXPOLL_POLLING) {
401 		mutex_lock(&instance->poll_state_serialize);
402 		switch (instance->poll_state) {
403 		case CXPOLL_STOPPED:
404 			/* start polling */
405 			instance->poll_state = CXPOLL_POLLING;
406 			break;
407 
408 		case CXPOLL_STOPPING:
409 			/* abort stop request */
410 			instance->poll_state = CXPOLL_POLLING;
411 			/* fall through */
412 		case CXPOLL_POLLING:
413 		case CXPOLL_SHUTDOWN:
414 			/* don't start polling */
415 			poll = -1;
416 		}
417 		mutex_unlock(&instance->poll_state_serialize);
418 	} else if (poll == CXPOLL_STOPPED) {
419 		mutex_lock(&instance->poll_state_serialize);
420 		/* request stop */
421 		if (instance->poll_state == CXPOLL_POLLING)
422 			instance->poll_state = CXPOLL_STOPPING;
423 		mutex_unlock(&instance->poll_state_serialize);
424 	}
425 
426 	mutex_unlock(&instance->adsl_state_serialize);
427 
428 	if (poll == CXPOLL_POLLING)
429 		cxacru_poll_status(&instance->poll_work.work);
430 
431 	return ret;
432 }
433 
434 /* CM_REQUEST_CARD_DATA_GET times out, so no show attribute */
435 
436 static ssize_t adsl_config_store(struct device *dev,
437 	struct device_attribute *attr, const char *buf, size_t count)
438 {
439 	struct cxacru_data *instance = to_usbatm_driver_data(
440 			to_usb_interface(dev));
441 	int len = strlen(buf);
442 	int ret, pos, num;
443 	__le32 data[CMD_PACKET_SIZE / 4];
444 
445 	if (!capable(CAP_NET_ADMIN))
446 		return -EACCES;
447 
448 	if (instance == NULL)
449 		return -ENODEV;
450 
451 	pos = 0;
452 	num = 0;
453 	while (pos < len) {
454 		int tmp;
455 		u32 index;
456 		u32 value;
457 
458 		ret = sscanf(buf + pos, "%x=%x%n", &index, &value, &tmp);
459 		if (ret < 2)
460 			return -EINVAL;
461 		if (index > 0x7f)
462 			return -EINVAL;
463 		if (tmp < 0 || tmp > len - pos)
464 			return -EINVAL;
465 		pos += tmp;
466 
467 		/* skip trailing newline */
468 		if (buf[pos] == '\n' && pos == len-1)
469 			pos++;
470 
471 		data[num * 2 + 1] = cpu_to_le32(index);
472 		data[num * 2 + 2] = cpu_to_le32(value);
473 		num++;
474 
475 		/* send config values when data buffer is full
476 		 * or no more data
477 		 */
478 		if (pos >= len || num >= CMD_MAX_CONFIG) {
479 			char log[CMD_MAX_CONFIG * 12 + 1]; /* %02x=%08x */
480 
481 			data[0] = cpu_to_le32(num);
482 			ret = cxacru_cm(instance, CM_REQUEST_CARD_DATA_SET,
483 				(u8 *) data, 4 + num * 8, NULL, 0);
484 			if (ret < 0) {
485 				atm_err(instance->usbatm,
486 					"set card data returned %d\n", ret);
487 				return -EIO;
488 			}
489 
490 			for (tmp = 0; tmp < num; tmp++)
491 				snprintf(log + tmp*12, 13, " %02x=%08x",
492 					le32_to_cpu(data[tmp * 2 + 1]),
493 					le32_to_cpu(data[tmp * 2 + 2]));
494 			atm_info(instance->usbatm, "config%s\n", log);
495 			num = 0;
496 		}
497 	}
498 
499 	return len;
500 }
501 
502 /*
503  * All device attributes are included in CXACRU_ALL_FILES
504  * so that the same list can be used multiple times:
505  *     INIT   (define the device attributes)
506  *     CREATE (create all the device files)
507  *     REMOVE (remove all the device files)
508  *
509  * With the last two being defined as needed in the functions
510  * they are used in before calling CXACRU_ALL_FILES()
511  */
512 #define CXACRU_ALL_FILES(_action) \
513 CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_RATE,           u32,  downstream_rate); \
514 CXACRU_ATTR_##_action(CXINF_UPSTREAM_RATE,             u32,  upstream_rate); \
515 CXACRU_ATTR_##_action(CXINF_LINK_STATUS,               LINK, link_status); \
516 CXACRU_ATTR_##_action(CXINF_LINE_STATUS,               LINE, line_status); \
517 CXACRU__ATTR_##_action(                                      mac_address); \
518 CXACRU_ATTR_##_action(CXINF_UPSTREAM_SNR_MARGIN,       dB,   upstream_snr_margin); \
519 CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_SNR_MARGIN,     dB,   downstream_snr_margin); \
520 CXACRU_ATTR_##_action(CXINF_UPSTREAM_ATTENUATION,      dB,   upstream_attenuation); \
521 CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_ATTENUATION,    dB,   downstream_attenuation); \
522 CXACRU_ATTR_##_action(CXINF_TRANSMITTER_POWER,         s8,   transmitter_power); \
523 CXACRU_ATTR_##_action(CXINF_UPSTREAM_BITS_PER_FRAME,   u32,  upstream_bits_per_frame); \
524 CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_BITS_PER_FRAME, u32,  downstream_bits_per_frame); \
525 CXACRU_ATTR_##_action(CXINF_STARTUP_ATTEMPTS,          u32,  startup_attempts); \
526 CXACRU_ATTR_##_action(CXINF_UPSTREAM_CRC_ERRORS,       u32,  upstream_crc_errors); \
527 CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_CRC_ERRORS,     u32,  downstream_crc_errors); \
528 CXACRU_ATTR_##_action(CXINF_UPSTREAM_FEC_ERRORS,       u32,  upstream_fec_errors); \
529 CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_FEC_ERRORS,     u32,  downstream_fec_errors); \
530 CXACRU_ATTR_##_action(CXINF_UPSTREAM_HEC_ERRORS,       u32,  upstream_hec_errors); \
531 CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_HEC_ERRORS,     u32,  downstream_hec_errors); \
532 CXACRU_ATTR_##_action(CXINF_LINE_STARTABLE,            bool, line_startable); \
533 CXACRU_ATTR_##_action(CXINF_MODULATION,                MODU, modulation); \
534 CXACRU_ATTR_##_action(CXINF_ADSL_HEADEND,              u32,  adsl_headend); \
535 CXACRU_ATTR_##_action(CXINF_ADSL_HEADEND_ENVIRONMENT,  u32,  adsl_headend_environment); \
536 CXACRU_ATTR_##_action(CXINF_CONTROLLER_VERSION,        u32,  adsl_controller_version); \
537 CXACRU_CMD_##_action(                                        adsl_state); \
538 CXACRU_SET_##_action(                                        adsl_config);
539 
540 CXACRU_ALL_FILES(INIT);
541 
542 /* the following three functions are stolen from drivers/usb/core/message.c */
543 static void cxacru_blocking_completion(struct urb *urb)
544 {
545 	complete(urb->context);
546 }
547 
548 struct cxacru_timer {
549 	struct timer_list timer;
550 	struct urb *urb;
551 };
552 
553 static void cxacru_timeout_kill(struct timer_list *t)
554 {
555 	struct cxacru_timer *timer = from_timer(timer, t, timer);
556 
557 	usb_unlink_urb(timer->urb);
558 }
559 
560 static int cxacru_start_wait_urb(struct urb *urb, struct completion *done,
561 				 int *actual_length)
562 {
563 	struct cxacru_timer timer = {
564 		.urb = urb,
565 	};
566 
567 	timer_setup_on_stack(&timer.timer, cxacru_timeout_kill, 0);
568 	mod_timer(&timer.timer, jiffies + msecs_to_jiffies(CMD_TIMEOUT));
569 	wait_for_completion(done);
570 	del_timer_sync(&timer.timer);
571 	destroy_timer_on_stack(&timer.timer);
572 
573 	if (actual_length)
574 		*actual_length = urb->actual_length;
575 	return urb->status; /* must read status after completion */
576 }
577 
578 static int cxacru_cm(struct cxacru_data *instance, enum cxacru_cm_request cm,
579 		     u8 *wdata, int wsize, u8 *rdata, int rsize)
580 {
581 	int ret, actlen;
582 	int offb, offd;
583 	const int stride = CMD_PACKET_SIZE - 4;
584 	u8 *wbuf = instance->snd_buf;
585 	u8 *rbuf = instance->rcv_buf;
586 	int wbuflen = ((wsize - 1) / stride + 1) * CMD_PACKET_SIZE;
587 	int rbuflen = ((rsize - 1) / stride + 1) * CMD_PACKET_SIZE;
588 
589 	if (wbuflen > PAGE_SIZE || rbuflen > PAGE_SIZE) {
590 		if (printk_ratelimit())
591 			usb_err(instance->usbatm, "requested transfer size too large (%d, %d)\n",
592 				wbuflen, rbuflen);
593 		ret = -ENOMEM;
594 		goto err;
595 	}
596 
597 	mutex_lock(&instance->cm_serialize);
598 
599 	/* submit reading urb before the writing one */
600 	init_completion(&instance->rcv_done);
601 	ret = usb_submit_urb(instance->rcv_urb, GFP_KERNEL);
602 	if (ret < 0) {
603 		if (printk_ratelimit())
604 			usb_err(instance->usbatm, "submit of read urb for cm %#x failed (%d)\n",
605 				cm, ret);
606 		goto fail;
607 	}
608 
609 	memset(wbuf, 0, wbuflen);
610 	/* handle wsize == 0 */
611 	wbuf[0] = cm;
612 	for (offb = offd = 0; offd < wsize; offd += stride, offb += CMD_PACKET_SIZE) {
613 		wbuf[offb] = cm;
614 		memcpy(wbuf + offb + 4, wdata + offd, min_t(int, stride, wsize - offd));
615 	}
616 
617 	instance->snd_urb->transfer_buffer_length = wbuflen;
618 	init_completion(&instance->snd_done);
619 	ret = usb_submit_urb(instance->snd_urb, GFP_KERNEL);
620 	if (ret < 0) {
621 		if (printk_ratelimit())
622 			usb_err(instance->usbatm, "submit of write urb for cm %#x failed (%d)\n",
623 				cm, ret);
624 		goto fail;
625 	}
626 
627 	ret = cxacru_start_wait_urb(instance->snd_urb, &instance->snd_done, NULL);
628 	if (ret < 0) {
629 		if (printk_ratelimit())
630 			usb_err(instance->usbatm, "send of cm %#x failed (%d)\n", cm, ret);
631 		goto fail;
632 	}
633 
634 	ret = cxacru_start_wait_urb(instance->rcv_urb, &instance->rcv_done, &actlen);
635 	if (ret < 0) {
636 		if (printk_ratelimit())
637 			usb_err(instance->usbatm, "receive of cm %#x failed (%d)\n", cm, ret);
638 		goto fail;
639 	}
640 	if (actlen % CMD_PACKET_SIZE || !actlen) {
641 		if (printk_ratelimit())
642 			usb_err(instance->usbatm, "invalid response length to cm %#x: %d\n",
643 				cm, actlen);
644 		ret = -EIO;
645 		goto fail;
646 	}
647 
648 	/* check the return status and copy the data to the output buffer, if needed */
649 	for (offb = offd = 0; offd < rsize && offb < actlen; offb += CMD_PACKET_SIZE) {
650 		if (rbuf[offb] != cm) {
651 			if (printk_ratelimit())
652 				usb_err(instance->usbatm, "wrong cm %#x in response to cm %#x\n",
653 					rbuf[offb], cm);
654 			ret = -EIO;
655 			goto fail;
656 		}
657 		if (rbuf[offb + 1] != CM_STATUS_SUCCESS) {
658 			if (printk_ratelimit())
659 				usb_err(instance->usbatm, "response to cm %#x failed: %#x\n",
660 					cm, rbuf[offb + 1]);
661 			ret = -EIO;
662 			goto fail;
663 		}
664 		if (offd >= rsize)
665 			break;
666 		memcpy(rdata + offd, rbuf + offb + 4, min_t(int, stride, rsize - offd));
667 		offd += stride;
668 	}
669 
670 	ret = offd;
671 	usb_dbg(instance->usbatm, "cm %#x\n", cm);
672 fail:
673 	mutex_unlock(&instance->cm_serialize);
674 err:
675 	return ret;
676 }
677 
678 static int cxacru_cm_get_array(struct cxacru_data *instance, enum cxacru_cm_request cm,
679 			       u32 *data, int size)
680 {
681 	int ret, len;
682 	__le32 *buf;
683 	int offb;
684 	unsigned int offd;
685 	const int stride = CMD_PACKET_SIZE / (4 * 2) - 1;
686 	int buflen =  ((size - 1) / stride + 1 + size * 2) * 4;
687 
688 	buf = kmalloc(buflen, GFP_KERNEL);
689 	if (!buf)
690 		return -ENOMEM;
691 
692 	ret = cxacru_cm(instance, cm, NULL, 0, (u8 *) buf, buflen);
693 	if (ret < 0)
694 		goto cleanup;
695 
696 	/* len > 0 && len % 4 == 0 guaranteed by cxacru_cm() */
697 	len = ret / 4;
698 	for (offb = 0; offb < len; ) {
699 		int l = le32_to_cpu(buf[offb++]);
700 
701 		if (l < 0 || l > stride || l > (len - offb) / 2) {
702 			if (printk_ratelimit())
703 				usb_err(instance->usbatm, "invalid data length from cm %#x: %d\n",
704 					cm, l);
705 			ret = -EIO;
706 			goto cleanup;
707 		}
708 		while (l--) {
709 			offd = le32_to_cpu(buf[offb++]);
710 			if (offd >= size) {
711 				if (printk_ratelimit())
712 					usb_err(instance->usbatm, "wrong index %#x in response to cm %#x\n",
713 						offd, cm);
714 				ret = -EIO;
715 				goto cleanup;
716 			}
717 			data[offd] = le32_to_cpu(buf[offb++]);
718 		}
719 	}
720 
721 	ret = 0;
722 
723 cleanup:
724 	kfree(buf);
725 	return ret;
726 }
727 
728 static int cxacru_card_status(struct cxacru_data *instance)
729 {
730 	int ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_STATUS, NULL, 0, NULL, 0);
731 
732 	if (ret < 0) {		/* firmware not loaded */
733 		usb_dbg(instance->usbatm, "cxacru_adsl_start: CARD_GET_STATUS returned %d\n", ret);
734 		return ret;
735 	}
736 	return 0;
737 }
738 
739 static void cxacru_remove_device_files(struct usbatm_data *usbatm_instance,
740 		struct atm_dev *atm_dev)
741 {
742 	struct usb_interface *intf = usbatm_instance->usb_intf;
743 
744 	#define CXACRU_DEVICE_REMOVE_FILE(_name) \
745 		device_remove_file(&intf->dev, &dev_attr_##_name);
746 	CXACRU_ALL_FILES(REMOVE);
747 	#undef CXACRU_DEVICE_REMOVE_FILE
748 }
749 
750 static int cxacru_atm_start(struct usbatm_data *usbatm_instance,
751 		struct atm_dev *atm_dev)
752 {
753 	struct cxacru_data *instance = usbatm_instance->driver_data;
754 	struct usb_interface *intf = usbatm_instance->usb_intf;
755 	int ret;
756 	int start_polling = 1;
757 
758 	dev_dbg(&intf->dev, "%s\n", __func__);
759 
760 	/* Read MAC address */
761 	ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_MAC_ADDRESS, NULL, 0,
762 			atm_dev->esi, sizeof(atm_dev->esi));
763 	if (ret < 0) {
764 		atm_err(usbatm_instance, "cxacru_atm_start: CARD_GET_MAC_ADDRESS returned %d\n", ret);
765 		return ret;
766 	}
767 
768 	#define CXACRU_DEVICE_CREATE_FILE(_name) \
769 		ret = device_create_file(&intf->dev, &dev_attr_##_name); \
770 		if (unlikely(ret)) \
771 			goto fail_sysfs;
772 	CXACRU_ALL_FILES(CREATE);
773 	#undef CXACRU_DEVICE_CREATE_FILE
774 
775 	/* start ADSL */
776 	mutex_lock(&instance->adsl_state_serialize);
777 	ret = cxacru_cm(instance, CM_REQUEST_CHIP_ADSL_LINE_START, NULL, 0, NULL, 0);
778 	if (ret < 0)
779 		atm_err(usbatm_instance, "cxacru_atm_start: CHIP_ADSL_LINE_START returned %d\n", ret);
780 
781 	/* Start status polling */
782 	mutex_lock(&instance->poll_state_serialize);
783 	switch (instance->poll_state) {
784 	case CXPOLL_STOPPED:
785 		/* start polling */
786 		instance->poll_state = CXPOLL_POLLING;
787 		break;
788 
789 	case CXPOLL_STOPPING:
790 		/* abort stop request */
791 		instance->poll_state = CXPOLL_POLLING;
792 		/* fall through */
793 	case CXPOLL_POLLING:
794 	case CXPOLL_SHUTDOWN:
795 		/* don't start polling */
796 		start_polling = 0;
797 	}
798 	mutex_unlock(&instance->poll_state_serialize);
799 	mutex_unlock(&instance->adsl_state_serialize);
800 
801 	printk(KERN_INFO "%s%d: %s %pM\n", atm_dev->type, atm_dev->number,
802 			usbatm_instance->description, atm_dev->esi);
803 
804 	if (start_polling)
805 		cxacru_poll_status(&instance->poll_work.work);
806 	return 0;
807 
808 fail_sysfs:
809 	usb_err(usbatm_instance, "cxacru_atm_start: device_create_file failed (%d)\n", ret);
810 	cxacru_remove_device_files(usbatm_instance, atm_dev);
811 	return ret;
812 }
813 
814 static void cxacru_poll_status(struct work_struct *work)
815 {
816 	struct cxacru_data *instance =
817 		container_of(work, struct cxacru_data, poll_work.work);
818 	u32 buf[CXINF_MAX] = {};
819 	struct usbatm_data *usbatm = instance->usbatm;
820 	struct atm_dev *atm_dev = usbatm->atm_dev;
821 	int keep_polling = 1;
822 	int ret;
823 
824 	ret = cxacru_cm_get_array(instance, CM_REQUEST_CARD_INFO_GET, buf, CXINF_MAX);
825 	if (ret < 0) {
826 		if (ret != -ESHUTDOWN)
827 			atm_warn(usbatm, "poll status: error %d\n", ret);
828 
829 		mutex_lock(&instance->poll_state_serialize);
830 		if (instance->poll_state != CXPOLL_SHUTDOWN) {
831 			instance->poll_state = CXPOLL_STOPPED;
832 
833 			if (ret != -ESHUTDOWN)
834 				atm_warn(usbatm, "polling disabled, set adsl_state"
835 						" to 'start' or 'poll' to resume\n");
836 		}
837 		mutex_unlock(&instance->poll_state_serialize);
838 		goto reschedule;
839 	}
840 
841 	memcpy(instance->card_info, buf, sizeof(instance->card_info));
842 
843 	if (instance->adsl_status != buf[CXINF_LINE_STARTABLE]) {
844 		instance->adsl_status = buf[CXINF_LINE_STARTABLE];
845 
846 		switch (instance->adsl_status) {
847 		case 0:
848 			atm_printk(KERN_INFO, usbatm, "ADSL state: running\n");
849 			break;
850 
851 		case 1:
852 			atm_printk(KERN_INFO, usbatm, "ADSL state: stopped\n");
853 			break;
854 
855 		default:
856 			atm_printk(KERN_INFO, usbatm, "Unknown adsl status %02x\n", instance->adsl_status);
857 			break;
858 		}
859 	}
860 
861 	if (instance->line_status == buf[CXINF_LINE_STATUS])
862 		goto reschedule;
863 
864 	instance->line_status = buf[CXINF_LINE_STATUS];
865 	switch (instance->line_status) {
866 	case 0:
867 		atm_dev_signal_change(atm_dev, ATM_PHY_SIG_LOST);
868 		atm_info(usbatm, "ADSL line: down\n");
869 		break;
870 
871 	case 1:
872 		atm_dev_signal_change(atm_dev, ATM_PHY_SIG_LOST);
873 		atm_info(usbatm, "ADSL line: attempting to activate\n");
874 		break;
875 
876 	case 2:
877 		atm_dev_signal_change(atm_dev, ATM_PHY_SIG_LOST);
878 		atm_info(usbatm, "ADSL line: training\n");
879 		break;
880 
881 	case 3:
882 		atm_dev_signal_change(atm_dev, ATM_PHY_SIG_LOST);
883 		atm_info(usbatm, "ADSL line: channel analysis\n");
884 		break;
885 
886 	case 4:
887 		atm_dev_signal_change(atm_dev, ATM_PHY_SIG_LOST);
888 		atm_info(usbatm, "ADSL line: exchange\n");
889 		break;
890 
891 	case 5:
892 		atm_dev->link_rate = buf[CXINF_DOWNSTREAM_RATE] * 1000 / 424;
893 		atm_dev_signal_change(atm_dev, ATM_PHY_SIG_FOUND);
894 
895 		atm_info(usbatm, "ADSL line: up (%d kb/s down | %d kb/s up)\n",
896 		     buf[CXINF_DOWNSTREAM_RATE], buf[CXINF_UPSTREAM_RATE]);
897 		break;
898 
899 	case 6:
900 		atm_dev_signal_change(atm_dev, ATM_PHY_SIG_LOST);
901 		atm_info(usbatm, "ADSL line: waiting\n");
902 		break;
903 
904 	case 7:
905 		atm_dev_signal_change(atm_dev, ATM_PHY_SIG_LOST);
906 		atm_info(usbatm, "ADSL line: initializing\n");
907 		break;
908 
909 	default:
910 		atm_dev_signal_change(atm_dev, ATM_PHY_SIG_UNKNOWN);
911 		atm_info(usbatm, "Unknown line state %02x\n", instance->line_status);
912 		break;
913 	}
914 reschedule:
915 
916 	mutex_lock(&instance->poll_state_serialize);
917 	if (instance->poll_state == CXPOLL_STOPPING &&
918 				instance->adsl_status == 1 && /* stopped */
919 				instance->line_status == 0) /* down */
920 		instance->poll_state = CXPOLL_STOPPED;
921 
922 	if (instance->poll_state == CXPOLL_STOPPED)
923 		keep_polling = 0;
924 	mutex_unlock(&instance->poll_state_serialize);
925 
926 	if (keep_polling)
927 		schedule_delayed_work(&instance->poll_work,
928 				round_jiffies_relative(POLL_INTERVAL*HZ));
929 }
930 
931 static int cxacru_fw(struct usb_device *usb_dev, enum cxacru_fw_request fw,
932 		     u8 code1, u8 code2, u32 addr, const u8 *data, int size)
933 {
934 	int ret;
935 	u8 *buf;
936 	int offd, offb;
937 	const int stride = CMD_PACKET_SIZE - 8;
938 
939 	buf = (u8 *) __get_free_page(GFP_KERNEL);
940 	if (!buf)
941 		return -ENOMEM;
942 
943 	offb = offd = 0;
944 	do {
945 		int l = min_t(int, stride, size - offd);
946 
947 		buf[offb++] = fw;
948 		buf[offb++] = l;
949 		buf[offb++] = code1;
950 		buf[offb++] = code2;
951 		put_unaligned(cpu_to_le32(addr), (__le32 *)(buf + offb));
952 		offb += 4;
953 		addr += l;
954 		if (l)
955 			memcpy(buf + offb, data + offd, l);
956 		if (l < stride)
957 			memset(buf + offb + l, 0, stride - l);
958 		offb += stride;
959 		offd += stride;
960 		if ((offb >= PAGE_SIZE) || (offd >= size)) {
961 			ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, CXACRU_EP_CMD),
962 					   buf, offb, NULL, CMD_TIMEOUT);
963 			if (ret < 0) {
964 				dev_dbg(&usb_dev->dev, "sending fw %#x failed\n", fw);
965 				goto cleanup;
966 			}
967 			offb = 0;
968 		}
969 	} while (offd < size);
970 	dev_dbg(&usb_dev->dev, "sent fw %#x\n", fw);
971 
972 	ret = 0;
973 
974 cleanup:
975 	free_page((unsigned long) buf);
976 	return ret;
977 }
978 
979 static void cxacru_upload_firmware(struct cxacru_data *instance,
980 				   const struct firmware *fw,
981 				   const struct firmware *bp)
982 {
983 	int ret;
984 	struct usbatm_data *usbatm = instance->usbatm;
985 	struct usb_device *usb_dev = usbatm->usb_dev;
986 	__le16 signature[] = { usb_dev->descriptor.idVendor,
987 			       usb_dev->descriptor.idProduct };
988 	__le32 val;
989 
990 	usb_dbg(usbatm, "%s\n", __func__);
991 
992 	/* FirmwarePllFClkValue */
993 	val = cpu_to_le32(instance->modem_type->pll_f_clk);
994 	ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, PLLFCLK_ADDR, (u8 *) &val, 4);
995 	if (ret) {
996 		usb_err(usbatm, "FirmwarePllFClkValue failed: %d\n", ret);
997 		return;
998 	}
999 
1000 	/* FirmwarePllBClkValue */
1001 	val = cpu_to_le32(instance->modem_type->pll_b_clk);
1002 	ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, PLLBCLK_ADDR, (u8 *) &val, 4);
1003 	if (ret) {
1004 		usb_err(usbatm, "FirmwarePllBClkValue failed: %d\n", ret);
1005 		return;
1006 	}
1007 
1008 	/* Enable SDRAM */
1009 	val = cpu_to_le32(SDRAM_ENA);
1010 	ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, SDRAMEN_ADDR, (u8 *) &val, 4);
1011 	if (ret) {
1012 		usb_err(usbatm, "Enable SDRAM failed: %d\n", ret);
1013 		return;
1014 	}
1015 
1016 	/* Firmware */
1017 	usb_info(usbatm, "loading firmware\n");
1018 	ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, FW_ADDR, fw->data, fw->size);
1019 	if (ret) {
1020 		usb_err(usbatm, "Firmware upload failed: %d\n", ret);
1021 		return;
1022 	}
1023 
1024 	/* Boot ROM patch */
1025 	if (instance->modem_type->boot_rom_patch) {
1026 		usb_info(usbatm, "loading boot ROM patch\n");
1027 		ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, BR_ADDR, bp->data, bp->size);
1028 		if (ret) {
1029 			usb_err(usbatm, "Boot ROM patching failed: %d\n", ret);
1030 			return;
1031 		}
1032 	}
1033 
1034 	/* Signature */
1035 	ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, SIG_ADDR, (u8 *) signature, 4);
1036 	if (ret) {
1037 		usb_err(usbatm, "Signature storing failed: %d\n", ret);
1038 		return;
1039 	}
1040 
1041 	usb_info(usbatm, "starting device\n");
1042 	if (instance->modem_type->boot_rom_patch) {
1043 		val = cpu_to_le32(BR_ADDR);
1044 		ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, BR_STACK_ADDR, (u8 *) &val, 4);
1045 	} else {
1046 		ret = cxacru_fw(usb_dev, FW_GOTO_MEM, 0x0, 0x0, FW_ADDR, NULL, 0);
1047 	}
1048 	if (ret) {
1049 		usb_err(usbatm, "Passing control to firmware failed: %d\n", ret);
1050 		return;
1051 	}
1052 
1053 	/* Delay to allow firmware to start up. */
1054 	msleep_interruptible(1000);
1055 
1056 	usb_clear_halt(usb_dev, usb_sndbulkpipe(usb_dev, CXACRU_EP_CMD));
1057 	usb_clear_halt(usb_dev, usb_rcvbulkpipe(usb_dev, CXACRU_EP_CMD));
1058 	usb_clear_halt(usb_dev, usb_sndbulkpipe(usb_dev, CXACRU_EP_DATA));
1059 	usb_clear_halt(usb_dev, usb_rcvbulkpipe(usb_dev, CXACRU_EP_DATA));
1060 
1061 	ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_STATUS, NULL, 0, NULL, 0);
1062 	if (ret < 0) {
1063 		usb_err(usbatm, "modem failed to initialize: %d\n", ret);
1064 		return;
1065 	}
1066 }
1067 
1068 static int cxacru_find_firmware(struct cxacru_data *instance,
1069 				char *phase, const struct firmware **fw_p)
1070 {
1071 	struct usbatm_data *usbatm = instance->usbatm;
1072 	struct device *dev = &usbatm->usb_intf->dev;
1073 	char buf[16];
1074 
1075 	sprintf(buf, "cxacru-%s.bin", phase);
1076 	usb_dbg(usbatm, "cxacru_find_firmware: looking for %s\n", buf);
1077 
1078 	if (request_firmware(fw_p, buf, dev)) {
1079 		usb_dbg(usbatm, "no stage %s firmware found\n", phase);
1080 		return -ENOENT;
1081 	}
1082 
1083 	usb_info(usbatm, "found firmware %s\n", buf);
1084 
1085 	return 0;
1086 }
1087 
1088 static int cxacru_heavy_init(struct usbatm_data *usbatm_instance,
1089 			     struct usb_interface *usb_intf)
1090 {
1091 	const struct firmware *fw, *bp;
1092 	struct cxacru_data *instance = usbatm_instance->driver_data;
1093 	int ret = cxacru_find_firmware(instance, "fw", &fw);
1094 
1095 	if (ret) {
1096 		usb_warn(usbatm_instance, "firmware (cxacru-fw.bin) unavailable (system misconfigured?)\n");
1097 		return ret;
1098 	}
1099 
1100 	if (instance->modem_type->boot_rom_patch) {
1101 		ret = cxacru_find_firmware(instance, "bp", &bp);
1102 		if (ret) {
1103 			usb_warn(usbatm_instance, "boot ROM patch (cxacru-bp.bin) unavailable (system misconfigured?)\n");
1104 			release_firmware(fw);
1105 			return ret;
1106 		}
1107 	}
1108 
1109 	cxacru_upload_firmware(instance, fw, bp);
1110 
1111 	if (instance->modem_type->boot_rom_patch)
1112 		release_firmware(bp);
1113 	release_firmware(fw);
1114 
1115 	ret = cxacru_card_status(instance);
1116 	if (ret)
1117 		usb_dbg(usbatm_instance, "modem initialisation failed\n");
1118 	else
1119 		usb_dbg(usbatm_instance, "done setting up the modem\n");
1120 
1121 	return ret;
1122 }
1123 
1124 static int cxacru_bind(struct usbatm_data *usbatm_instance,
1125 		       struct usb_interface *intf, const struct usb_device_id *id)
1126 {
1127 	struct cxacru_data *instance;
1128 	struct usb_device *usb_dev = interface_to_usbdev(intf);
1129 	struct usb_host_endpoint *cmd_ep = usb_dev->ep_in[CXACRU_EP_CMD];
1130 	int ret;
1131 
1132 	/* instance init */
1133 	instance = kzalloc(sizeof(*instance), GFP_KERNEL);
1134 	if (!instance)
1135 		return -ENOMEM;
1136 
1137 	instance->usbatm = usbatm_instance;
1138 	instance->modem_type = (struct cxacru_modem_type *) id->driver_info;
1139 
1140 	mutex_init(&instance->poll_state_serialize);
1141 	instance->poll_state = CXPOLL_STOPPED;
1142 	instance->line_status = -1;
1143 	instance->adsl_status = -1;
1144 
1145 	mutex_init(&instance->adsl_state_serialize);
1146 
1147 	instance->rcv_buf = (u8 *) __get_free_page(GFP_KERNEL);
1148 	if (!instance->rcv_buf) {
1149 		usb_dbg(usbatm_instance, "cxacru_bind: no memory for rcv_buf\n");
1150 		ret = -ENOMEM;
1151 		goto fail;
1152 	}
1153 	instance->snd_buf = (u8 *) __get_free_page(GFP_KERNEL);
1154 	if (!instance->snd_buf) {
1155 		usb_dbg(usbatm_instance, "cxacru_bind: no memory for snd_buf\n");
1156 		ret = -ENOMEM;
1157 		goto fail;
1158 	}
1159 	instance->rcv_urb = usb_alloc_urb(0, GFP_KERNEL);
1160 	if (!instance->rcv_urb) {
1161 		ret = -ENOMEM;
1162 		goto fail;
1163 	}
1164 	instance->snd_urb = usb_alloc_urb(0, GFP_KERNEL);
1165 	if (!instance->snd_urb) {
1166 		ret = -ENOMEM;
1167 		goto fail;
1168 	}
1169 
1170 	if (!cmd_ep) {
1171 		usb_dbg(usbatm_instance, "cxacru_bind: no command endpoint\n");
1172 		ret = -ENODEV;
1173 		goto fail;
1174 	}
1175 
1176 	if ((cmd_ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1177 			== USB_ENDPOINT_XFER_INT) {
1178 		usb_fill_int_urb(instance->rcv_urb,
1179 			usb_dev, usb_rcvintpipe(usb_dev, CXACRU_EP_CMD),
1180 			instance->rcv_buf, PAGE_SIZE,
1181 			cxacru_blocking_completion, &instance->rcv_done, 1);
1182 
1183 		usb_fill_int_urb(instance->snd_urb,
1184 			usb_dev, usb_sndintpipe(usb_dev, CXACRU_EP_CMD),
1185 			instance->snd_buf, PAGE_SIZE,
1186 			cxacru_blocking_completion, &instance->snd_done, 4);
1187 	} else {
1188 		usb_fill_bulk_urb(instance->rcv_urb,
1189 			usb_dev, usb_rcvbulkpipe(usb_dev, CXACRU_EP_CMD),
1190 			instance->rcv_buf, PAGE_SIZE,
1191 			cxacru_blocking_completion, &instance->rcv_done);
1192 
1193 		usb_fill_bulk_urb(instance->snd_urb,
1194 			usb_dev, usb_sndbulkpipe(usb_dev, CXACRU_EP_CMD),
1195 			instance->snd_buf, PAGE_SIZE,
1196 			cxacru_blocking_completion, &instance->snd_done);
1197 	}
1198 
1199 	mutex_init(&instance->cm_serialize);
1200 
1201 	INIT_DELAYED_WORK(&instance->poll_work, cxacru_poll_status);
1202 
1203 	usbatm_instance->driver_data = instance;
1204 
1205 	usbatm_instance->flags = (cxacru_card_status(instance) ? 0 : UDSL_SKIP_HEAVY_INIT);
1206 
1207 	return 0;
1208 
1209  fail:
1210 	free_page((unsigned long) instance->snd_buf);
1211 	free_page((unsigned long) instance->rcv_buf);
1212 	usb_free_urb(instance->snd_urb);
1213 	usb_free_urb(instance->rcv_urb);
1214 	kfree(instance);
1215 
1216 	return ret;
1217 }
1218 
1219 static void cxacru_unbind(struct usbatm_data *usbatm_instance,
1220 		struct usb_interface *intf)
1221 {
1222 	struct cxacru_data *instance = usbatm_instance->driver_data;
1223 	int is_polling = 1;
1224 
1225 	usb_dbg(usbatm_instance, "cxacru_unbind entered\n");
1226 
1227 	if (!instance) {
1228 		usb_dbg(usbatm_instance, "cxacru_unbind: NULL instance!\n");
1229 		return;
1230 	}
1231 
1232 	mutex_lock(&instance->poll_state_serialize);
1233 	BUG_ON(instance->poll_state == CXPOLL_SHUTDOWN);
1234 
1235 	/* ensure that status polling continues unless
1236 	 * it has already stopped */
1237 	if (instance->poll_state == CXPOLL_STOPPED)
1238 		is_polling = 0;
1239 
1240 	/* stop polling from being stopped or started */
1241 	instance->poll_state = CXPOLL_SHUTDOWN;
1242 	mutex_unlock(&instance->poll_state_serialize);
1243 
1244 	if (is_polling)
1245 		cancel_delayed_work_sync(&instance->poll_work);
1246 
1247 	usb_kill_urb(instance->snd_urb);
1248 	usb_kill_urb(instance->rcv_urb);
1249 	usb_free_urb(instance->snd_urb);
1250 	usb_free_urb(instance->rcv_urb);
1251 
1252 	free_page((unsigned long) instance->snd_buf);
1253 	free_page((unsigned long) instance->rcv_buf);
1254 
1255 	kfree(instance);
1256 
1257 	usbatm_instance->driver_data = NULL;
1258 }
1259 
1260 static const struct cxacru_modem_type cxacru_cafe = {
1261 	.pll_f_clk = 0x02d874df,
1262 	.pll_b_clk = 0x0196a51a,
1263 	.boot_rom_patch = 1,
1264 };
1265 
1266 static const struct cxacru_modem_type cxacru_cb00 = {
1267 	.pll_f_clk = 0x5,
1268 	.pll_b_clk = 0x3,
1269 	.boot_rom_patch = 0,
1270 };
1271 
1272 static const struct usb_device_id cxacru_usb_ids[] = {
1273 	{ /* V = Conexant			P = ADSL modem (Euphrates project)	*/
1274 		USB_DEVICE(0x0572, 0xcafe),	.driver_info = (unsigned long) &cxacru_cafe
1275 	},
1276 	{ /* V = Conexant			P = ADSL modem (Hasbani project)	*/
1277 		USB_DEVICE(0x0572, 0xcb00),	.driver_info = (unsigned long) &cxacru_cb00
1278 	},
1279 	{ /* V = Conexant			P = ADSL modem				*/
1280 		USB_DEVICE(0x0572, 0xcb01),	.driver_info = (unsigned long) &cxacru_cb00
1281 	},
1282 	{ /* V = Conexant			P = ADSL modem (Well PTI-800) */
1283 		USB_DEVICE(0x0572, 0xcb02),	.driver_info = (unsigned long) &cxacru_cb00
1284 	},
1285 	{ /* V = Conexant			P = ADSL modem				*/
1286 		USB_DEVICE(0x0572, 0xcb06),	.driver_info = (unsigned long) &cxacru_cb00
1287 	},
1288 	{ /* V = Conexant			P = ADSL modem (ZTE ZXDSL 852)		*/
1289 		USB_DEVICE(0x0572, 0xcb07),	.driver_info = (unsigned long) &cxacru_cb00
1290 	},
1291 	{ /* V = Olitec				P = ADSL modem version 2		*/
1292 		USB_DEVICE(0x08e3, 0x0100),	.driver_info = (unsigned long) &cxacru_cafe
1293 	},
1294 	{ /* V = Olitec				P = ADSL modem version 3		*/
1295 		USB_DEVICE(0x08e3, 0x0102),	.driver_info = (unsigned long) &cxacru_cb00
1296 	},
1297 	{ /* V = Trust/Amigo Technology Co.	P = AMX-CA86U				*/
1298 		USB_DEVICE(0x0eb0, 0x3457),	.driver_info = (unsigned long) &cxacru_cafe
1299 	},
1300 	{ /* V = Zoom				P = 5510				*/
1301 		USB_DEVICE(0x1803, 0x5510),	.driver_info = (unsigned long) &cxacru_cb00
1302 	},
1303 	{ /* V = Draytek			P = Vigor 318				*/
1304 		USB_DEVICE(0x0675, 0x0200),	.driver_info = (unsigned long) &cxacru_cb00
1305 	},
1306 	{ /* V = Zyxel				P = 630-C1 aka OMNI ADSL USB (Annex A)	*/
1307 		USB_DEVICE(0x0586, 0x330a),	.driver_info = (unsigned long) &cxacru_cb00
1308 	},
1309 	{ /* V = Zyxel				P = 630-C3 aka OMNI ADSL USB (Annex B)	*/
1310 		USB_DEVICE(0x0586, 0x330b),	.driver_info = (unsigned long) &cxacru_cb00
1311 	},
1312 	{ /* V = Aethra				P = Starmodem UM1020			*/
1313 		USB_DEVICE(0x0659, 0x0020),	.driver_info = (unsigned long) &cxacru_cb00
1314 	},
1315 	{ /* V = Aztech Systems			P = ? AKA Pirelli AUA-010		*/
1316 		USB_DEVICE(0x0509, 0x0812),	.driver_info = (unsigned long) &cxacru_cb00
1317 	},
1318 	{ /* V = Netopia			P = Cayman 3341(Annex A)/3351(Annex B)	*/
1319 		USB_DEVICE(0x100d, 0xcb01),	.driver_info = (unsigned long) &cxacru_cb00
1320 	},
1321 	{ /* V = Netopia			P = Cayman 3342(Annex A)/3352(Annex B)	*/
1322 		USB_DEVICE(0x100d, 0x3342),	.driver_info = (unsigned long) &cxacru_cb00
1323 	},
1324 	{}
1325 };
1326 
1327 MODULE_DEVICE_TABLE(usb, cxacru_usb_ids);
1328 
1329 static struct usbatm_driver cxacru_driver = {
1330 	.driver_name	= cxacru_driver_name,
1331 	.bind		= cxacru_bind,
1332 	.heavy_init	= cxacru_heavy_init,
1333 	.unbind		= cxacru_unbind,
1334 	.atm_start	= cxacru_atm_start,
1335 	.atm_stop	= cxacru_remove_device_files,
1336 	.bulk_in	= CXACRU_EP_DATA,
1337 	.bulk_out	= CXACRU_EP_DATA,
1338 	.rx_padding	= 3,
1339 	.tx_padding	= 11,
1340 };
1341 
1342 static int cxacru_usb_probe(struct usb_interface *intf,
1343 		const struct usb_device_id *id)
1344 {
1345 	struct usb_device *usb_dev = interface_to_usbdev(intf);
1346 	char buf[15];
1347 
1348 	/* Avoid ADSL routers (cx82310_eth).
1349 	 * Abort if bDeviceClass is 0xff and iProduct is "USB NET CARD".
1350 	 */
1351 	if (usb_dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC
1352 			&& usb_string(usb_dev, usb_dev->descriptor.iProduct,
1353 				buf, sizeof(buf)) > 0) {
1354 		if (!strcmp(buf, "USB NET CARD")) {
1355 			dev_info(&intf->dev, "ignoring cx82310_eth device\n");
1356 			return -ENODEV;
1357 		}
1358 	}
1359 
1360 	return usbatm_usb_probe(intf, id, &cxacru_driver);
1361 }
1362 
1363 static struct usb_driver cxacru_usb_driver = {
1364 	.name		= cxacru_driver_name,
1365 	.probe		= cxacru_usb_probe,
1366 	.disconnect	= usbatm_usb_disconnect,
1367 	.id_table	= cxacru_usb_ids
1368 };
1369 
1370 module_usb_driver(cxacru_usb_driver);
1371 
1372 MODULE_AUTHOR(DRIVER_AUTHOR);
1373 MODULE_DESCRIPTION(DRIVER_DESC);
1374 MODULE_LICENSE("GPL");
1375