usb_wwan.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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_AUTHOR "Matthias Urlichs <smurf@smurf.noris.de>"
  16. #define DRIVER_DESC "USB Driver for GSM modems"
  17. #include <linux/kernel.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/errno.h>
  20. #include <linux/slab.h>
  21. #include <linux/tty.h>
  22. #include <linux/tty_flip.h>
  23. #include <linux/module.h>
  24. #include <linux/bitops.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/usb.h>
  27. #include <linux/usb/serial.h>
  28. #include <linux/serial.h>
  29. #include "usb-wwan.h"
  30. /*
  31. * Generate DTR/RTS signals on the port using the SET_CONTROL_LINE_STATE request
  32. * in CDC ACM.
  33. */
  34. static int usb_wwan_send_setup(struct usb_serial_port *port)
  35. {
  36. struct usb_serial *serial = port->serial;
  37. struct usb_wwan_port_private *portdata;
  38. int val = 0;
  39. int ifnum;
  40. int res;
  41. portdata = usb_get_serial_port_data(port);
  42. if (portdata->dtr_state)
  43. val |= 0x01;
  44. if (portdata->rts_state)
  45. val |= 0x02;
  46. ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
  47. res = usb_autopm_get_interface(serial->interface);
  48. if (res)
  49. return res;
  50. res = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  51. 0x22, 0x21, val, ifnum, NULL, 0,
  52. USB_CTRL_SET_TIMEOUT);
  53. usb_autopm_put_interface(port->serial->interface);
  54. return res;
  55. }
  56. void usb_wwan_dtr_rts(struct usb_serial_port *port, int on)
  57. {
  58. struct usb_wwan_port_private *portdata;
  59. struct usb_wwan_intf_private *intfdata;
  60. intfdata = usb_get_serial_data(port->serial);
  61. if (!intfdata->use_send_setup)
  62. return;
  63. portdata = usb_get_serial_port_data(port);
  64. /* FIXME: locking */
  65. portdata->rts_state = on;
  66. portdata->dtr_state = on;
  67. usb_wwan_send_setup(port);
  68. }
  69. EXPORT_SYMBOL(usb_wwan_dtr_rts);
  70. int usb_wwan_tiocmget(struct tty_struct *tty)
  71. {
  72. struct usb_serial_port *port = tty->driver_data;
  73. unsigned int value;
  74. struct usb_wwan_port_private *portdata;
  75. portdata = usb_get_serial_port_data(port);
  76. value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
  77. ((portdata->dtr_state) ? TIOCM_DTR : 0) |
  78. ((portdata->cts_state) ? TIOCM_CTS : 0) |
  79. ((portdata->dsr_state) ? TIOCM_DSR : 0) |
  80. ((portdata->dcd_state) ? TIOCM_CAR : 0) |
  81. ((portdata->ri_state) ? TIOCM_RNG : 0);
  82. return value;
  83. }
  84. EXPORT_SYMBOL(usb_wwan_tiocmget);
  85. int usb_wwan_tiocmset(struct tty_struct *tty,
  86. unsigned int set, unsigned int clear)
  87. {
  88. struct usb_serial_port *port = tty->driver_data;
  89. struct usb_wwan_port_private *portdata;
  90. struct usb_wwan_intf_private *intfdata;
  91. portdata = usb_get_serial_port_data(port);
  92. intfdata = usb_get_serial_data(port->serial);
  93. if (!intfdata->use_send_setup)
  94. return -EINVAL;
  95. /* FIXME: what locks portdata fields ? */
  96. if (set & TIOCM_RTS)
  97. portdata->rts_state = 1;
  98. if (set & TIOCM_DTR)
  99. portdata->dtr_state = 1;
  100. if (clear & TIOCM_RTS)
  101. portdata->rts_state = 0;
  102. if (clear & TIOCM_DTR)
  103. portdata->dtr_state = 0;
  104. return usb_wwan_send_setup(port);
  105. }
  106. EXPORT_SYMBOL(usb_wwan_tiocmset);
  107. static int get_serial_info(struct usb_serial_port *port,
  108. struct serial_struct __user *retinfo)
  109. {
  110. struct serial_struct tmp;
  111. if (!retinfo)
  112. return -EFAULT;
  113. memset(&tmp, 0, sizeof(tmp));
  114. tmp.line = port->minor;
  115. tmp.port = port->port_number;
  116. tmp.baud_base = tty_get_baud_rate(port->port.tty);
  117. tmp.close_delay = port->port.close_delay / 10;
  118. tmp.closing_wait = port->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
  119. ASYNC_CLOSING_WAIT_NONE :
  120. port->port.closing_wait / 10;
  121. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  122. return -EFAULT;
  123. return 0;
  124. }
  125. static int set_serial_info(struct usb_serial_port *port,
  126. struct serial_struct __user *newinfo)
  127. {
  128. struct serial_struct new_serial;
  129. unsigned int closing_wait, close_delay;
  130. int retval = 0;
  131. if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
  132. return -EFAULT;
  133. close_delay = new_serial.close_delay * 10;
  134. closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
  135. ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
  136. mutex_lock(&port->port.mutex);
  137. if (!capable(CAP_SYS_ADMIN)) {
  138. if ((close_delay != port->port.close_delay) ||
  139. (closing_wait != port->port.closing_wait))
  140. retval = -EPERM;
  141. else
  142. retval = -EOPNOTSUPP;
  143. } else {
  144. port->port.close_delay = close_delay;
  145. port->port.closing_wait = closing_wait;
  146. }
  147. mutex_unlock(&port->port.mutex);
  148. return retval;
  149. }
  150. int usb_wwan_ioctl(struct tty_struct *tty,
  151. unsigned int cmd, unsigned long arg)
  152. {
  153. struct usb_serial_port *port = tty->driver_data;
  154. dev_dbg(&port->dev, "%s cmd 0x%04x\n", __func__, cmd);
  155. switch (cmd) {
  156. case TIOCGSERIAL:
  157. return get_serial_info(port,
  158. (struct serial_struct __user *) arg);
  159. case TIOCSSERIAL:
  160. return set_serial_info(port,
  161. (struct serial_struct __user *) arg);
  162. default:
  163. break;
  164. }
  165. dev_dbg(&port->dev, "%s arg not supported\n", __func__);
  166. return -ENOIOCTLCMD;
  167. }
  168. EXPORT_SYMBOL(usb_wwan_ioctl);
  169. int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port,
  170. const unsigned char *buf, int count)
  171. {
  172. struct usb_wwan_port_private *portdata;
  173. struct usb_wwan_intf_private *intfdata;
  174. int i;
  175. int left, todo;
  176. struct urb *this_urb = NULL; /* spurious */
  177. int err;
  178. unsigned long flags;
  179. portdata = usb_get_serial_port_data(port);
  180. intfdata = usb_get_serial_data(port->serial);
  181. dev_dbg(&port->dev, "%s: write (%d chars)\n", __func__, count);
  182. i = 0;
  183. left = count;
  184. for (i = 0; left > 0 && i < N_OUT_URB; i++) {
  185. todo = left;
  186. if (todo > OUT_BUFLEN)
  187. todo = OUT_BUFLEN;
  188. this_urb = portdata->out_urbs[i];
  189. if (test_and_set_bit(i, &portdata->out_busy)) {
  190. if (time_before(jiffies,
  191. portdata->tx_start_time[i] + 10 * HZ))
  192. continue;
  193. usb_unlink_urb(this_urb);
  194. continue;
  195. }
  196. dev_dbg(&port->dev, "%s: endpoint %d buf %d\n", __func__,
  197. usb_pipeendpoint(this_urb->pipe), i);
  198. err = usb_autopm_get_interface_async(port->serial->interface);
  199. if (err < 0) {
  200. clear_bit(i, &portdata->out_busy);
  201. break;
  202. }
  203. /* send the data */
  204. memcpy(this_urb->transfer_buffer, buf, todo);
  205. this_urb->transfer_buffer_length = todo;
  206. spin_lock_irqsave(&intfdata->susp_lock, flags);
  207. if (intfdata->suspended) {
  208. usb_anchor_urb(this_urb, &portdata->delayed);
  209. spin_unlock_irqrestore(&intfdata->susp_lock, flags);
  210. } else {
  211. intfdata->in_flight++;
  212. spin_unlock_irqrestore(&intfdata->susp_lock, flags);
  213. err = usb_submit_urb(this_urb, GFP_ATOMIC);
  214. if (err) {
  215. dev_err(&port->dev,
  216. "%s: submit urb %d failed: %d\n",
  217. __func__, i, err);
  218. clear_bit(i, &portdata->out_busy);
  219. spin_lock_irqsave(&intfdata->susp_lock, flags);
  220. intfdata->in_flight--;
  221. spin_unlock_irqrestore(&intfdata->susp_lock,
  222. flags);
  223. usb_autopm_put_interface_async(port->serial->interface);
  224. break;
  225. }
  226. }
  227. portdata->tx_start_time[i] = jiffies;
  228. buf += todo;
  229. left -= todo;
  230. }
  231. count -= left;
  232. dev_dbg(&port->dev, "%s: wrote (did %d)\n", __func__, count);
  233. return count;
  234. }
  235. EXPORT_SYMBOL(usb_wwan_write);
  236. static void usb_wwan_indat_callback(struct urb *urb)
  237. {
  238. int err;
  239. int endpoint;
  240. struct usb_serial_port *port;
  241. struct device *dev;
  242. unsigned char *data = urb->transfer_buffer;
  243. int status = urb->status;
  244. endpoint = usb_pipeendpoint(urb->pipe);
  245. port = urb->context;
  246. dev = &port->dev;
  247. if (status) {
  248. dev_dbg(dev, "%s: nonzero status: %d on endpoint %02x.\n",
  249. __func__, status, endpoint);
  250. } else {
  251. if (urb->actual_length) {
  252. tty_insert_flip_string(&port->port, data,
  253. urb->actual_length);
  254. tty_flip_buffer_push(&port->port);
  255. } else
  256. dev_dbg(dev, "%s: empty read urb received\n", __func__);
  257. }
  258. /* Resubmit urb so we continue receiving */
  259. err = usb_submit_urb(urb, GFP_ATOMIC);
  260. if (err) {
  261. if (err != -EPERM && err != -ENODEV) {
  262. dev_err(dev, "%s: resubmit read urb failed. (%d)\n",
  263. __func__, err);
  264. /* busy also in error unless we are killed */
  265. usb_mark_last_busy(port->serial->dev);
  266. }
  267. } else {
  268. usb_mark_last_busy(port->serial->dev);
  269. }
  270. }
  271. static void usb_wwan_outdat_callback(struct urb *urb)
  272. {
  273. struct usb_serial_port *port;
  274. struct usb_wwan_port_private *portdata;
  275. struct usb_wwan_intf_private *intfdata;
  276. int i;
  277. port = urb->context;
  278. intfdata = usb_get_serial_data(port->serial);
  279. usb_serial_port_softint(port);
  280. usb_autopm_put_interface_async(port->serial->interface);
  281. portdata = usb_get_serial_port_data(port);
  282. spin_lock(&intfdata->susp_lock);
  283. intfdata->in_flight--;
  284. spin_unlock(&intfdata->susp_lock);
  285. for (i = 0; i < N_OUT_URB; ++i) {
  286. if (portdata->out_urbs[i] == urb) {
  287. smp_mb__before_atomic();
  288. clear_bit(i, &portdata->out_busy);
  289. break;
  290. }
  291. }
  292. }
  293. int usb_wwan_write_room(struct tty_struct *tty)
  294. {
  295. struct usb_serial_port *port = tty->driver_data;
  296. struct usb_wwan_port_private *portdata;
  297. int i;
  298. int data_len = 0;
  299. struct urb *this_urb;
  300. portdata = usb_get_serial_port_data(port);
  301. for (i = 0; i < N_OUT_URB; i++) {
  302. this_urb = portdata->out_urbs[i];
  303. if (this_urb && !test_bit(i, &portdata->out_busy))
  304. data_len += OUT_BUFLEN;
  305. }
  306. dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
  307. return data_len;
  308. }
  309. EXPORT_SYMBOL(usb_wwan_write_room);
  310. int usb_wwan_chars_in_buffer(struct tty_struct *tty)
  311. {
  312. struct usb_serial_port *port = tty->driver_data;
  313. struct usb_wwan_port_private *portdata;
  314. int i;
  315. int data_len = 0;
  316. struct urb *this_urb;
  317. portdata = usb_get_serial_port_data(port);
  318. for (i = 0; i < N_OUT_URB; i++) {
  319. this_urb = portdata->out_urbs[i];
  320. /* FIXME: This locking is insufficient as this_urb may
  321. go unused during the test */
  322. if (this_urb && test_bit(i, &portdata->out_busy))
  323. data_len += this_urb->transfer_buffer_length;
  324. }
  325. dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
  326. return data_len;
  327. }
  328. EXPORT_SYMBOL(usb_wwan_chars_in_buffer);
  329. int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port)
  330. {
  331. struct usb_wwan_port_private *portdata;
  332. struct usb_wwan_intf_private *intfdata;
  333. struct usb_serial *serial = port->serial;
  334. int i, err;
  335. struct urb *urb;
  336. portdata = usb_get_serial_port_data(port);
  337. intfdata = usb_get_serial_data(serial);
  338. if (port->interrupt_in_urb) {
  339. err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  340. if (err) {
  341. dev_err(&port->dev, "%s: submit int urb failed: %d\n",
  342. __func__, err);
  343. }
  344. }
  345. /* Start reading from the IN endpoint */
  346. for (i = 0; i < N_IN_URB; i++) {
  347. urb = portdata->in_urbs[i];
  348. if (!urb)
  349. continue;
  350. err = usb_submit_urb(urb, GFP_KERNEL);
  351. if (err) {
  352. dev_err(&port->dev,
  353. "%s: submit read urb %d failed: %d\n",
  354. __func__, i, err);
  355. }
  356. }
  357. spin_lock_irq(&intfdata->susp_lock);
  358. if (++intfdata->open_ports == 1)
  359. serial->interface->needs_remote_wakeup = 1;
  360. spin_unlock_irq(&intfdata->susp_lock);
  361. /* this balances a get in the generic USB serial code */
  362. usb_autopm_put_interface(serial->interface);
  363. return 0;
  364. }
  365. EXPORT_SYMBOL(usb_wwan_open);
  366. static void unbusy_queued_urb(struct urb *urb,
  367. struct usb_wwan_port_private *portdata)
  368. {
  369. int i;
  370. for (i = 0; i < N_OUT_URB; i++) {
  371. if (urb == portdata->out_urbs[i]) {
  372. clear_bit(i, &portdata->out_busy);
  373. break;
  374. }
  375. }
  376. }
  377. void usb_wwan_close(struct usb_serial_port *port)
  378. {
  379. int i;
  380. struct usb_serial *serial = port->serial;
  381. struct usb_wwan_port_private *portdata;
  382. struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
  383. struct urb *urb;
  384. portdata = usb_get_serial_port_data(port);
  385. /*
  386. * Need to take susp_lock to make sure port is not already being
  387. * resumed, but no need to hold it due to initialized
  388. */
  389. spin_lock_irq(&intfdata->susp_lock);
  390. if (--intfdata->open_ports == 0)
  391. serial->interface->needs_remote_wakeup = 0;
  392. spin_unlock_irq(&intfdata->susp_lock);
  393. for (;;) {
  394. urb = usb_get_from_anchor(&portdata->delayed);
  395. if (!urb)
  396. break;
  397. unbusy_queued_urb(urb, portdata);
  398. usb_autopm_put_interface_async(serial->interface);
  399. }
  400. for (i = 0; i < N_IN_URB; i++)
  401. usb_kill_urb(portdata->in_urbs[i]);
  402. for (i = 0; i < N_OUT_URB; i++)
  403. usb_kill_urb(portdata->out_urbs[i]);
  404. usb_kill_urb(port->interrupt_in_urb);
  405. usb_autopm_get_interface_no_resume(serial->interface);
  406. }
  407. EXPORT_SYMBOL(usb_wwan_close);
  408. static struct urb *usb_wwan_setup_urb(struct usb_serial_port *port,
  409. int endpoint,
  410. int dir, void *ctx, char *buf, int len,
  411. void (*callback) (struct urb *))
  412. {
  413. struct usb_serial *serial = port->serial;
  414. struct urb *urb;
  415. urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
  416. if (!urb)
  417. return NULL;
  418. usb_fill_bulk_urb(urb, serial->dev,
  419. usb_sndbulkpipe(serial->dev, endpoint) | dir,
  420. buf, len, callback, ctx);
  421. /*+++ vern, Added for zero packet,20190422 +++*/
  422. #if 1
  423. if (dir == USB_DIR_OUT)
  424. {
  425. struct usb_device_descriptor *desc = &serial->dev->descriptor;
  426. if (desc->idVendor == cpu_to_le16(0x05C6) && desc->idProduct == cpu_to_le16(0x9090))
  427. urb->transfer_flags |= URB_ZERO_PACKET;
  428. if (desc->idVendor == cpu_to_le16(0x05C6) && desc->idProduct == cpu_to_le16(0x9003))
  429. urb->transfer_flags |= URB_ZERO_PACKET;
  430. if (desc->idVendor == cpu_to_le16(0x05C6) && desc->idProduct == cpu_to_le16(0x9215))
  431. urb->transfer_flags |= URB_ZERO_PACKET;
  432. if (desc->idVendor == cpu_to_le16(0x2C7C))
  433. urb->transfer_flags |= URB_ZERO_PACKET;
  434. }
  435. #endif
  436. /* --- vern, Added for zero packet,20190422 ----*/
  437. return urb;
  438. }
  439. int usb_wwan_port_probe(struct usb_serial_port *port)
  440. {
  441. struct usb_wwan_port_private *portdata;
  442. struct urb *urb;
  443. u8 *buffer;
  444. int i;
  445. if (!port->bulk_in_size || !port->bulk_out_size)
  446. return -ENODEV;
  447. portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
  448. if (!portdata)
  449. return -ENOMEM;
  450. init_usb_anchor(&portdata->delayed);
  451. for (i = 0; i < N_IN_URB; i++) {
  452. buffer = (u8 *)__get_free_page(GFP_KERNEL);
  453. if (!buffer)
  454. goto bail_out_error;
  455. portdata->in_buffer[i] = buffer;
  456. urb = usb_wwan_setup_urb(port, port->bulk_in_endpointAddress,
  457. USB_DIR_IN, port,
  458. buffer, IN_BUFLEN,
  459. usb_wwan_indat_callback);
  460. portdata->in_urbs[i] = urb;
  461. }
  462. for (i = 0; i < N_OUT_URB; i++) {
  463. buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL);
  464. if (!buffer)
  465. goto bail_out_error2;
  466. portdata->out_buffer[i] = buffer;
  467. urb = usb_wwan_setup_urb(port, port->bulk_out_endpointAddress,
  468. USB_DIR_OUT, port,
  469. buffer, OUT_BUFLEN,
  470. usb_wwan_outdat_callback);
  471. portdata->out_urbs[i] = urb;
  472. }
  473. usb_set_serial_port_data(port, portdata);
  474. return 0;
  475. bail_out_error2:
  476. for (i = 0; i < N_OUT_URB; i++) {
  477. usb_free_urb(portdata->out_urbs[i]);
  478. kfree(portdata->out_buffer[i]);
  479. }
  480. bail_out_error:
  481. for (i = 0; i < N_IN_URB; i++) {
  482. usb_free_urb(portdata->in_urbs[i]);
  483. free_page((unsigned long)portdata->in_buffer[i]);
  484. }
  485. kfree(portdata);
  486. return -ENOMEM;
  487. }
  488. EXPORT_SYMBOL_GPL(usb_wwan_port_probe);
  489. int usb_wwan_port_remove(struct usb_serial_port *port)
  490. {
  491. int i;
  492. struct usb_wwan_port_private *portdata;
  493. portdata = usb_get_serial_port_data(port);
  494. usb_set_serial_port_data(port, NULL);
  495. for (i = 0; i < N_IN_URB; i++) {
  496. usb_free_urb(portdata->in_urbs[i]);
  497. free_page((unsigned long)portdata->in_buffer[i]);
  498. }
  499. for (i = 0; i < N_OUT_URB; i++) {
  500. usb_free_urb(portdata->out_urbs[i]);
  501. kfree(portdata->out_buffer[i]);
  502. }
  503. kfree(portdata);
  504. return 0;
  505. }
  506. EXPORT_SYMBOL(usb_wwan_port_remove);
  507. #ifdef CONFIG_PM
  508. static void stop_urbs(struct usb_serial *serial)
  509. {
  510. int i, j;
  511. struct usb_serial_port *port;
  512. struct usb_wwan_port_private *portdata;
  513. for (i = 0; i < serial->num_ports; ++i) {
  514. port = serial->port[i];
  515. portdata = usb_get_serial_port_data(port);
  516. if (!portdata)
  517. continue;
  518. for (j = 0; j < N_IN_URB; j++)
  519. usb_kill_urb(portdata->in_urbs[j]);
  520. for (j = 0; j < N_OUT_URB; j++)
  521. usb_kill_urb(portdata->out_urbs[j]);
  522. usb_kill_urb(port->interrupt_in_urb);
  523. }
  524. }
  525. int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message)
  526. {
  527. struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
  528. spin_lock_irq(&intfdata->susp_lock);
  529. if (PMSG_IS_AUTO(message)) {
  530. if (intfdata->in_flight) {
  531. spin_unlock_irq(&intfdata->susp_lock);
  532. return -EBUSY;
  533. }
  534. }
  535. intfdata->suspended = 1;
  536. spin_unlock_irq(&intfdata->susp_lock);
  537. stop_urbs(serial);
  538. return 0;
  539. }
  540. EXPORT_SYMBOL(usb_wwan_suspend);
  541. /* Caller must hold susp_lock. */
  542. static int usb_wwan_submit_delayed_urbs(struct usb_serial_port *port)
  543. {
  544. struct usb_serial *serial = port->serial;
  545. struct usb_wwan_intf_private *data = usb_get_serial_data(serial);
  546. struct usb_wwan_port_private *portdata;
  547. struct urb *urb;
  548. int err_count = 0;
  549. int err;
  550. portdata = usb_get_serial_port_data(port);
  551. for (;;) {
  552. urb = usb_get_from_anchor(&portdata->delayed);
  553. if (!urb)
  554. break;
  555. err = usb_submit_urb(urb, GFP_ATOMIC);
  556. if (err) {
  557. dev_err(&port->dev, "%s: submit urb failed: %d\n",
  558. __func__, err);
  559. err_count++;
  560. unbusy_queued_urb(urb, portdata);
  561. usb_autopm_put_interface_async(serial->interface);
  562. continue;
  563. }
  564. data->in_flight++;
  565. }
  566. if (err_count)
  567. return -EIO;
  568. return 0;
  569. }
  570. int usb_wwan_resume(struct usb_serial *serial)
  571. {
  572. int i, j;
  573. struct usb_serial_port *port;
  574. struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
  575. struct usb_wwan_port_private *portdata;
  576. struct urb *urb;
  577. int err;
  578. int err_count = 0;
  579. spin_lock_irq(&intfdata->susp_lock);
  580. for (i = 0; i < serial->num_ports; i++) {
  581. port = serial->port[i];
  582. if (!tty_port_initialized(&port->port))
  583. continue;
  584. portdata = usb_get_serial_port_data(port);
  585. if (port->interrupt_in_urb) {
  586. err = usb_submit_urb(port->interrupt_in_urb,
  587. GFP_ATOMIC);
  588. if (err) {
  589. dev_err(&port->dev,
  590. "%s: submit int urb failed: %d\n",
  591. __func__, err);
  592. err_count++;
  593. }
  594. }
  595. err = usb_wwan_submit_delayed_urbs(port);
  596. if (err)
  597. err_count++;
  598. for (j = 0; j < N_IN_URB; j++) {
  599. urb = portdata->in_urbs[j];
  600. err = usb_submit_urb(urb, GFP_ATOMIC);
  601. if (err < 0) {
  602. dev_err(&port->dev,
  603. "%s: submit read urb %d failed: %d\n",
  604. __func__, i, err);
  605. err_count++;
  606. }
  607. }
  608. }
  609. intfdata->suspended = 0;
  610. spin_unlock_irq(&intfdata->susp_lock);
  611. if (err_count)
  612. return -EIO;
  613. return 0;
  614. }
  615. EXPORT_SYMBOL(usb_wwan_resume);
  616. #endif
  617. MODULE_AUTHOR(DRIVER_AUTHOR);
  618. MODULE_DESCRIPTION(DRIVER_DESC);
  619. MODULE_LICENSE("GPL");