blob: 4bead3da38507d0d9a2f6ae4cb802343bcd4e3cf [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
108/* reply codes to the commands above */
109enum cxacru_cm_status {
110 CM_STATUS_UNDEFINED,
111 CM_STATUS_SUCCESS,
112 CM_STATUS_ERROR,
113 CM_STATUS_UNSUPPORTED,
114 CM_STATUS_UNIMPLEMENTED,
115 CM_STATUS_PARAMETER_ERROR,
116 CM_STATUS_DBG_LOOPBACK,
117 CM_STATUS_MAX,
118};
119
120/* indices into CARD_INFO_GET return array */
121enum cxacru_info_idx {
122 CXINF_DOWNSTREAM_RATE,
123 CXINF_UPSTREAM_RATE,
124 CXINF_LINK_STATUS,
125 CXINF_LINE_STATUS,
126 CXINF_MAC_ADDRESS_HIGH,
127 CXINF_MAC_ADDRESS_LOW,
128 CXINF_UPSTREAM_SNR_MARGIN,
129 CXINF_DOWNSTREAM_SNR_MARGIN,
130 CXINF_UPSTREAM_ATTENUATION,
131 CXINF_DOWNSTREAM_ATTENUATION,
132 CXINF_TRANSMITTER_POWER,
133 CXINF_UPSTREAM_BITS_PER_FRAME,
134 CXINF_DOWNSTREAM_BITS_PER_FRAME,
135 CXINF_STARTUP_ATTEMPTS,
136 CXINF_UPSTREAM_CRC_ERRORS,
137 CXINF_DOWNSTREAM_CRC_ERRORS,
138 CXINF_UPSTREAM_FEC_ERRORS,
139 CXINF_DOWNSTREAM_FEC_ERRORS,
140 CXINF_UPSTREAM_HEC_ERRORS,
141 CXINF_DOWNSTREAM_HEC_ERRORS,
142 CXINF_LINE_STARTABLE,
143 CXINF_MODULATION,
144 CXINF_ADSL_HEADEND,
145 CXINF_ADSL_HEADEND_ENVIRONMENT,
146 CXINF_CONTROLLER_VERSION,
147 /* dunno what the missing two mean */
148 CXINF_MAX = 0x1c,
149};
150
Simon Arlott6a02c9962007-04-26 00:38:05 -0700151enum cxacru_poll_state {
152 CXPOLL_STOPPING,
153 CXPOLL_STOPPED,
154 CXPOLL_POLLING,
155 CXPOLL_SHUTDOWN
156};
157
Duncan Sands1b0e6142005-05-11 20:19:29 +0200158struct cxacru_modem_type {
159 u32 pll_f_clk;
160 u32 pll_b_clk;
161 int boot_rom_patch;
162};
163
164struct cxacru_data {
165 struct usbatm_data *usbatm;
166
167 const struct cxacru_modem_type *modem_type;
168
169 int line_status;
Simon Arlott6a02c9962007-04-26 00:38:05 -0700170 struct mutex adsl_state_serialize;
171 int adsl_status;
David Howellsc4028952006-11-22 14:57:56 +0000172 struct delayed_work poll_work;
Simon Arlottfa70fe42007-03-06 02:47:45 -0800173 u32 card_info[CXINF_MAX];
Simon Arlott6a02c9962007-04-26 00:38:05 -0700174 struct mutex poll_state_serialize;
Simon Arlott87e71b42007-05-10 23:04:11 -0700175 enum cxacru_poll_state poll_state;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200176
177 /* contol handles */
Arjan van de Venab3c81f2006-01-13 15:52:55 +0100178 struct mutex cm_serialize;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200179 u8 *rcv_buf;
180 u8 *snd_buf;
181 struct urb *rcv_urb;
182 struct urb *snd_urb;
183 struct completion rcv_done;
184 struct completion snd_done;
185};
186
Simon Arlott6a02c9962007-04-26 00:38:05 -0700187static int cxacru_cm(struct cxacru_data *instance, enum cxacru_cm_request cm,
188 u8 *wdata, int wsize, u8 *rdata, int rsize);
189static void cxacru_poll_status(struct work_struct *work);
190
Simon Arlottfa70fe42007-03-06 02:47:45 -0800191/* Card info exported through sysfs */
192#define CXACRU__ATTR_INIT(_name) \
193static DEVICE_ATTR(_name, S_IRUGO, cxacru_sysfs_show_##_name, NULL)
194
Simon Arlott6a02c9962007-04-26 00:38:05 -0700195#define CXACRU_CMD_INIT(_name) \
196static DEVICE_ATTR(_name, S_IWUSR | S_IRUGO, \
197 cxacru_sysfs_show_##_name, cxacru_sysfs_store_##_name)
198
Simon Arlottfa70fe42007-03-06 02:47:45 -0800199#define CXACRU_ATTR_INIT(_value, _type, _name) \
200static ssize_t cxacru_sysfs_show_##_name(struct device *dev, \
201 struct device_attribute *attr, char *buf) \
202{ \
Simon Arlott9fc950d2009-11-21 15:33:51 +0000203 struct cxacru_data *instance = to_usbatm_driver_data(\
204 to_usb_interface(dev)); \
205\
206 if (instance == NULL) \
207 return -ENODEV; \
208\
Simon Arlottfa70fe42007-03-06 02:47:45 -0800209 return cxacru_sysfs_showattr_##_type(instance->card_info[_value], buf); \
210} \
211CXACRU__ATTR_INIT(_name)
212
213#define CXACRU_ATTR_CREATE(_v, _t, _name) CXACRU_DEVICE_CREATE_FILE(_name)
Simon Arlott6a02c9962007-04-26 00:38:05 -0700214#define CXACRU_CMD_CREATE(_name) CXACRU_DEVICE_CREATE_FILE(_name)
Simon Arlottfa70fe42007-03-06 02:47:45 -0800215#define CXACRU__ATTR_CREATE(_name) CXACRU_DEVICE_CREATE_FILE(_name)
216
217#define CXACRU_ATTR_REMOVE(_v, _t, _name) CXACRU_DEVICE_REMOVE_FILE(_name)
Simon Arlott6a02c9962007-04-26 00:38:05 -0700218#define CXACRU_CMD_REMOVE(_name) CXACRU_DEVICE_REMOVE_FILE(_name)
Simon Arlottfa70fe42007-03-06 02:47:45 -0800219#define CXACRU__ATTR_REMOVE(_name) CXACRU_DEVICE_REMOVE_FILE(_name)
220
221static ssize_t cxacru_sysfs_showattr_u32(u32 value, char *buf)
222{
223 return snprintf(buf, PAGE_SIZE, "%u\n", value);
224}
225
226static ssize_t cxacru_sysfs_showattr_s8(s8 value, char *buf)
227{
228 return snprintf(buf, PAGE_SIZE, "%d\n", value);
229}
230
231static ssize_t cxacru_sysfs_showattr_dB(s16 value, char *buf)
232{
Simon Arlott10107bd2009-04-23 18:19:02 +0100233 if (likely(value >= 0)) {
234 return snprintf(buf, PAGE_SIZE, "%u.%02u\n",
235 value / 100, value % 100);
236 } else {
237 value = -value;
238 return snprintf(buf, PAGE_SIZE, "-%u.%02u\n",
239 value / 100, value % 100);
240 }
Simon Arlottfa70fe42007-03-06 02:47:45 -0800241}
242
243static ssize_t cxacru_sysfs_showattr_bool(u32 value, char *buf)
244{
Simon Arlott87e71b42007-05-10 23:04:11 -0700245 static char *str[] = { "no", "yes" };
246 if (unlikely(value >= ARRAY_SIZE(str)))
247 return snprintf(buf, PAGE_SIZE, "%u\n", value);
248 return snprintf(buf, PAGE_SIZE, "%s\n", str[value]);
Simon Arlottfa70fe42007-03-06 02:47:45 -0800249}
250
251static ssize_t cxacru_sysfs_showattr_LINK(u32 value, char *buf)
252{
Simon Arlott87e71b42007-05-10 23:04:11 -0700253 static char *str[] = { NULL, "not connected", "connected", "lost" };
254 if (unlikely(value >= ARRAY_SIZE(str) || str[value] == NULL))
255 return snprintf(buf, PAGE_SIZE, "%u\n", value);
256 return snprintf(buf, PAGE_SIZE, "%s\n", str[value]);
Simon Arlottfa70fe42007-03-06 02:47:45 -0800257}
258
259static ssize_t cxacru_sysfs_showattr_LINE(u32 value, char *buf)
260{
Simon Arlott87e71b42007-05-10 23:04:11 -0700261 static char *str[] = { "down", "attempting to activate",
262 "training", "channel analysis", "exchange", "up",
263 "waiting", "initialising"
264 };
265 if (unlikely(value >= ARRAY_SIZE(str)))
266 return snprintf(buf, PAGE_SIZE, "%u\n", value);
267 return snprintf(buf, PAGE_SIZE, "%s\n", str[value]);
Simon Arlottfa70fe42007-03-06 02:47:45 -0800268}
269
270static ssize_t cxacru_sysfs_showattr_MODU(u32 value, char *buf)
271{
Simon Arlott87e71b42007-05-10 23:04:11 -0700272 static char *str[] = {
Simon Arlott1bfbd282009-11-21 15:03:23 +0000273 "",
Simon Arlott87e71b42007-05-10 23:04:11 -0700274 "ANSI T1.413",
275 "ITU-T G.992.1 (G.DMT)",
276 "ITU-T G.992.2 (G.LITE)"
277 };
Simon Arlott1bfbd282009-11-21 15:03:23 +0000278 if (unlikely(value >= ARRAY_SIZE(str)))
Simon Arlott87e71b42007-05-10 23:04:11 -0700279 return snprintf(buf, PAGE_SIZE, "%u\n", value);
280 return snprintf(buf, PAGE_SIZE, "%s\n", str[value]);
Simon Arlottfa70fe42007-03-06 02:47:45 -0800281}
282
283/*
284 * This could use MAC_ADDRESS_HIGH and MAC_ADDRESS_LOW, but since
285 * this data is already in atm_dev there's no point.
286 *
287 * MAC_ADDRESS_HIGH = 0x????5544
288 * MAC_ADDRESS_LOW = 0x33221100
289 * Where 00-55 are bytes 0-5 of the MAC.
290 */
291static ssize_t cxacru_sysfs_show_mac_address(struct device *dev,
292 struct device_attribute *attr, char *buf)
293{
Simon Arlott9fc950d2009-11-21 15:33:51 +0000294 struct cxacru_data *instance = to_usbatm_driver_data(
295 to_usb_interface(dev));
Simon Arlottfa70fe42007-03-06 02:47:45 -0800296
Simon Arlott9fc950d2009-11-21 15:33:51 +0000297 if (instance == NULL || instance->usbatm->atm_dev == NULL)
298 return -ENODEV;
299
300 return snprintf(buf, PAGE_SIZE, "%pM\n",
301 instance->usbatm->atm_dev->esi);
Simon Arlottfa70fe42007-03-06 02:47:45 -0800302}
303
Simon Arlott6a02c9962007-04-26 00:38:05 -0700304static ssize_t cxacru_sysfs_show_adsl_state(struct device *dev,
305 struct device_attribute *attr, char *buf)
306{
Simon Arlott87e71b42007-05-10 23:04:11 -0700307 static char *str[] = { "running", "stopped" };
Simon Arlott9fc950d2009-11-21 15:33:51 +0000308 struct cxacru_data *instance = to_usbatm_driver_data(
309 to_usb_interface(dev));
310 u32 value;
311
312 if (instance == NULL)
313 return -ENODEV;
314
315 value = instance->card_info[CXINF_LINE_STARTABLE];
Simon Arlott87e71b42007-05-10 23:04:11 -0700316 if (unlikely(value >= ARRAY_SIZE(str)))
317 return snprintf(buf, PAGE_SIZE, "%u\n", value);
318 return snprintf(buf, PAGE_SIZE, "%s\n", str[value]);
Simon Arlott6a02c9962007-04-26 00:38:05 -0700319}
320
321static ssize_t cxacru_sysfs_store_adsl_state(struct device *dev,
322 struct device_attribute *attr, const char *buf, size_t count)
323{
Simon Arlott9fc950d2009-11-21 15:33:51 +0000324 struct cxacru_data *instance = to_usbatm_driver_data(
325 to_usb_interface(dev));
Simon Arlott6a02c9962007-04-26 00:38:05 -0700326 int ret;
327 int poll = -1;
328 char str_cmd[8];
329 int len = strlen(buf);
330
331 if (!capable(CAP_NET_ADMIN))
332 return -EACCES;
333
334 ret = sscanf(buf, "%7s", str_cmd);
335 if (ret != 1)
336 return -EINVAL;
337 ret = 0;
338
Simon Arlott9fc950d2009-11-21 15:33:51 +0000339 if (instance == NULL)
340 return -ENODEV;
341
Simon Arlott6a02c9962007-04-26 00:38:05 -0700342 if (mutex_lock_interruptible(&instance->adsl_state_serialize))
343 return -ERESTARTSYS;
344
345 if (!strcmp(str_cmd, "stop") || !strcmp(str_cmd, "restart")) {
346 ret = cxacru_cm(instance, CM_REQUEST_CHIP_ADSL_LINE_STOP, NULL, 0, NULL, 0);
347 if (ret < 0) {
Simon Arlott9fc950d2009-11-21 15:33:51 +0000348 atm_err(instance->usbatm, "change adsl state:"
Simon Arlott6a02c9962007-04-26 00:38:05 -0700349 " CHIP_ADSL_LINE_STOP returned %d\n", ret);
350
351 ret = -EIO;
352 } else {
353 ret = len;
354 poll = CXPOLL_STOPPED;
355 }
356 }
357
358 /* Line status is only updated every second
359 * and the device appears to only react to
360 * START/STOP every second too. Wait 1.5s to
361 * be sure that restart will have an effect. */
362 if (!strcmp(str_cmd, "restart"))
363 msleep(1500);
364
365 if (!strcmp(str_cmd, "start") || !strcmp(str_cmd, "restart")) {
366 ret = cxacru_cm(instance, CM_REQUEST_CHIP_ADSL_LINE_START, 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_START returned %d\n", ret);
370
371 ret = -EIO;
372 } else {
373 ret = len;
374 poll = CXPOLL_POLLING;
375 }
376 }
377
378 if (!strcmp(str_cmd, "poll")) {
379 ret = len;
380 poll = CXPOLL_POLLING;
381 }
382
383 if (ret == 0) {
384 ret = -EINVAL;
385 poll = -1;
386 }
387
388 if (poll == CXPOLL_POLLING) {
389 mutex_lock(&instance->poll_state_serialize);
390 switch (instance->poll_state) {
391 case CXPOLL_STOPPED:
392 /* start polling */
393 instance->poll_state = CXPOLL_POLLING;
394 break;
395
396 case CXPOLL_STOPPING:
397 /* abort stop request */
398 instance->poll_state = CXPOLL_POLLING;
399 case CXPOLL_POLLING:
400 case CXPOLL_SHUTDOWN:
401 /* don't start polling */
402 poll = -1;
403 }
404 mutex_unlock(&instance->poll_state_serialize);
405 } else if (poll == CXPOLL_STOPPED) {
406 mutex_lock(&instance->poll_state_serialize);
407 /* request stop */
408 if (instance->poll_state == CXPOLL_POLLING)
409 instance->poll_state = CXPOLL_STOPPING;
410 mutex_unlock(&instance->poll_state_serialize);
411 }
412
413 mutex_unlock(&instance->adsl_state_serialize);
414
415 if (poll == CXPOLL_POLLING)
416 cxacru_poll_status(&instance->poll_work.work);
417
418 return ret;
419}
420
Simon Arlottfa70fe42007-03-06 02:47:45 -0800421/*
422 * All device attributes are included in CXACRU_ALL_FILES
423 * so that the same list can be used multiple times:
424 * INIT (define the device attributes)
425 * CREATE (create all the device files)
426 * REMOVE (remove all the device files)
427 *
428 * With the last two being defined as needed in the functions
429 * they are used in before calling CXACRU_ALL_FILES()
430 */
431#define CXACRU_ALL_FILES(_action) \
432CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_RATE, u32, downstream_rate); \
433CXACRU_ATTR_##_action(CXINF_UPSTREAM_RATE, u32, upstream_rate); \
434CXACRU_ATTR_##_action(CXINF_LINK_STATUS, LINK, link_status); \
435CXACRU_ATTR_##_action(CXINF_LINE_STATUS, LINE, line_status); \
436CXACRU__ATTR_##_action( mac_address); \
437CXACRU_ATTR_##_action(CXINF_UPSTREAM_SNR_MARGIN, dB, upstream_snr_margin); \
438CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_SNR_MARGIN, dB, downstream_snr_margin); \
439CXACRU_ATTR_##_action(CXINF_UPSTREAM_ATTENUATION, dB, upstream_attenuation); \
440CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_ATTENUATION, dB, downstream_attenuation); \
441CXACRU_ATTR_##_action(CXINF_TRANSMITTER_POWER, s8, transmitter_power); \
442CXACRU_ATTR_##_action(CXINF_UPSTREAM_BITS_PER_FRAME, u32, upstream_bits_per_frame); \
443CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_BITS_PER_FRAME, u32, downstream_bits_per_frame); \
444CXACRU_ATTR_##_action(CXINF_STARTUP_ATTEMPTS, u32, startup_attempts); \
445CXACRU_ATTR_##_action(CXINF_UPSTREAM_CRC_ERRORS, u32, upstream_crc_errors); \
446CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_CRC_ERRORS, u32, downstream_crc_errors); \
447CXACRU_ATTR_##_action(CXINF_UPSTREAM_FEC_ERRORS, u32, upstream_fec_errors); \
448CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_FEC_ERRORS, u32, downstream_fec_errors); \
449CXACRU_ATTR_##_action(CXINF_UPSTREAM_HEC_ERRORS, u32, upstream_hec_errors); \
450CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_HEC_ERRORS, u32, downstream_hec_errors); \
451CXACRU_ATTR_##_action(CXINF_LINE_STARTABLE, bool, line_startable); \
452CXACRU_ATTR_##_action(CXINF_MODULATION, MODU, modulation); \
453CXACRU_ATTR_##_action(CXINF_ADSL_HEADEND, u32, adsl_headend); \
454CXACRU_ATTR_##_action(CXINF_ADSL_HEADEND_ENVIRONMENT, u32, adsl_headend_environment); \
Simon Arlott6a02c9962007-04-26 00:38:05 -0700455CXACRU_ATTR_##_action(CXINF_CONTROLLER_VERSION, u32, adsl_controller_version); \
456CXACRU_CMD_##_action( adsl_state);
Simon Arlottfa70fe42007-03-06 02:47:45 -0800457
458CXACRU_ALL_FILES(INIT);
459
Duncan Sands1b0e6142005-05-11 20:19:29 +0200460/* the following three functions are stolen from drivers/usb/core/message.c */
David Howells7d12e782006-10-05 14:55:46 +0100461static void cxacru_blocking_completion(struct urb *urb)
Duncan Sands1b0e6142005-05-11 20:19:29 +0200462{
Ming Leicdc97792008-02-24 18:41:47 +0800463 complete(urb->context);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200464}
465
466static void cxacru_timeout_kill(unsigned long data)
467{
468 usb_unlink_urb((struct urb *) data);
469}
470
471static int cxacru_start_wait_urb(struct urb *urb, struct completion *done,
472 int* actual_length)
473{
474 struct timer_list timer;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200475
476 init_timer(&timer);
477 timer.expires = jiffies + msecs_to_jiffies(CMD_TIMEOUT);
478 timer.data = (unsigned long) urb;
479 timer.function = cxacru_timeout_kill;
480 add_timer(&timer);
481 wait_for_completion(done);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200482 del_timer_sync(&timer);
483
484 if (actual_length)
485 *actual_length = urb->actual_length;
Oliver Neukum3b79cc22007-08-16 16:06:06 +0200486 return urb->status; /* must read status after completion */
Duncan Sands1b0e6142005-05-11 20:19:29 +0200487}
488
489static int cxacru_cm(struct cxacru_data *instance, enum cxacru_cm_request cm,
490 u8 *wdata, int wsize, u8 *rdata, int rsize)
491{
492 int ret, actlen;
493 int offb, offd;
494 const int stride = CMD_PACKET_SIZE - 4;
495 u8 *wbuf = instance->snd_buf;
496 u8 *rbuf = instance->rcv_buf;
497 int wbuflen = ((wsize - 1) / stride + 1) * CMD_PACKET_SIZE;
498 int rbuflen = ((rsize - 1) / stride + 1) * CMD_PACKET_SIZE;
499
500 if (wbuflen > PAGE_SIZE || rbuflen > PAGE_SIZE) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100501 if (printk_ratelimit())
502 usb_err(instance->usbatm, "requested transfer size too large (%d, %d)\n",
503 wbuflen, rbuflen);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200504 ret = -ENOMEM;
Jiri Slabyeeafa642009-03-11 21:47:36 +0100505 goto err;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200506 }
507
Arjan van de Venab3c81f2006-01-13 15:52:55 +0100508 mutex_lock(&instance->cm_serialize);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200509
510 /* submit reading urb before the writing one */
511 init_completion(&instance->rcv_done);
512 ret = usb_submit_urb(instance->rcv_urb, GFP_KERNEL);
513 if (ret < 0) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100514 if (printk_ratelimit())
515 usb_err(instance->usbatm, "submit of read urb for cm %#x failed (%d)\n",
516 cm, ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200517 goto fail;
518 }
519
520 memset(wbuf, 0, wbuflen);
521 /* handle wsize == 0 */
522 wbuf[0] = cm;
523 for (offb = offd = 0; offd < wsize; offd += stride, offb += CMD_PACKET_SIZE) {
524 wbuf[offb] = cm;
525 memcpy(wbuf + offb + 4, wdata + offd, min_t(int, stride, wsize - offd));
526 }
527
528 instance->snd_urb->transfer_buffer_length = wbuflen;
529 init_completion(&instance->snd_done);
530 ret = usb_submit_urb(instance->snd_urb, GFP_KERNEL);
531 if (ret < 0) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100532 if (printk_ratelimit())
533 usb_err(instance->usbatm, "submit of write urb for cm %#x failed (%d)\n",
534 cm, ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200535 goto fail;
536 }
537
538 ret = cxacru_start_wait_urb(instance->snd_urb, &instance->snd_done, NULL);
539 if (ret < 0) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100540 if (printk_ratelimit())
541 usb_err(instance->usbatm, "send of cm %#x failed (%d)\n", cm, ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200542 goto fail;
543 }
544
545 ret = cxacru_start_wait_urb(instance->rcv_urb, &instance->rcv_done, &actlen);
546 if (ret < 0) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100547 if (printk_ratelimit())
548 usb_err(instance->usbatm, "receive of cm %#x failed (%d)\n", cm, ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200549 goto fail;
550 }
551 if (actlen % CMD_PACKET_SIZE || !actlen) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100552 if (printk_ratelimit())
553 usb_err(instance->usbatm, "invalid response length to cm %#x: %d\n",
554 cm, actlen);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200555 ret = -EIO;
556 goto fail;
557 }
558
559 /* check the return status and copy the data to the output buffer, if needed */
560 for (offb = offd = 0; offd < rsize && offb < actlen; offb += CMD_PACKET_SIZE) {
561 if (rbuf[offb] != cm) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100562 if (printk_ratelimit())
563 usb_err(instance->usbatm, "wrong cm %#x in response to cm %#x\n",
564 rbuf[offb], cm);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200565 ret = -EIO;
566 goto fail;
567 }
568 if (rbuf[offb + 1] != CM_STATUS_SUCCESS) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100569 if (printk_ratelimit())
570 usb_err(instance->usbatm, "response to cm %#x failed: %#x\n",
571 cm, rbuf[offb + 1]);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200572 ret = -EIO;
573 goto fail;
574 }
575 if (offd >= rsize)
576 break;
577 memcpy(rdata + offd, rbuf + offb + 4, min_t(int, stride, rsize - offd));
578 offd += stride;
579 }
580
581 ret = offd;
582 dbg("cm %#x", cm);
583fail:
Arjan van de Venab3c81f2006-01-13 15:52:55 +0100584 mutex_unlock(&instance->cm_serialize);
Jiri Slabyeeafa642009-03-11 21:47:36 +0100585err:
Duncan Sands1b0e6142005-05-11 20:19:29 +0200586 return ret;
587}
588
589static int cxacru_cm_get_array(struct cxacru_data *instance, enum cxacru_cm_request cm,
590 u32 *data, int size)
591{
592 int ret, len;
Al Virofd05e722008-04-28 07:00:16 +0100593 __le32 *buf;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200594 int offb, offd;
595 const int stride = CMD_PACKET_SIZE / (4 * 2) - 1;
596 int buflen = ((size - 1) / stride + 1 + size * 2) * 4;
597
598 buf = kmalloc(buflen, GFP_KERNEL);
599 if (!buf)
600 return -ENOMEM;
601
602 ret = cxacru_cm(instance, cm, NULL, 0, (u8 *) buf, buflen);
603 if (ret < 0)
604 goto cleanup;
605
606 /* len > 0 && len % 4 == 0 guaranteed by cxacru_cm() */
607 len = ret / 4;
608 for (offb = 0; offb < len; ) {
609 int l = le32_to_cpu(buf[offb++]);
Simon Arlott5d0a9c72009-11-21 15:07:14 +0000610 if (l < 0 || l > stride || l > (len - offb) / 2) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100611 if (printk_ratelimit())
612 usb_err(instance->usbatm, "invalid data length from cm %#x: %d\n",
613 cm, l);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200614 ret = -EIO;
615 goto cleanup;
616 }
617 while (l--) {
618 offd = le32_to_cpu(buf[offb++]);
619 if (offd >= size) {
Simon Arlott4ac07182007-09-25 20:20:10 +0100620 if (printk_ratelimit())
Simon Arlott230ffc72008-07-12 22:19:48 +0100621 usb_err(instance->usbatm, "wrong index %#x in response to cm %#x\n",
Simon Arlott4ac07182007-09-25 20:20:10 +0100622 offd, cm);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200623 ret = -EIO;
624 goto cleanup;
625 }
626 data[offd] = le32_to_cpu(buf[offb++]);
627 }
628 }
629
630 ret = 0;
631
632cleanup:
633 kfree(buf);
634 return ret;
635}
636
637static int cxacru_card_status(struct cxacru_data *instance)
638{
639 int ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_STATUS, NULL, 0, NULL, 0);
640 if (ret < 0) { /* firmware not loaded */
641 dbg("cxacru_adsl_start: CARD_GET_STATUS returned %d", ret);
642 return ret;
643 }
644 return 0;
645}
646
Simon Arlottda1f82b52007-05-10 23:04:12 -0700647static void cxacru_remove_device_files(struct usbatm_data *usbatm_instance,
648 struct atm_dev *atm_dev)
649{
650 struct usb_interface *intf = usbatm_instance->usb_intf;
651
652 #define CXACRU_DEVICE_REMOVE_FILE(_name) \
653 device_remove_file(&intf->dev, &dev_attr_##_name);
654 CXACRU_ALL_FILES(REMOVE);
655 #undef CXACRU_DEVICE_REMOVE_FILE
656}
657
Duncan Sands1b0e6142005-05-11 20:19:29 +0200658static int cxacru_atm_start(struct usbatm_data *usbatm_instance,
659 struct atm_dev *atm_dev)
660{
661 struct cxacru_data *instance = usbatm_instance->driver_data;
Simon Arlottda1f82b52007-05-10 23:04:12 -0700662 struct usb_interface *intf = usbatm_instance->usb_intf;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200663 int ret;
Simon Arlott6a02c9962007-04-26 00:38:05 -0700664 int start_polling = 1;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200665
666 dbg("cxacru_atm_start");
667
668 /* Read MAC address */
669 ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_MAC_ADDRESS, NULL, 0,
670 atm_dev->esi, sizeof(atm_dev->esi));
671 if (ret < 0) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100672 atm_err(usbatm_instance, "cxacru_atm_start: CARD_GET_MAC_ADDRESS returned %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200673 return ret;
674 }
675
Simon Arlottda1f82b52007-05-10 23:04:12 -0700676 #define CXACRU_DEVICE_CREATE_FILE(_name) \
677 ret = device_create_file(&intf->dev, &dev_attr_##_name); \
678 if (unlikely(ret)) \
679 goto fail_sysfs;
680 CXACRU_ALL_FILES(CREATE);
681 #undef CXACRU_DEVICE_CREATE_FILE
682
Duncan Sands1b0e6142005-05-11 20:19:29 +0200683 /* start ADSL */
Simon Arlott6a02c9962007-04-26 00:38:05 -0700684 mutex_lock(&instance->adsl_state_serialize);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200685 ret = cxacru_cm(instance, CM_REQUEST_CHIP_ADSL_LINE_START, NULL, 0, NULL, 0);
Simon Arlottfd209e32007-05-10 23:04:13 -0700686 if (ret < 0)
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100687 atm_err(usbatm_instance, "cxacru_atm_start: CHIP_ADSL_LINE_START returned %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200688
689 /* Start status polling */
Simon Arlott6a02c9962007-04-26 00:38:05 -0700690 mutex_lock(&instance->poll_state_serialize);
691 switch (instance->poll_state) {
692 case CXPOLL_STOPPED:
693 /* start polling */
694 instance->poll_state = CXPOLL_POLLING;
695 break;
696
697 case CXPOLL_STOPPING:
698 /* abort stop request */
699 instance->poll_state = CXPOLL_POLLING;
700 case CXPOLL_POLLING:
701 case CXPOLL_SHUTDOWN:
702 /* don't start polling */
703 start_polling = 0;
704 }
705 mutex_unlock(&instance->poll_state_serialize);
706 mutex_unlock(&instance->adsl_state_serialize);
707
708 if (start_polling)
709 cxacru_poll_status(&instance->poll_work.work);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200710 return 0;
Simon Arlottda1f82b52007-05-10 23:04:12 -0700711
712fail_sysfs:
713 usb_err(usbatm_instance, "cxacru_atm_start: device_create_file failed (%d)\n", ret);
714 cxacru_remove_device_files(usbatm_instance, atm_dev);
715 return ret;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200716}
717
David Howellsc4028952006-11-22 14:57:56 +0000718static void cxacru_poll_status(struct work_struct *work)
Duncan Sands1b0e6142005-05-11 20:19:29 +0200719{
David Howellsc4028952006-11-22 14:57:56 +0000720 struct cxacru_data *instance =
721 container_of(work, struct cxacru_data, poll_work.work);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200722 u32 buf[CXINF_MAX] = {};
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100723 struct usbatm_data *usbatm = instance->usbatm;
724 struct atm_dev *atm_dev = usbatm->atm_dev;
Simon Arlott6a02c9962007-04-26 00:38:05 -0700725 int keep_polling = 1;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200726 int ret;
727
728 ret = cxacru_cm_get_array(instance, CM_REQUEST_CARD_INFO_GET, buf, CXINF_MAX);
729 if (ret < 0) {
Simon Arlott6a02c9962007-04-26 00:38:05 -0700730 if (ret != -ESHUTDOWN)
731 atm_warn(usbatm, "poll status: error %d\n", ret);
732
733 mutex_lock(&instance->poll_state_serialize);
734 if (instance->poll_state != CXPOLL_SHUTDOWN) {
735 instance->poll_state = CXPOLL_STOPPED;
736
737 if (ret != -ESHUTDOWN)
738 atm_warn(usbatm, "polling disabled, set adsl_state"
739 " to 'start' or 'poll' to resume\n");
740 }
741 mutex_unlock(&instance->poll_state_serialize);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200742 goto reschedule;
743 }
744
Simon Arlottfa70fe42007-03-06 02:47:45 -0800745 memcpy(instance->card_info, buf, sizeof(instance->card_info));
746
Simon Arlott6a02c9962007-04-26 00:38:05 -0700747 if (instance->adsl_status != buf[CXINF_LINE_STARTABLE]) {
748 instance->adsl_status = buf[CXINF_LINE_STARTABLE];
749
750 switch (instance->adsl_status) {
751 case 0:
752 atm_printk(KERN_INFO, usbatm, "ADSL state: running\n");
753 break;
754
755 case 1:
756 atm_printk(KERN_INFO, usbatm, "ADSL state: stopped\n");
757 break;
758
759 default:
760 atm_printk(KERN_INFO, usbatm, "Unknown adsl status %02x\n", instance->adsl_status);
761 break;
762 }
763 }
764
Duncan Sands1b0e6142005-05-11 20:19:29 +0200765 if (instance->line_status == buf[CXINF_LINE_STATUS])
766 goto reschedule;
767
768 instance->line_status = buf[CXINF_LINE_STATUS];
769 switch (instance->line_status) {
770 case 0:
771 atm_dev->signal = ATM_PHY_SIG_LOST;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100772 atm_info(usbatm, "ADSL line: down\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200773 break;
774
775 case 1:
776 atm_dev->signal = ATM_PHY_SIG_LOST;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100777 atm_info(usbatm, "ADSL line: attempting to activate\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200778 break;
779
780 case 2:
781 atm_dev->signal = ATM_PHY_SIG_LOST;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100782 atm_info(usbatm, "ADSL line: training\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200783 break;
784
785 case 3:
786 atm_dev->signal = ATM_PHY_SIG_LOST;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100787 atm_info(usbatm, "ADSL line: channel analysis\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200788 break;
789
790 case 4:
791 atm_dev->signal = ATM_PHY_SIG_LOST;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100792 atm_info(usbatm, "ADSL line: exchange\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200793 break;
794
795 case 5:
796 atm_dev->link_rate = buf[CXINF_DOWNSTREAM_RATE] * 1000 / 424;
797 atm_dev->signal = ATM_PHY_SIG_FOUND;
798
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100799 atm_info(usbatm, "ADSL line: up (%d kb/s down | %d kb/s up)\n",
Duncan Sands1b0e6142005-05-11 20:19:29 +0200800 buf[CXINF_DOWNSTREAM_RATE], buf[CXINF_UPSTREAM_RATE]);
801 break;
802
803 case 6:
804 atm_dev->signal = ATM_PHY_SIG_LOST;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100805 atm_info(usbatm, "ADSL line: waiting\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200806 break;
807
808 case 7:
809 atm_dev->signal = ATM_PHY_SIG_LOST;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100810 atm_info(usbatm, "ADSL line: initializing\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +0200811 break;
812
813 default:
814 atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100815 atm_info(usbatm, "Unknown line state %02x\n", instance->line_status);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200816 break;
817 }
818reschedule:
Simon Arlott6a02c9962007-04-26 00:38:05 -0700819
820 mutex_lock(&instance->poll_state_serialize);
821 if (instance->poll_state == CXPOLL_STOPPING &&
822 instance->adsl_status == 1 && /* stopped */
823 instance->line_status == 0) /* down */
824 instance->poll_state = CXPOLL_STOPPED;
825
826 if (instance->poll_state == CXPOLL_STOPPED)
827 keep_polling = 0;
828 mutex_unlock(&instance->poll_state_serialize);
829
830 if (keep_polling)
831 schedule_delayed_work(&instance->poll_work,
832 round_jiffies_relative(POLL_INTERVAL*HZ));
Duncan Sands1b0e6142005-05-11 20:19:29 +0200833}
834
835static int cxacru_fw(struct usb_device *usb_dev, enum cxacru_fw_request fw,
David Woodhouse3b216d12008-05-24 00:05:28 +0100836 u8 code1, u8 code2, u32 addr, const u8 *data, int size)
Duncan Sands1b0e6142005-05-11 20:19:29 +0200837{
838 int ret;
839 u8 *buf;
840 int offd, offb;
841 const int stride = CMD_PACKET_SIZE - 8;
842
843 buf = (u8 *) __get_free_page(GFP_KERNEL);
844 if (!buf)
845 return -ENOMEM;
846
847 offb = offd = 0;
848 do {
849 int l = min_t(int, stride, size - offd);
850 buf[offb++] = fw;
851 buf[offb++] = l;
852 buf[offb++] = code1;
853 buf[offb++] = code2;
Al Virofd05e722008-04-28 07:00:16 +0100854 put_unaligned(cpu_to_le32(addr), (__le32 *)(buf + offb));
Duncan Sands1b0e6142005-05-11 20:19:29 +0200855 offb += 4;
856 addr += l;
857 if(l)
858 memcpy(buf + offb, data + offd, l);
859 if (l < stride)
860 memset(buf + offb + l, 0, stride - l);
861 offb += stride;
862 offd += stride;
863 if ((offb >= PAGE_SIZE) || (offd >= size)) {
864 ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, CXACRU_EP_CMD),
865 buf, offb, NULL, CMD_TIMEOUT);
866 if (ret < 0) {
867 dbg("sending fw %#x failed", fw);
868 goto cleanup;
869 }
870 offb = 0;
871 }
872 } while(offd < size);
873 dbg("sent fw %#x", fw);
874
875 ret = 0;
876
877cleanup:
878 free_page((unsigned long) buf);
879 return ret;
880}
881
882static void cxacru_upload_firmware(struct cxacru_data *instance,
883 const struct firmware *fw,
884 const struct firmware *bp,
885 const struct firmware *cf)
886{
887 int ret;
888 int off;
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100889 struct usbatm_data *usbatm = instance->usbatm;
890 struct usb_device *usb_dev = usbatm->usb_dev;
Al Virofd05e722008-04-28 07:00:16 +0100891 __le16 signature[] = { usb_dev->descriptor.idVendor,
892 usb_dev->descriptor.idProduct };
893 __le32 val;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200894
895 dbg("cxacru_upload_firmware");
896
897 /* FirmwarePllFClkValue */
898 val = cpu_to_le32(instance->modem_type->pll_f_clk);
899 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, PLLFCLK_ADDR, (u8 *) &val, 4);
900 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100901 usb_err(usbatm, "FirmwarePllFClkValue failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200902 return;
903 }
904
905 /* FirmwarePllBClkValue */
906 val = cpu_to_le32(instance->modem_type->pll_b_clk);
907 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, PLLBCLK_ADDR, (u8 *) &val, 4);
908 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100909 usb_err(usbatm, "FirmwarePllBClkValue failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200910 return;
911 }
912
913 /* Enable SDRAM */
914 val = cpu_to_le32(SDRAM_ENA);
915 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, SDRAMEN_ADDR, (u8 *) &val, 4);
916 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100917 usb_err(usbatm, "Enable SDRAM failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200918 return;
919 }
920
921 /* Firmware */
922 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, FW_ADDR, fw->data, fw->size);
923 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100924 usb_err(usbatm, "Firmware upload failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200925 return;
926 }
927
928 /* Boot ROM patch */
929 if (instance->modem_type->boot_rom_patch) {
930 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, BR_ADDR, bp->data, bp->size);
931 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100932 usb_err(usbatm, "Boot ROM patching failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200933 return;
934 }
935 }
936
937 /* Signature */
938 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, SIG_ADDR, (u8 *) signature, 4);
939 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100940 usb_err(usbatm, "Signature storing failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200941 return;
942 }
943
944 if (instance->modem_type->boot_rom_patch) {
945 val = cpu_to_le32(BR_ADDR);
946 ret = cxacru_fw(usb_dev, FW_WRITE_MEM, 0x2, 0x0, BR_STACK_ADDR, (u8 *) &val, 4);
947 }
948 else {
949 ret = cxacru_fw(usb_dev, FW_GOTO_MEM, 0x0, 0x0, FW_ADDR, NULL, 0);
950 }
951 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100952 usb_err(usbatm, "Passing control to firmware failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200953 return;
954 }
955
956 /* Delay to allow firmware to start up. */
957 msleep_interruptible(1000);
958
959 usb_clear_halt(usb_dev, usb_sndbulkpipe(usb_dev, CXACRU_EP_CMD));
960 usb_clear_halt(usb_dev, usb_rcvbulkpipe(usb_dev, CXACRU_EP_CMD));
961 usb_clear_halt(usb_dev, usb_sndbulkpipe(usb_dev, CXACRU_EP_DATA));
962 usb_clear_halt(usb_dev, usb_rcvbulkpipe(usb_dev, CXACRU_EP_DATA));
963
964 ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_STATUS, NULL, 0, NULL, 0);
965 if (ret < 0) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100966 usb_err(usbatm, "modem failed to initialize: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200967 return;
968 }
969
970 /* Load config data (le32), doing one packet at a time */
971 if (cf)
972 for (off = 0; off < cf->size / 4; ) {
Al Virofd05e722008-04-28 07:00:16 +0100973 __le32 buf[CMD_PACKET_SIZE / 4 - 1];
Duncan Sands1b0e6142005-05-11 20:19:29 +0200974 int i, len = min_t(int, cf->size / 4 - off, CMD_PACKET_SIZE / 4 / 2 - 1);
975 buf[0] = cpu_to_le32(len);
976 for (i = 0; i < len; i++, off++) {
977 buf[i * 2 + 1] = cpu_to_le32(off);
978 memcpy(buf + i * 2 + 2, cf->data + off * 4, 4);
979 }
980 ret = cxacru_cm(instance, CM_REQUEST_CARD_DATA_SET,
981 (u8 *) buf, len, NULL, 0);
982 if (ret < 0) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100983 usb_err(usbatm, "load config data failed: %d\n", ret);
Duncan Sands1b0e6142005-05-11 20:19:29 +0200984 return;
985 }
986 }
987
988 msleep_interruptible(4000);
989}
990
991static int cxacru_find_firmware(struct cxacru_data *instance,
992 char* phase, const struct firmware **fw_p)
993{
Duncan Sands0ec3c7e2006-01-17 11:15:13 +0100994 struct usbatm_data *usbatm = instance->usbatm;
995 struct device *dev = &usbatm->usb_intf->dev;
Duncan Sands1b0e6142005-05-11 20:19:29 +0200996 char buf[16];
997
998 sprintf(buf, "cxacru-%s.bin", phase);
999 dbg("cxacru_find_firmware: looking for %s", buf);
1000
1001 if (request_firmware(fw_p, buf, dev)) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +01001002 usb_dbg(usbatm, "no stage %s firmware found\n", phase);
Duncan Sands1b0e6142005-05-11 20:19:29 +02001003 return -ENOENT;
1004 }
1005
Duncan Sands0ec3c7e2006-01-17 11:15:13 +01001006 usb_info(usbatm, "found firmware %s\n", buf);
Duncan Sands1b0e6142005-05-11 20:19:29 +02001007
1008 return 0;
1009}
1010
1011static int cxacru_heavy_init(struct usbatm_data *usbatm_instance,
1012 struct usb_interface *usb_intf)
1013{
Duncan Sands1b0e6142005-05-11 20:19:29 +02001014 const struct firmware *fw, *bp, *cf;
1015 struct cxacru_data *instance = usbatm_instance->driver_data;
1016
1017 int ret = cxacru_find_firmware(instance, "fw", &fw);
1018 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +01001019 usb_warn(usbatm_instance, "firmware (cxacru-fw.bin) unavailable (system misconfigured?)\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +02001020 return ret;
1021 }
1022
1023 if (instance->modem_type->boot_rom_patch) {
1024 ret = cxacru_find_firmware(instance, "bp", &bp);
1025 if (ret) {
Duncan Sands0ec3c7e2006-01-17 11:15:13 +01001026 usb_warn(usbatm_instance, "boot ROM patch (cxacru-bp.bin) unavailable (system misconfigured?)\n");
Duncan Sands1b0e6142005-05-11 20:19:29 +02001027 release_firmware(fw);
1028 return ret;
1029 }
1030 }
1031
1032 if (cxacru_find_firmware(instance, "cf", &cf)) /* optional */
1033 cf = NULL;
1034
1035 cxacru_upload_firmware(instance, fw, bp, cf);
1036
1037 if (cf)
1038 release_firmware(cf);
1039 if (instance->modem_type->boot_rom_patch)
1040 release_firmware(bp);
1041 release_firmware(fw);
1042
1043 ret = cxacru_card_status(instance);
1044 if (ret)
1045 dbg("modem initialisation failed");
1046 else
1047 dbg("done setting up the modem");
1048
1049 return ret;
1050}
1051
1052static int cxacru_bind(struct usbatm_data *usbatm_instance,
Duncan Sands35644b02006-01-17 11:16:13 +01001053 struct usb_interface *intf, const struct usb_device_id *id)
Duncan Sands1b0e6142005-05-11 20:19:29 +02001054{
1055 struct cxacru_data *instance;
1056 struct usb_device *usb_dev = interface_to_usbdev(intf);
1057 int ret;
1058
1059 /* instance init */
Duncan Sands9a734ef2006-01-13 09:38:22 +01001060 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
Duncan Sands1b0e6142005-05-11 20:19:29 +02001061 if (!instance) {
1062 dbg("cxacru_bind: no memory for instance data");
1063 return -ENOMEM;
1064 }
1065
Duncan Sands1b0e6142005-05-11 20:19:29 +02001066 instance->usbatm = usbatm_instance;
1067 instance->modem_type = (struct cxacru_modem_type *) id->driver_info;
1068
Simon Arlott6a02c9962007-04-26 00:38:05 -07001069 mutex_init(&instance->poll_state_serialize);
1070 instance->poll_state = CXPOLL_STOPPED;
1071 instance->line_status = -1;
1072 instance->adsl_status = -1;
1073
1074 mutex_init(&instance->adsl_state_serialize);
1075
Duncan Sands1b0e6142005-05-11 20:19:29 +02001076 instance->rcv_buf = (u8 *) __get_free_page(GFP_KERNEL);
1077 if (!instance->rcv_buf) {
1078 dbg("cxacru_bind: no memory for rcv_buf");
1079 ret = -ENOMEM;
1080 goto fail;
1081 }
1082 instance->snd_buf = (u8 *) __get_free_page(GFP_KERNEL);
1083 if (!instance->snd_buf) {
1084 dbg("cxacru_bind: no memory for snd_buf");
1085 ret = -ENOMEM;
1086 goto fail;
1087 }
1088 instance->rcv_urb = usb_alloc_urb(0, GFP_KERNEL);
1089 if (!instance->rcv_urb) {
1090 dbg("cxacru_bind: no memory for rcv_urb");
1091 ret = -ENOMEM;
1092 goto fail;
1093 }
1094 instance->snd_urb = usb_alloc_urb(0, GFP_KERNEL);
1095 if (!instance->snd_urb) {
1096 dbg("cxacru_bind: no memory for snd_urb");
1097 ret = -ENOMEM;
1098 goto fail;
1099 }
1100
1101 usb_fill_int_urb(instance->rcv_urb,
1102 usb_dev, usb_rcvintpipe(usb_dev, CXACRU_EP_CMD),
1103 instance->rcv_buf, PAGE_SIZE,
1104 cxacru_blocking_completion, &instance->rcv_done, 1);
Duncan Sands1b0e6142005-05-11 20:19:29 +02001105
1106 usb_fill_int_urb(instance->snd_urb,
1107 usb_dev, usb_sndintpipe(usb_dev, CXACRU_EP_CMD),
1108 instance->snd_buf, PAGE_SIZE,
1109 cxacru_blocking_completion, &instance->snd_done, 4);
Duncan Sands1b0e6142005-05-11 20:19:29 +02001110
Arjan van de Venab3c81f2006-01-13 15:52:55 +01001111 mutex_init(&instance->cm_serialize);
Duncan Sands1b0e6142005-05-11 20:19:29 +02001112
David Howellsc4028952006-11-22 14:57:56 +00001113 INIT_DELAYED_WORK(&instance->poll_work, cxacru_poll_status);
Duncan Sands1b0e6142005-05-11 20:19:29 +02001114
1115 usbatm_instance->driver_data = instance;
1116
Duncan Sands35644b02006-01-17 11:16:13 +01001117 usbatm_instance->flags = (cxacru_card_status(instance) ? 0 : UDSL_SKIP_HEAVY_INIT);
Duncan Sands1b0e6142005-05-11 20:19:29 +02001118
1119 return 0;
1120
1121 fail:
1122 free_page((unsigned long) instance->snd_buf);
1123 free_page((unsigned long) instance->rcv_buf);
1124 usb_free_urb(instance->snd_urb);
1125 usb_free_urb(instance->rcv_urb);
1126 kfree(instance);
1127
1128 return ret;
1129}
1130
1131static void cxacru_unbind(struct usbatm_data *usbatm_instance,
1132 struct usb_interface *intf)
1133{
1134 struct cxacru_data *instance = usbatm_instance->driver_data;
Simon Arlott6a02c9962007-04-26 00:38:05 -07001135 int is_polling = 1;
Duncan Sands1b0e6142005-05-11 20:19:29 +02001136
1137 dbg("cxacru_unbind entered");
1138
1139 if (!instance) {
1140 dbg("cxacru_unbind: NULL instance!");
1141 return;
1142 }
1143
Simon Arlott6a02c9962007-04-26 00:38:05 -07001144 mutex_lock(&instance->poll_state_serialize);
1145 BUG_ON(instance->poll_state == CXPOLL_SHUTDOWN);
1146
1147 /* ensure that status polling continues unless
1148 * it has already stopped */
1149 if (instance->poll_state == CXPOLL_STOPPED)
1150 is_polling = 0;
1151
1152 /* stop polling from being stopped or started */
1153 instance->poll_state = CXPOLL_SHUTDOWN;
1154 mutex_unlock(&instance->poll_state_serialize);
1155
1156 if (is_polling)
1157 cancel_rearming_delayed_work(&instance->poll_work);
Duncan Sands1b0e6142005-05-11 20:19:29 +02001158
1159 usb_kill_urb(instance->snd_urb);
1160 usb_kill_urb(instance->rcv_urb);
1161 usb_free_urb(instance->snd_urb);
1162 usb_free_urb(instance->rcv_urb);
1163
1164 free_page((unsigned long) instance->snd_buf);
1165 free_page((unsigned long) instance->rcv_buf);
Simon Arlottfa70fe42007-03-06 02:47:45 -08001166
Duncan Sands1b0e6142005-05-11 20:19:29 +02001167 kfree(instance);
1168
1169 usbatm_instance->driver_data = NULL;
1170}
1171
1172static const struct cxacru_modem_type cxacru_cafe = {
1173 .pll_f_clk = 0x02d874df,
1174 .pll_b_clk = 0x0196a51a,
1175 .boot_rom_patch = 1,
1176};
1177
1178static const struct cxacru_modem_type cxacru_cb00 = {
1179 .pll_f_clk = 0x5,
1180 .pll_b_clk = 0x3,
1181 .boot_rom_patch = 0,
1182};
1183
1184static const struct usb_device_id cxacru_usb_ids[] = {
1185 { /* V = Conexant P = ADSL modem (Euphrates project) */
1186 USB_DEVICE(0x0572, 0xcafe), .driver_info = (unsigned long) &cxacru_cafe
1187 },
1188 { /* V = Conexant P = ADSL modem (Hasbani project) */
1189 USB_DEVICE(0x0572, 0xcb00), .driver_info = (unsigned long) &cxacru_cb00
1190 },
Duncan Sands1b0e6142005-05-11 20:19:29 +02001191 { /* V = Conexant P = ADSL modem */
1192 USB_DEVICE(0x0572, 0xcb01), .driver_info = (unsigned long) &cxacru_cb00
1193 },
Duncan Sands0ec3c7e2006-01-17 11:15:13 +01001194 { /* V = Conexant P = ADSL modem (Well PTI-800) */
1195 USB_DEVICE(0x0572, 0xcb02), .driver_info = (unsigned long) &cxacru_cb00
1196 },
Duncan Sands1b0e6142005-05-11 20:19:29 +02001197 { /* V = Conexant P = ADSL modem */
1198 USB_DEVICE(0x0572, 0xcb06), .driver_info = (unsigned long) &cxacru_cb00
1199 },
Duncan Sands44960af2006-10-05 11:05:50 +02001200 { /* V = Conexant P = ADSL modem (ZTE ZXDSL 852) */
1201 USB_DEVICE(0x0572, 0xcb07), .driver_info = (unsigned long) &cxacru_cb00
1202 },
Duncan Sands1b0e6142005-05-11 20:19:29 +02001203 { /* V = Olitec P = ADSL modem version 2 */
1204 USB_DEVICE(0x08e3, 0x0100), .driver_info = (unsigned long) &cxacru_cafe
1205 },
1206 { /* V = Olitec P = ADSL modem version 3 */
1207 USB_DEVICE(0x08e3, 0x0102), .driver_info = (unsigned long) &cxacru_cb00
1208 },
1209 { /* V = Trust/Amigo Technology Co. P = AMX-CA86U */
1210 USB_DEVICE(0x0eb0, 0x3457), .driver_info = (unsigned long) &cxacru_cafe
1211 },
1212 { /* V = Zoom P = 5510 */
1213 USB_DEVICE(0x1803, 0x5510), .driver_info = (unsigned long) &cxacru_cb00
1214 },
1215 { /* V = Draytek P = Vigor 318 */
1216 USB_DEVICE(0x0675, 0x0200), .driver_info = (unsigned long) &cxacru_cb00
1217 },
1218 { /* V = Zyxel P = 630-C1 aka OMNI ADSL USB (Annex A) */
1219 USB_DEVICE(0x0586, 0x330a), .driver_info = (unsigned long) &cxacru_cb00
1220 },
1221 { /* V = Zyxel P = 630-C3 aka OMNI ADSL USB (Annex B) */
1222 USB_DEVICE(0x0586, 0x330b), .driver_info = (unsigned long) &cxacru_cb00
1223 },
1224 { /* V = Aethra P = Starmodem UM1020 */
1225 USB_DEVICE(0x0659, 0x0020), .driver_info = (unsigned long) &cxacru_cb00
1226 },
1227 { /* V = Aztech Systems P = ? AKA Pirelli AUA-010 */
1228 USB_DEVICE(0x0509, 0x0812), .driver_info = (unsigned long) &cxacru_cb00
1229 },
1230 { /* V = Netopia P = Cayman 3341(Annex A)/3351(Annex B) */
1231 USB_DEVICE(0x100d, 0xcb01), .driver_info = (unsigned long) &cxacru_cb00
1232 },
1233 { /* V = Netopia P = Cayman 3342(Annex A)/3352(Annex B) */
1234 USB_DEVICE(0x100d, 0x3342), .driver_info = (unsigned long) &cxacru_cb00
1235 },
1236 {}
1237};
1238
1239MODULE_DEVICE_TABLE(usb, cxacru_usb_ids);
1240
1241static struct usbatm_driver cxacru_driver = {
Duncan Sands1b0e6142005-05-11 20:19:29 +02001242 .driver_name = cxacru_driver_name,
1243 .bind = cxacru_bind,
1244 .heavy_init = cxacru_heavy_init,
1245 .unbind = cxacru_unbind,
1246 .atm_start = cxacru_atm_start,
Simon Arlottda1f82b52007-05-10 23:04:12 -07001247 .atm_stop = cxacru_remove_device_files,
Duncan Sands80aae7a2006-01-13 10:59:23 +01001248 .bulk_in = CXACRU_EP_DATA,
1249 .bulk_out = CXACRU_EP_DATA,
Duncan Sands1b0e6142005-05-11 20:19:29 +02001250 .rx_padding = 3,
1251 .tx_padding = 11,
1252};
1253
1254static int cxacru_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1255{
1256 return usbatm_usb_probe(intf, id, &cxacru_driver);
1257}
1258
1259static struct usb_driver cxacru_usb_driver = {
Duncan Sands1b0e6142005-05-11 20:19:29 +02001260 .name = cxacru_driver_name,
1261 .probe = cxacru_usb_probe,
1262 .disconnect = usbatm_usb_disconnect,
1263 .id_table = cxacru_usb_ids
1264};
1265
1266static int __init cxacru_init(void)
1267{
1268 return usb_register(&cxacru_usb_driver);
1269}
1270
1271static void __exit cxacru_cleanup(void)
1272{
1273 usb_deregister(&cxacru_usb_driver);
1274}
1275
1276module_init(cxacru_init);
1277module_exit(cxacru_cleanup);
1278
1279MODULE_AUTHOR(DRIVER_AUTHOR);
1280MODULE_DESCRIPTION(DRIVER_DESC);
1281MODULE_LICENSE("GPL");
1282MODULE_VERSION(DRIVER_VERSION);