speedtch.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  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. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program; if not, write to the Free Software Foundation, Inc., 59
  20. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. *
  22. ******************************************************************************/
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/gfp.h>
  26. #include <linux/kernel.h>
  27. #include <linux/sched.h>
  28. #include <linux/timer.h>
  29. #include <linux/errno.h>
  30. #include <linux/proc_fs.h>
  31. #include <linux/slab.h>
  32. #include <linux/wait.h>
  33. #include <linux/list.h>
  34. #include <asm/processor.h>
  35. #include <asm/uaccess.h>
  36. #include <linux/smp_lock.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/atm.h>
  39. #include <linux/atmdev.h>
  40. #include <linux/crc32.h>
  41. #include <linux/init.h>
  42. #include <linux/firmware.h>
  43. #include "usb_atm.h"
  44. #if defined(CONFIG_FW_LOADER) || defined(CONFIG_FW_LOADER_MODULE)
  45. # define USE_FW_LOADER
  46. #endif
  47. #define DRIVER_AUTHOR "Johan Verrept, Duncan Sands <duncan.sands@free.fr>"
  48. #define DRIVER_VERSION "1.8"
  49. #define DRIVER_DESC "Alcatel SpeedTouch USB driver version " DRIVER_VERSION
  50. static const char speedtch_driver_name[] = "speedtch";
  51. #define SPEEDTOUCH_VENDORID 0x06b9
  52. #define SPEEDTOUCH_PRODUCTID 0x4061
  53. /* Timeout in jiffies */
  54. #define CTRL_TIMEOUT 2000
  55. #define DATA_TIMEOUT 2000
  56. #define OFFSET_7 0 /* size 1 */
  57. #define OFFSET_b 1 /* size 8 */
  58. #define OFFSET_d 9 /* size 4 */
  59. #define OFFSET_e 13 /* size 1 */
  60. #define OFFSET_f 14 /* size 1 */
  61. #define TOTAL 15
  62. #define SIZE_7 1
  63. #define SIZE_b 8
  64. #define SIZE_d 4
  65. #define SIZE_e 1
  66. #define SIZE_f 1
  67. static int dl_512_first = 0;
  68. static int sw_buffering = 0;
  69. module_param(dl_512_first, bool, 0444);
  70. MODULE_PARM_DESC(dl_512_first, "Read 512 bytes before sending firmware");
  71. module_param(sw_buffering, uint, 0444);
  72. MODULE_PARM_DESC(sw_buffering, "Enable software buffering");
  73. #define UDSL_IOCTL_LINE_UP 1
  74. #define UDSL_IOCTL_LINE_DOWN 2
  75. #define SPEEDTCH_ENDPOINT_INT 0x81
  76. #define SPEEDTCH_ENDPOINT_DATA 0x07
  77. #define SPEEDTCH_ENDPOINT_FIRMWARE 0x05
  78. #define hex2int(c) ( (c >= '0') && (c <= '9') ? (c - '0') : ((c & 0xf) + 9) )
  79. static struct usb_device_id speedtch_usb_ids[] = {
  80. {USB_DEVICE(SPEEDTOUCH_VENDORID, SPEEDTOUCH_PRODUCTID)},
  81. {}
  82. };
  83. MODULE_DEVICE_TABLE(usb, speedtch_usb_ids);
  84. struct speedtch_instance_data {
  85. struct udsl_instance_data u;
  86. /* Status */
  87. struct urb *int_urb;
  88. unsigned char int_data[16];
  89. struct work_struct poll_work;
  90. struct timer_list poll_timer;
  91. };
  92. /* USB */
  93. static int speedtch_usb_probe(struct usb_interface *intf,
  94. const struct usb_device_id *id);
  95. static void speedtch_usb_disconnect(struct usb_interface *intf);
  96. static int speedtch_usb_ioctl(struct usb_interface *intf, unsigned int code,
  97. void *user_data);
  98. static void speedtch_handle_int(struct urb *urb, struct pt_regs *regs);
  99. static void speedtch_poll_status(struct speedtch_instance_data *instance);
  100. static struct usb_driver speedtch_usb_driver = {
  101. .owner = THIS_MODULE,
  102. .name = speedtch_driver_name,
  103. .probe = speedtch_usb_probe,
  104. .disconnect = speedtch_usb_disconnect,
  105. .ioctl = speedtch_usb_ioctl,
  106. .id_table = speedtch_usb_ids,
  107. };
  108. /***************
  109. ** firmware **
  110. ***************/
  111. static void speedtch_got_firmware(struct speedtch_instance_data *instance,
  112. int got_it)
  113. {
  114. int err;
  115. struct usb_interface *intf;
  116. down(&instance->u.serialize); /* vs self, speedtch_firmware_start */
  117. if (instance->u.status == UDSL_LOADED_FIRMWARE)
  118. goto out;
  119. if (!got_it) {
  120. instance->u.status = UDSL_NO_FIRMWARE;
  121. goto out;
  122. }
  123. if ((err = usb_set_interface(instance->u.usb_dev, 1, 1)) < 0) {
  124. dbg("speedtch_got_firmware: usb_set_interface returned %d!", err);
  125. instance->u.status = UDSL_NO_FIRMWARE;
  126. goto out;
  127. }
  128. /* Set up interrupt endpoint */
  129. intf = usb_ifnum_to_if(instance->u.usb_dev, 0);
  130. if (intf && !usb_driver_claim_interface(&speedtch_usb_driver, intf, NULL)) {
  131. instance->int_urb = usb_alloc_urb(0, GFP_KERNEL);
  132. if (instance->int_urb) {
  133. usb_fill_int_urb(instance->int_urb, instance->u.usb_dev,
  134. usb_rcvintpipe(instance->u.usb_dev, SPEEDTCH_ENDPOINT_INT),
  135. instance->int_data,
  136. sizeof(instance->int_data),
  137. speedtch_handle_int, instance, 50);
  138. err = usb_submit_urb(instance->int_urb, GFP_KERNEL);
  139. if (err) {
  140. /* Doesn't matter; we'll poll anyway */
  141. dbg("speedtch_got_firmware: Submission of interrupt URB failed %d", err);
  142. usb_free_urb(instance->int_urb);
  143. instance->int_urb = NULL;
  144. usb_driver_release_interface(&speedtch_usb_driver, intf);
  145. }
  146. }
  147. }
  148. /* Start status polling */
  149. mod_timer(&instance->poll_timer, jiffies + (1 * HZ));
  150. instance->u.status = UDSL_LOADED_FIRMWARE;
  151. tasklet_schedule(&instance->u.receive_tasklet);
  152. out:
  153. up(&instance->u.serialize);
  154. wake_up_interruptible(&instance->u.firmware_waiters);
  155. }
  156. static int speedtch_set_swbuff(struct speedtch_instance_data *instance,
  157. int state)
  158. {
  159. struct usb_device *dev = instance->u.usb_dev;
  160. int ret;
  161. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  162. 0x32, 0x40, state ? 0x01 : 0x00,
  163. 0x00, NULL, 0, 100);
  164. if (ret < 0) {
  165. printk("Warning: %sabling SW buffering: usb_control_msg returned %d\n",
  166. state ? "En" : "Dis", ret);
  167. return ret;
  168. }
  169. dbg("speedtch_set_swbuff: %sbled SW buffering", state ? "En" : "Dis");
  170. return 0;
  171. }
  172. static void speedtch_test_sequence(struct speedtch_instance_data *instance)
  173. {
  174. struct usb_device *dev = instance->u.usb_dev;
  175. unsigned char buf[10];
  176. int ret;
  177. /* URB 147 */
  178. buf[0] = 0x1c;
  179. buf[1] = 0x50;
  180. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  181. 0x01, 0x40, 0x0b, 0x00, buf, 2, 100);
  182. if (ret < 0)
  183. printk(KERN_WARNING "%s failed on URB147: %d\n", __func__, ret);
  184. /* URB 148 */
  185. buf[0] = 0x32;
  186. buf[1] = 0x00;
  187. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  188. 0x01, 0x40, 0x02, 0x00, buf, 2, 100);
  189. if (ret < 0)
  190. printk(KERN_WARNING "%s failed on URB148: %d\n", __func__, ret);
  191. /* URB 149 */
  192. buf[0] = 0x01;
  193. buf[1] = 0x00;
  194. buf[2] = 0x01;
  195. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  196. 0x01, 0x40, 0x03, 0x00, buf, 3, 100);
  197. if (ret < 0)
  198. printk(KERN_WARNING "%s failed on URB149: %d\n", __func__, ret);
  199. /* URB 150 */
  200. buf[0] = 0x01;
  201. buf[1] = 0x00;
  202. buf[2] = 0x01;
  203. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  204. 0x01, 0x40, 0x04, 0x00, buf, 3, 100);
  205. if (ret < 0)
  206. printk(KERN_WARNING "%s failed on URB150: %d\n", __func__, ret);
  207. }
  208. static int speedtch_start_synchro(struct speedtch_instance_data *instance)
  209. {
  210. struct usb_device *dev = instance->u.usb_dev;
  211. unsigned char buf[2];
  212. int ret;
  213. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  214. 0x12, 0xc0, 0x04, 0x00,
  215. buf, sizeof(buf), CTRL_TIMEOUT);
  216. if (ret < 0) {
  217. printk(KERN_WARNING "SpeedTouch: Failed to start ADSL synchronisation: %d\n", ret);
  218. return ret;
  219. }
  220. dbg("speedtch_start_synchro: modem prodded. %d Bytes returned: %02x %02x", ret, buf[0], buf[1]);
  221. return 0;
  222. }
  223. static void speedtch_handle_int(struct urb *urb, struct pt_regs *regs)
  224. {
  225. struct speedtch_instance_data *instance = urb->context;
  226. unsigned int count = urb->actual_length;
  227. int ret;
  228. /* The magic interrupt for "up state" */
  229. const static unsigned char up_int[6] = { 0xa1, 0x00, 0x01, 0x00, 0x00, 0x00 };
  230. /* The magic interrupt for "down state" */
  231. const static unsigned char down_int[6] = { 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00 };
  232. switch (urb->status) {
  233. case 0:
  234. /* success */
  235. break;
  236. case -ECONNRESET:
  237. case -ENOENT:
  238. case -ESHUTDOWN:
  239. /* this urb is terminated; clean up */
  240. dbg("%s - urb shutting down with status: %d", __func__, urb->status);
  241. return;
  242. default:
  243. dbg("%s - nonzero urb status received: %d", __func__, urb->status);
  244. goto exit;
  245. }
  246. if (count < 6) {
  247. dbg("%s - int packet too short", __func__);
  248. goto exit;
  249. }
  250. if (!memcmp(up_int, instance->int_data, 6)) {
  251. del_timer(&instance->poll_timer);
  252. printk(KERN_NOTICE "DSL line goes up\n");
  253. } else if (!memcmp(down_int, instance->int_data, 6)) {
  254. printk(KERN_NOTICE "DSL line goes down\n");
  255. } else {
  256. int i;
  257. printk(KERN_DEBUG "Unknown interrupt packet of %d bytes:", count);
  258. for (i = 0; i < count; i++)
  259. printk(" %02x", instance->int_data[i]);
  260. printk("\n");
  261. }
  262. schedule_work(&instance->poll_work);
  263. exit:
  264. rmb();
  265. if (!instance->int_urb)
  266. return;
  267. ret = usb_submit_urb(urb, GFP_ATOMIC);
  268. if (ret)
  269. err("%s - usb_submit_urb failed with result %d", __func__, ret);
  270. }
  271. static int speedtch_get_status(struct speedtch_instance_data *instance,
  272. unsigned char *buf)
  273. {
  274. struct usb_device *dev = instance->u.usb_dev;
  275. int ret;
  276. memset(buf, 0, TOTAL);
  277. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  278. 0x12, 0xc0, 0x07, 0x00, buf + OFFSET_7, SIZE_7,
  279. CTRL_TIMEOUT);
  280. if (ret < 0) {
  281. dbg("MSG 7 failed");
  282. return ret;
  283. }
  284. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  285. 0x12, 0xc0, 0x0b, 0x00, buf + OFFSET_b, SIZE_b,
  286. CTRL_TIMEOUT);
  287. if (ret < 0) {
  288. dbg("MSG B failed");
  289. return ret;
  290. }
  291. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  292. 0x12, 0xc0, 0x0d, 0x00, buf + OFFSET_d, SIZE_d,
  293. CTRL_TIMEOUT);
  294. if (ret < 0) {
  295. dbg("MSG D failed");
  296. return ret;
  297. }
  298. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  299. 0x01, 0xc0, 0x0e, 0x00, buf + OFFSET_e, SIZE_e,
  300. CTRL_TIMEOUT);
  301. if (ret < 0) {
  302. dbg("MSG E failed");
  303. return ret;
  304. }
  305. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  306. 0x01, 0xc0, 0x0f, 0x00, buf + OFFSET_f, SIZE_f,
  307. CTRL_TIMEOUT);
  308. if (ret < 0) {
  309. dbg("MSG F failed");
  310. return ret;
  311. }
  312. return 0;
  313. }
  314. static void speedtch_poll_status(struct speedtch_instance_data *instance)
  315. {
  316. unsigned char buf[TOTAL];
  317. int ret;
  318. ret = speedtch_get_status(instance, buf);
  319. if (ret) {
  320. printk(KERN_WARNING
  321. "SpeedTouch: Error %d fetching device status\n", ret);
  322. return;
  323. }
  324. dbg("Line state %02x", buf[OFFSET_7]);
  325. switch (buf[OFFSET_7]) {
  326. case 0:
  327. if (instance->u.atm_dev->signal != ATM_PHY_SIG_LOST) {
  328. instance->u.atm_dev->signal = ATM_PHY_SIG_LOST;
  329. printk(KERN_NOTICE "ADSL line is down\n");
  330. /* It'll never resync again unless we ask it to... */
  331. speedtch_start_synchro(instance);
  332. }
  333. break;
  334. case 0x08:
  335. if (instance->u.atm_dev->signal != ATM_PHY_SIG_UNKNOWN) {
  336. instance->u.atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
  337. printk(KERN_NOTICE "ADSL line is blocked?\n");
  338. }
  339. break;
  340. case 0x10:
  341. if (instance->u.atm_dev->signal != ATM_PHY_SIG_LOST) {
  342. instance->u.atm_dev->signal = ATM_PHY_SIG_LOST;
  343. printk(KERN_NOTICE "ADSL line is synchronising\n");
  344. }
  345. break;
  346. case 0x20:
  347. if (instance->u.atm_dev->signal != ATM_PHY_SIG_FOUND) {
  348. int down_speed = buf[OFFSET_b] | (buf[OFFSET_b + 1] << 8)
  349. | (buf[OFFSET_b + 2] << 16) | (buf[OFFSET_b + 3] << 24);
  350. int up_speed = buf[OFFSET_b + 4] | (buf[OFFSET_b + 5] << 8)
  351. | (buf[OFFSET_b + 6] << 16) | (buf[OFFSET_b + 7] << 24);
  352. if (!(down_speed & 0x0000ffff) &&
  353. !(up_speed & 0x0000ffff)) {
  354. down_speed >>= 16;
  355. up_speed >>= 16;
  356. }
  357. instance->u.atm_dev->link_rate = down_speed * 1000 / 424;
  358. instance->u.atm_dev->signal = ATM_PHY_SIG_FOUND;
  359. printk(KERN_NOTICE
  360. "ADSL line is up (%d Kib/s down | %d Kib/s up)\n",
  361. down_speed, up_speed);
  362. }
  363. break;
  364. default:
  365. if (instance->u.atm_dev->signal != ATM_PHY_SIG_UNKNOWN) {
  366. instance->u.atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
  367. printk(KERN_NOTICE "Unknown line state %02x\n", buf[OFFSET_7]);
  368. }
  369. break;
  370. }
  371. }
  372. static void speedtch_timer_poll(unsigned long data)
  373. {
  374. struct speedtch_instance_data *instance = (void *)data;
  375. schedule_work(&instance->poll_work);
  376. mod_timer(&instance->poll_timer, jiffies + (5 * HZ));
  377. }
  378. #ifdef USE_FW_LOADER
  379. static void speedtch_upload_firmware(struct speedtch_instance_data *instance,
  380. const struct firmware *fw1,
  381. const struct firmware *fw2)
  382. {
  383. unsigned char *buffer;
  384. struct usb_device *usb_dev = instance->u.usb_dev;
  385. struct usb_interface *intf;
  386. int actual_length, ret;
  387. int offset;
  388. dbg("speedtch_upload_firmware");
  389. if (!(intf = usb_ifnum_to_if(usb_dev, 2))) {
  390. dbg("speedtch_upload_firmware: interface not found!");
  391. goto fail;
  392. }
  393. if (!(buffer = (unsigned char *)__get_free_page(GFP_KERNEL))) {
  394. dbg("speedtch_upload_firmware: no memory for buffer!");
  395. goto fail;
  396. }
  397. /* A user-space firmware loader may already have claimed interface #2 */
  398. if ((ret =
  399. usb_driver_claim_interface(&speedtch_usb_driver, intf, NULL)) < 0) {
  400. dbg("speedtch_upload_firmware: interface in use (%d)!", ret);
  401. goto fail_free;
  402. }
  403. /* URB 7 */
  404. if (dl_512_first) { /* some modems need a read before writing the firmware */
  405. ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, SPEEDTCH_ENDPOINT_FIRMWARE),
  406. buffer, 0x200, &actual_length, 2000);
  407. if (ret < 0 && ret != -ETIMEDOUT)
  408. dbg("speedtch_upload_firmware: read BLOCK0 from modem failed (%d)!", ret);
  409. else
  410. dbg("speedtch_upload_firmware: BLOCK0 downloaded (%d bytes)", ret);
  411. }
  412. /* URB 8 : both leds are static green */
  413. for (offset = 0; offset < fw1->size; offset += PAGE_SIZE) {
  414. int thislen = min_t(int, PAGE_SIZE, fw1->size - offset);
  415. memcpy(buffer, fw1->data + offset, thislen);
  416. ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, SPEEDTCH_ENDPOINT_FIRMWARE),
  417. buffer, thislen, &actual_length, DATA_TIMEOUT);
  418. if (ret < 0) {
  419. dbg("speedtch_upload_firmware: write BLOCK1 to modem failed (%d)!", ret);
  420. goto fail_release;
  421. }
  422. dbg("speedtch_upload_firmware: BLOCK1 uploaded (%zu bytes)", fw1->size);
  423. }
  424. /* USB led blinking green, ADSL led off */
  425. /* URB 11 */
  426. ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, SPEEDTCH_ENDPOINT_FIRMWARE),
  427. buffer, 0x200, &actual_length, DATA_TIMEOUT);
  428. if (ret < 0) {
  429. dbg("speedtch_upload_firmware: read BLOCK2 from modem failed (%d)!", ret);
  430. goto fail_release;
  431. }
  432. dbg("speedtch_upload_firmware: BLOCK2 downloaded (%d bytes)", actual_length);
  433. /* URBs 12 to 139 - USB led blinking green, ADSL led off */
  434. for (offset = 0; offset < fw2->size; offset += PAGE_SIZE) {
  435. int thislen = min_t(int, PAGE_SIZE, fw2->size - offset);
  436. memcpy(buffer, fw2->data + offset, thislen);
  437. ret = usb_bulk_msg(usb_dev, usb_sndbulkpipe(usb_dev, SPEEDTCH_ENDPOINT_FIRMWARE),
  438. buffer, thislen, &actual_length, DATA_TIMEOUT);
  439. if (ret < 0) {
  440. dbg("speedtch_upload_firmware: write BLOCK3 to modem failed (%d)!", ret);
  441. goto fail_release;
  442. }
  443. }
  444. dbg("speedtch_upload_firmware: BLOCK3 uploaded (%zu bytes)", fw2->size);
  445. /* USB led static green, ADSL led static red */
  446. /* URB 142 */
  447. ret = usb_bulk_msg(usb_dev, usb_rcvbulkpipe(usb_dev, SPEEDTCH_ENDPOINT_FIRMWARE),
  448. buffer, 0x200, &actual_length, DATA_TIMEOUT);
  449. if (ret < 0) {
  450. dbg("speedtch_upload_firmware: read BLOCK4 from modem failed (%d)!", ret);
  451. goto fail_release;
  452. }
  453. /* success */
  454. dbg("speedtch_upload_firmware: BLOCK4 downloaded (%d bytes)", actual_length);
  455. /* Delay to allow firmware to start up. We can do this here
  456. because we're in our own kernel thread anyway. */
  457. msleep(1000);
  458. /* Enable software buffering, if requested */
  459. if (sw_buffering)
  460. speedtch_set_swbuff(instance, 1);
  461. /* Magic spell; don't ask us what this does */
  462. speedtch_test_sequence(instance);
  463. /* Start modem synchronisation */
  464. if (speedtch_start_synchro(instance))
  465. dbg("speedtch_start_synchro: failed");
  466. speedtch_got_firmware(instance, 1);
  467. free_page((unsigned long)buffer);
  468. return;
  469. fail_release:
  470. /* Only release interface #2 if uploading failed; we don't release it
  471. we succeeded. This prevents the userspace tools from trying to load
  472. the firmware themselves */
  473. usb_driver_release_interface(&speedtch_usb_driver, intf);
  474. fail_free:
  475. free_page((unsigned long)buffer);
  476. fail:
  477. speedtch_got_firmware(instance, 0);
  478. }
  479. static int speedtch_find_firmware(struct speedtch_instance_data
  480. *instance, int phase,
  481. const struct firmware **fw_p)
  482. {
  483. char buf[24];
  484. const u16 bcdDevice = le16_to_cpu(instance->u.usb_dev->descriptor.bcdDevice);
  485. const u8 major_revision = bcdDevice >> 8;
  486. const u8 minor_revision = bcdDevice & 0xff;
  487. sprintf(buf, "speedtch-%d.bin.%x.%02x", phase, major_revision, minor_revision);
  488. dbg("speedtch_find_firmware: looking for %s", buf);
  489. if (request_firmware(fw_p, buf, &instance->u.usb_dev->dev)) {
  490. sprintf(buf, "speedtch-%d.bin.%x", phase, major_revision);
  491. dbg("speedtch_find_firmware: looking for %s", buf);
  492. if (request_firmware(fw_p, buf, &instance->u.usb_dev->dev)) {
  493. sprintf(buf, "speedtch-%d.bin", phase);
  494. dbg("speedtch_find_firmware: looking for %s", buf);
  495. if (request_firmware(fw_p, buf, &instance->u.usb_dev->dev)) {
  496. dev_warn(&instance->u.usb_dev->dev, "no stage %d firmware found!", phase);
  497. return -ENOENT;
  498. }
  499. }
  500. }
  501. dev_info(&instance->u.usb_dev->dev, "found stage %d firmware %s\n", phase, buf);
  502. return 0;
  503. }
  504. static int speedtch_load_firmware(void *arg)
  505. {
  506. const struct firmware *fw1, *fw2;
  507. struct speedtch_instance_data *instance = arg;
  508. BUG_ON(!instance);
  509. daemonize("firmware/speedtch");
  510. if (!speedtch_find_firmware(instance, 1, &fw1)) {
  511. if (!speedtch_find_firmware(instance, 2, &fw2)) {
  512. speedtch_upload_firmware(instance, fw1, fw2);
  513. release_firmware(fw2);
  514. }
  515. release_firmware(fw1);
  516. }
  517. /* In case we failed, set state back to NO_FIRMWARE so that
  518. another later attempt may work. Otherwise, we never actually
  519. manage to recover if, for example, the firmware is on /usr and
  520. we look for it too early. */
  521. speedtch_got_firmware(instance, 0);
  522. module_put(THIS_MODULE);
  523. udsl_put_instance(&instance->u);
  524. return 0;
  525. }
  526. #endif /* USE_FW_LOADER */
  527. static void speedtch_firmware_start(struct speedtch_instance_data *instance)
  528. {
  529. #ifdef USE_FW_LOADER
  530. int ret;
  531. #endif
  532. dbg("speedtch_firmware_start");
  533. down(&instance->u.serialize); /* vs self, speedtch_got_firmware */
  534. if (instance->u.status >= UDSL_LOADING_FIRMWARE) {
  535. up(&instance->u.serialize);
  536. return;
  537. }
  538. instance->u.status = UDSL_LOADING_FIRMWARE;
  539. up(&instance->u.serialize);
  540. #ifdef USE_FW_LOADER
  541. udsl_get_instance(&instance->u);
  542. try_module_get(THIS_MODULE);
  543. ret = kernel_thread(speedtch_load_firmware, instance,
  544. CLONE_FS | CLONE_FILES);
  545. if (ret >= 0)
  546. return; /* OK */
  547. dbg("speedtch_firmware_start: kernel_thread failed (%d)!", ret);
  548. module_put(THIS_MODULE);
  549. udsl_put_instance(&instance->u);
  550. /* Just pretend it never happened... hope modem_run happens */
  551. #endif /* USE_FW_LOADER */
  552. speedtch_got_firmware(instance, 0);
  553. }
  554. static int speedtch_firmware_wait(struct udsl_instance_data *instance)
  555. {
  556. speedtch_firmware_start((void *)instance);
  557. if (wait_event_interruptible(instance->firmware_waiters, instance->status != UDSL_LOADING_FIRMWARE) < 0)
  558. return -ERESTARTSYS;
  559. return (instance->status == UDSL_LOADED_FIRMWARE) ? 0 : -EAGAIN;
  560. }
  561. /**********
  562. ** USB **
  563. **********/
  564. static int speedtch_usb_ioctl(struct usb_interface *intf, unsigned int code,
  565. void *user_data)
  566. {
  567. struct speedtch_instance_data *instance = usb_get_intfdata(intf);
  568. dbg("speedtch_usb_ioctl entered");
  569. if (!instance) {
  570. dbg("speedtch_usb_ioctl: NULL instance!");
  571. return -ENODEV;
  572. }
  573. switch (code) {
  574. case UDSL_IOCTL_LINE_UP:
  575. instance->u.atm_dev->signal = ATM_PHY_SIG_FOUND;
  576. speedtch_got_firmware(instance, 1);
  577. return (instance->u.status == UDSL_LOADED_FIRMWARE) ? 0 : -EIO;
  578. case UDSL_IOCTL_LINE_DOWN:
  579. instance->u.atm_dev->signal = ATM_PHY_SIG_LOST;
  580. return 0;
  581. default:
  582. return -ENOTTY;
  583. }
  584. }
  585. static int speedtch_usb_probe(struct usb_interface *intf,
  586. const struct usb_device_id *id)
  587. {
  588. struct usb_device *dev = interface_to_usbdev(intf);
  589. int ifnum = intf->altsetting->desc.bInterfaceNumber;
  590. struct speedtch_instance_data *instance;
  591. unsigned char mac_str[13];
  592. int ret, i;
  593. char buf7[SIZE_7];
  594. dbg("speedtch_usb_probe: trying device with vendor=0x%x, product=0x%x, ifnum %d",
  595. le16_to_cpu(dev->descriptor.idVendor),
  596. le16_to_cpu(dev->descriptor.idProduct), ifnum);
  597. if ((dev->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) ||
  598. (ifnum != 1))
  599. return -ENODEV;
  600. dbg("speedtch_usb_probe: device accepted");
  601. /* instance init */
  602. instance = kmalloc(sizeof(*instance), GFP_KERNEL);
  603. if (!instance) {
  604. dbg("speedtch_usb_probe: no memory for instance data!");
  605. return -ENOMEM;
  606. }
  607. memset(instance, 0, sizeof(struct speedtch_instance_data));
  608. if ((ret = usb_set_interface(dev, 0, 0)) < 0)
  609. goto fail;
  610. if ((ret = usb_set_interface(dev, 2, 0)) < 0)
  611. goto fail;
  612. instance->u.data_endpoint = SPEEDTCH_ENDPOINT_DATA;
  613. instance->u.firmware_wait = speedtch_firmware_wait;
  614. instance->u.driver_name = speedtch_driver_name;
  615. ret = udsl_instance_setup(dev, &instance->u);
  616. if (ret)
  617. goto fail;
  618. init_timer(&instance->poll_timer);
  619. instance->poll_timer.function = speedtch_timer_poll;
  620. instance->poll_timer.data = (unsigned long)instance;
  621. INIT_WORK(&instance->poll_work, (void *)speedtch_poll_status, instance);
  622. /* set MAC address, it is stored in the serial number */
  623. memset(instance->u.atm_dev->esi, 0, sizeof(instance->u.atm_dev->esi));
  624. if (usb_string(dev, dev->descriptor.iSerialNumber, mac_str, sizeof(mac_str)) == 12) {
  625. for (i = 0; i < 6; i++)
  626. instance->u.atm_dev->esi[i] =
  627. (hex2int(mac_str[i * 2]) * 16) + (hex2int(mac_str[i * 2 + 1]));
  628. }
  629. /* First check whether the modem already seems to be alive */
  630. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  631. 0x12, 0xc0, 0x07, 0x00, buf7, SIZE_7, 500);
  632. if (ret == SIZE_7) {
  633. dbg("firmware appears to be already loaded");
  634. speedtch_got_firmware(instance, 1);
  635. speedtch_poll_status(instance);
  636. } else {
  637. speedtch_firmware_start(instance);
  638. }
  639. usb_set_intfdata(intf, instance);
  640. return 0;
  641. fail:
  642. kfree(instance);
  643. return -ENOMEM;
  644. }
  645. static void speedtch_usb_disconnect(struct usb_interface *intf)
  646. {
  647. struct speedtch_instance_data *instance = usb_get_intfdata(intf);
  648. dbg("speedtch_usb_disconnect entered");
  649. if (!instance) {
  650. dbg("speedtch_usb_disconnect: NULL instance!");
  651. return;
  652. }
  653. /*QQ need to handle disconnects on interface #2 while uploading firmware */
  654. /*QQ and what about interface #1? */
  655. if (instance->int_urb) {
  656. struct urb *int_urb = instance->int_urb;
  657. instance->int_urb = NULL;
  658. wmb();
  659. usb_unlink_urb(int_urb);
  660. usb_free_urb(int_urb);
  661. }
  662. instance->int_data[0] = 1;
  663. del_timer_sync(&instance->poll_timer);
  664. wmb();
  665. flush_scheduled_work();
  666. udsl_instance_disconnect(&instance->u);
  667. /* clean up */
  668. usb_set_intfdata(intf, NULL);
  669. udsl_put_instance(&instance->u);
  670. }
  671. /***********
  672. ** init **
  673. ***********/
  674. static int __init speedtch_usb_init(void)
  675. {
  676. dbg("speedtch_usb_init: driver version " DRIVER_VERSION);
  677. return usb_register(&speedtch_usb_driver);
  678. }
  679. static void __exit speedtch_usb_cleanup(void)
  680. {
  681. dbg("speedtch_usb_cleanup entered");
  682. usb_deregister(&speedtch_usb_driver);
  683. }
  684. module_init(speedtch_usb_init);
  685. module_exit(speedtch_usb_cleanup);
  686. MODULE_AUTHOR(DRIVER_AUTHOR);
  687. MODULE_DESCRIPTION(DRIVER_DESC);
  688. MODULE_LICENSE("GPL");
  689. MODULE_VERSION(DRIVER_VERSION);