speedtch.c 28 KB

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