cxacru.c 39 KB

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