cxacru.c 38 KB

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