usb_wwan.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. /*
  2. USB Driver layer for GSM modems
  3. Copyright (C) 2005 Matthias Urlichs <smurf@smurf.noris.de>
  4. This driver is free software; you can redistribute it and/or modify
  5. it under the terms of Version 2 of the GNU General Public License as
  6. published by the Free Software Foundation.
  7. Portions copied from the Keyspan driver by Hugh Blemings <hugh@blemings.org>
  8. History: see the git log.
  9. Work sponsored by: Sigos GmbH, Germany <info@sigos.de>
  10. This driver exists because the "normal" serial driver doesn't work too well
  11. with GSM modems. Issues:
  12. - data loss -- one single Receive URB is not nearly enough
  13. - controlling the baud rate doesn't make sense
  14. */
  15. #define DRIVER_VERSION "v0.7.2"
  16. #define DRIVER_AUTHOR "Matthias Urlichs <smurf@smurf.noris.de>"
  17. #define DRIVER_DESC "USB Driver for GSM modems"
  18. #include <linux/kernel.h>
  19. #include <linux/jiffies.h>
  20. #include <linux/errno.h>
  21. #include <linux/slab.h>
  22. #include <linux/tty.h>
  23. #include <linux/tty_flip.h>
  24. #include <linux/module.h>
  25. #include <linux/bitops.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/usb.h>
  28. #include <linux/usb/serial.h>
  29. #include <linux/serial.h>
  30. #include "usb-wwan.h"
  31. static bool debug;
  32. void usb_wwan_dtr_rts(struct usb_serial_port *port, int on)
  33. {
  34. struct usb_serial *serial = port->serial;
  35. struct usb_wwan_port_private *portdata;
  36. struct usb_wwan_intf_private *intfdata;
  37. dbg("%s", __func__);
  38. intfdata = port->serial->private;
  39. if (!intfdata->send_setup)
  40. return;
  41. portdata = usb_get_serial_port_data(port);
  42. mutex_lock(&serial->disc_mutex);
  43. portdata->rts_state = on;
  44. portdata->dtr_state = on;
  45. if (serial->dev)
  46. intfdata->send_setup(port);
  47. mutex_unlock(&serial->disc_mutex);
  48. }
  49. EXPORT_SYMBOL(usb_wwan_dtr_rts);
  50. void usb_wwan_set_termios(struct tty_struct *tty,
  51. struct usb_serial_port *port,
  52. struct ktermios *old_termios)
  53. {
  54. struct usb_wwan_intf_private *intfdata = port->serial->private;
  55. dbg("%s", __func__);
  56. /* Doesn't support option setting */
  57. tty_termios_copy_hw(tty->termios, old_termios);
  58. if (intfdata->send_setup)
  59. intfdata->send_setup(port);
  60. }
  61. EXPORT_SYMBOL(usb_wwan_set_termios);
  62. int usb_wwan_tiocmget(struct tty_struct *tty)
  63. {
  64. struct usb_serial_port *port = tty->driver_data;
  65. unsigned int value;
  66. struct usb_wwan_port_private *portdata;
  67. portdata = usb_get_serial_port_data(port);
  68. value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
  69. ((portdata->dtr_state) ? TIOCM_DTR : 0) |
  70. ((portdata->cts_state) ? TIOCM_CTS : 0) |
  71. ((portdata->dsr_state) ? TIOCM_DSR : 0) |
  72. ((portdata->dcd_state) ? TIOCM_CAR : 0) |
  73. ((portdata->ri_state) ? TIOCM_RNG : 0);
  74. return value;
  75. }
  76. EXPORT_SYMBOL(usb_wwan_tiocmget);
  77. int usb_wwan_tiocmset(struct tty_struct *tty,
  78. unsigned int set, unsigned int clear)
  79. {
  80. struct usb_serial_port *port = tty->driver_data;
  81. struct usb_wwan_port_private *portdata;
  82. struct usb_wwan_intf_private *intfdata;
  83. portdata = usb_get_serial_port_data(port);
  84. intfdata = port->serial->private;
  85. if (!intfdata->send_setup)
  86. return -EINVAL;
  87. /* FIXME: what locks portdata fields ? */
  88. if (set & TIOCM_RTS)
  89. portdata->rts_state = 1;
  90. if (set & TIOCM_DTR)
  91. portdata->dtr_state = 1;
  92. if (clear & TIOCM_RTS)
  93. portdata->rts_state = 0;
  94. if (clear & TIOCM_DTR)
  95. portdata->dtr_state = 0;
  96. return intfdata->send_setup(port);
  97. }
  98. EXPORT_SYMBOL(usb_wwan_tiocmset);
  99. static int get_serial_info(struct usb_serial_port *port,
  100. struct serial_struct __user *retinfo)
  101. {
  102. struct serial_struct tmp;
  103. if (!retinfo)
  104. return -EFAULT;
  105. memset(&tmp, 0, sizeof(tmp));
  106. tmp.line = port->serial->minor;
  107. tmp.port = port->number;
  108. tmp.baud_base = tty_get_baud_rate(port->port.tty);
  109. tmp.close_delay = port->port.close_delay / 10;
  110. tmp.closing_wait = port->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
  111. ASYNC_CLOSING_WAIT_NONE :
  112. port->port.closing_wait / 10;
  113. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  114. return -EFAULT;
  115. return 0;
  116. }
  117. static int set_serial_info(struct usb_serial_port *port,
  118. struct serial_struct __user *newinfo)
  119. {
  120. struct serial_struct new_serial;
  121. unsigned int closing_wait, close_delay;
  122. int retval = 0;
  123. if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
  124. return -EFAULT;
  125. close_delay = new_serial.close_delay * 10;
  126. closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
  127. ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
  128. mutex_lock(&port->port.mutex);
  129. if (!capable(CAP_SYS_ADMIN)) {
  130. if ((close_delay != port->port.close_delay) ||
  131. (closing_wait != port->port.closing_wait))
  132. retval = -EPERM;
  133. else
  134. retval = -EOPNOTSUPP;
  135. } else {
  136. port->port.close_delay = close_delay;
  137. port->port.closing_wait = closing_wait;
  138. }
  139. mutex_unlock(&port->port.mutex);
  140. return retval;
  141. }
  142. int usb_wwan_ioctl(struct tty_struct *tty,
  143. unsigned int cmd, unsigned long arg)
  144. {
  145. struct usb_serial_port *port = tty->driver_data;
  146. dbg("%s cmd 0x%04x", __func__, cmd);
  147. switch (cmd) {
  148. case TIOCGSERIAL:
  149. return get_serial_info(port,
  150. (struct serial_struct __user *) arg);
  151. case TIOCSSERIAL:
  152. return set_serial_info(port,
  153. (struct serial_struct __user *) arg);
  154. default:
  155. break;
  156. }
  157. dbg("%s arg not supported", __func__);
  158. return -ENOIOCTLCMD;
  159. }
  160. EXPORT_SYMBOL(usb_wwan_ioctl);
  161. /* Write */
  162. int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port,
  163. const unsigned char *buf, int count)
  164. {
  165. struct usb_wwan_port_private *portdata;
  166. struct usb_wwan_intf_private *intfdata;
  167. int i;
  168. int left, todo;
  169. struct urb *this_urb = NULL; /* spurious */
  170. int err;
  171. unsigned long flags;
  172. portdata = usb_get_serial_port_data(port);
  173. intfdata = port->serial->private;
  174. dbg("%s: write (%d chars)", __func__, count);
  175. i = 0;
  176. left = count;
  177. for (i = 0; left > 0 && i < N_OUT_URB; i++) {
  178. todo = left;
  179. if (todo > OUT_BUFLEN)
  180. todo = OUT_BUFLEN;
  181. this_urb = portdata->out_urbs[i];
  182. if (test_and_set_bit(i, &portdata->out_busy)) {
  183. if (time_before(jiffies,
  184. portdata->tx_start_time[i] + 10 * HZ))
  185. continue;
  186. usb_unlink_urb(this_urb);
  187. continue;
  188. }
  189. dbg("%s: endpoint %d buf %d", __func__,
  190. usb_pipeendpoint(this_urb->pipe), i);
  191. err = usb_autopm_get_interface_async(port->serial->interface);
  192. if (err < 0)
  193. break;
  194. /* send the data */
  195. memcpy(this_urb->transfer_buffer, buf, todo);
  196. this_urb->transfer_buffer_length = todo;
  197. spin_lock_irqsave(&intfdata->susp_lock, flags);
  198. if (intfdata->suspended) {
  199. usb_anchor_urb(this_urb, &portdata->delayed);
  200. spin_unlock_irqrestore(&intfdata->susp_lock, flags);
  201. } else {
  202. intfdata->in_flight++;
  203. spin_unlock_irqrestore(&intfdata->susp_lock, flags);
  204. err = usb_submit_urb(this_urb, GFP_ATOMIC);
  205. if (err) {
  206. dbg("usb_submit_urb %p (write bulk) failed "
  207. "(%d)", this_urb, err);
  208. clear_bit(i, &portdata->out_busy);
  209. spin_lock_irqsave(&intfdata->susp_lock, flags);
  210. intfdata->in_flight--;
  211. spin_unlock_irqrestore(&intfdata->susp_lock,
  212. flags);
  213. usb_autopm_put_interface_async(port->serial->interface);
  214. break;
  215. }
  216. }
  217. portdata->tx_start_time[i] = jiffies;
  218. buf += todo;
  219. left -= todo;
  220. }
  221. count -= left;
  222. dbg("%s: wrote (did %d)", __func__, count);
  223. return count;
  224. }
  225. EXPORT_SYMBOL(usb_wwan_write);
  226. static void usb_wwan_indat_callback(struct urb *urb)
  227. {
  228. int err;
  229. int endpoint;
  230. struct usb_serial_port *port;
  231. struct tty_struct *tty;
  232. unsigned char *data = urb->transfer_buffer;
  233. int status = urb->status;
  234. dbg("%s: %p", __func__, urb);
  235. endpoint = usb_pipeendpoint(urb->pipe);
  236. port = urb->context;
  237. if (status) {
  238. dbg("%s: nonzero status: %d on endpoint %02x.",
  239. __func__, status, endpoint);
  240. } else {
  241. tty = tty_port_tty_get(&port->port);
  242. if (tty) {
  243. if (urb->actual_length) {
  244. tty_insert_flip_string(tty, data,
  245. urb->actual_length);
  246. tty_flip_buffer_push(tty);
  247. } else
  248. dbg("%s: empty read urb received", __func__);
  249. tty_kref_put(tty);
  250. }
  251. /* Resubmit urb so we continue receiving */
  252. err = usb_submit_urb(urb, GFP_ATOMIC);
  253. if (err) {
  254. if (err != -EPERM) {
  255. printk(KERN_ERR "%s: resubmit read urb failed. "
  256. "(%d)", __func__, err);
  257. /* busy also in error unless we are killed */
  258. usb_mark_last_busy(port->serial->dev);
  259. }
  260. } else {
  261. usb_mark_last_busy(port->serial->dev);
  262. }
  263. }
  264. }
  265. static void usb_wwan_outdat_callback(struct urb *urb)
  266. {
  267. struct usb_serial_port *port;
  268. struct usb_wwan_port_private *portdata;
  269. struct usb_wwan_intf_private *intfdata;
  270. int i;
  271. dbg("%s", __func__);
  272. port = urb->context;
  273. intfdata = port->serial->private;
  274. usb_serial_port_softint(port);
  275. usb_autopm_put_interface_async(port->serial->interface);
  276. portdata = usb_get_serial_port_data(port);
  277. spin_lock(&intfdata->susp_lock);
  278. intfdata->in_flight--;
  279. spin_unlock(&intfdata->susp_lock);
  280. for (i = 0; i < N_OUT_URB; ++i) {
  281. if (portdata->out_urbs[i] == urb) {
  282. smp_mb__before_clear_bit();
  283. clear_bit(i, &portdata->out_busy);
  284. break;
  285. }
  286. }
  287. }
  288. int usb_wwan_write_room(struct tty_struct *tty)
  289. {
  290. struct usb_serial_port *port = tty->driver_data;
  291. struct usb_wwan_port_private *portdata;
  292. int i;
  293. int data_len = 0;
  294. struct urb *this_urb;
  295. portdata = usb_get_serial_port_data(port);
  296. for (i = 0; i < N_OUT_URB; i++) {
  297. this_urb = portdata->out_urbs[i];
  298. if (this_urb && !test_bit(i, &portdata->out_busy))
  299. data_len += OUT_BUFLEN;
  300. }
  301. dbg("%s: %d", __func__, data_len);
  302. return data_len;
  303. }
  304. EXPORT_SYMBOL(usb_wwan_write_room);
  305. int usb_wwan_chars_in_buffer(struct tty_struct *tty)
  306. {
  307. struct usb_serial_port *port = tty->driver_data;
  308. struct usb_wwan_port_private *portdata;
  309. int i;
  310. int data_len = 0;
  311. struct urb *this_urb;
  312. portdata = usb_get_serial_port_data(port);
  313. for (i = 0; i < N_OUT_URB; i++) {
  314. this_urb = portdata->out_urbs[i];
  315. /* FIXME: This locking is insufficient as this_urb may
  316. go unused during the test */
  317. if (this_urb && test_bit(i, &portdata->out_busy))
  318. data_len += this_urb->transfer_buffer_length;
  319. }
  320. dbg("%s: %d", __func__, data_len);
  321. return data_len;
  322. }
  323. EXPORT_SYMBOL(usb_wwan_chars_in_buffer);
  324. int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port)
  325. {
  326. struct usb_wwan_port_private *portdata;
  327. struct usb_wwan_intf_private *intfdata;
  328. struct usb_serial *serial = port->serial;
  329. int i, err;
  330. struct urb *urb;
  331. portdata = usb_get_serial_port_data(port);
  332. intfdata = serial->private;
  333. dbg("%s", __func__);
  334. /* Start reading from the IN endpoint */
  335. for (i = 0; i < N_IN_URB; i++) {
  336. urb = portdata->in_urbs[i];
  337. if (!urb)
  338. continue;
  339. err = usb_submit_urb(urb, GFP_KERNEL);
  340. if (err) {
  341. dbg("%s: submit urb %d failed (%d) %d",
  342. __func__, i, err, urb->transfer_buffer_length);
  343. }
  344. }
  345. if (intfdata->send_setup)
  346. intfdata->send_setup(port);
  347. serial->interface->needs_remote_wakeup = 1;
  348. spin_lock_irq(&intfdata->susp_lock);
  349. portdata->opened = 1;
  350. spin_unlock_irq(&intfdata->susp_lock);
  351. /* this balances a get in the generic USB serial code */
  352. usb_autopm_put_interface(serial->interface);
  353. return 0;
  354. }
  355. EXPORT_SYMBOL(usb_wwan_open);
  356. void usb_wwan_close(struct usb_serial_port *port)
  357. {
  358. int i;
  359. struct usb_serial *serial = port->serial;
  360. struct usb_wwan_port_private *portdata;
  361. struct usb_wwan_intf_private *intfdata = port->serial->private;
  362. dbg("%s", __func__);
  363. portdata = usb_get_serial_port_data(port);
  364. if (serial->dev) {
  365. /* Stop reading/writing urbs */
  366. spin_lock_irq(&intfdata->susp_lock);
  367. portdata->opened = 0;
  368. spin_unlock_irq(&intfdata->susp_lock);
  369. for (i = 0; i < N_IN_URB; i++)
  370. usb_kill_urb(portdata->in_urbs[i]);
  371. for (i = 0; i < N_OUT_URB; i++)
  372. usb_kill_urb(portdata->out_urbs[i]);
  373. /* balancing - important as an error cannot be handled*/
  374. usb_autopm_get_interface_no_resume(serial->interface);
  375. serial->interface->needs_remote_wakeup = 0;
  376. }
  377. }
  378. EXPORT_SYMBOL(usb_wwan_close);
  379. /* Helper functions used by usb_wwan_setup_urbs */
  380. static struct urb *usb_wwan_setup_urb(struct usb_serial *serial, int endpoint,
  381. int dir, void *ctx, char *buf, int len,
  382. void (*callback) (struct urb *))
  383. {
  384. struct urb *urb;
  385. if (endpoint == -1)
  386. return NULL; /* endpoint not needed */
  387. urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
  388. if (urb == NULL) {
  389. dbg("%s: alloc for endpoint %d failed.", __func__, endpoint);
  390. return NULL;
  391. }
  392. /* Fill URB using supplied data. */
  393. usb_fill_bulk_urb(urb, serial->dev,
  394. usb_sndbulkpipe(serial->dev, endpoint) | dir,
  395. buf, len, callback, ctx);
  396. return urb;
  397. }
  398. /* Setup urbs */
  399. static void usb_wwan_setup_urbs(struct usb_serial *serial)
  400. {
  401. int i, j;
  402. struct usb_serial_port *port;
  403. struct usb_wwan_port_private *portdata;
  404. dbg("%s", __func__);
  405. for (i = 0; i < serial->num_ports; i++) {
  406. port = serial->port[i];
  407. portdata = usb_get_serial_port_data(port);
  408. /* Do indat endpoints first */
  409. for (j = 0; j < N_IN_URB; ++j) {
  410. portdata->in_urbs[j] = usb_wwan_setup_urb(serial,
  411. port->
  412. bulk_in_endpointAddress,
  413. USB_DIR_IN,
  414. port,
  415. portdata->
  416. in_buffer[j],
  417. IN_BUFLEN,
  418. usb_wwan_indat_callback);
  419. }
  420. /* outdat endpoints */
  421. for (j = 0; j < N_OUT_URB; ++j) {
  422. portdata->out_urbs[j] = usb_wwan_setup_urb(serial,
  423. port->
  424. bulk_out_endpointAddress,
  425. USB_DIR_OUT,
  426. port,
  427. portdata->
  428. out_buffer
  429. [j],
  430. OUT_BUFLEN,
  431. usb_wwan_outdat_callback);
  432. }
  433. }
  434. }
  435. int usb_wwan_startup(struct usb_serial *serial)
  436. {
  437. int i, j, err;
  438. struct usb_serial_port *port;
  439. struct usb_wwan_port_private *portdata;
  440. u8 *buffer;
  441. dbg("%s", __func__);
  442. /* Now setup per port private data */
  443. for (i = 0; i < serial->num_ports; i++) {
  444. port = serial->port[i];
  445. portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
  446. if (!portdata) {
  447. dbg("%s: kmalloc for usb_wwan_port_private (%d) failed!.",
  448. __func__, i);
  449. return 1;
  450. }
  451. init_usb_anchor(&portdata->delayed);
  452. for (j = 0; j < N_IN_URB; j++) {
  453. buffer = (u8 *) __get_free_page(GFP_KERNEL);
  454. if (!buffer)
  455. goto bail_out_error;
  456. portdata->in_buffer[j] = buffer;
  457. }
  458. for (j = 0; j < N_OUT_URB; j++) {
  459. buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL);
  460. if (!buffer)
  461. goto bail_out_error2;
  462. portdata->out_buffer[j] = buffer;
  463. }
  464. usb_set_serial_port_data(port, portdata);
  465. if (!port->interrupt_in_urb)
  466. continue;
  467. err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  468. if (err)
  469. dbg("%s: submit irq_in urb failed %d", __func__, err);
  470. }
  471. usb_wwan_setup_urbs(serial);
  472. return 0;
  473. bail_out_error2:
  474. for (j = 0; j < N_OUT_URB; j++)
  475. kfree(portdata->out_buffer[j]);
  476. bail_out_error:
  477. for (j = 0; j < N_IN_URB; j++)
  478. if (portdata->in_buffer[j])
  479. free_page((unsigned long)portdata->in_buffer[j]);
  480. kfree(portdata);
  481. return 1;
  482. }
  483. EXPORT_SYMBOL(usb_wwan_startup);
  484. static void stop_read_write_urbs(struct usb_serial *serial)
  485. {
  486. int i, j;
  487. struct usb_serial_port *port;
  488. struct usb_wwan_port_private *portdata;
  489. /* Stop reading/writing urbs */
  490. for (i = 0; i < serial->num_ports; ++i) {
  491. port = serial->port[i];
  492. portdata = usb_get_serial_port_data(port);
  493. for (j = 0; j < N_IN_URB; j++)
  494. usb_kill_urb(portdata->in_urbs[j]);
  495. for (j = 0; j < N_OUT_URB; j++)
  496. usb_kill_urb(portdata->out_urbs[j]);
  497. }
  498. }
  499. void usb_wwan_disconnect(struct usb_serial *serial)
  500. {
  501. dbg("%s", __func__);
  502. stop_read_write_urbs(serial);
  503. }
  504. EXPORT_SYMBOL(usb_wwan_disconnect);
  505. void usb_wwan_release(struct usb_serial *serial)
  506. {
  507. int i, j;
  508. struct usb_serial_port *port;
  509. struct usb_wwan_port_private *portdata;
  510. dbg("%s", __func__);
  511. /* Now free them */
  512. for (i = 0; i < serial->num_ports; ++i) {
  513. port = serial->port[i];
  514. portdata = usb_get_serial_port_data(port);
  515. for (j = 0; j < N_IN_URB; j++) {
  516. usb_free_urb(portdata->in_urbs[j]);
  517. free_page((unsigned long)
  518. portdata->in_buffer[j]);
  519. portdata->in_urbs[j] = NULL;
  520. }
  521. for (j = 0; j < N_OUT_URB; j++) {
  522. usb_free_urb(portdata->out_urbs[j]);
  523. kfree(portdata->out_buffer[j]);
  524. portdata->out_urbs[j] = NULL;
  525. }
  526. }
  527. /* Now free per port private data */
  528. for (i = 0; i < serial->num_ports; i++) {
  529. port = serial->port[i];
  530. kfree(usb_get_serial_port_data(port));
  531. }
  532. }
  533. EXPORT_SYMBOL(usb_wwan_release);
  534. #ifdef CONFIG_PM
  535. int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message)
  536. {
  537. struct usb_wwan_intf_private *intfdata = serial->private;
  538. int b;
  539. dbg("%s entered", __func__);
  540. if (PMSG_IS_AUTO(message)) {
  541. spin_lock_irq(&intfdata->susp_lock);
  542. b = intfdata->in_flight;
  543. spin_unlock_irq(&intfdata->susp_lock);
  544. if (b)
  545. return -EBUSY;
  546. }
  547. spin_lock_irq(&intfdata->susp_lock);
  548. intfdata->suspended = 1;
  549. spin_unlock_irq(&intfdata->susp_lock);
  550. stop_read_write_urbs(serial);
  551. return 0;
  552. }
  553. EXPORT_SYMBOL(usb_wwan_suspend);
  554. static void unbusy_queued_urb(struct urb *urb, struct usb_wwan_port_private *portdata)
  555. {
  556. int i;
  557. for (i = 0; i < N_OUT_URB; i++) {
  558. if (urb == portdata->out_urbs[i]) {
  559. clear_bit(i, &portdata->out_busy);
  560. break;
  561. }
  562. }
  563. }
  564. static void play_delayed(struct usb_serial_port *port)
  565. {
  566. struct usb_wwan_intf_private *data;
  567. struct usb_wwan_port_private *portdata;
  568. struct urb *urb;
  569. int err;
  570. portdata = usb_get_serial_port_data(port);
  571. data = port->serial->private;
  572. while ((urb = usb_get_from_anchor(&portdata->delayed))) {
  573. err = usb_submit_urb(urb, GFP_ATOMIC);
  574. if (!err) {
  575. data->in_flight++;
  576. } else {
  577. /* we have to throw away the rest */
  578. do {
  579. unbusy_queued_urb(urb, portdata);
  580. usb_autopm_put_interface_no_suspend(port->serial->interface);
  581. } while ((urb = usb_get_from_anchor(&portdata->delayed)));
  582. break;
  583. }
  584. }
  585. }
  586. int usb_wwan_resume(struct usb_serial *serial)
  587. {
  588. int i, j;
  589. struct usb_serial_port *port;
  590. struct usb_wwan_intf_private *intfdata = serial->private;
  591. struct usb_wwan_port_private *portdata;
  592. struct urb *urb;
  593. int err = 0;
  594. dbg("%s entered", __func__);
  595. /* get the interrupt URBs resubmitted unconditionally */
  596. for (i = 0; i < serial->num_ports; i++) {
  597. port = serial->port[i];
  598. if (!port->interrupt_in_urb) {
  599. dbg("%s: No interrupt URB for port %d", __func__, i);
  600. continue;
  601. }
  602. err = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
  603. dbg("Submitted interrupt URB for port %d (result %d)", i, err);
  604. if (err < 0) {
  605. dev_err(&port->dev, "%s: Error %d for interrupt URB\n",
  606. __func__, err);
  607. goto err_out;
  608. }
  609. }
  610. for (i = 0; i < serial->num_ports; i++) {
  611. /* walk all ports */
  612. port = serial->port[i];
  613. portdata = usb_get_serial_port_data(port);
  614. /* skip closed ports */
  615. spin_lock_irq(&intfdata->susp_lock);
  616. if (!portdata->opened) {
  617. spin_unlock_irq(&intfdata->susp_lock);
  618. continue;
  619. }
  620. for (j = 0; j < N_IN_URB; j++) {
  621. urb = portdata->in_urbs[j];
  622. err = usb_submit_urb(urb, GFP_ATOMIC);
  623. if (err < 0) {
  624. dev_err(&port->dev, "%s: Error %d for bulk URB %d\n",
  625. __func__, err, i);
  626. spin_unlock_irq(&intfdata->susp_lock);
  627. goto err_out;
  628. }
  629. }
  630. play_delayed(port);
  631. spin_unlock_irq(&intfdata->susp_lock);
  632. }
  633. spin_lock_irq(&intfdata->susp_lock);
  634. intfdata->suspended = 0;
  635. spin_unlock_irq(&intfdata->susp_lock);
  636. err_out:
  637. return err;
  638. }
  639. EXPORT_SYMBOL(usb_wwan_resume);
  640. #endif
  641. MODULE_AUTHOR(DRIVER_AUTHOR);
  642. MODULE_DESCRIPTION(DRIVER_DESC);
  643. MODULE_VERSION(DRIVER_VERSION);
  644. MODULE_LICENSE("GPL");
  645. module_param(debug, bool, S_IRUGO | S_IWUSR);
  646. MODULE_PARM_DESC(debug, "Debug messages");