speedtch.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. /******************************************************************************
  2. * speedtch.c - Alcatel SpeedTouch USB xDSL modem driver
  3. *
  4. * Copyright (C) 2001, Alcatel
  5. * Copyright (C) 2003, Duncan Sands
  6. * Copyright (C) 2004, David Woodhouse
  7. *
  8. * Based on "modem_run.c", copyright (C) 2001, Benoit Papillault
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the Free
  12. * Software Foundation; either version 2 of the License, or (at your option)
  13. * any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  18. * more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along with
  21. * this program; if not, write to the Free Software Foundation, Inc., 59
  22. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. *
  24. ******************************************************************************/
  25. #include <asm/page.h>
  26. #include <linux/device.h>
  27. #include <linux/errno.h>
  28. #include <linux/firmware.h>
  29. #include <linux/init.h>
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/moduleparam.h>
  33. #include <linux/slab.h>
  34. #include <linux/stat.h>
  35. #include <linux/timer.h>
  36. #include <linux/types.h>
  37. #include <linux/usb/ch9.h>
  38. #include <linux/workqueue.h>
  39. #include "usbatm.h"
  40. #define DRIVER_AUTHOR "Johan Verrept, Duncan Sands <duncan.sands@free.fr>"
  41. #define DRIVER_VERSION "1.10"
  42. #define DRIVER_DESC "Alcatel SpeedTouch USB driver version " DRIVER_VERSION
  43. static const char speedtch_driver_name[] = "speedtch";
  44. #define CTRL_TIMEOUT 2000 /* milliseconds */
  45. #define DATA_TIMEOUT 2000 /* milliseconds */
  46. #define OFFSET_7 0 /* size 1 */
  47. #define OFFSET_b 1 /* size 8 */
  48. #define OFFSET_d 9 /* size 4 */
  49. #define OFFSET_e 13 /* size 1 */
  50. #define OFFSET_f 14 /* size 1 */
  51. #define SIZE_7 1
  52. #define SIZE_b 8
  53. #define SIZE_d 4
  54. #define SIZE_e 1
  55. #define SIZE_f 1
  56. #define MIN_POLL_DELAY 5000 /* milliseconds */
  57. #define MAX_POLL_DELAY 60000 /* milliseconds */
  58. #define RESUBMIT_DELAY 1000 /* milliseconds */
  59. #define DEFAULT_BULK_ALTSETTING 1
  60. #define DEFAULT_ISOC_ALTSETTING 3
  61. #define DEFAULT_DL_512_FIRST 0
  62. #define DEFAULT_ENABLE_ISOC 0
  63. #define DEFAULT_SW_BUFFERING 0
  64. static unsigned int altsetting = 0; /* zero means: use the default */
  65. static int dl_512_first = DEFAULT_DL_512_FIRST;
  66. static int enable_isoc = DEFAULT_ENABLE_ISOC;
  67. static int sw_buffering = DEFAULT_SW_BUFFERING;
  68. #define DEFAULT_B_MAX_DSL 8128
  69. #define DEFAULT_MODEM_MODE 11
  70. #define MODEM_OPTION_LENGTH 16
  71. static const unsigned char DEFAULT_MODEM_OPTION[MODEM_OPTION_LENGTH] = {
  72. 0x10, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  73. };
  74. static unsigned int BMaxDSL = DEFAULT_B_MAX_DSL;
  75. static unsigned char ModemMode = DEFAULT_MODEM_MODE;
  76. static unsigned char ModemOption[MODEM_OPTION_LENGTH];
  77. static unsigned int num_ModemOption;
  78. module_param(altsetting, uint, S_IRUGO | S_IWUSR);
  79. MODULE_PARM_DESC(altsetting,
  80. "Alternative setting for data interface (bulk_default: "
  81. __MODULE_STRING(DEFAULT_BULK_ALTSETTING) "; isoc_default: "
  82. __MODULE_STRING(DEFAULT_ISOC_ALTSETTING) ")");
  83. module_param(dl_512_first, bool, S_IRUGO | S_IWUSR);
  84. MODULE_PARM_DESC(dl_512_first,
  85. "Read 512 bytes before sending firmware (default: "
  86. __MODULE_STRING(DEFAULT_DL_512_FIRST) ")");
  87. module_param(enable_isoc, bool, S_IRUGO | S_IWUSR);
  88. MODULE_PARM_DESC(enable_isoc,
  89. "Use isochronous transfers if available (default: "
  90. __MODULE_STRING(DEFAULT_ENABLE_ISOC) ")");
  91. module_param(sw_buffering, bool, S_IRUGO | S_IWUSR);
  92. MODULE_PARM_DESC(sw_buffering,
  93. "Enable software buffering (default: "
  94. __MODULE_STRING(DEFAULT_SW_BUFFERING) ")");
  95. module_param(BMaxDSL, uint, S_IRUGO | S_IWUSR);
  96. MODULE_PARM_DESC(BMaxDSL,
  97. "default: " __MODULE_STRING(DEFAULT_B_MAX_DSL));
  98. module_param(ModemMode, byte, S_IRUGO | S_IWUSR);
  99. MODULE_PARM_DESC(ModemMode,
  100. "default: " __MODULE_STRING(DEFAULT_MODEM_MODE));
  101. module_param_array(ModemOption, byte, &num_ModemOption, S_IRUGO);
  102. MODULE_PARM_DESC(ModemOption, "default: 0x10,0x00,0x00,0x00,0x20");
  103. #define INTERFACE_DATA 1
  104. #define ENDPOINT_INT 0x81
  105. #define ENDPOINT_BULK_DATA 0x07
  106. #define ENDPOINT_ISOC_DATA 0x07
  107. #define ENDPOINT_FIRMWARE 0x05
  108. struct speedtch_params {
  109. unsigned int altsetting;
  110. unsigned int BMaxDSL;
  111. unsigned char ModemMode;
  112. unsigned char ModemOption[MODEM_OPTION_LENGTH];
  113. };
  114. struct speedtch_instance_data {
  115. struct usbatm_data *usbatm;
  116. struct speedtch_params params; /* set in probe, constant afterwards */
  117. struct timer_list status_check_timer;
  118. struct work_struct status_check_work;
  119. unsigned char last_status;
  120. int poll_delay; /* milliseconds */
  121. struct timer_list resubmit_timer;
  122. struct urb *int_urb;
  123. unsigned char int_data[16];
  124. unsigned char scratch_buffer[16];
  125. };
  126. /***************
  127. ** firmware **
  128. ***************/
  129. static void speedtch_set_swbuff(struct speedtch_instance_data *instance, int state)
  130. {
  131. struct usbatm_data *usbatm = instance->usbatm;
  132. struct usb_device *usb_dev = usbatm->usb_dev;
  133. int ret;
  134. ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  135. 0x32, 0x40, state ? 0x01 : 0x00, 0x00, NULL, 0, CTRL_TIMEOUT);
  136. if (ret < 0)
  137. usb_warn(usbatm,
  138. "%sabling SW buffering: usb_control_msg returned %d\n",
  139. state ? "En" : "Dis", ret);
  140. else
  141. dbg("speedtch_set_swbuff: %sbled SW buffering", state ? "En" : "Dis");
  142. }
  143. static void speedtch_test_sequence(struct speedtch_instance_data *instance)
  144. {
  145. struct usbatm_data *usbatm = instance->usbatm;
  146. struct usb_device *usb_dev = usbatm->usb_dev;
  147. unsigned char *buf = instance->scratch_buffer;
  148. int ret;
  149. /* URB 147 */
  150. buf[0] = 0x1c;
  151. buf[1] = 0x50;
  152. ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  153. 0x01, 0x40, 0x0b, 0x00, buf, 2, CTRL_TIMEOUT);
  154. if (ret < 0)
  155. usb_warn(usbatm, "%s failed on URB147: %d\n", __func__, ret);
  156. /* URB 148 */
  157. buf[0] = 0x32;
  158. buf[1] = 0x00;
  159. ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  160. 0x01, 0x40, 0x02, 0x00, buf, 2, CTRL_TIMEOUT);
  161. if (ret < 0)
  162. usb_warn(usbatm, "%s failed on URB148: %d\n", __func__, ret);
  163. /* URB 149 */
  164. buf[0] = 0x01;
  165. buf[1] = 0x00;
  166. buf[2] = 0x01;
  167. ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  168. 0x01, 0x40, 0x03, 0x00, buf, 3, CTRL_TIMEOUT);
  169. if (ret < 0)
  170. usb_warn(usbatm, "%s failed on URB149: %d\n", __func__, ret);
  171. /* URB 150 */
  172. buf[0] = 0x01;
  173. buf[1] = 0x00;
  174. buf[2] = 0x01;
  175. ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  176. 0x01, 0x40, 0x04, 0x00, buf, 3, CTRL_TIMEOUT);
  177. if (ret < 0)
  178. usb_warn(usbatm, "%s failed on URB150: %d\n", __func__, ret);
  179. /* Extra initialisation in recent drivers - gives higher speeds */
  180. /* URBext1 */
  181. buf[0] = instance->params.ModemMode;
  182. ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  183. 0x01, 0x40, 0x11, 0x00, buf, 1, CTRL_TIMEOUT);
  184. if (ret < 0)
  185. usb_warn(usbatm, "%s failed on URBext1: %d\n", __func__, ret);
  186. /* URBext2 */
  187. /* This seems to be the one which actually triggers the higher sync
  188. rate -- it does require the new firmware too, although it works OK
  189. with older firmware */
  190. ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  191. 0x01, 0x40, 0x14, 0x00,
  192. instance->params.ModemOption,
  193. MODEM_OPTION_LENGTH, CTRL_TIMEOUT);
  194. if (ret < 0)
  195. usb_warn(usbatm, "%s failed on URBext2: %d\n", __func__, ret);
  196. /* URBext3 */
  197. buf[0] = instance->params.BMaxDSL & 0xff;
  198. buf[1] = instance->params.BMaxDSL >> 8;
  199. ret = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  200. 0x01, 0x40, 0x12, 0x00, buf, 2, CTRL_TIMEOUT);
  201. if (ret < 0)
  202. usb_warn(usbatm, "%s failed on URBext3: %d\n", __func__, ret);
  203. }
  204. static int speedtch_upload_firmware(struct speedtch_instance_data *instance,
  205. const struct firmware *fw1,
  206. const struct firmware *fw2)
  207. {
  208. unsigned char *buffer;
  209. struct usbatm_data *usbatm = instance->usbatm;
  210. struct usb_device *usb_dev = usbatm->usb_dev;
  211. int actual_length;
  212. int ret = 0;
  213. int offset;
  214. usb_dbg(usbatm, "%s entered\n", __func__);
  215. if (!(buffer = (unsigned char *)__get_free_page(GFP_KERNEL))) {
  216. ret = -ENOMEM;
  217. usb_dbg(usbatm, "%s: no memory for buffer!\n", __func__);
  218. goto out;
  219. }
  220. if (!usb_ifnum_to_if(usb_dev, 2)) {
  221. ret = -ENODEV;
  222. usb_dbg(usbatm, "%s: interface not found!\n", __func__);
  223. goto out_free;
  224. }
  225. /* URB 7 */
  226. if (dl_512_first) { /* some modems need a read before writing the firmware */
  227. ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
  228. buffer, 0x200, &actual_length, 2000);
  229. if (ret < 0 && ret != -ETIMEDOUT)
  230. usb_warn(usbatm, "%s: read BLOCK0 from modem failed (%d)!\n", __func__, ret);
  231. else
  232. usb_dbg(usbatm, "%s: BLOCK0 downloaded (%d bytes)\n", __func__, ret);
  233. }
  234. /* URB 8 : both leds are static green */
  235. for (offset = 0; offset < fw1->size; offset += PAGE_SIZE) {
  236. int thislen = min_t(int, PAGE_SIZE, fw1->size - offset);
  237. memcpy(buffer, fw1->data + offset, thislen);
  238. ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
  239. buffer, thislen, &actual_length, DATA_TIMEOUT);
  240. if (ret < 0) {
  241. usb_err(usbatm, "%s: write BLOCK1 to modem failed (%d)!\n", __func__, ret);
  242. goto out_free;
  243. }
  244. usb_dbg(usbatm, "%s: BLOCK1 uploaded (%zu bytes)\n", __func__, fw1->size);
  245. }
  246. /* USB led blinking green, ADSL led off */
  247. /* URB 11 */
  248. ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
  249. buffer, 0x200, &actual_length, DATA_TIMEOUT);
  250. if (ret < 0) {
  251. usb_err(usbatm, "%s: read BLOCK2 from modem failed (%d)!\n", __func__, ret);
  252. goto out_free;
  253. }
  254. usb_dbg(usbatm, "%s: BLOCK2 downloaded (%d bytes)\n", __func__, actual_length);
  255. /* URBs 12 to 139 - USB led blinking green, ADSL led off */
  256. for (offset = 0; offset < fw2->size; offset += PAGE_SIZE) {
  257. int thislen = min_t(int, PAGE_SIZE, fw2->size - offset);
  258. memcpy(buffer, fw2->data + offset, thislen);
  259. ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
  260. buffer, thislen, &actual_length, DATA_TIMEOUT);
  261. if (ret < 0) {
  262. usb_err(usbatm, "%s: write BLOCK3 to modem failed (%d)!\n", __func__, ret);
  263. goto out_free;
  264. }
  265. }
  266. usb_dbg(usbatm, "%s: BLOCK3 uploaded (%zu bytes)\n", __func__, fw2->size);
  267. /* USB led static green, ADSL led static red */
  268. /* URB 142 */
  269. ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, ENDPOINT_FIRMWARE),
  270. buffer, 0x200, &actual_length, DATA_TIMEOUT);
  271. if (ret < 0) {
  272. usb_err(usbatm, "%s: read BLOCK4 from modem failed (%d)!\n", __func__, ret);
  273. goto out_free;
  274. }
  275. /* success */
  276. usb_dbg(usbatm, "%s: BLOCK4 downloaded (%d bytes)\n", __func__, actual_length);
  277. /* Delay to allow firmware to start up. We can do this here
  278. because we're in our own kernel thread anyway. */
  279. msleep_interruptible(1000);
  280. if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, instance->params.altsetting)) < 0) {
  281. usb_err(usbatm, "%s: setting interface to %d failed (%d)!\n", __func__, instance->params.altsetting, ret);
  282. goto out_free;
  283. }
  284. /* Enable software buffering, if requested */
  285. if (sw_buffering)
  286. speedtch_set_swbuff(instance, 1);
  287. /* Magic spell; don't ask us what this does */
  288. speedtch_test_sequence(instance);
  289. ret = 0;
  290. out_free:
  291. free_page((unsigned long)buffer);
  292. out:
  293. return ret;
  294. }
  295. static int speedtch_find_firmware(struct usbatm_data *usbatm, struct usb_interface *intf,
  296. int phase, const struct firmware **fw_p)
  297. {
  298. struct device *dev = &intf->dev;
  299. const u16 bcdDevice = le16_to_cpu(interface_to_usbdev(intf)->descriptor.bcdDevice);
  300. const u8 major_revision = bcdDevice >> 8;
  301. const u8 minor_revision = bcdDevice & 0xff;
  302. char buf[24];
  303. sprintf(buf, "speedtch-%d.bin.%x.%02x", phase, major_revision, minor_revision);
  304. usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
  305. if (request_firmware(fw_p, buf, dev)) {
  306. sprintf(buf, "speedtch-%d.bin.%x", phase, major_revision);
  307. usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
  308. if (request_firmware(fw_p, buf, dev)) {
  309. sprintf(buf, "speedtch-%d.bin", phase);
  310. usb_dbg(usbatm, "%s: looking for %s\n", __func__, buf);
  311. if (request_firmware(fw_p, buf, dev)) {
  312. usb_err(usbatm, "%s: no stage %d firmware found!\n", __func__, phase);
  313. return -ENOENT;
  314. }
  315. }
  316. }
  317. usb_info(usbatm, "found stage %d firmware %s\n", phase, buf);
  318. return 0;
  319. }
  320. static int speedtch_heavy_init(struct usbatm_data *usbatm, struct usb_interface *intf)
  321. {
  322. const struct firmware *fw1, *fw2;
  323. struct speedtch_instance_data *instance = usbatm->driver_data;
  324. int ret;
  325. if ((ret = speedtch_find_firmware(usbatm, intf, 1, &fw1)) < 0)
  326. return ret;
  327. if ((ret = speedtch_find_firmware(usbatm, intf, 2, &fw2)) < 0) {
  328. release_firmware(fw1);
  329. return ret;
  330. }
  331. if ((ret = speedtch_upload_firmware(instance, fw1, fw2)) < 0)
  332. usb_err(usbatm, "%s: firmware upload failed (%d)!\n", __func__, ret);
  333. release_firmware(fw2);
  334. release_firmware(fw1);
  335. return ret;
  336. }
  337. /**********
  338. ** ATM **
  339. **********/
  340. static int speedtch_read_status(struct speedtch_instance_data *instance)
  341. {
  342. struct usbatm_data *usbatm = instance->usbatm;
  343. struct usb_device *usb_dev = usbatm->usb_dev;
  344. unsigned char *buf = instance->scratch_buffer;
  345. int ret;
  346. memset(buf, 0, 16);
  347. ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  348. 0x12, 0xc0, 0x07, 0x00, buf + OFFSET_7, SIZE_7,
  349. CTRL_TIMEOUT);
  350. if (ret < 0) {
  351. atm_dbg(usbatm, "%s: MSG 7 failed\n", __func__);
  352. return ret;
  353. }
  354. ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  355. 0x12, 0xc0, 0x0b, 0x00, buf + OFFSET_b, SIZE_b,
  356. CTRL_TIMEOUT);
  357. if (ret < 0) {
  358. atm_dbg(usbatm, "%s: MSG B failed\n", __func__);
  359. return ret;
  360. }
  361. ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  362. 0x12, 0xc0, 0x0d, 0x00, buf + OFFSET_d, SIZE_d,
  363. CTRL_TIMEOUT);
  364. if (ret < 0) {
  365. atm_dbg(usbatm, "%s: MSG D failed\n", __func__);
  366. return ret;
  367. }
  368. ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  369. 0x01, 0xc0, 0x0e, 0x00, buf + OFFSET_e, SIZE_e,
  370. CTRL_TIMEOUT);
  371. if (ret < 0) {
  372. atm_dbg(usbatm, "%s: MSG E failed\n", __func__);
  373. return ret;
  374. }
  375. ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  376. 0x01, 0xc0, 0x0f, 0x00, buf + OFFSET_f, SIZE_f,
  377. CTRL_TIMEOUT);
  378. if (ret < 0) {
  379. atm_dbg(usbatm, "%s: MSG F failed\n", __func__);
  380. return ret;
  381. }
  382. return 0;
  383. }
  384. static int speedtch_start_synchro(struct speedtch_instance_data *instance)
  385. {
  386. struct usbatm_data *usbatm = instance->usbatm;
  387. struct usb_device *usb_dev = usbatm->usb_dev;
  388. unsigned char *buf = instance->scratch_buffer;
  389. int ret;
  390. atm_dbg(usbatm, "%s entered\n", __func__);
  391. memset(buf, 0, 2);
  392. ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  393. 0x12, 0xc0, 0x04, 0x00,
  394. buf, 2, CTRL_TIMEOUT);
  395. if (ret < 0)
  396. atm_warn(usbatm, "failed to start ADSL synchronisation: %d\n", ret);
  397. else
  398. atm_dbg(usbatm, "%s: modem prodded. %d bytes returned: %02x %02x\n",
  399. __func__, ret, buf[0], buf[1]);
  400. return ret;
  401. }
  402. static void speedtch_check_status(struct work_struct *work)
  403. {
  404. struct speedtch_instance_data *instance =
  405. container_of(work, struct speedtch_instance_data,
  406. status_check_work);
  407. struct usbatm_data *usbatm = instance->usbatm;
  408. struct atm_dev *atm_dev = usbatm->atm_dev;
  409. unsigned char *buf = instance->scratch_buffer;
  410. int down_speed, up_speed, ret;
  411. unsigned char status;
  412. #ifdef VERBOSE_DEBUG
  413. atm_dbg(usbatm, "%s entered\n", __func__);
  414. #endif
  415. ret = speedtch_read_status(instance);
  416. if (ret < 0) {
  417. atm_warn(usbatm, "error %d fetching device status\n", ret);
  418. instance->poll_delay = min(2 * instance->poll_delay, MAX_POLL_DELAY);
  419. return;
  420. }
  421. instance->poll_delay = max(instance->poll_delay / 2, MIN_POLL_DELAY);
  422. status = buf[OFFSET_7];
  423. if ((status != instance->last_status) || !status) {
  424. atm_dbg(usbatm, "%s: line state 0x%02x\n", __func__, status);
  425. switch (status) {
  426. case 0:
  427. atm_dev_signal_change(atm_dev, ATM_PHY_SIG_LOST);
  428. if (instance->last_status)
  429. atm_info(usbatm, "ADSL line is down\n");
  430. /* It may never resync again unless we ask it to... */
  431. ret = speedtch_start_synchro(instance);
  432. break;
  433. case 0x08:
  434. atm_dev_signal_change(atm_dev, ATM_PHY_SIG_UNKNOWN);
  435. atm_info(usbatm, "ADSL line is blocked?\n");
  436. break;
  437. case 0x10:
  438. atm_dev_signal_change(atm_dev, ATM_PHY_SIG_LOST);
  439. atm_info(usbatm, "ADSL line is synchronising\n");
  440. break;
  441. case 0x20:
  442. down_speed = buf[OFFSET_b] | (buf[OFFSET_b + 1] << 8)
  443. | (buf[OFFSET_b + 2] << 16) | (buf[OFFSET_b + 3] << 24);
  444. up_speed = buf[OFFSET_b + 4] | (buf[OFFSET_b + 5] << 8)
  445. | (buf[OFFSET_b + 6] << 16) | (buf[OFFSET_b + 7] << 24);
  446. if (!(down_speed & 0x0000ffff) && !(up_speed & 0x0000ffff)) {
  447. down_speed >>= 16;
  448. up_speed >>= 16;
  449. }
  450. atm_dev->link_rate = down_speed * 1000 / 424;
  451. atm_dev_signal_change(atm_dev, ATM_PHY_SIG_FOUND);
  452. atm_info(usbatm,
  453. "ADSL line is up (%d kb/s down | %d kb/s up)\n",
  454. down_speed, up_speed);
  455. break;
  456. default:
  457. atm_dev_signal_change(atm_dev, ATM_PHY_SIG_UNKNOWN);
  458. atm_info(usbatm, "unknown line state %02x\n", status);
  459. break;
  460. }
  461. instance->last_status = status;
  462. }
  463. }
  464. static void speedtch_status_poll(unsigned long data)
  465. {
  466. struct speedtch_instance_data *instance = (void *)data;
  467. schedule_work(&instance->status_check_work);
  468. /* The following check is racy, but the race is harmless */
  469. if (instance->poll_delay < MAX_POLL_DELAY)
  470. mod_timer(&instance->status_check_timer, jiffies + msecs_to_jiffies(instance->poll_delay));
  471. else
  472. atm_warn(instance->usbatm, "Too many failures - disabling line status polling\n");
  473. }
  474. static void speedtch_resubmit_int(unsigned long data)
  475. {
  476. struct speedtch_instance_data *instance = (void *)data;
  477. struct urb *int_urb = instance->int_urb;
  478. int ret;
  479. atm_dbg(instance->usbatm, "%s entered\n", __func__);
  480. if (int_urb) {
  481. ret = usb_submit_urb(int_urb, GFP_ATOMIC);
  482. if (!ret)
  483. schedule_work(&instance->status_check_work);
  484. else {
  485. atm_dbg(instance->usbatm, "%s: usb_submit_urb failed with result %d\n", __func__, ret);
  486. mod_timer(&instance->resubmit_timer, jiffies + msecs_to_jiffies(RESUBMIT_DELAY));
  487. }
  488. }
  489. }
  490. static void speedtch_handle_int(struct urb *int_urb)
  491. {
  492. struct speedtch_instance_data *instance = int_urb->context;
  493. struct usbatm_data *usbatm = instance->usbatm;
  494. unsigned int count = int_urb->actual_length;
  495. int status = int_urb->status;
  496. int ret;
  497. /* The magic interrupt for "up state" */
  498. static const unsigned char up_int[6] = { 0xa1, 0x00, 0x01, 0x00, 0x00, 0x00 };
  499. /* The magic interrupt for "down state" */
  500. static const unsigned char down_int[6] = { 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00 };
  501. atm_dbg(usbatm, "%s entered\n", __func__);
  502. if (status < 0) {
  503. atm_dbg(usbatm, "%s: nonzero urb status %d!\n", __func__, status);
  504. goto fail;
  505. }
  506. if ((count == 6) && !memcmp(up_int, instance->int_data, 6)) {
  507. del_timer(&instance->status_check_timer);
  508. atm_info(usbatm, "DSL line goes up\n");
  509. } else if ((count == 6) && !memcmp(down_int, instance->int_data, 6)) {
  510. atm_info(usbatm, "DSL line goes down\n");
  511. } else {
  512. int i;
  513. atm_dbg(usbatm, "%s: unknown interrupt packet of length %d:", __func__, count);
  514. for (i = 0; i < count; i++)
  515. printk(" %02x", instance->int_data[i]);
  516. printk("\n");
  517. goto fail;
  518. }
  519. if ((int_urb = instance->int_urb)) {
  520. ret = usb_submit_urb(int_urb, GFP_ATOMIC);
  521. schedule_work(&instance->status_check_work);
  522. if (ret < 0) {
  523. atm_dbg(usbatm, "%s: usb_submit_urb failed with result %d\n", __func__, ret);
  524. goto fail;
  525. }
  526. }
  527. return;
  528. fail:
  529. if ((int_urb = instance->int_urb))
  530. mod_timer(&instance->resubmit_timer, jiffies + msecs_to_jiffies(RESUBMIT_DELAY));
  531. }
  532. static int speedtch_atm_start(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
  533. {
  534. struct usb_device *usb_dev = usbatm->usb_dev;
  535. struct speedtch_instance_data *instance = usbatm->driver_data;
  536. int i, ret;
  537. unsigned char mac_str[13];
  538. atm_dbg(usbatm, "%s entered\n", __func__);
  539. /* Set MAC address, it is stored in the serial number */
  540. memset(atm_dev->esi, 0, sizeof(atm_dev->esi));
  541. if (usb_string(usb_dev, usb_dev->descriptor.iSerialNumber, mac_str, sizeof(mac_str)) == 12) {
  542. for (i = 0; i < 6; i++)
  543. atm_dev->esi[i] = (hex_to_bin(mac_str[i * 2]) << 4) +
  544. hex_to_bin(mac_str[i * 2 + 1]);
  545. }
  546. /* Start modem synchronisation */
  547. ret = speedtch_start_synchro(instance);
  548. /* Set up interrupt endpoint */
  549. if (instance->int_urb) {
  550. ret = usb_submit_urb(instance->int_urb, GFP_KERNEL);
  551. if (ret < 0) {
  552. /* Doesn't matter; we'll poll anyway */
  553. atm_dbg(usbatm, "%s: submission of interrupt URB failed (%d)!\n", __func__, ret);
  554. usb_free_urb(instance->int_urb);
  555. instance->int_urb = NULL;
  556. }
  557. }
  558. /* Start status polling */
  559. mod_timer(&instance->status_check_timer, jiffies + msecs_to_jiffies(1000));
  560. return 0;
  561. }
  562. static void speedtch_atm_stop(struct usbatm_data *usbatm, struct atm_dev *atm_dev)
  563. {
  564. struct speedtch_instance_data *instance = usbatm->driver_data;
  565. struct urb *int_urb = instance->int_urb;
  566. atm_dbg(usbatm, "%s entered\n", __func__);
  567. del_timer_sync(&instance->status_check_timer);
  568. /*
  569. * Since resubmit_timer and int_urb can schedule themselves and
  570. * each other, shutting them down correctly takes some care
  571. */
  572. instance->int_urb = NULL; /* signal shutdown */
  573. mb();
  574. usb_kill_urb(int_urb);
  575. del_timer_sync(&instance->resubmit_timer);
  576. /*
  577. * At this point, speedtch_handle_int and speedtch_resubmit_int
  578. * can run or be running, but instance->int_urb == NULL means that
  579. * they will not reschedule
  580. */
  581. usb_kill_urb(int_urb);
  582. del_timer_sync(&instance->resubmit_timer);
  583. usb_free_urb(int_urb);
  584. flush_work_sync(&instance->status_check_work);
  585. }
  586. static int speedtch_pre_reset(struct usb_interface *intf)
  587. {
  588. return 0;
  589. }
  590. static int speedtch_post_reset(struct usb_interface *intf)
  591. {
  592. return 0;
  593. }
  594. /**********
  595. ** USB **
  596. **********/
  597. static struct usb_device_id speedtch_usb_ids[] = {
  598. {USB_DEVICE(0x06b9, 0x4061)},
  599. {}
  600. };
  601. MODULE_DEVICE_TABLE(usb, speedtch_usb_ids);
  602. static int speedtch_usb_probe(struct usb_interface *, const struct usb_device_id *);
  603. static struct usb_driver speedtch_usb_driver = {
  604. .name = speedtch_driver_name,
  605. .probe = speedtch_usb_probe,
  606. .disconnect = usbatm_usb_disconnect,
  607. .pre_reset = speedtch_pre_reset,
  608. .post_reset = speedtch_post_reset,
  609. .id_table = speedtch_usb_ids
  610. };
  611. static void speedtch_release_interfaces(struct usb_device *usb_dev,
  612. int num_interfaces)
  613. {
  614. struct usb_interface *cur_intf;
  615. int i;
  616. for (i = 0; i < num_interfaces; i++)
  617. if ((cur_intf = usb_ifnum_to_if(usb_dev, i))) {
  618. usb_set_intfdata(cur_intf, NULL);
  619. usb_driver_release_interface(&speedtch_usb_driver, cur_intf);
  620. }
  621. }
  622. static int speedtch_bind(struct usbatm_data *usbatm,
  623. struct usb_interface *intf,
  624. const struct usb_device_id *id)
  625. {
  626. struct usb_device *usb_dev = interface_to_usbdev(intf);
  627. struct usb_interface *cur_intf, *data_intf;
  628. struct speedtch_instance_data *instance;
  629. int ifnum = intf->altsetting->desc.bInterfaceNumber;
  630. int num_interfaces = usb_dev->actconfig->desc.bNumInterfaces;
  631. int i, ret;
  632. int use_isoc;
  633. usb_dbg(usbatm, "%s entered\n", __func__);
  634. /* sanity checks */
  635. if (usb_dev->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) {
  636. usb_err(usbatm, "%s: wrong device class %d\n", __func__, usb_dev->descriptor.bDeviceClass);
  637. return -ENODEV;
  638. }
  639. if (!(data_intf = usb_ifnum_to_if(usb_dev, INTERFACE_DATA))) {
  640. usb_err(usbatm, "%s: data interface not found!\n", __func__);
  641. return -ENODEV;
  642. }
  643. /* claim all interfaces */
  644. for (i = 0; i < num_interfaces; i++) {
  645. cur_intf = usb_ifnum_to_if(usb_dev, i);
  646. if ((i != ifnum) && cur_intf) {
  647. ret = usb_driver_claim_interface(&speedtch_usb_driver, cur_intf, usbatm);
  648. if (ret < 0) {
  649. usb_err(usbatm, "%s: failed to claim interface %2d (%d)!\n", __func__, i, ret);
  650. speedtch_release_interfaces(usb_dev, i);
  651. return ret;
  652. }
  653. }
  654. }
  655. instance = kzalloc(sizeof(*instance), GFP_KERNEL);
  656. if (!instance) {
  657. usb_err(usbatm, "%s: no memory for instance data!\n", __func__);
  658. ret = -ENOMEM;
  659. goto fail_release;
  660. }
  661. instance->usbatm = usbatm;
  662. /* module parameters may change at any moment, so take a snapshot */
  663. instance->params.altsetting = altsetting;
  664. instance->params.BMaxDSL = BMaxDSL;
  665. instance->params.ModemMode = ModemMode;
  666. memcpy(instance->params.ModemOption, DEFAULT_MODEM_OPTION, MODEM_OPTION_LENGTH);
  667. memcpy(instance->params.ModemOption, ModemOption, num_ModemOption);
  668. use_isoc = enable_isoc;
  669. if (instance->params.altsetting)
  670. if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, instance->params.altsetting)) < 0) {
  671. usb_err(usbatm, "%s: setting interface to %2d failed (%d)!\n", __func__, instance->params.altsetting, ret);
  672. instance->params.altsetting = 0; /* fall back to default */
  673. }
  674. if (!instance->params.altsetting && use_isoc)
  675. if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, DEFAULT_ISOC_ALTSETTING)) < 0) {
  676. usb_dbg(usbatm, "%s: setting interface to %2d failed (%d)!\n", __func__, DEFAULT_ISOC_ALTSETTING, ret);
  677. use_isoc = 0; /* fall back to bulk */
  678. }
  679. if (use_isoc) {
  680. const struct usb_host_interface *desc = data_intf->cur_altsetting;
  681. const __u8 target_address = USB_DIR_IN | usbatm->driver->isoc_in;
  682. use_isoc = 0; /* fall back to bulk if endpoint not found */
  683. for (i = 0; i < desc->desc.bNumEndpoints; i++) {
  684. const struct usb_endpoint_descriptor *endpoint_desc = &desc->endpoint[i].desc;
  685. if ((endpoint_desc->bEndpointAddress == target_address)) {
  686. use_isoc =
  687. usb_endpoint_xfer_isoc(endpoint_desc);
  688. break;
  689. }
  690. }
  691. if (!use_isoc)
  692. usb_info(usbatm, "isochronous transfer not supported - using bulk\n");
  693. }
  694. if (!use_isoc && !instance->params.altsetting)
  695. if ((ret = usb_set_interface(usb_dev, INTERFACE_DATA, DEFAULT_BULK_ALTSETTING)) < 0) {
  696. usb_err(usbatm, "%s: setting interface to %2d failed (%d)!\n", __func__, DEFAULT_BULK_ALTSETTING, ret);
  697. goto fail_free;
  698. }
  699. if (!instance->params.altsetting)
  700. instance->params.altsetting = use_isoc ? DEFAULT_ISOC_ALTSETTING : DEFAULT_BULK_ALTSETTING;
  701. usbatm->flags |= (use_isoc ? UDSL_USE_ISOC : 0);
  702. INIT_WORK(&instance->status_check_work, speedtch_check_status);
  703. init_timer(&instance->status_check_timer);
  704. instance->status_check_timer.function = speedtch_status_poll;
  705. instance->status_check_timer.data = (unsigned long)instance;
  706. instance->last_status = 0xff;
  707. instance->poll_delay = MIN_POLL_DELAY;
  708. init_timer(&instance->resubmit_timer);
  709. instance->resubmit_timer.function = speedtch_resubmit_int;
  710. instance->resubmit_timer.data = (unsigned long)instance;
  711. instance->int_urb = usb_alloc_urb(0, GFP_KERNEL);
  712. if (instance->int_urb)
  713. usb_fill_int_urb(instance->int_urb, usb_dev,
  714. usb_rcvintpipe(usb_dev, ENDPOINT_INT),
  715. instance->int_data, sizeof(instance->int_data),
  716. speedtch_handle_int, instance, 50);
  717. else
  718. usb_dbg(usbatm, "%s: no memory for interrupt urb!\n", __func__);
  719. /* check whether the modem already seems to be alive */
  720. ret = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  721. 0x12, 0xc0, 0x07, 0x00,
  722. instance->scratch_buffer + OFFSET_7, SIZE_7, 500);
  723. usbatm->flags |= (ret == SIZE_7 ? UDSL_SKIP_HEAVY_INIT : 0);
  724. usb_dbg(usbatm, "%s: firmware %s loaded\n", __func__, usbatm->flags & UDSL_SKIP_HEAVY_INIT ? "already" : "not");
  725. if (!(usbatm->flags & UDSL_SKIP_HEAVY_INIT))
  726. if ((ret = usb_reset_device(usb_dev)) < 0) {
  727. usb_err(usbatm, "%s: device reset failed (%d)!\n", __func__, ret);
  728. goto fail_free;
  729. }
  730. usbatm->driver_data = instance;
  731. return 0;
  732. fail_free:
  733. usb_free_urb(instance->int_urb);
  734. kfree(instance);
  735. fail_release:
  736. speedtch_release_interfaces(usb_dev, num_interfaces);
  737. return ret;
  738. }
  739. static void speedtch_unbind(struct usbatm_data *usbatm, struct usb_interface *intf)
  740. {
  741. struct usb_device *usb_dev = interface_to_usbdev(intf);
  742. struct speedtch_instance_data *instance = usbatm->driver_data;
  743. usb_dbg(usbatm, "%s entered\n", __func__);
  744. speedtch_release_interfaces(usb_dev, usb_dev->actconfig->desc.bNumInterfaces);
  745. usb_free_urb(instance->int_urb);
  746. kfree(instance);
  747. }
  748. /***********
  749. ** init **
  750. ***********/
  751. static struct usbatm_driver speedtch_usbatm_driver = {
  752. .driver_name = speedtch_driver_name,
  753. .bind = speedtch_bind,
  754. .heavy_init = speedtch_heavy_init,
  755. .unbind = speedtch_unbind,
  756. .atm_start = speedtch_atm_start,
  757. .atm_stop = speedtch_atm_stop,
  758. .bulk_in = ENDPOINT_BULK_DATA,
  759. .bulk_out = ENDPOINT_BULK_DATA,
  760. .isoc_in = ENDPOINT_ISOC_DATA
  761. };
  762. static int speedtch_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
  763. {
  764. return usbatm_usb_probe(intf, id, &speedtch_usbatm_driver);
  765. }
  766. static int __init speedtch_usb_init(void)
  767. {
  768. dbg("%s: driver version %s", __func__, DRIVER_VERSION);
  769. return usb_register(&speedtch_usb_driver);
  770. }
  771. static void __exit speedtch_usb_cleanup(void)
  772. {
  773. dbg("%s", __func__);
  774. usb_deregister(&speedtch_usb_driver);
  775. }
  776. module_init(speedtch_usb_init);
  777. module_exit(speedtch_usb_cleanup);
  778. MODULE_AUTHOR(DRIVER_AUTHOR);
  779. MODULE_DESCRIPTION(DRIVER_DESC);
  780. MODULE_LICENSE("GPL");
  781. MODULE_VERSION(DRIVER_VERSION);