cxacru.c 39 KB

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