blob: 5dc21383aa83231e6d7898e1a55f512093fac347 [file] [log] [blame]
Duncan Sands1b0e6142005-05-11 20:19:29 +02001/******************************************************************************
2 * cxacru.c - driver for USB ADSL modems based on
3 * Conexant AccessRunner chipset
4 *
5 * Copyright (C) 2004 David Woodhouse, Duncan Sands, Roman Kagan
6 * Copyright (C) 2005 Duncan Sands, Roman Kagan (rkagan % mail ! ru)
Simon Arlott6a02c9962007-04-26 00:38:05 -07007 * Copyright (C) 2007 Simon Arlott
Duncan Sands1b0e6142005-05-11 20:19:29 +02008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program; if not, write to the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 *
23 ******************************************************************************/
24
25/*
26 * Credit is due for Josep Comas, who created the original patch to speedtch.c
27 * to support the different padding used by the AccessRunner (now generalized
28 * into usbatm), and the userspace firmware loading utility.
29 */
30
31#include <linux/module.h>
32#include <linux/moduleparam.h>
33#include <linux/kernel.h>
34#include <linux/timer.h>
35#include <linux/errno.h>
36#include <linux/slab.h>
37#include <linux/init.h>
Simon Arlottfa70fe42007-03-06 02:47:45 -080038#include <linux/device.h>
Duncan Sands1b0e6142005-05-11 20:19:29 +020039#include <linux/firmware.h>
Arjan van de Venab3c81f2006-01-13 15:52:55 +010040#include <linux/mutex.h>
Al Virofd05e722008-04-28 07:00:16 +010041#include <asm/unaligned.h>
Duncan Sands1b0e6142005-05-11 20:19:29 +020042
43#include "usbatm.h"
44
Simon Arlottfa70fe42007-03-06 02:47:45 -080045#define DRIVER_AUTHOR "Roman Kagan, David Woodhouse, Duncan Sands, Simon Arlott"
46#define DRIVER_VERSION "0.3"
Duncan Sands1b0e6142005-05-11 20:19:29 +020047#define DRIVER_DESC "Conexant AccessRunner ADSL USB modem driver"
48
49static const char cxacru_driver_name[] = "cxacru";
50
51#define CXACRU_EP_CMD 0x01 /* Bulk/interrupt in/out */
52#define CXACRU_EP_DATA 0x02 /* Bulk in/out */
53
54#define CMD_PACKET_SIZE 64 /* Should be maxpacket(ep)? */
55
56/* Addresses */
57#define PLLFCLK_ADDR 0x00350068
58#define PLLBCLK_ADDR 0x0035006c
59#define SDRAMEN_ADDR 0x00350010
60#define FW_ADDR 0x00801000
61#define BR_ADDR 0x00180600
62#define SIG_ADDR 0x00180500
63#define BR_STACK_ADDR 0x00187f10
64
65/* Values */
66#define SDRAM_ENA 0x1
67
68#define CMD_TIMEOUT 2000 /* msecs */
Simon Arlottfa70fe42007-03-06 02:47:45 -080069#define POLL_INTERVAL 1 /* secs */
Duncan Sands1b0e6142005-05-11 20:19:29 +020070
71/* commands for interaction with the modem through the control channel before
72 * firmware is loaded */
73enum cxacru_fw_request {
74 FW_CMD_ERR,
75 FW_GET_VER,
76 FW_READ_MEM,
77 FW_WRITE_MEM,
78 FW_RMW_MEM,
79 FW_CHECKSUM_MEM,
80 FW_GOTO_MEM,
81};
82
83/* commands for interaction with the modem through the control channel once
84 * firmware is loaded */
85enum cxacru_cm_request {
86 CM_REQUEST_UNDEFINED = 0x80,
87 CM_REQUEST_TEST,
88 CM_REQUEST_CHIP_GET_MAC_ADDRESS,
89 CM_REQUEST_CHIP_GET_DP_VERSIONS,
90 CM_REQUEST_CHIP_ADSL_LINE_START,
91 CM_REQUEST_CHIP_ADSL_LINE_STOP,
92 CM_REQUEST_CHIP_ADSL_LINE_GET_STATUS,
93 CM_REQUEST_CHIP_ADSL_LINE_GET_SPEED,
94 CM_REQUEST_CARD_INFO_GET,
95 CM_REQUEST_CARD_DATA_GET,
96 CM_REQUEST_CARD_DATA_SET,
97 CM_REQUEST_COMMAND_HW_IO,
98 CM_REQUEST_INTERFACE_HW_IO,
99 CM_REQUEST_CARD_SERIAL_DATA_PATH_GET,
100 CM_REQUEST_CARD_SERIAL_DATA_PATH_SET,
101 CM_REQUEST_CARD_CONTROLLER_VERSION_GET,
102 CM_REQUEST_CARD_GET_STATUS,
103 CM_REQUEST_CARD_GET_MAC_ADDRESS,
104 CM_REQUEST_CARD_GET_DATA_LINK_STATUS,
105 CM_REQUEST_MAX,
106};
107
Simon Arlottc68bb0d2009-11-21 15:12:21 +0000108/* commands for interaction with the flash memory
109 *
110 * read: response is the contents of the first 60 bytes of flash memory
111 * write: request contains the 60 bytes of data to write to flash memory
112 * response is the contents of the first 60 bytes of flash memory
113 *
114 * layout: PP PP VV VV MM MM MM MM MM MM ?? ?? SS SS SS SS SS SS SS SS
115 * SS SS SS SS SS SS SS SS 00 00 00 00 00 00 00 00 00 00 00 00
116 * 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
117 *
118 * P: le16 USB Product ID
119 * V: le16 USB Vendor ID
120 * M: be48 MAC Address
121 * S: le16 ASCII Serial Number
122 */
123enum cxacru_cm_flash {
124 CM_FLASH_READ = 0xa1,
125 CM_FLASH_WRITE = 0xa2
126};
127
Duncan Sands1b0e6142005-05-11 20:19:29 +0200128/* reply codes to the commands above */
129enum cxacru_cm_status {
130 CM_STATUS_UNDEFINED,
131 CM_STATUS_SUCCESS,
132 CM_STATUS_ERROR,
133 CM_STATUS_UNSUPPORTED,
134 CM_STATUS_UNIMPLEMENTED,
135 CM_STATUS_PARAMETER_ERROR,
136 CM_STATUS_DBG_LOOPBACK,
137 CM_STATUS_MAX,
138};
139
140/* indices into CARD_INFO_GET return array */
141enum cxacru_info_idx {
142 CXINF_DOWNSTREAM_RATE,
143 CXINF_UPSTREAM_RATE,
144 CXINF_LINK_STATUS,
145 CXINF_LINE_STATUS,
146 CXINF_MAC_ADDRESS_HIGH,
147 CXINF_MAC_ADDRESS_LOW,
148 CXINF_UPSTREAM_SNR_MARGIN,
149 CXINF_DOWNSTREAM_SNR_MARGIN,
150 CXINF_UPSTREAM_ATTENUATION,
151 CXINF_DOWNSTREAM_ATTENUATION,
152 CXINF_TRANSMITTER_POWER,
153 CXINF_UPSTREAM_BITS_PER_FRAME,
154 CXINF_DOWNSTREAM_BITS_PER_FRAME,
155 CXINF_STARTUP_ATTEMPTS,
156 CXINF_UPSTREAM_CRC_ERRORS,
157 CXINF_DOWNSTREAM_CRC_ERRORS,
158 CXINF_UPSTREAM_FEC_ERRORS,
159 CXINF_DOWNSTREAM_FEC_ERRORS,
160 CXINF_UPSTREAM_HEC_ERRORS,
161 CXINF_DOWNSTREAM_HEC_ERRORS,
162 CXINF_LINE_STARTABLE,
163 CXINF_MODULATION,
164 CXINF_ADSL_HEADEND,
165 CXINF_ADSL_HEADEND_ENVIRONMENT,
166 CXINF_CONTROLLER_VERSION,
167 /* dunno what the missing two mean */
168 CXINF_MAX = 0x1c,
169};
170
Simon Arlott6a02c9962007-04-26 00:38:05 -0700171enum cxacru_poll_state {
172 CXPOLL_STOPPING,
173 CXPOLL_STOPPED,
174 CXPOLL_POLLING,
175 CXPOLL_SHUTDOWN
176};
177
Duncan Sands1b0e6142005-05-11 20:19:29 +0200178struct cxacru_modem_type {
179 u32 pll_f_clk;
180 u32 pll_b_clk;
181 int boot_rom_patch;
182};
183
184struct cxacru_data {
185 struct usbatm_data *usbatm;
186
187 const struct cxacru_modem_type *modem_type;
188
189 int line_status;
Simon Arlott6a02c9962007-04-26 00:38:05 -0700190 struct mutex adsl_state_serialize;
191 int adsl_status;
David Howellsc4028952006-11-22 14:57:56 +0000192 struct delayed_work poll_work;
Simon Arlottfa70fe42007-03-06 02:47:45 -0800193 u32 card_info[CXINF_MAX];
Simon Arlott6a02c9962007-04-26 00:38:05 -0700194 struct mutex poll_state_serialize;
Simon Arlott87e71b42007-05-10 23:04:11 -0700195 enum cxacru_poll_state poll_state;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200196
197 /* contol handles */
Arjan van de Venab3c81f2006-01-13 15:52:55 +0100198 struct mutex cm_serialize;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200199 u8 *rcv_buf;
200 u8 *snd_buf;
201 struct urb *rcv_urb;
202 struct urb *snd_urb;
203 struct completion rcv_done;
204 struct completion snd_done;
205};
206
Simon Arlott6a02c9962007-04-26 00:38:05 -0700207static int cxacru_cm(struct cxacru_data *instance, enum cxacru_cm_request cm,
208 u8 *wdata, int wsize, u8 *rdata, int rsize);
209static void cxacru_poll_status(struct work_struct *work);
210
Simon Arlottfa70fe42007-03-06 02:47:45 -0800211/* Card info exported through sysfs */
212#define CXACRU__ATTR_INIT(_name) \
213static DEVICE_ATTR(_name, S_IRUGO, cxacru_sysfs_show_##_name, NULL)
214
Simon Arlott6a02c9962007-04-26 00:38:05 -0700215#define CXACRU_CMD_INIT(_name) \
216static DEVICE_ATTR(_name, S_IWUSR | S_IRUGO, \
217 cxacru_sysfs_show_##_name, cxacru_sysfs_store_##_name)
218
Simon Arlottfa70fe42007-03-06 02:47:45 -0800219#define CXACRU_ATTR_INIT(_value, _type, _name) \
220static ssize_t cxacru_sysfs_show_##_name(struct device *dev, \
221 struct device_attribute *attr, char *buf) \
222{ \
Simon Arlott9fc950d2009-11-21 15:33:51 +0000223 struct cxacru_data *instance = to_usbatm_driver_data(\
224 to_usb_interface(dev)); \
225\
226 if (instance == NULL) \
227 return -ENODEV; \
228\
Simon Arlottfa70fe42007-03-06 02:47:45 -0800229 return cxacru_sysfs_showattr_##_type(instance->card_info[_value], buf); \
230} \
231CXACRU__ATTR_INIT(_name)
232
233#define CXACRU_ATTR_CREATE(_v, _t, _name) CXACRU_DEVICE_CREATE_FILE(_name)
Simon Arlott6a02c9962007-04-26 00:38:05 -0700234#define CXACRU_CMD_CREATE(_name) CXACRU_DEVICE_CREATE_FILE(_name)
Simon Arlottfa70fe42007-03-06 02:47:45 -0800235#define CXACRU__ATTR_CREATE(_name) CXACRU_DEVICE_CREATE_FILE(_name)
236
237#define CXACRU_ATTR_REMOVE(_v, _t, _name) CXACRU_DEVICE_REMOVE_FILE(_name)
Simon Arlott6a02c9962007-04-26 00:38:05 -0700238#define CXACRU_CMD_REMOVE(_name) CXACRU_DEVICE_REMOVE_FILE(_name)
Simon Arlottfa70fe42007-03-06 02:47:45 -0800239#define CXACRU__ATTR_REMOVE(_name) CXACRU_DEVICE_REMOVE_FILE(_name)
240
241static ssize_t cxacru_sysfs_showattr_u32(u32 value, char *buf)
242{
243 return snprintf(buf, PAGE_SIZE, "%u\n", value);
244}
245
246static ssize_t cxacru_sysfs_showattr_s8(s8 value, char *buf)
247{
248 return snprintf(buf, PAGE_SIZE, "%d\n", value);
249}
250
251static ssize_t cxacru_sysfs_showattr_dB(s16 value, char *buf)
252{
Simon Arlott10107bd2009-04-23 18:19:02 +0100253 if (likely(value >= 0)) {
254 return snprintf(buf, PAGE_SIZE, "%u.%02u\n",
255 value / 100, value % 100);
256 } else {
257 value = -value;
258 return snprintf(buf, PAGE_SIZE, "-%u.%02u\n",
259 value / 100, value % 100);
260 }
Simon Arlottfa70fe42007-03-06 02:47:45 -0800261}
262
263static ssize_t cxacru_sysfs_showattr_bool(u32 value, char *buf)
264{
Simon Arlott87e71b42007-05-10 23:04:11 -0700265 static char *str[] = { "no", "yes" };
266 if (unlikely(value >= ARRAY_SIZE(str)))
267 return snprintf(buf, PAGE_SIZE, "%u\n", value);
268 return snprintf(buf, PAGE_SIZE, "%s\n", str[value]);
Simon Arlottfa70fe42007-03-06 02:47:45 -0800269}
270
271static ssize_t cxacru_sysfs_showattr_LINK(u32 value, char *buf)
272{
Simon Arlott87e71b42007-05-10 23:04:11 -0700273 static char *str[] = { NULL, "not connected", "connected", "lost" };
274 if (unlikely(value >= ARRAY_SIZE(str) || str[value] == NULL))
275 return snprintf(buf, PAGE_SIZE, "%u\n", value);
276 return snprintf(buf, PAGE_SIZE, "%s\n", str[value]);
Simon Arlottfa70fe42007-03-06 02:47:45 -0800277}
278
279static ssize_t cxacru_sysfs_showattr_LINE(u32 value, char *buf)
280{
Simon Arlott87e71b42007-05-10 23:04:11 -0700281 static char *str[] = { "down", "attempting to activate",
282 "training", "channel analysis", "exchange", "up",
283 "waiting", "initialising"
284 };
285 if (unlikely(value >= ARRAY_SIZE(str)))
286 return snprintf(buf, PAGE_SIZE, "%u\n", value);
287 return snprintf(buf, PAGE_SIZE, "%s\n", str[value]);
Simon Arlottfa70fe42007-03-06 02:47:45 -0800288}
289
290static ssize_t cxacru_sysfs_showattr_MODU(u32 value, char *buf)
291{
Simon Arlott87e71b42007-05-10 23:04:11 -0700292 static char *str[] = {
Simon Arlott1bfbd282009-11-21 15:03:23 +0000293 "",
Simon Arlott87e71b42007-05-10 23:04:11 -0700294 "ANSI T1.413",
295 "ITU-T G.992.1 (G.DMT)",
296 "ITU-T G.992.2 (G.LITE)"
297 };
Simon Arlott1bfbd282009-11-21 15:03:23 +0000298 if (unlikely(value >= ARRAY_SIZE(str)))
Simon Arlott87e71b42007-05-10 23:04:11 -0700299 return snprintf(buf, PAGE_SIZE, "%u\n", value);
300 return snprintf(buf, PAGE_SIZE, "%s\n", str[value]);
Simon Arlottfa70fe42007-03-06 02:47:45 -0800301}
302
303/*
304 * This could use MAC_ADDRESS_HIGH and MAC_ADDRESS_LOW, but since
305 * this data is already in atm_dev there's no point.
306 *
307 * MAC_ADDRESS_HIGH = 0x????5544
308 * MAC_ADDRESS_LOW = 0x33221100
309 * Where 00-55 are bytes 0-5 of the MAC.
310 */
311static ssize_t cxacru_sysfs_show_mac_address(struct device *dev,
312 struct device_attribute *attr, char *buf)
313{
Simon Arlott9fc950d2009-11-21 15:33:51 +0000314 struct cxacru_data *instance = to_usbatm_driver_data(
315 to_usb_interface(dev));
Simon Arlottfa70fe42007-03-06 02:47:45 -0800316
Simon Arlott9fc950d2009-11-21 15:33:51 +0000317 if (instance == NULL || instance->usbatm->atm_dev == NULL)
318 return -ENODEV;
319
320 return snprintf(buf, PAGE_SIZE, "%pM\n",
321 instance->usbatm->atm_dev->esi);
Simon Arlottfa70fe42007-03-06 02:47:45 -0800322}
323
Simon Arlott6a02c9962007-04-26 00:38:05 -0700324static ssize_t cxacru_sysfs_show_adsl_state(struct device *dev,
325 struct device_attribute *attr, char *buf)
326{
Simon Arlott87e71b42007-05-10 23:04:11 -0700327 static char *str[] = { "running", "stopped" };
Simon Arlott9fc950d2009-11-21 15:33:51 +0000328 struct cxacru_data *instance = to_usbatm_driver_data(
329 to_usb_interface(dev));
330 u32 value;
331
332 if (instance == NULL)
333 return -ENODEV;
334
335 value = instance->card_info[CXINF_LINE_STARTABLE];
Simon Arlott87e71b42007-05-10 23:04:11 -0700336 if (unlikely(value >= ARRAY_SIZE(str)))
337 return snprintf(buf, PAGE_SIZE, "%u\n", value);
338 return snprintf(buf, PAGE_SIZE, "%s\n", str[value]);
Simon Arlott6a02c9962007-04-26 00:38:05 -0700339}
340
341static ssize_t cxacru_sysfs_store_adsl_state(struct device *dev,
342 struct device_attribute *attr, const char *buf, size_t count)
343{
Simon Arlott9fc950d2009-11-21 15:33:51 +0000344 struct cxacru_data *instance = to_usbatm_driver_data(
345 to_usb_interface(dev));
Simon Arlott6a02c9962007-04-26 00:38:05 -0700346 int ret;
347 int poll = -1;
348 char str_cmd[8];
349 int len = strlen(buf);
350
351 if (!capable(CAP_NET_ADMIN))
352 return -EACCES;
353
354 ret = sscanf(buf, "%7s", str_cmd);
355 if (ret != 1)
356 return -EINVAL;
357 ret = 0;
358
Simon Arlott9fc950d2009-11-21 15:33:51 +0000359 if (instance == NULL)
360 return -ENODEV;
361
Simon Arlott6a02c9962007-04-26 00:38:05 -0700362 if (mutex_lock_interruptible(&instance->adsl_state_serialize))
363 return -ERESTARTSYS;
364
365 if (!strcmp(str_cmd, "stop") || !strcmp(str_cmd, "restart")) {
366 ret = cxacru_cm(instance, CM_REQUEST_CHIP_ADSL_LINE_STOP, NULL, 0, NULL, 0);
367 if (ret < 0) {
Simon Arlott9fc950d2009-11-21 15:33:51 +0000368 atm_err(instance->usbatm, "change adsl state:"
Simon Arlott6a02c9962007-04-26 00:38:05 -0700369 " CHIP_ADSL_LINE_STOP returned %d\n", ret);
370
371 ret = -EIO;
372 } else {
373 ret = len;
374 poll = CXPOLL_STOPPED;
375 }
376 }
377
378 /* Line status is only updated every second
379 * and the device appears to only react to
380 * START/STOP every second too. Wait 1.5s to
381 * be sure that restart will have an effect. */
382 if (!strcmp(str_cmd, "restart"))
383 msleep(1500);
384
385 if (!strcmp(str_cmd, "start") || !strcmp(str_cmd, "restart")) {
386 ret = cxacru_cm(instance, CM_REQUEST_CHIP_ADSL_LINE_START, NULL, 0, NULL, 0);
387 if (ret < 0) {
Simon Arlott9fc950d2009-11-21 15:33:51 +0000388 atm_err(instance->usbatm, "change adsl state:"
Simon Arlott6a02c9962007-04-26 00:38:05 -0700389 " CHIP_ADSL_LINE_START returned %d\n", ret);
390
391 ret = -EIO;
392 } else {
393 ret = len;
394 poll = CXPOLL_POLLING;
395 }
396 }
397
398 if (!strcmp(str_cmd, "poll")) {
399 ret = len;
400 poll = CXPOLL_POLLING;
401 }
402
403 if (ret == 0) {
404 ret = -EINVAL;
405 poll = -1;
406 }
407
408 if (poll == CXPOLL_POLLING) {
409 mutex_lock(&instance->poll_state_serialize);
410 switch (instance->poll_state) {
411 case CXPOLL_STOPPED:
412 /* start polling */
413 instance->poll_state = CXPOLL_POLLING;
414 break;
415
416 case CXPOLL_STOPPING:
417 /* abort stop request */
418 instance->poll_state = CXPOLL_POLLING;
419 case CXPOLL_POLLING:
420 case CXPOLL_SHUTDOWN:
421 /* don't start polling */
422 poll = -1;
423 }
424 mutex_unlock(&instance->poll_state_serialize);
425 } else if (poll == CXPOLL_STOPPED) {
426 mutex_lock(&instance->poll_state_serialize);
427 /* request stop */
428 if (instance->poll_state == CXPOLL_POLLING)
429 instance->poll_state = CXPOLL_STOPPING;
430 mutex_unlock(&instance->poll_state_serialize);
431 }
432
433 mutex_unlock(&instance->adsl_state_serialize);
434
435 if (poll == CXPOLL_POLLING)
436 cxacru_poll_status(&instance->poll_work.work);
437
438 return ret;
439}
440
Simon Arlottfa70fe42007-03-06 02:47:45 -0800441/*
442 * All device attributes are included in CXACRU_ALL_FILES
443 * so that the same list can be used multiple times:
444 * INIT (define the device attributes)
445 * CREATE (create all the device files)
446 * REMOVE (remove all the device files)
447 *
448 * With the last two being defined as needed in the functions
449 * they are used in before calling CXACRU_ALL_FILES()
450 */
451#define CXACRU_ALL_FILES(_action) \
452CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_RATE, u32, downstream_rate); \
453CXACRU_ATTR_##_action(CXINF_UPSTREAM_RATE, u32, upstream_rate); \
454CXACRU_ATTR_##_action(CXINF_LINK_STATUS, LINK, link_status); \
455CXACRU_ATTR_##_action(CXINF_LINE_STATUS, LINE, line_status); \
456CXACRU__ATTR_##_action( mac_address); \
457CXACRU_ATTR_##_action(CXINF_UPSTREAM_SNR_MARGIN, dB, upstream_snr_margin); \
458CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_SNR_MARGIN, dB, downstream_snr_margin); \
459CXACRU_ATTR_##_action(CXINF_UPSTREAM_ATTENUATION, dB, upstream_attenuation); \
460CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_ATTENUATION, dB, downstream_attenuation); \
461CXACRU_ATTR_##_action(CXINF_TRANSMITTER_POWER, s8, transmitter_power); \
462CXACRU_ATTR_##_action(CXINF_UPSTREAM_BITS_PER_FRAME, u32, upstream_bits_per_frame); \
463CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_BITS_PER_FRAME, u32, downstream_bits_per_frame); \
464CXACRU_ATTR_##_action(CXINF_STARTUP_ATTEMPTS, u32, startup_attempts); \
465CXACRU_ATTR_##_action(CXINF_UPSTREAM_CRC_ERRORS, u32, upstream_crc_errors); \
466CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_CRC_ERRORS, u32, downstream_crc_errors); \
467CXACRU_ATTR_##_action(CXINF_UPSTREAM_FEC_ERRORS, u32, upstream_fec_errors); \
468CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_FEC_ERRORS, u32, downstream_fec_errors); \
469CXACRU_ATTR_##_action(CXINF_UPSTREAM_HEC_ERRORS, u32, upstream_hec_errors); \
470CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_HEC_ERRORS, u32, downstream_hec_errors); \
471CXACRU_ATTR_##_action(CXINF_LINE_STARTABLE, bool, line_startable); \
472CXACRU_ATTR_##_action(CXINF_MODULATION, MODU, modulation); \
473CXACRU_ATTR_##_action(CXINF_ADSL_HEADEND, u32, adsl_headend); \
474CXACRU_ATTR_##_action(CXINF_ADSL_HEADEND_ENVIRONMENT, u32, adsl_headend_environment); \
Simon Arlott6a02c9962007-04-26 00:38:05 -0700475CXACRU_ATTR_##_action(CXINF_CONTROLLER_VERSION, u32, adsl_controller_version); \
476CXACRU_CMD_##_action( adsl_state);
Simon Arlottfa70fe42007-03-06 02:47:45 -0800477
478CXACRU_ALL_FILES(INIT);
479
Duncan Sands1b0e6142005-05-11 20:19:29 +0200480/* the following three functions are stolen from drivers/usb/core/message.c */
David Howells7d12e782006-10-05 14:55:46 +0100481static void cxacru_blocking_completion(struct urb *urb)
Duncan Sands1b0e6142005-05-11 20:19:29 +0200482{
Ming Leicdc97792008-02-24 18:41:47 +0800483 complete(urb->context);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200484}
485
486static void cxacru_timeout_kill(unsigned long data)
487{
488 usb_unlink_urb((struct urb *) data);
489}
490
491static int cxacru_start_wait_urb(struct urb *urb, struct completion *done,
492 int* actual_length)
493{
494 struct timer_list timer;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200495
496 init_timer(&timer);
497 timer.expires = jiffies + msecs_to_jiffies(CMD_TIMEOUT);
498 timer.data = (unsigned long) urb;
499 timer.function = cxacru_timeout_kill;
500 add_timer(&timer);
501 wait_for_completion(done);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200502 del_timer_sync(&timer);
503
504 if (actual_length)
505 *actual_length = urb->actual_length;
Oliver Neukum3b79cc22007-08-16 16:06:06 +0200506 return urb->status; /* must read status after completion */
Duncan Sands1b0e6142005-05-11 20:19:29 +0200507}
508
509static int cxacru_cm(struct cxacru_data *instance, enum cxacru_cm_request cm,
510 u8 *wdata, int wsize, u8 *rdata, int rsize)
511{
512 int ret, actlen;
513 int offb, offd;
514 const int stride = CMD_PACKET_SIZE - 4;
515 u8 *wbuf = instance->snd_buf;
516 u8 *rbuf = instance->rcv_buf;
517 int wbuflen = ((wsize - 1) / stride + 1) * CMD_PACKET_SIZE;
518 int rbuflen = ((rsize - 1) / stride + 1) * CMD_PACKET_SIZE;
519
520 if (wbuflen > PAGE_SIZE || rbuflen > PAGE_SIZE) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100521 if (printk_ratelimit())
522 usb_err(instance->usbatm, "requested transfer size too large (%d, %d)\n",
523 wbuflen, rbuflen);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200524 ret = -ENOMEM;
Jiri Slabyeeafa642009-03-11 21:47:36 +0100525 goto err;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200526 }
527
Arjan van de Venab3c81f2006-01-13 15:52:55 +0100528 mutex_lock(&instance->cm_serialize);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200529
530 /* submit reading urb before the writing one */
531 init_completion(&instance->rcv_done);
532 ret = usb_submit_urb(instance->rcv_urb, GFP_KERNEL);
533 if (ret < 0) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100534 if (printk_ratelimit())
535 usb_err(instance->usbatm, "submit of read urb for cm %#x failed (%d)\n",
536 cm, ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200537 goto fail;
538 }
539
540 memset(wbuf, 0, wbuflen);
541 /* handle wsize == 0 */
542 wbuf[0] = cm;
543 for (offb = offd = 0; offd < wsize; offd += stride, offb += CMD_PACKET_SIZE) {
544 wbuf[offb] = cm;
545 memcpy(wbuf + offb + 4, wdata + offd, min_t(int, stride, wsize - offd));
546 }
547
548 instance->snd_urb->transfer_buffer_length = wbuflen;
549 init_completion(&instance->snd_done);
550 ret = usb_submit_urb(instance->snd_urb, GFP_KERNEL);
551 if (ret < 0) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100552 if (printk_ratelimit())
553 usb_err(instance->usbatm, "submit of write urb for cm %#x failed (%d)\n",
554 cm, ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200555 goto fail;
556 }
557
558 ret = cxacru_start_wait_urb(instance->snd_urb, &instance->snd_done, NULL);
559 if (ret < 0) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100560 if (printk_ratelimit())
561 usb_err(instance->usbatm, "send of cm %#x failed (%d)\n", cm, ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200562 goto fail;
563 }
564
565 ret = cxacru_start_wait_urb(instance->rcv_urb, &instance->rcv_done, &actlen);
566 if (ret < 0) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100567 if (printk_ratelimit())
568 usb_err(instance->usbatm, "receive of cm %#x failed (%d)\n", cm, ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200569 goto fail;
570 }
571 if (actlen % CMD_PACKET_SIZE || !actlen) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100572 if (printk_ratelimit())
573 usb_err(instance->usbatm, "invalid response length to cm %#x: %d\n",
574 cm, actlen);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200575 ret = -EIO;
576 goto fail;
577 }
578
579 /* check the return status and copy the data to the output buffer, if needed */
580 for (offb = offd = 0; offd < rsize && offb < actlen; offb += CMD_PACKET_SIZE) {
581 if (rbuf[offb] != cm) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100582 if (printk_ratelimit())
583 usb_err(instance->usbatm, "wrong cm %#x in response to cm %#x\n",
584 rbuf[offb], cm);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200585 ret = -EIO;
586 goto fail;
587 }
588 if (rbuf[offb + 1] != CM_STATUS_SUCCESS) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100589 if (printk_ratelimit())
590 usb_err(instance->usbatm, "response to cm %#x failed: %#x\n",
591 cm, rbuf[offb + 1]);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200592 ret = -EIO;
593 goto fail;
594 }
595 if (offd >= rsize)
596 break;
597 memcpy(rdata + offd, rbuf + offb + 4, min_t(int, stride, rsize - offd));
598 offd += stride;
599 }
600
601 ret = offd;
602 dbg("cm %#x", cm);
603fail:
Arjan van de Venab3c81f2006-01-13 15:52:55 +0100604 mutex_unlock(&instance->cm_serialize);
Jiri Slabyeeafa642009-03-11 21:47:36 +0100605err:
Duncan Sands1b0e6142005-05-11 20:19:29 +0200606 return ret;
607}
608
609static int cxacru_cm_get_array(struct cxacru_data *instance, enum cxacru_cm_request cm,
610 u32 *data, int size)
611{
612 int ret, len;
Al Virofd05e722008-04-28 07:00:16 +0100613 __le32 *buf;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200614 int offb, offd;
615 const int stride = CMD_PACKET_SIZE / (4 * 2) - 1;
616 int buflen = ((size - 1) / stride + 1 + size * 2) * 4;
617
618 buf = kmalloc(buflen, GFP_KERNEL);
619 if (!buf)
620 return -ENOMEM;
621
622 ret = cxacru_cm(instance, cm, NULL, 0, (u8 *) buf, buflen);
623 if (ret < 0)
624 goto cleanup;
625
626 /* len > 0 && len % 4 == 0 guaranteed by cxacru_cm() */
627 len = ret / 4;
628 for (offb = 0; offb < len; ) {
629 int l = le32_to_cpu(buf[offb++]);
Simon Arlott5d0a9c72009-11-21 15:07:14 +0000630 if (l < 0 || l > stride || l > (len - offb) / 2) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100631 if (printk_ratelimit())
632 usb_err(instance->usbatm, "invalid data length from cm %#x: %d\n",
633 cm, l);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200634 ret = -EIO;
635 goto cleanup;
636 }
637 while (l--) {
638 offd = le32_to_cpu(buf[offb++]);
639 if (offd >= size) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100640 if (printk_ratelimit())
Simon Arlott230ffc72008-07-12 22:19:48 +0100641 usb_err(instance->usbatm, "wrong index %#x in response to cm %#x\n",
Simon Arlott4ac07182007-09-25 20:20:10 +0100642 offd, cm);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200643 ret = -EIO;
644 goto cleanup;
645 }
646 data[offd] = le32_to_cpu(buf[offb++]);
647 }
648 }
649
650 ret = 0;
651
652cleanup:
653 kfree(buf);
654 return ret;
655}
656
657static int cxacru_card_status(struct cxacru_data *instance)
658{
659 int ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_STATUS, NULL, 0, NULL, 0);
660 if (ret < 0) { /* firmware not loaded */
661 dbg("cxacru_adsl_start: CARD_GET_STATUS returned %d", ret);
662 return ret;
663 }
664 return 0;
665}
666
Simon Arlottda1f82b52007-05-10 23:04:12 -0700667static void cxacru_remove_device_files(struct usbatm_data *usbatm_instance,
668 struct atm_dev *atm_dev)
669{
670 struct usb_interface *intf = usbatm_instance->usb_intf;
671
672 #define CXACRU_DEVICE_REMOVE_FILE(_name) \
673 device_remove_file(&intf->dev, &dev_attr_##_name);
674 CXACRU_ALL_FILES(REMOVE);
675 #undef CXACRU_DEVICE_REMOVE_FILE
676}
677
Duncan Sands1b0e6142005-05-11 20:19:29 +0200678static int cxacru_atm_start(struct usbatm_data *usbatm_instance,
679 struct atm_dev *atm_dev)
680{
681 struct cxacru_data *instance = usbatm_instance->driver_data;
Simon Arlottda1f82b52007-05-10 23:04:12 -0700682 struct usb_interface *intf = usbatm_instance->usb_intf;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200683 int ret;
Simon Arlott6a02c9962007-04-26 00:38:05 -0700684 int start_polling = 1;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200685
686 dbg("cxacru_atm_start");
687
688 /* Read MAC address */
689 ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_MAC_ADDRESS, NULL, 0,
690 atm_dev->esi, sizeof(atm_dev->esi));
691 if (ret < 0) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100692 atm_err(usbatm_instance, "cxacru_atm_start: CARD_GET_MAC_ADDRESS returned %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200693 return ret;
694 }
695
Simon Arlottda1f82b52007-05-10 23:04:12 -0700696 #define CXACRU_DEVICE_CREATE_FILE(_name) \
697 ret = device_create_file(&intf->dev, &dev_attr_##_name); \
698 if (unlikely(ret)) \
699 goto fail_sysfs;
700 CXACRU_ALL_FILES(CREATE);
701 #undef CXACRU_DEVICE_CREATE_FILE
702
Duncan Sands1b0e6142005-05-11 20:19:29 +0200703 /* start ADSL */
Simon Arlott6a02c9962007-04-26 00:38:05 -0700704 mutex_lock(&instance->adsl_state_serialize);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200705 ret = cxacru_cm(instance, CM_REQUEST_CHIP_ADSL_LINE_START, NULL, 0, NULL, 0);
Simon Arlottfd209e32007-05-10 23:04:13 -0700706 if (ret < 0)
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100707 atm_err(usbatm_instance, "cxacru_atm_start: CHIP_ADSL_LINE_START returned %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200708
709 /* Start status polling */
Simon Arlott6a02c9962007-04-26 00:38:05 -0700710 mutex_lock(&instance->poll_state_serialize);
711 switch (instance->poll_state) {
712 case CXPOLL_STOPPED:
713 /* start polling */
714 instance->poll_state = CXPOLL_POLLING;
715 break;
716
717 case CXPOLL_STOPPING:
718 /* abort stop request */
719 instance->poll_state = CXPOLL_POLLING;
720 case CXPOLL_POLLING:
721 case CXPOLL_SHUTDOWN:
722 /* don't start polling */
723 start_polling = 0;
724 }
725 mutex_unlock(&instance->poll_state_serialize);
726 mutex_unlock(&instance->adsl_state_serialize);
727
728 if (start_polling)
729 cxacru_poll_status(&instance->poll_work.work);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200730 return 0;
Simon Arlottda1f82b52007-05-10 23:04:12 -0700731
732fail_sysfs:
733 usb_err(usbatm_instance, "cxacru_atm_start: device_create_file failed (%d)\n", ret);
734 cxacru_remove_device_files(usbatm_instance, atm_dev);
735 return ret;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200736}
737
David Howellsc4028952006-11-22 14:57:56 +0000738static void cxacru_poll_status(struct work_struct *work)
Duncan Sands1b0e6142005-05-11 20:19:29 +0200739{
David Howellsc4028952006-11-22 14:57:56 +0000740 struct cxacru_data *instance =
741 container_of(work, struct cxacru_data, poll_work.work);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200742 u32 buf[CXINF_MAX] = {};
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100743 struct usbatm_data *usbatm = instance->usbatm;
744 struct atm_dev *atm_dev = usbatm->atm_dev;
Simon Arlott6a02c9962007-04-26 00:38:05 -0700745 int keep_polling = 1;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200746 int ret;
747
748 ret = cxacru_cm_get_array(instance, CM_REQUEST_CARD_INFO_GET, buf, CXINF_MAX);
749 if (ret < 0) {
Simon Arlott6a02c9962007-04-26 00:38:05 -0700750 if (ret != -ESHUTDOWN)
751 atm_warn(usbatm, "poll status: error %d\n", ret);
752
753 mutex_lock(&instance->poll_state_serialize);
754 if (instance->poll_state != CXPOLL_SHUTDOWN) {
755 instance->poll_state = CXPOLL_STOPPED;
756
757 if (ret != -ESHUTDOWN)
758 atm_warn(usbatm, "polling disabled, set adsl_state"
759 " to 'start' or 'poll' to resume\n");
760 }
761 mutex_unlock(&instance->poll_state_serialize);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200762 goto reschedule;
763 }
764
Simon Arlottfa70fe42007-03-06 02:47:45 -0800765 memcpy(instance->card_info, buf, sizeof(instance->card_info));
766
Simon Arlott6a02c9962007-04-26 00:38:05 -0700767 if (instance->adsl_status != buf[CXINF_LINE_STARTABLE]) {
768 instance->adsl_status = buf[CXINF_LINE_STARTABLE];
769
770 switch (instance->adsl_status) {
771 case 0:
772 atm_printk(KERN_INFO, usbatm, "ADSL state: running\n");
773 break;
774
775 case 1:
776 atm_printk(KERN_INFO, usbatm, "ADSL state: stopped\n");
777 break;
778
779 default:
780 atm_printk(KERN_INFO, usbatm, "Unknown adsl status %02x\n", instance->adsl_status);
781 break;
782 }
783 }
784
Duncan Sands1b0e6142005-05-11 20:19:29 +0200785 if (instance->line_status == buf[CXINF_LINE_STATUS])
786 goto reschedule;
787
788 instance->line_status = buf[CXINF_LINE_STATUS];
789 switch (instance->line_status) {
790 case 0:
791 atm_dev->signal = ATM_PHY_SIG_LOST;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100792 atm_info(usbatm, "ADSL line: down\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200793 break;
794
795 case 1:
796 atm_dev->signal = ATM_PHY_SIG_LOST;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100797 atm_info(usbatm, "ADSL line: attempting to activate\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200798 break;
799
800 case 2:
801 atm_dev->signal = ATM_PHY_SIG_LOST;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100802 atm_info(usbatm, "ADSL line: training\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200803 break;
804
805 case 3:
806 atm_dev->signal = ATM_PHY_SIG_LOST;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100807 atm_info(usbatm, "ADSL line: channel analysis\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200808 break;
809
810 case 4:
811 atm_dev->signal = ATM_PHY_SIG_LOST;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100812 atm_info(usbatm, "ADSL line: exchange\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200813 break;
814
815 case 5:
816 atm_dev->link_rate = buf[CXINF_DOWNSTREAM_RATE] * 1000 / 424;
817 atm_dev->signal = ATM_PHY_SIG_FOUND;
818
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100819 atm_info(usbatm, "ADSL line: up (%d kb/s down | %d kb/s up)\n",
Duncan Sands1b0e6142005-05-11 20:19:29 +0200820 buf[CXINF_DOWNSTREAM_RATE], buf[CXINF_UPSTREAM_RATE]);
821 break;
822
823 case 6:
824 atm_dev->signal = ATM_PHY_SIG_LOST;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100825 atm_info(usbatm, "ADSL line: waiting\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200826 break;
827
828 case 7:
829 atm_dev->signal = ATM_PHY_SIG_LOST;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100830 atm_info(usbatm, "ADSL line: initializing\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200831 break;
832
833 default:
834 atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100835 atm_info(usbatm, "Unknown line state %02x\n", instance->line_status);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200836 break;
837 }
838reschedule:
Simon Arlott6a02c9962007-04-26 00:38:05 -0700839
840 mutex_lock(&instance->poll_state_serialize);
841 if (instance->poll_state == CXPOLL_STOPPING &&
842 instance->adsl_status == 1 && /* stopped */
843 instance->line_status == 0) /* down */
844 instance->poll_state = CXPOLL_STOPPED;
845
846 if (instance->poll_state == CXPOLL_STOPPED)
847 keep_polling = 0;
848 mutex_unlock(&instance->poll_state_serialize);
849
850 if (keep_polling)
851 schedule_delayed_work(&instance->poll_work,
852 round_jiffies_relative(POLL_INTERVAL*HZ));
Duncan Sands1b0e6142005-05-11 20:19:29 +0200853}
854
855static int cxacru_fw(struct usb_device *usb_dev, enum cxacru_fw_request fw,
David Woodhouse3b216d12008-05-24 00:05:28 +0100856 u8 code1, u8 code2, u32 addr, const u8 *data, int size)
Duncan Sands1b0e6142005-05-11 20:19:29 +0200857{
858 int ret;
859 u8 *buf;
860 int offd, offb;
861 const int stride = CMD_PACKET_SIZE - 8;
862
863 buf = (u8 *) __get_free_page(GFP_KERNEL);
864 if (!buf)
865 return -ENOMEM;
866
867 offb = offd = 0;
868 do {
869 int l = min_t(int, stride, size - offd);
870 buf[offb++] = fw;
871 buf[offb++] = l;
872 buf[offb++] = code1;
873 buf[offb++] = code2;
Al Virofd05e722008-04-28 07:00:16 +0100874 put_unaligned(cpu_to_le32(addr), (__le32 *)(buf + offb));
Duncan Sands1b0e6142005-05-11 20:19:29 +0200875 offb += 4;
876 addr += l;
877 if(l)
878 memcpy(buf + offb, data + offd, l);
879 if (l < stride)
880 memset(buf + offb + l, 0, stride - l);
881 offb += stride;
882 offd += stride;
883 if ((offb >= PAGE_SIZE) || (offd >= size)) {
884 ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, CXACRU_EP_CMD),
885 buf, offb, NULL, CMD_TIMEOUT);
886 if (ret < 0) {
887 dbg("sending fw %#x failed", fw);
888 goto cleanup;
889 }
890 offb = 0;
891 }
892 } while(offd < size);
893 dbg("sent fw %#x", fw);
894
895 ret = 0;
896
897cleanup:
898 free_page((unsigned long) buf);
899 return ret;
900}
901
902static void cxacru_upload_firmware(struct cxacru_data *instance,
903 const struct firmware *fw,
904 const struct firmware *bp,
905 const struct firmware *cf)
906{
907 int ret;
908 int off;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100909 struct usbatm_data *usbatm = instance->usbatm;
910 struct usb_device *usb_dev = usbatm->usb_dev;
Al Virofd05e722008-04-28 07:00:16 +0100911 __le16 signature[] = { usb_dev->descriptor.idVendor,
912 usb_dev->descriptor.idProduct };
913 __le32 val;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200914
915 dbg("cxacru_upload_firmware");
916
917 /* FirmwarePllFClkValue */
918 val = cpu_to_le32(instance->modem_type->pll_f_clk);
919 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, PLLFCLK_ADDR, (u8 *) &val, 4);
920 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100921 usb_err(usbatm, "FirmwarePllFClkValue failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200922 return;
923 }
924
925 /* FirmwarePllBClkValue */
926 val = cpu_to_le32(instance->modem_type->pll_b_clk);
927 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, PLLBCLK_ADDR, (u8 *) &val, 4);
928 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100929 usb_err(usbatm, "FirmwarePllBClkValue failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200930 return;
931 }
932
933 /* Enable SDRAM */
934 val = cpu_to_le32(SDRAM_ENA);
935 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, SDRAMEN_ADDR, (u8 *) &val, 4);
936 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100937 usb_err(usbatm, "Enable SDRAM failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200938 return;
939 }
940
941 /* Firmware */
942 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, FW_ADDR, fw->data, fw->size);
943 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100944 usb_err(usbatm, "Firmware upload failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200945 return;
946 }
947
948 /* Boot ROM patch */
949 if (instance->modem_type->boot_rom_patch) {
950 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, BR_ADDR, bp->data, bp->size);
951 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100952 usb_err(usbatm, "Boot ROM patching failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200953 return;
954 }
955 }
956
957 /* Signature */
958 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, SIG_ADDR, (u8 *) signature, 4);
959 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100960 usb_err(usbatm, "Signature storing failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200961 return;
962 }
963
964 if (instance->modem_type->boot_rom_patch) {
965 val = cpu_to_le32(BR_ADDR);
966 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, BR_STACK_ADDR, (u8 *) &val, 4);
967 }
968 else {
969 ret = cxacru_fw(usb_dev, FW_GOTO_MEM, 0x0, 0x0, FW_ADDR, NULL, 0);
970 }
971 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100972 usb_err(usbatm, "Passing control to firmware failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200973 return;
974 }
975
976 /* Delay to allow firmware to start up. */
977 msleep_interruptible(1000);
978
979 usb_clear_halt(usb_dev, usb_sndbulkpipe(usb_dev, CXACRU_EP_CMD));
980 usb_clear_halt(usb_dev, usb_rcvbulkpipe(usb_dev, CXACRU_EP_CMD));
981 usb_clear_halt(usb_dev, usb_sndbulkpipe(usb_dev, CXACRU_EP_DATA));
982 usb_clear_halt(usb_dev, usb_rcvbulkpipe(usb_dev, CXACRU_EP_DATA));
983
984 ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_STATUS, NULL, 0, NULL, 0);
985 if (ret < 0) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100986 usb_err(usbatm, "modem failed to initialize: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200987 return;
988 }
989
990 /* Load config data (le32), doing one packet at a time */
991 if (cf)
992 for (off = 0; off < cf->size / 4; ) {
Al Virofd05e722008-04-28 07:00:16 +0100993 __le32 buf[CMD_PACKET_SIZE / 4 - 1];
Duncan Sands1b0e6142005-05-11 20:19:29 +0200994 int i, len = min_t(int, cf->size / 4 - off, CMD_PACKET_SIZE / 4 / 2 - 1);
995 buf[0] = cpu_to_le32(len);
996 for (i = 0; i < len; i++, off++) {
997 buf[i * 2 + 1] = cpu_to_le32(off);
998 memcpy(buf + i * 2 + 2, cf->data + off * 4, 4);
999 }
1000 ret = cxacru_cm(instance, CM_REQUEST_CARD_DATA_SET,
1001 (u8 *) buf, len, NULL, 0);
1002 if (ret < 0) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +01001003 usb_err(usbatm, "load config data failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +02001004 return;
1005 }
1006 }
1007
1008 msleep_interruptible(4000);
1009}
1010
1011static int cxacru_find_firmware(struct cxacru_data *instance,
1012 char* phase, const struct firmware **fw_p)
1013{
Duncan Sands0ec3c7e2006-01-17 11:15:13 +01001014 struct usbatm_data *usbatm = instance->usbatm;
1015 struct device *dev = &usbatm->usb_intf->dev;
Duncan Sands1b0e6142005-05-11 20:19:29 +02001016 char buf[16];
1017
1018 sprintf(buf, "cxacru-%s.bin", phase);
1019 dbg("cxacru_find_firmware: looking for %s", buf);
1020
1021 if (request_firmware(fw_p, buf, dev)) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +01001022 usb_dbg(usbatm, "no stage %s firmware found\n", phase);
Duncan Sands1b0e6142005-05-11 20:19:29 +02001023 return -ENOENT;
1024 }
1025
Duncan Sands0ec3c7e2006-01-17 11:15:13 +01001026 usb_info(usbatm, "found firmware %s\n", buf);
Duncan Sands1b0e6142005-05-11 20:19:29 +02001027
1028 return 0;
1029}
1030
1031static int cxacru_heavy_init(struct usbatm_data *usbatm_instance,
1032 struct usb_interface *usb_intf)
1033{
Duncan Sands1b0e6142005-05-11 20:19:29 +02001034 const struct firmware *fw, *bp, *cf;
1035 struct cxacru_data *instance = usbatm_instance->driver_data;
1036
1037 int ret = cxacru_find_firmware(instance, "fw", &fw);
1038 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +01001039 usb_warn(usbatm_instance, "firmware (cxacru-fw.bin) unavailable (system misconfigured?)\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +02001040 return ret;
1041 }
1042
1043 if (instance->modem_type->boot_rom_patch) {
1044 ret = cxacru_find_firmware(instance, "bp", &bp);
1045 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +01001046 usb_warn(usbatm_instance, "boot ROM patch (cxacru-bp.bin) unavailable (system misconfigured?)\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +02001047 release_firmware(fw);
1048 return ret;
1049 }
1050 }
1051
1052 if (cxacru_find_firmware(instance, "cf", &cf)) /* optional */
1053 cf = NULL;
1054
1055 cxacru_upload_firmware(instance, fw, bp, cf);
1056
1057 if (cf)
1058 release_firmware(cf);
1059 if (instance->modem_type->boot_rom_patch)
1060 release_firmware(bp);
1061 release_firmware(fw);
1062
1063 ret = cxacru_card_status(instance);
1064 if (ret)
1065 dbg("modem initialisation failed");
1066 else
1067 dbg("done setting up the modem");
1068
1069 return ret;
1070}
1071
1072static int cxacru_bind(struct usbatm_data *usbatm_instance,
Duncan Sands35644b02006-01-17 11:16:13 +01001073 struct usb_interface *intf, const struct usb_device_id *id)
Duncan Sands1b0e6142005-05-11 20:19:29 +02001074{
1075 struct cxacru_data *instance;
1076 struct usb_device *usb_dev = interface_to_usbdev(intf);
1077 int ret;
1078
1079 /* instance init */
Duncan Sands9a734ef2006-01-13 09:38:22 +01001080 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
Duncan Sands1b0e6142005-05-11 20:19:29 +02001081 if (!instance) {
1082 dbg("cxacru_bind: no memory for instance data");
1083 return -ENOMEM;
1084 }
1085
Duncan Sands1b0e6142005-05-11 20:19:29 +02001086 instance->usbatm = usbatm_instance;
1087 instance->modem_type = (struct cxacru_modem_type *) id->driver_info;
1088
Simon Arlott6a02c9962007-04-26 00:38:05 -07001089 mutex_init(&instance->poll_state_serialize);
1090 instance->poll_state = CXPOLL_STOPPED;
1091 instance->line_status = -1;
1092 instance->adsl_status = -1;
1093
1094 mutex_init(&instance->adsl_state_serialize);
1095
Duncan Sands1b0e6142005-05-11 20:19:29 +02001096 instance->rcv_buf = (u8 *) __get_free_page(GFP_KERNEL);
1097 if (!instance->rcv_buf) {
1098 dbg("cxacru_bind: no memory for rcv_buf");
1099 ret = -ENOMEM;
1100 goto fail;
1101 }
1102 instance->snd_buf = (u8 *) __get_free_page(GFP_KERNEL);
1103 if (!instance->snd_buf) {
1104 dbg("cxacru_bind: no memory for snd_buf");
1105 ret = -ENOMEM;
1106 goto fail;
1107 }
1108 instance->rcv_urb = usb_alloc_urb(0, GFP_KERNEL);
1109 if (!instance->rcv_urb) {
1110 dbg("cxacru_bind: no memory for rcv_urb");
1111 ret = -ENOMEM;
1112 goto fail;
1113 }
1114 instance->snd_urb = usb_alloc_urb(0, GFP_KERNEL);
1115 if (!instance->snd_urb) {
1116 dbg("cxacru_bind: no memory for snd_urb");
1117 ret = -ENOMEM;
1118 goto fail;
1119 }
1120
1121 usb_fill_int_urb(instance->rcv_urb,
1122 usb_dev, usb_rcvintpipe(usb_dev, CXACRU_EP_CMD),
1123 instance->rcv_buf, PAGE_SIZE,
1124 cxacru_blocking_completion, &instance->rcv_done, 1);
Duncan Sands1b0e6142005-05-11 20:19:29 +02001125
1126 usb_fill_int_urb(instance->snd_urb,
1127 usb_dev, usb_sndintpipe(usb_dev, CXACRU_EP_CMD),
1128 instance->snd_buf, PAGE_SIZE,
1129 cxacru_blocking_completion, &instance->snd_done, 4);
Duncan Sands1b0e6142005-05-11 20:19:29 +02001130
Arjan van de Venab3c81f2006-01-13 15:52:55 +01001131 mutex_init(&instance->cm_serialize);
Duncan Sands1b0e6142005-05-11 20:19:29 +02001132
David Howellsc4028952006-11-22 14:57:56 +00001133 INIT_DELAYED_WORK(&instance->poll_work, cxacru_poll_status);
Duncan Sands1b0e6142005-05-11 20:19:29 +02001134
1135 usbatm_instance->driver_data = instance;
1136
Duncan Sands35644b02006-01-17 11:16:13 +01001137 usbatm_instance->flags = (cxacru_card_status(instance) ? 0 : UDSL_SKIP_HEAVY_INIT);
Duncan Sands1b0e6142005-05-11 20:19:29 +02001138
1139 return 0;
1140
1141 fail:
1142 free_page((unsigned long) instance->snd_buf);
1143 free_page((unsigned long) instance->rcv_buf);
1144 usb_free_urb(instance->snd_urb);
1145 usb_free_urb(instance->rcv_urb);
1146 kfree(instance);
1147
1148 return ret;
1149}
1150
1151static void cxacru_unbind(struct usbatm_data *usbatm_instance,
1152 struct usb_interface *intf)
1153{
1154 struct cxacru_data *instance = usbatm_instance->driver_data;
Simon Arlott6a02c9962007-04-26 00:38:05 -07001155 int is_polling = 1;
Duncan Sands1b0e6142005-05-11 20:19:29 +02001156
1157 dbg("cxacru_unbind entered");
1158
1159 if (!instance) {
1160 dbg("cxacru_unbind: NULL instance!");
1161 return;
1162 }
1163
Simon Arlott6a02c9962007-04-26 00:38:05 -07001164 mutex_lock(&instance->poll_state_serialize);
1165 BUG_ON(instance->poll_state == CXPOLL_SHUTDOWN);
1166
1167 /* ensure that status polling continues unless
1168 * it has already stopped */
1169 if (instance->poll_state == CXPOLL_STOPPED)
1170 is_polling = 0;
1171
1172 /* stop polling from being stopped or started */
1173 instance->poll_state = CXPOLL_SHUTDOWN;
1174 mutex_unlock(&instance->poll_state_serialize);
1175
1176 if (is_polling)
1177 cancel_rearming_delayed_work(&instance->poll_work);
Duncan Sands1b0e6142005-05-11 20:19:29 +02001178
1179 usb_kill_urb(instance->snd_urb);
1180 usb_kill_urb(instance->rcv_urb);
1181 usb_free_urb(instance->snd_urb);
1182 usb_free_urb(instance->rcv_urb);
1183
1184 free_page((unsigned long) instance->snd_buf);
1185 free_page((unsigned long) instance->rcv_buf);
Simon Arlottfa70fe42007-03-06 02:47:45 -08001186
Duncan Sands1b0e6142005-05-11 20:19:29 +02001187 kfree(instance);
1188
1189 usbatm_instance->driver_data = NULL;
1190}
1191
1192static const struct cxacru_modem_type cxacru_cafe = {
1193 .pll_f_clk = 0x02d874df,
1194 .pll_b_clk = 0x0196a51a,
1195 .boot_rom_patch = 1,
1196};
1197
1198static const struct cxacru_modem_type cxacru_cb00 = {
1199 .pll_f_clk = 0x5,
1200 .pll_b_clk = 0x3,
1201 .boot_rom_patch = 0,
1202};
1203
1204static const struct usb_device_id cxacru_usb_ids[] = {
1205 { /* V = Conexant P = ADSL modem (Euphrates project) */
1206 USB_DEVICE(0x0572, 0xcafe), .driver_info = (unsigned long) &cxacru_cafe
1207 },
1208 { /* V = Conexant P = ADSL modem (Hasbani project) */
1209 USB_DEVICE(0x0572, 0xcb00), .driver_info = (unsigned long) &cxacru_cb00
1210 },
Duncan Sands1b0e6142005-05-11 20:19:29 +02001211 { /* V = Conexant P = ADSL modem */
1212 USB_DEVICE(0x0572, 0xcb01), .driver_info = (unsigned long) &cxacru_cb00
1213 },
Duncan Sands0ec3c7e2006-01-17 11:15:13 +01001214 { /* V = Conexant P = ADSL modem (Well PTI-800) */
1215 USB_DEVICE(0x0572, 0xcb02), .driver_info = (unsigned long) &cxacru_cb00
1216 },
Duncan Sands1b0e6142005-05-11 20:19:29 +02001217 { /* V = Conexant P = ADSL modem */
1218 USB_DEVICE(0x0572, 0xcb06), .driver_info = (unsigned long) &cxacru_cb00
1219 },
Duncan Sands44960af2006-10-05 11:05:50 +02001220 { /* V = Conexant P = ADSL modem (ZTE ZXDSL 852) */
1221 USB_DEVICE(0x0572, 0xcb07), .driver_info = (unsigned long) &cxacru_cb00
1222 },
Duncan Sands1b0e6142005-05-11 20:19:29 +02001223 { /* V = Olitec P = ADSL modem version 2 */
1224 USB_DEVICE(0x08e3, 0x0100), .driver_info = (unsigned long) &cxacru_cafe
1225 },
1226 { /* V = Olitec P = ADSL modem version 3 */
1227 USB_DEVICE(0x08e3, 0x0102), .driver_info = (unsigned long) &cxacru_cb00
1228 },
1229 { /* V = Trust/Amigo Technology Co. P = AMX-CA86U */
1230 USB_DEVICE(0x0eb0, 0x3457), .driver_info = (unsigned long) &cxacru_cafe
1231 },
1232 { /* V = Zoom P = 5510 */
1233 USB_DEVICE(0x1803, 0x5510), .driver_info = (unsigned long) &cxacru_cb00
1234 },
1235 { /* V = Draytek P = Vigor 318 */
1236 USB_DEVICE(0x0675, 0x0200), .driver_info = (unsigned long) &cxacru_cb00
1237 },
1238 { /* V = Zyxel P = 630-C1 aka OMNI ADSL USB (Annex A) */
1239 USB_DEVICE(0x0586, 0x330a), .driver_info = (unsigned long) &cxacru_cb00
1240 },
1241 { /* V = Zyxel P = 630-C3 aka OMNI ADSL USB (Annex B) */
1242 USB_DEVICE(0x0586, 0x330b), .driver_info = (unsigned long) &cxacru_cb00
1243 },
1244 { /* V = Aethra P = Starmodem UM1020 */
1245 USB_DEVICE(0x0659, 0x0020), .driver_info = (unsigned long) &cxacru_cb00
1246 },
1247 { /* V = Aztech Systems P = ? AKA Pirelli AUA-010 */
1248 USB_DEVICE(0x0509, 0x0812), .driver_info = (unsigned long) &cxacru_cb00
1249 },
1250 { /* V = Netopia P = Cayman 3341(Annex A)/3351(Annex B) */
1251 USB_DEVICE(0x100d, 0xcb01), .driver_info = (unsigned long) &cxacru_cb00
1252 },
1253 { /* V = Netopia P = Cayman 3342(Annex A)/3352(Annex B) */
1254 USB_DEVICE(0x100d, 0x3342), .driver_info = (unsigned long) &cxacru_cb00
1255 },
1256 {}
1257};
1258
1259MODULE_DEVICE_TABLE(usb, cxacru_usb_ids);
1260
1261static struct usbatm_driver cxacru_driver = {
Duncan Sands1b0e6142005-05-11 20:19:29 +02001262 .driver_name = cxacru_driver_name,
1263 .bind = cxacru_bind,
1264 .heavy_init = cxacru_heavy_init,
1265 .unbind = cxacru_unbind,
1266 .atm_start = cxacru_atm_start,
Simon Arlottda1f82b52007-05-10 23:04:12 -07001267 .atm_stop = cxacru_remove_device_files,
Duncan Sands80aae7a2006-01-13 10:59:23 +01001268 .bulk_in = CXACRU_EP_DATA,
1269 .bulk_out = CXACRU_EP_DATA,
Duncan Sands1b0e6142005-05-11 20:19:29 +02001270 .rx_padding = 3,
1271 .tx_padding = 11,
1272};
1273
1274static int cxacru_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1275{
1276 return usbatm_usb_probe(intf, id, &cxacru_driver);
1277}
1278
1279static struct usb_driver cxacru_usb_driver = {
Duncan Sands1b0e6142005-05-11 20:19:29 +02001280 .name = cxacru_driver_name,
1281 .probe = cxacru_usb_probe,
1282 .disconnect = usbatm_usb_disconnect,
1283 .id_table = cxacru_usb_ids
1284};
1285
1286static int __init cxacru_init(void)
1287{
1288 return usb_register(&cxacru_usb_driver);
1289}
1290
1291static void __exit cxacru_cleanup(void)
1292{
1293 usb_deregister(&cxacru_usb_driver);
1294}
1295
1296module_init(cxacru_init);
1297module_exit(cxacru_cleanup);
1298
1299MODULE_AUTHOR(DRIVER_AUTHOR);
1300MODULE_DESCRIPTION(DRIVER_DESC);
1301MODULE_LICENSE("GPL");
1302MODULE_VERSION(DRIVER_VERSION);