cxacru.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  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. /* control 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 sprintf(buf, "%u\n", value);
  205. }
  206. static ssize_t cxacru_sysfs_showattr_s8(s8 value, char *buf)
  207. {
  208. return sprintf(buf, "%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 sprintf(buf, "%u\n", value);
  226. return sprintf(buf, "%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 sprintf(buf, "%u\n", value);
  233. return sprintf(buf, "%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 sprintf(buf, "%u\n", value);
  243. return sprintf(buf, "%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 sprintf(buf, "%u\n", value);
  255. return sprintf(buf, "%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 sprintf(buf, "%pM\n", instance->usbatm->atm_dev->esi);
  273. }
  274. static ssize_t adsl_state_show(struct device *dev,
  275. struct device_attribute *attr, char *buf)
  276. {
  277. static char *str[] = { "running", "stopped" };
  278. struct cxacru_data *instance = to_usbatm_driver_data(
  279. to_usb_interface(dev));
  280. u32 value;
  281. if (instance == NULL)
  282. return -ENODEV;
  283. value = instance->card_info[CXINF_LINE_STARTABLE];
  284. if (unlikely(value >= ARRAY_SIZE(str)))
  285. return sprintf(buf, "%u\n", value);
  286. return sprintf(buf, "%s\n", str[value]);
  287. }
  288. static ssize_t adsl_state_store(struct device *dev,
  289. struct device_attribute *attr, const char *buf, size_t count)
  290. {
  291. struct cxacru_data *instance = to_usbatm_driver_data(
  292. to_usb_interface(dev));
  293. int ret;
  294. int poll = -1;
  295. char str_cmd[8];
  296. int len = strlen(buf);
  297. if (!capable(CAP_NET_ADMIN))
  298. return -EACCES;
  299. ret = sscanf(buf, "%7s", str_cmd);
  300. if (ret != 1)
  301. return -EINVAL;
  302. ret = 0;
  303. if (instance == NULL)
  304. return -ENODEV;
  305. if (mutex_lock_interruptible(&instance->adsl_state_serialize))
  306. return -ERESTARTSYS;
  307. if (!strcmp(str_cmd, "stop") || !strcmp(str_cmd, "restart")) {
  308. ret = cxacru_cm(instance, CM_REQUEST_CHIP_ADSL_LINE_STOP, NULL, 0, NULL, 0);
  309. if (ret < 0) {
  310. atm_err(instance->usbatm, "change adsl state:"
  311. " CHIP_ADSL_LINE_STOP returned %d\n", ret);
  312. ret = -EIO;
  313. } else {
  314. ret = len;
  315. poll = CXPOLL_STOPPED;
  316. }
  317. }
  318. /* Line status is only updated every second
  319. * and the device appears to only react to
  320. * START/STOP every second too. Wait 1.5s to
  321. * be sure that restart will have an effect. */
  322. if (!strcmp(str_cmd, "restart"))
  323. msleep(1500);
  324. if (!strcmp(str_cmd, "start") || !strcmp(str_cmd, "restart")) {
  325. ret = cxacru_cm(instance, CM_REQUEST_CHIP_ADSL_LINE_START, NULL, 0, NULL, 0);
  326. if (ret < 0) {
  327. atm_err(instance->usbatm, "change adsl state:"
  328. " CHIP_ADSL_LINE_START returned %d\n", ret);
  329. ret = -EIO;
  330. } else {
  331. ret = len;
  332. poll = CXPOLL_POLLING;
  333. }
  334. }
  335. if (!strcmp(str_cmd, "poll")) {
  336. ret = len;
  337. poll = CXPOLL_POLLING;
  338. }
  339. if (ret == 0) {
  340. ret = -EINVAL;
  341. poll = -1;
  342. }
  343. if (poll == CXPOLL_POLLING) {
  344. mutex_lock(&instance->poll_state_serialize);
  345. switch (instance->poll_state) {
  346. case CXPOLL_STOPPED:
  347. /* start polling */
  348. instance->poll_state = CXPOLL_POLLING;
  349. break;
  350. case CXPOLL_STOPPING:
  351. /* abort stop request */
  352. instance->poll_state = CXPOLL_POLLING;
  353. fallthrough;
  354. case CXPOLL_POLLING:
  355. case CXPOLL_SHUTDOWN:
  356. /* don't start polling */
  357. poll = -1;
  358. }
  359. mutex_unlock(&instance->poll_state_serialize);
  360. } else if (poll == CXPOLL_STOPPED) {
  361. mutex_lock(&instance->poll_state_serialize);
  362. /* request stop */
  363. if (instance->poll_state == CXPOLL_POLLING)
  364. instance->poll_state = CXPOLL_STOPPING;
  365. mutex_unlock(&instance->poll_state_serialize);
  366. }
  367. mutex_unlock(&instance->adsl_state_serialize);
  368. if (poll == CXPOLL_POLLING)
  369. cxacru_poll_status(&instance->poll_work.work);
  370. return ret;
  371. }
  372. /* CM_REQUEST_CARD_DATA_GET times out, so no show attribute */
  373. static ssize_t adsl_config_store(struct device *dev,
  374. struct device_attribute *attr, const char *buf, size_t count)
  375. {
  376. struct cxacru_data *instance = to_usbatm_driver_data(
  377. to_usb_interface(dev));
  378. int len = strlen(buf);
  379. int ret, pos, num;
  380. __le32 data[CMD_PACKET_SIZE / 4];
  381. if (!capable(CAP_NET_ADMIN))
  382. return -EACCES;
  383. if (instance == NULL)
  384. return -ENODEV;
  385. pos = 0;
  386. num = 0;
  387. while (pos < len) {
  388. int tmp;
  389. u32 index;
  390. u32 value;
  391. ret = sscanf(buf + pos, "%x=%x%n", &index, &value, &tmp);
  392. if (ret < 2)
  393. return -EINVAL;
  394. if (index > 0x7f)
  395. return -EINVAL;
  396. if (tmp < 0 || tmp > len - pos)
  397. return -EINVAL;
  398. pos += tmp;
  399. /* skip trailing newline */
  400. if (buf[pos] == '\n' && pos == len-1)
  401. pos++;
  402. data[num * 2 + 1] = cpu_to_le32(index);
  403. data[num * 2 + 2] = cpu_to_le32(value);
  404. num++;
  405. /* send config values when data buffer is full
  406. * or no more data
  407. */
  408. if (pos >= len || num >= CMD_MAX_CONFIG) {
  409. char log[CMD_MAX_CONFIG * 12 + 1]; /* %02x=%08x */
  410. data[0] = cpu_to_le32(num);
  411. ret = cxacru_cm(instance, CM_REQUEST_CARD_DATA_SET,
  412. (u8 *) data, 4 + num * 8, NULL, 0);
  413. if (ret < 0) {
  414. atm_err(instance->usbatm,
  415. "set card data returned %d\n", ret);
  416. return -EIO;
  417. }
  418. for (tmp = 0; tmp < num; tmp++)
  419. snprintf(log + tmp*12, 13, " %02x=%08x",
  420. le32_to_cpu(data[tmp * 2 + 1]),
  421. le32_to_cpu(data[tmp * 2 + 2]));
  422. atm_info(instance->usbatm, "config%s\n", log);
  423. num = 0;
  424. }
  425. }
  426. return len;
  427. }
  428. /*
  429. * All device attributes are included in CXACRU_ALL_FILES
  430. * so that the same list can be used multiple times:
  431. * INIT (define the device attributes)
  432. * CREATE (create all the device files)
  433. * REMOVE (remove all the device files)
  434. *
  435. * With the last two being defined as needed in the functions
  436. * they are used in before calling CXACRU_ALL_FILES()
  437. */
  438. #define CXACRU_ALL_FILES(_action) \
  439. CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_RATE, u32, downstream_rate); \
  440. CXACRU_ATTR_##_action(CXINF_UPSTREAM_RATE, u32, upstream_rate); \
  441. CXACRU_ATTR_##_action(CXINF_LINK_STATUS, LINK, link_status); \
  442. CXACRU_ATTR_##_action(CXINF_LINE_STATUS, LINE, line_status); \
  443. CXACRU__ATTR_##_action( mac_address); \
  444. CXACRU_ATTR_##_action(CXINF_UPSTREAM_SNR_MARGIN, dB, upstream_snr_margin); \
  445. CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_SNR_MARGIN, dB, downstream_snr_margin); \
  446. CXACRU_ATTR_##_action(CXINF_UPSTREAM_ATTENUATION, dB, upstream_attenuation); \
  447. CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_ATTENUATION, dB, downstream_attenuation); \
  448. CXACRU_ATTR_##_action(CXINF_TRANSMITTER_POWER, s8, transmitter_power); \
  449. CXACRU_ATTR_##_action(CXINF_UPSTREAM_BITS_PER_FRAME, u32, upstream_bits_per_frame); \
  450. CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_BITS_PER_FRAME, u32, downstream_bits_per_frame); \
  451. CXACRU_ATTR_##_action(CXINF_STARTUP_ATTEMPTS, u32, startup_attempts); \
  452. CXACRU_ATTR_##_action(CXINF_UPSTREAM_CRC_ERRORS, u32, upstream_crc_errors); \
  453. CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_CRC_ERRORS, u32, downstream_crc_errors); \
  454. CXACRU_ATTR_##_action(CXINF_UPSTREAM_FEC_ERRORS, u32, upstream_fec_errors); \
  455. CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_FEC_ERRORS, u32, downstream_fec_errors); \
  456. CXACRU_ATTR_##_action(CXINF_UPSTREAM_HEC_ERRORS, u32, upstream_hec_errors); \
  457. CXACRU_ATTR_##_action(CXINF_DOWNSTREAM_HEC_ERRORS, u32, downstream_hec_errors); \
  458. CXACRU_ATTR_##_action(CXINF_LINE_STARTABLE, bool, line_startable); \
  459. CXACRU_ATTR_##_action(CXINF_MODULATION, MODU, modulation); \
  460. CXACRU_ATTR_##_action(CXINF_ADSL_HEADEND, u32, adsl_headend); \
  461. CXACRU_ATTR_##_action(CXINF_ADSL_HEADEND_ENVIRONMENT, u32, adsl_headend_environment); \
  462. CXACRU_ATTR_##_action(CXINF_CONTROLLER_VERSION, u32, adsl_controller_version); \
  463. CXACRU_CMD_##_action( adsl_state); \
  464. CXACRU_SET_##_action( adsl_config);
  465. CXACRU_ALL_FILES(INIT);
  466. static struct attribute *cxacru_attrs[] = {
  467. &dev_attr_adsl_config.attr,
  468. &dev_attr_adsl_state.attr,
  469. &dev_attr_adsl_controller_version.attr,
  470. &dev_attr_adsl_headend_environment.attr,
  471. &dev_attr_adsl_headend.attr,
  472. &dev_attr_modulation.attr,
  473. &dev_attr_line_startable.attr,
  474. &dev_attr_downstream_hec_errors.attr,
  475. &dev_attr_upstream_hec_errors.attr,
  476. &dev_attr_downstream_fec_errors.attr,
  477. &dev_attr_upstream_fec_errors.attr,
  478. &dev_attr_downstream_crc_errors.attr,
  479. &dev_attr_upstream_crc_errors.attr,
  480. &dev_attr_startup_attempts.attr,
  481. &dev_attr_downstream_bits_per_frame.attr,
  482. &dev_attr_upstream_bits_per_frame.attr,
  483. &dev_attr_transmitter_power.attr,
  484. &dev_attr_downstream_attenuation.attr,
  485. &dev_attr_upstream_attenuation.attr,
  486. &dev_attr_downstream_snr_margin.attr,
  487. &dev_attr_upstream_snr_margin.attr,
  488. &dev_attr_mac_address.attr,
  489. &dev_attr_line_status.attr,
  490. &dev_attr_link_status.attr,
  491. &dev_attr_upstream_rate.attr,
  492. &dev_attr_downstream_rate.attr,
  493. NULL,
  494. };
  495. ATTRIBUTE_GROUPS(cxacru);
  496. /* the following three functions are stolen from drivers/usb/core/message.c */
  497. static void cxacru_blocking_completion(struct urb *urb)
  498. {
  499. complete(urb->context);
  500. }
  501. struct cxacru_timer {
  502. struct timer_list timer;
  503. struct urb *urb;
  504. };
  505. static void cxacru_timeout_kill(struct timer_list *t)
  506. {
  507. struct cxacru_timer *timer = from_timer(timer, t, timer);
  508. usb_unlink_urb(timer->urb);
  509. }
  510. static int cxacru_start_wait_urb(struct urb *urb, struct completion *done,
  511. int *actual_length)
  512. {
  513. struct cxacru_timer timer = {
  514. .urb = urb,
  515. };
  516. timer_setup_on_stack(&timer.timer, cxacru_timeout_kill, 0);
  517. mod_timer(&timer.timer, jiffies + msecs_to_jiffies(CMD_TIMEOUT));
  518. wait_for_completion(done);
  519. del_timer_sync(&timer.timer);
  520. destroy_timer_on_stack(&timer.timer);
  521. if (actual_length)
  522. *actual_length = urb->actual_length;
  523. return urb->status; /* must read status after completion */
  524. }
  525. static int cxacru_cm(struct cxacru_data *instance, enum cxacru_cm_request cm,
  526. u8 *wdata, int wsize, u8 *rdata, int rsize)
  527. {
  528. int ret, actlen;
  529. int offb, offd;
  530. const int stride = CMD_PACKET_SIZE - 4;
  531. u8 *wbuf = instance->snd_buf;
  532. u8 *rbuf = instance->rcv_buf;
  533. int wbuflen = ((wsize - 1) / stride + 1) * CMD_PACKET_SIZE;
  534. int rbuflen = ((rsize - 1) / stride + 1) * CMD_PACKET_SIZE;
  535. if (wbuflen > PAGE_SIZE || rbuflen > PAGE_SIZE) {
  536. if (printk_ratelimit())
  537. usb_err(instance->usbatm, "requested transfer size too large (%d, %d)\n",
  538. wbuflen, rbuflen);
  539. ret = -ENOMEM;
  540. goto err;
  541. }
  542. mutex_lock(&instance->cm_serialize);
  543. /* submit reading urb before the writing one */
  544. init_completion(&instance->rcv_done);
  545. ret = usb_submit_urb(instance->rcv_urb, GFP_KERNEL);
  546. if (ret < 0) {
  547. if (printk_ratelimit())
  548. usb_err(instance->usbatm, "submit of read urb for cm %#x failed (%d)\n",
  549. cm, ret);
  550. goto fail;
  551. }
  552. memset(wbuf, 0, wbuflen);
  553. /* handle wsize == 0 */
  554. wbuf[0] = cm;
  555. for (offb = offd = 0; offd < wsize; offd += stride, offb += CMD_PACKET_SIZE) {
  556. wbuf[offb] = cm;
  557. memcpy(wbuf + offb + 4, wdata + offd, min_t(int, stride, wsize - offd));
  558. }
  559. instance->snd_urb->transfer_buffer_length = wbuflen;
  560. init_completion(&instance->snd_done);
  561. ret = usb_submit_urb(instance->snd_urb, GFP_KERNEL);
  562. if (ret < 0) {
  563. if (printk_ratelimit())
  564. usb_err(instance->usbatm, "submit of write urb for cm %#x failed (%d)\n",
  565. cm, ret);
  566. goto fail;
  567. }
  568. ret = cxacru_start_wait_urb(instance->snd_urb, &instance->snd_done, NULL);
  569. if (ret < 0) {
  570. if (printk_ratelimit())
  571. usb_err(instance->usbatm, "send of cm %#x failed (%d)\n", cm, ret);
  572. goto fail;
  573. }
  574. ret = cxacru_start_wait_urb(instance->rcv_urb, &instance->rcv_done, &actlen);
  575. if (ret < 0) {
  576. if (printk_ratelimit())
  577. usb_err(instance->usbatm, "receive of cm %#x failed (%d)\n", cm, ret);
  578. goto fail;
  579. }
  580. if (actlen % CMD_PACKET_SIZE || !actlen) {
  581. if (printk_ratelimit())
  582. usb_err(instance->usbatm, "invalid response length to cm %#x: %d\n",
  583. cm, actlen);
  584. ret = -EIO;
  585. goto fail;
  586. }
  587. /* check the return status and copy the data to the output buffer, if needed */
  588. for (offb = offd = 0; offd < rsize && offb < actlen; offb += CMD_PACKET_SIZE) {
  589. if (rbuf[offb] != cm) {
  590. if (printk_ratelimit())
  591. usb_err(instance->usbatm, "wrong cm %#x in response to cm %#x\n",
  592. rbuf[offb], cm);
  593. ret = -EIO;
  594. goto fail;
  595. }
  596. if (rbuf[offb + 1] != CM_STATUS_SUCCESS) {
  597. if (printk_ratelimit())
  598. usb_err(instance->usbatm, "response to cm %#x failed: %#x\n",
  599. cm, rbuf[offb + 1]);
  600. ret = -EIO;
  601. goto fail;
  602. }
  603. if (offd >= rsize)
  604. break;
  605. memcpy(rdata + offd, rbuf + offb + 4, min_t(int, stride, rsize - offd));
  606. offd += stride;
  607. }
  608. ret = offd;
  609. usb_dbg(instance->usbatm, "cm %#x\n", cm);
  610. fail:
  611. mutex_unlock(&instance->cm_serialize);
  612. err:
  613. return ret;
  614. }
  615. static int cxacru_cm_get_array(struct cxacru_data *instance, enum cxacru_cm_request cm,
  616. u32 *data, int size)
  617. {
  618. int ret, len;
  619. __le32 *buf;
  620. int offb;
  621. unsigned int offd;
  622. const int stride = CMD_PACKET_SIZE / (4 * 2) - 1;
  623. int buflen = ((size - 1) / stride + 1 + size * 2) * 4;
  624. buf = kmalloc(buflen, GFP_KERNEL);
  625. if (!buf)
  626. return -ENOMEM;
  627. ret = cxacru_cm(instance, cm, NULL, 0, (u8 *) buf, buflen);
  628. if (ret < 0)
  629. goto cleanup;
  630. /* len > 0 && len % 4 == 0 guaranteed by cxacru_cm() */
  631. len = ret / 4;
  632. for (offb = 0; offb < len; ) {
  633. int l = le32_to_cpu(buf[offb++]);
  634. if (l < 0 || l > stride || l > (len - offb) / 2) {
  635. if (printk_ratelimit())
  636. usb_err(instance->usbatm, "invalid data length from cm %#x: %d\n",
  637. cm, l);
  638. ret = -EIO;
  639. goto cleanup;
  640. }
  641. while (l--) {
  642. offd = le32_to_cpu(buf[offb++]);
  643. if (offd >= size) {
  644. if (printk_ratelimit())
  645. usb_err(instance->usbatm, "wrong index %#x in response to cm %#x\n",
  646. offd, cm);
  647. ret = -EIO;
  648. goto cleanup;
  649. }
  650. data[offd] = le32_to_cpu(buf[offb++]);
  651. }
  652. }
  653. ret = 0;
  654. cleanup:
  655. kfree(buf);
  656. return ret;
  657. }
  658. static int cxacru_card_status(struct cxacru_data *instance)
  659. {
  660. int ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_STATUS, NULL, 0, NULL, 0);
  661. if (ret < 0) { /* firmware not loaded */
  662. usb_dbg(instance->usbatm, "cxacru_adsl_start: CARD_GET_STATUS returned %d\n", ret);
  663. return ret;
  664. }
  665. return 0;
  666. }
  667. static int cxacru_atm_start(struct usbatm_data *usbatm_instance,
  668. struct atm_dev *atm_dev)
  669. {
  670. struct cxacru_data *instance = usbatm_instance->driver_data;
  671. struct usb_interface *intf = usbatm_instance->usb_intf;
  672. int ret;
  673. int start_polling = 1;
  674. dev_dbg(&intf->dev, "%s\n", __func__);
  675. /* Read MAC address */
  676. ret = cxacru_cm(instance, CM_REQUEST_CARD_GET_MAC_ADDRESS, NULL, 0,
  677. atm_dev->esi, sizeof(atm_dev->esi));
  678. if (ret < 0) {
  679. atm_err(usbatm_instance, "cxacru_atm_start: CARD_GET_MAC_ADDRESS returned %d\n", ret);
  680. return ret;
  681. }
  682. /* start ADSL */
  683. mutex_lock(&instance->adsl_state_serialize);
  684. ret = cxacru_cm(instance, CM_REQUEST_CHIP_ADSL_LINE_START, NULL, 0, NULL, 0);
  685. if (ret < 0)
  686. atm_err(usbatm_instance, "cxacru_atm_start: CHIP_ADSL_LINE_START returned %d\n", ret);
  687. /* Start status polling */
  688. mutex_lock(&instance->poll_state_serialize);
  689. switch (instance->poll_state) {
  690. case CXPOLL_STOPPED:
  691. /* start polling */
  692. instance->poll_state = CXPOLL_POLLING;
  693. break;
  694. case CXPOLL_STOPPING:
  695. /* abort stop request */
  696. instance->poll_state = CXPOLL_POLLING;
  697. fallthrough;
  698. case CXPOLL_POLLING:
  699. case CXPOLL_SHUTDOWN:
  700. /* don't start polling */
  701. start_polling = 0;
  702. }
  703. mutex_unlock(&instance->poll_state_serialize);
  704. mutex_unlock(&instance->adsl_state_serialize);
  705. if (start_polling)
  706. cxacru_poll_status(&instance->poll_work.work);
  707. return 0;
  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_info(usbatm, "ADSL state: running\n");
  738. break;
  739. case 1:
  740. atm_info(usbatm, "ADSL state: stopped\n");
  741. break;
  742. default:
  743. atm_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. dev_dbg(&usb_dev->dev, "sending fw %#x failed\n", fw);
  834. goto cleanup;
  835. }
  836. offb = 0;
  837. }
  838. } while (offd < size);
  839. dev_dbg(&usb_dev->dev, "sent fw %#x\n", 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. usb_dbg(usbatm, "%s\n", __func__);
  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. usb_dbg(usbatm, "cxacru_find_firmware: looking for %s\n", 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. usb_dbg(usbatm_instance, "modem initialisation failed\n");
  962. else
  963. usb_dbg(usbatm_instance, "done setting up the modem\n");
  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. return -ENOMEM;
  977. instance->usbatm = usbatm_instance;
  978. instance->modem_type = (struct cxacru_modem_type *) id->driver_info;
  979. mutex_init(&instance->poll_state_serialize);
  980. instance->poll_state = CXPOLL_STOPPED;
  981. instance->line_status = -1;
  982. instance->adsl_status = -1;
  983. mutex_init(&instance->adsl_state_serialize);
  984. instance->rcv_buf = (u8 *) __get_free_page(GFP_KERNEL);
  985. if (!instance->rcv_buf) {
  986. usb_dbg(usbatm_instance, "cxacru_bind: no memory for rcv_buf\n");
  987. ret = -ENOMEM;
  988. goto fail;
  989. }
  990. instance->snd_buf = (u8 *) __get_free_page(GFP_KERNEL);
  991. if (!instance->snd_buf) {
  992. usb_dbg(usbatm_instance, "cxacru_bind: no memory for snd_buf\n");
  993. ret = -ENOMEM;
  994. goto fail;
  995. }
  996. instance->rcv_urb = usb_alloc_urb(0, GFP_KERNEL);
  997. if (!instance->rcv_urb) {
  998. ret = -ENOMEM;
  999. goto fail;
  1000. }
  1001. instance->snd_urb = usb_alloc_urb(0, GFP_KERNEL);
  1002. if (!instance->snd_urb) {
  1003. ret = -ENOMEM;
  1004. goto fail;
  1005. }
  1006. if (!cmd_ep) {
  1007. usb_dbg(usbatm_instance, "cxacru_bind: no command endpoint\n");
  1008. ret = -ENODEV;
  1009. goto fail;
  1010. }
  1011. if ((cmd_ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  1012. == USB_ENDPOINT_XFER_INT) {
  1013. usb_fill_int_urb(instance->rcv_urb,
  1014. usb_dev, usb_rcvintpipe(usb_dev, CXACRU_EP_CMD),
  1015. instance->rcv_buf, PAGE_SIZE,
  1016. cxacru_blocking_completion, &instance->rcv_done, 1);
  1017. usb_fill_int_urb(instance->snd_urb,
  1018. usb_dev, usb_sndintpipe(usb_dev, CXACRU_EP_CMD),
  1019. instance->snd_buf, PAGE_SIZE,
  1020. cxacru_blocking_completion, &instance->snd_done, 4);
  1021. } else {
  1022. usb_fill_bulk_urb(instance->rcv_urb,
  1023. usb_dev, usb_rcvbulkpipe(usb_dev, CXACRU_EP_CMD),
  1024. instance->rcv_buf, PAGE_SIZE,
  1025. cxacru_blocking_completion, &instance->rcv_done);
  1026. usb_fill_bulk_urb(instance->snd_urb,
  1027. usb_dev, usb_sndbulkpipe(usb_dev, CXACRU_EP_CMD),
  1028. instance->snd_buf, PAGE_SIZE,
  1029. cxacru_blocking_completion, &instance->snd_done);
  1030. }
  1031. mutex_init(&instance->cm_serialize);
  1032. INIT_DELAYED_WORK(&instance->poll_work, cxacru_poll_status);
  1033. usbatm_instance->driver_data = instance;
  1034. usbatm_instance->flags = (cxacru_card_status(instance) ? 0 : UDSL_SKIP_HEAVY_INIT);
  1035. return 0;
  1036. fail:
  1037. free_page((unsigned long) instance->snd_buf);
  1038. free_page((unsigned long) instance->rcv_buf);
  1039. usb_free_urb(instance->snd_urb);
  1040. usb_free_urb(instance->rcv_urb);
  1041. kfree(instance);
  1042. return ret;
  1043. }
  1044. static void cxacru_unbind(struct usbatm_data *usbatm_instance,
  1045. struct usb_interface *intf)
  1046. {
  1047. struct cxacru_data *instance = usbatm_instance->driver_data;
  1048. int is_polling = 1;
  1049. usb_dbg(usbatm_instance, "cxacru_unbind entered\n");
  1050. if (!instance) {
  1051. usb_dbg(usbatm_instance, "cxacru_unbind: NULL instance!\n");
  1052. return;
  1053. }
  1054. mutex_lock(&instance->poll_state_serialize);
  1055. BUG_ON(instance->poll_state == CXPOLL_SHUTDOWN);
  1056. /* ensure that status polling continues unless
  1057. * it has already stopped */
  1058. if (instance->poll_state == CXPOLL_STOPPED)
  1059. is_polling = 0;
  1060. /* stop polling from being stopped or started */
  1061. instance->poll_state = CXPOLL_SHUTDOWN;
  1062. mutex_unlock(&instance->poll_state_serialize);
  1063. if (is_polling)
  1064. cancel_delayed_work_sync(&instance->poll_work);
  1065. usb_kill_urb(instance->snd_urb);
  1066. usb_kill_urb(instance->rcv_urb);
  1067. usb_free_urb(instance->snd_urb);
  1068. usb_free_urb(instance->rcv_urb);
  1069. free_page((unsigned long) instance->snd_buf);
  1070. free_page((unsigned long) instance->rcv_buf);
  1071. kfree(instance);
  1072. usbatm_instance->driver_data = NULL;
  1073. }
  1074. static const struct cxacru_modem_type cxacru_cafe = {
  1075. .pll_f_clk = 0x02d874df,
  1076. .pll_b_clk = 0x0196a51a,
  1077. .boot_rom_patch = 1,
  1078. };
  1079. static const struct cxacru_modem_type cxacru_cb00 = {
  1080. .pll_f_clk = 0x5,
  1081. .pll_b_clk = 0x3,
  1082. .boot_rom_patch = 0,
  1083. };
  1084. static const struct usb_device_id cxacru_usb_ids[] = {
  1085. { /* V = Conexant P = ADSL modem (Euphrates project) */
  1086. USB_DEVICE(0x0572, 0xcafe), .driver_info = (unsigned long) &cxacru_cafe
  1087. },
  1088. { /* V = Conexant P = ADSL modem (Hasbani project) */
  1089. USB_DEVICE(0x0572, 0xcb00), .driver_info = (unsigned long) &cxacru_cb00
  1090. },
  1091. { /* V = Conexant P = ADSL modem */
  1092. USB_DEVICE(0x0572, 0xcb01), .driver_info = (unsigned long) &cxacru_cb00
  1093. },
  1094. { /* V = Conexant P = ADSL modem (Well PTI-800) */
  1095. USB_DEVICE(0x0572, 0xcb02), .driver_info = (unsigned long) &cxacru_cb00
  1096. },
  1097. { /* V = Conexant P = ADSL modem */
  1098. USB_DEVICE(0x0572, 0xcb06), .driver_info = (unsigned long) &cxacru_cb00
  1099. },
  1100. { /* V = Conexant P = ADSL modem (ZTE ZXDSL 852) */
  1101. USB_DEVICE(0x0572, 0xcb07), .driver_info = (unsigned long) &cxacru_cb00
  1102. },
  1103. { /* V = Olitec P = ADSL modem version 2 */
  1104. USB_DEVICE(0x08e3, 0x0100), .driver_info = (unsigned long) &cxacru_cafe
  1105. },
  1106. { /* V = Olitec P = ADSL modem version 3 */
  1107. USB_DEVICE(0x08e3, 0x0102), .driver_info = (unsigned long) &cxacru_cb00
  1108. },
  1109. { /* V = Trust/Amigo Technology Co. P = AMX-CA86U */
  1110. USB_DEVICE(0x0eb0, 0x3457), .driver_info = (unsigned long) &cxacru_cafe
  1111. },
  1112. { /* V = Zoom P = 5510 */
  1113. USB_DEVICE(0x1803, 0x5510), .driver_info = (unsigned long) &cxacru_cb00
  1114. },
  1115. { /* V = Draytek P = Vigor 318 */
  1116. USB_DEVICE(0x0675, 0x0200), .driver_info = (unsigned long) &cxacru_cb00
  1117. },
  1118. { /* V = Zyxel P = 630-C1 aka OMNI ADSL USB (Annex A) */
  1119. USB_DEVICE(0x0586, 0x330a), .driver_info = (unsigned long) &cxacru_cb00
  1120. },
  1121. { /* V = Zyxel P = 630-C3 aka OMNI ADSL USB (Annex B) */
  1122. USB_DEVICE(0x0586, 0x330b), .driver_info = (unsigned long) &cxacru_cb00
  1123. },
  1124. { /* V = Aethra P = Starmodem UM1020 */
  1125. USB_DEVICE(0x0659, 0x0020), .driver_info = (unsigned long) &cxacru_cb00
  1126. },
  1127. { /* V = Aztech Systems P = ? AKA Pirelli AUA-010 */
  1128. USB_DEVICE(0x0509, 0x0812), .driver_info = (unsigned long) &cxacru_cb00
  1129. },
  1130. { /* V = Netopia P = Cayman 3341(Annex A)/3351(Annex B) */
  1131. USB_DEVICE(0x100d, 0xcb01), .driver_info = (unsigned long) &cxacru_cb00
  1132. },
  1133. { /* V = Netopia P = Cayman 3342(Annex A)/3352(Annex B) */
  1134. USB_DEVICE(0x100d, 0x3342), .driver_info = (unsigned long) &cxacru_cb00
  1135. },
  1136. {}
  1137. };
  1138. MODULE_DEVICE_TABLE(usb, cxacru_usb_ids);
  1139. static struct usbatm_driver cxacru_driver = {
  1140. .driver_name = cxacru_driver_name,
  1141. .bind = cxacru_bind,
  1142. .heavy_init = cxacru_heavy_init,
  1143. .unbind = cxacru_unbind,
  1144. .atm_start = cxacru_atm_start,
  1145. .bulk_in = CXACRU_EP_DATA,
  1146. .bulk_out = CXACRU_EP_DATA,
  1147. .rx_padding = 3,
  1148. .tx_padding = 11,
  1149. };
  1150. static int cxacru_usb_probe(struct usb_interface *intf,
  1151. const struct usb_device_id *id)
  1152. {
  1153. struct usb_device *usb_dev = interface_to_usbdev(intf);
  1154. char buf[15];
  1155. /* Avoid ADSL routers (cx82310_eth).
  1156. * Abort if bDeviceClass is 0xff and iProduct is "USB NET CARD".
  1157. */
  1158. if (usb_dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC
  1159. && usb_string(usb_dev, usb_dev->descriptor.iProduct,
  1160. buf, sizeof(buf)) > 0) {
  1161. if (!strcmp(buf, "USB NET CARD")) {
  1162. dev_info(&intf->dev, "ignoring cx82310_eth device\n");
  1163. return -ENODEV;
  1164. }
  1165. }
  1166. return usbatm_usb_probe(intf, id, &cxacru_driver);
  1167. }
  1168. static struct usb_driver cxacru_usb_driver = {
  1169. .name = cxacru_driver_name,
  1170. .probe = cxacru_usb_probe,
  1171. .disconnect = usbatm_usb_disconnect,
  1172. .id_table = cxacru_usb_ids,
  1173. .dev_groups = cxacru_groups,
  1174. };
  1175. module_usb_driver(cxacru_usb_driver);
  1176. MODULE_AUTHOR(DRIVER_AUTHOR);
  1177. MODULE_DESCRIPTION(DRIVER_DESC);
  1178. MODULE_LICENSE("GPL");