cdc-wdm.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * cdc-wdm.c
  4. *
  5. * This driver supports USB CDC WCM Device Management.
  6. *
  7. * Copyright (c) 2007-2009 Oliver Neukum
  8. *
  9. * Some code taken from cdc-acm.c
  10. *
  11. * Released under the GPLv2.
  12. *
  13. * Many thanks to Carl Nordbeck
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/errno.h>
  17. #include <linux/ioctl.h>
  18. #include <linux/slab.h>
  19. #include <linux/module.h>
  20. #include <linux/mutex.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/bitops.h>
  23. #include <linux/poll.h>
  24. #include <linux/skbuff.h>
  25. #include <linux/usb.h>
  26. #include <linux/usb/cdc.h>
  27. #include <linux/wwan.h>
  28. #include <asm/byteorder.h>
  29. #include <linux/unaligned.h>
  30. #include <linux/usb/cdc-wdm.h>
  31. #define DRIVER_AUTHOR "Oliver Neukum"
  32. #define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management"
  33. static const struct usb_device_id wdm_ids[] = {
  34. {
  35. .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
  36. USB_DEVICE_ID_MATCH_INT_SUBCLASS,
  37. .bInterfaceClass = USB_CLASS_COMM,
  38. .bInterfaceSubClass = USB_CDC_SUBCLASS_DMM
  39. },
  40. { }
  41. };
  42. MODULE_DEVICE_TABLE (usb, wdm_ids);
  43. #define WDM_MINOR_BASE 176
  44. #define WDM_IN_USE 1
  45. #define WDM_DISCONNECTING 2
  46. #define WDM_RESULT 3
  47. #define WDM_READ 4
  48. #define WDM_INT_STALL 5
  49. #define WDM_POLL_RUNNING 6
  50. #define WDM_RESPONDING 7
  51. #define WDM_SUSPENDING 8
  52. #define WDM_RESETTING 9
  53. #define WDM_OVERFLOW 10
  54. #define WDM_WWAN_IN_USE 11
  55. #define WDM_MAX 16
  56. /* we cannot wait forever at flush() */
  57. #define WDM_FLUSH_TIMEOUT (30 * HZ)
  58. /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
  59. #define WDM_DEFAULT_BUFSIZE 256
  60. static DEFINE_MUTEX(wdm_mutex);
  61. static DEFINE_SPINLOCK(wdm_device_list_lock);
  62. static LIST_HEAD(wdm_device_list);
  63. /* --- method tables --- */
  64. struct wdm_device {
  65. u8 *inbuf; /* buffer for response */
  66. u8 *outbuf; /* buffer for command */
  67. u8 *sbuf; /* buffer for status */
  68. u8 *ubuf; /* buffer for copy to user space */
  69. struct urb *command;
  70. struct urb *response;
  71. struct urb *validity;
  72. struct usb_interface *intf;
  73. struct usb_ctrlrequest *orq;
  74. struct usb_ctrlrequest *irq;
  75. spinlock_t iuspin;
  76. unsigned long flags;
  77. u16 bufsize;
  78. u16 wMaxCommand;
  79. u16 wMaxPacketSize;
  80. __le16 inum;
  81. int length;
  82. int read;
  83. int count;
  84. dma_addr_t shandle;
  85. dma_addr_t ihandle;
  86. struct mutex wlock;
  87. struct mutex rlock;
  88. wait_queue_head_t wait;
  89. struct work_struct rxwork;
  90. struct work_struct service_outs_intr;
  91. int werr;
  92. int rerr;
  93. int resp_count;
  94. struct list_head device_list;
  95. int (*manage_power)(struct usb_interface *, int);
  96. enum wwan_port_type wwanp_type;
  97. struct wwan_port *wwanp;
  98. };
  99. static struct usb_driver wdm_driver;
  100. /* return intfdata if we own the interface, else look up intf in the list */
  101. static struct wdm_device *wdm_find_device(struct usb_interface *intf)
  102. {
  103. struct wdm_device *desc;
  104. spin_lock(&wdm_device_list_lock);
  105. list_for_each_entry(desc, &wdm_device_list, device_list)
  106. if (desc->intf == intf)
  107. goto found;
  108. desc = NULL;
  109. found:
  110. spin_unlock(&wdm_device_list_lock);
  111. return desc;
  112. }
  113. static struct wdm_device *wdm_find_device_by_minor(int minor)
  114. {
  115. struct wdm_device *desc;
  116. spin_lock(&wdm_device_list_lock);
  117. list_for_each_entry(desc, &wdm_device_list, device_list)
  118. if (desc->intf->minor == minor)
  119. goto found;
  120. desc = NULL;
  121. found:
  122. spin_unlock(&wdm_device_list_lock);
  123. return desc;
  124. }
  125. /* --- callbacks --- */
  126. static void wdm_out_callback(struct urb *urb)
  127. {
  128. struct wdm_device *desc;
  129. unsigned long flags;
  130. desc = urb->context;
  131. spin_lock_irqsave(&desc->iuspin, flags);
  132. desc->werr = urb->status;
  133. spin_unlock_irqrestore(&desc->iuspin, flags);
  134. kfree(desc->outbuf);
  135. desc->outbuf = NULL;
  136. clear_bit(WDM_IN_USE, &desc->flags);
  137. wake_up_all(&desc->wait);
  138. }
  139. static void wdm_wwan_rx(struct wdm_device *desc, int length);
  140. static void wdm_in_callback(struct urb *urb)
  141. {
  142. unsigned long flags;
  143. struct wdm_device *desc = urb->context;
  144. int status = urb->status;
  145. int length = urb->actual_length;
  146. spin_lock_irqsave(&desc->iuspin, flags);
  147. clear_bit(WDM_RESPONDING, &desc->flags);
  148. if (status) {
  149. switch (status) {
  150. case -ENOENT:
  151. dev_dbg(&desc->intf->dev,
  152. "nonzero urb status received: -ENOENT\n");
  153. goto skip_error;
  154. case -ECONNRESET:
  155. dev_dbg(&desc->intf->dev,
  156. "nonzero urb status received: -ECONNRESET\n");
  157. goto skip_error;
  158. case -ESHUTDOWN:
  159. dev_dbg(&desc->intf->dev,
  160. "nonzero urb status received: -ESHUTDOWN\n");
  161. goto skip_error;
  162. case -EPIPE:
  163. dev_err(&desc->intf->dev,
  164. "nonzero urb status received: -EPIPE\n");
  165. break;
  166. default:
  167. dev_err(&desc->intf->dev,
  168. "Unexpected error %d\n", status);
  169. break;
  170. }
  171. }
  172. if (test_bit(WDM_WWAN_IN_USE, &desc->flags)) {
  173. wdm_wwan_rx(desc, length);
  174. goto out;
  175. }
  176. /*
  177. * only set a new error if there is no previous error.
  178. * Errors are only cleared during read/open
  179. * Avoid propagating -EPIPE (stall) to userspace since it is
  180. * better handled as an empty read
  181. */
  182. if (desc->rerr == 0 && status != -EPIPE)
  183. desc->rerr = status;
  184. if (length == 0) {
  185. dev_dbg(&desc->intf->dev, "received ZLP\n");
  186. goto skip_zlp;
  187. }
  188. if (length + desc->length > desc->wMaxCommand) {
  189. /* The buffer would overflow */
  190. set_bit(WDM_OVERFLOW, &desc->flags);
  191. } else {
  192. /* we may already be in overflow */
  193. if (!test_bit(WDM_OVERFLOW, &desc->flags)) {
  194. memmove(desc->ubuf + desc->length, desc->inbuf, length);
  195. desc->length += length;
  196. }
  197. }
  198. skip_error:
  199. if (desc->rerr) {
  200. /*
  201. * If there was a ZLP or an error, userspace may decide to not
  202. * read any data after poll'ing.
  203. * We should respond to further attempts from the device to send
  204. * data, so that we can get unstuck.
  205. */
  206. skip_zlp:
  207. schedule_work(&desc->service_outs_intr);
  208. } else {
  209. set_bit(WDM_READ, &desc->flags);
  210. wake_up(&desc->wait);
  211. }
  212. out:
  213. spin_unlock_irqrestore(&desc->iuspin, flags);
  214. }
  215. static void wdm_int_callback(struct urb *urb)
  216. {
  217. unsigned long flags;
  218. int rv = 0;
  219. int responding;
  220. int status = urb->status;
  221. struct wdm_device *desc;
  222. struct usb_cdc_notification *dr;
  223. desc = urb->context;
  224. dr = (struct usb_cdc_notification *)desc->sbuf;
  225. if (status) {
  226. switch (status) {
  227. case -ESHUTDOWN:
  228. case -ENOENT:
  229. case -ECONNRESET:
  230. return; /* unplug */
  231. case -EPIPE:
  232. set_bit(WDM_INT_STALL, &desc->flags);
  233. dev_err(&desc->intf->dev, "Stall on int endpoint\n");
  234. goto sw; /* halt is cleared in work */
  235. default:
  236. dev_err_ratelimited(&desc->intf->dev,
  237. "nonzero urb status received: %d\n", status);
  238. break;
  239. }
  240. }
  241. if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
  242. dev_err_ratelimited(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
  243. urb->actual_length);
  244. goto exit;
  245. }
  246. switch (dr->bNotificationType) {
  247. case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
  248. dev_dbg(&desc->intf->dev,
  249. "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d\n",
  250. le16_to_cpu(dr->wIndex), le16_to_cpu(dr->wLength));
  251. break;
  252. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  253. dev_dbg(&desc->intf->dev,
  254. "NOTIFY_NETWORK_CONNECTION %s network\n",
  255. dr->wValue ? "connected to" : "disconnected from");
  256. goto exit;
  257. case USB_CDC_NOTIFY_SPEED_CHANGE:
  258. dev_dbg(&desc->intf->dev, "SPEED_CHANGE received (len %u)\n",
  259. urb->actual_length);
  260. goto exit;
  261. default:
  262. clear_bit(WDM_POLL_RUNNING, &desc->flags);
  263. dev_err(&desc->intf->dev,
  264. "unknown notification %d received: index %d len %d\n",
  265. dr->bNotificationType,
  266. le16_to_cpu(dr->wIndex),
  267. le16_to_cpu(dr->wLength));
  268. goto exit;
  269. }
  270. spin_lock_irqsave(&desc->iuspin, flags);
  271. responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
  272. if (!desc->resp_count++ && !responding
  273. && !test_bit(WDM_DISCONNECTING, &desc->flags)
  274. && !test_bit(WDM_SUSPENDING, &desc->flags)) {
  275. rv = usb_submit_urb(desc->response, GFP_ATOMIC);
  276. dev_dbg(&desc->intf->dev, "submit response URB %d\n", rv);
  277. }
  278. spin_unlock_irqrestore(&desc->iuspin, flags);
  279. if (rv < 0) {
  280. clear_bit(WDM_RESPONDING, &desc->flags);
  281. if (rv == -EPERM)
  282. return;
  283. if (rv == -ENOMEM) {
  284. sw:
  285. rv = schedule_work(&desc->rxwork);
  286. if (rv)
  287. dev_err(&desc->intf->dev,
  288. "Cannot schedule work\n");
  289. }
  290. }
  291. exit:
  292. rv = usb_submit_urb(urb, GFP_ATOMIC);
  293. if (rv)
  294. dev_err(&desc->intf->dev,
  295. "%s - usb_submit_urb failed with result %d\n",
  296. __func__, rv);
  297. }
  298. static void poison_urbs(struct wdm_device *desc)
  299. {
  300. /* the order here is essential */
  301. usb_poison_urb(desc->command);
  302. usb_poison_urb(desc->validity);
  303. usb_poison_urb(desc->response);
  304. }
  305. static void unpoison_urbs(struct wdm_device *desc)
  306. {
  307. /*
  308. * the order here is not essential
  309. * it is symmetrical just to be nice
  310. */
  311. usb_unpoison_urb(desc->response);
  312. usb_unpoison_urb(desc->validity);
  313. usb_unpoison_urb(desc->command);
  314. }
  315. static void free_urbs(struct wdm_device *desc)
  316. {
  317. usb_free_urb(desc->validity);
  318. usb_free_urb(desc->response);
  319. usb_free_urb(desc->command);
  320. }
  321. static void cleanup(struct wdm_device *desc)
  322. {
  323. kfree(desc->sbuf);
  324. kfree(desc->inbuf);
  325. kfree(desc->orq);
  326. kfree(desc->irq);
  327. kfree(desc->ubuf);
  328. free_urbs(desc);
  329. kfree(desc);
  330. }
  331. static ssize_t wdm_write
  332. (struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  333. {
  334. u8 *buf;
  335. int rv = -EMSGSIZE, r, we;
  336. struct wdm_device *desc = file->private_data;
  337. struct usb_ctrlrequest *req;
  338. if (count > desc->wMaxCommand)
  339. count = desc->wMaxCommand;
  340. spin_lock_irq(&desc->iuspin);
  341. we = desc->werr;
  342. desc->werr = 0;
  343. spin_unlock_irq(&desc->iuspin);
  344. if (we < 0)
  345. return usb_translate_errors(we);
  346. buf = memdup_user(buffer, count);
  347. if (IS_ERR(buf))
  348. return PTR_ERR(buf);
  349. /* concurrent writes and disconnect */
  350. r = mutex_lock_interruptible(&desc->wlock);
  351. rv = -ERESTARTSYS;
  352. if (r)
  353. goto out_free_mem;
  354. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  355. rv = -ENODEV;
  356. goto out_free_mem_lock;
  357. }
  358. r = usb_autopm_get_interface(desc->intf);
  359. if (r < 0) {
  360. rv = usb_translate_errors(r);
  361. goto out_free_mem_lock;
  362. }
  363. if (!(file->f_flags & O_NONBLOCK))
  364. r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
  365. &desc->flags));
  366. else
  367. if (test_bit(WDM_IN_USE, &desc->flags))
  368. r = -EAGAIN;
  369. if (test_bit(WDM_RESETTING, &desc->flags))
  370. r = -EIO;
  371. if (test_bit(WDM_DISCONNECTING, &desc->flags))
  372. r = -ENODEV;
  373. if (r < 0) {
  374. rv = r;
  375. goto out_free_mem_pm;
  376. }
  377. req = desc->orq;
  378. usb_fill_control_urb(
  379. desc->command,
  380. interface_to_usbdev(desc->intf),
  381. /* using common endpoint 0 */
  382. usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
  383. (unsigned char *)req,
  384. buf,
  385. count,
  386. wdm_out_callback,
  387. desc
  388. );
  389. req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
  390. USB_RECIP_INTERFACE);
  391. req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
  392. req->wValue = 0;
  393. req->wIndex = desc->inum; /* already converted */
  394. req->wLength = cpu_to_le16(count);
  395. set_bit(WDM_IN_USE, &desc->flags);
  396. desc->outbuf = buf;
  397. rv = usb_submit_urb(desc->command, GFP_KERNEL);
  398. if (rv < 0) {
  399. desc->outbuf = NULL;
  400. clear_bit(WDM_IN_USE, &desc->flags);
  401. wake_up_all(&desc->wait); /* for wdm_wait_for_response() */
  402. dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
  403. rv = usb_translate_errors(rv);
  404. goto out_free_mem_pm;
  405. } else {
  406. dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d\n",
  407. le16_to_cpu(req->wIndex));
  408. }
  409. usb_autopm_put_interface(desc->intf);
  410. mutex_unlock(&desc->wlock);
  411. return count;
  412. out_free_mem_pm:
  413. usb_autopm_put_interface(desc->intf);
  414. out_free_mem_lock:
  415. mutex_unlock(&desc->wlock);
  416. out_free_mem:
  417. kfree(buf);
  418. return rv;
  419. }
  420. /*
  421. * Submit the read urb if resp_count is non-zero.
  422. *
  423. * Called with desc->iuspin locked
  424. */
  425. static int service_outstanding_interrupt(struct wdm_device *desc)
  426. {
  427. int rv = 0;
  428. /* submit read urb only if the device is waiting for it */
  429. if (!desc->resp_count || !--desc->resp_count)
  430. goto out;
  431. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  432. rv = -ENODEV;
  433. goto out;
  434. }
  435. if (test_bit(WDM_RESETTING, &desc->flags)) {
  436. rv = -EIO;
  437. goto out;
  438. }
  439. set_bit(WDM_RESPONDING, &desc->flags);
  440. spin_unlock_irq(&desc->iuspin);
  441. rv = usb_submit_urb(desc->response, GFP_KERNEL);
  442. spin_lock_irq(&desc->iuspin);
  443. if (rv) {
  444. if (!test_bit(WDM_DISCONNECTING, &desc->flags))
  445. dev_err(&desc->intf->dev,
  446. "usb_submit_urb failed with result %d\n", rv);
  447. /* make sure the next notification trigger a submit */
  448. clear_bit(WDM_RESPONDING, &desc->flags);
  449. desc->resp_count = 0;
  450. }
  451. out:
  452. return rv;
  453. }
  454. static ssize_t wdm_read
  455. (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  456. {
  457. int rv, cntr;
  458. int i = 0;
  459. struct wdm_device *desc = file->private_data;
  460. rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
  461. if (rv < 0)
  462. return -ERESTARTSYS;
  463. cntr = READ_ONCE(desc->length);
  464. if (cntr == 0) {
  465. desc->read = 0;
  466. retry:
  467. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  468. rv = -ENODEV;
  469. goto err;
  470. }
  471. if (test_bit(WDM_OVERFLOW, &desc->flags)) {
  472. clear_bit(WDM_OVERFLOW, &desc->flags);
  473. rv = -ENOBUFS;
  474. goto err;
  475. }
  476. i++;
  477. if (file->f_flags & O_NONBLOCK) {
  478. if (!test_bit(WDM_READ, &desc->flags)) {
  479. rv = -EAGAIN;
  480. goto err;
  481. }
  482. rv = 0;
  483. } else {
  484. rv = wait_event_interruptible(desc->wait,
  485. test_bit(WDM_READ, &desc->flags));
  486. }
  487. /* may have happened while we slept */
  488. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  489. rv = -ENODEV;
  490. goto err;
  491. }
  492. if (test_bit(WDM_RESETTING, &desc->flags)) {
  493. rv = -EIO;
  494. goto err;
  495. }
  496. usb_mark_last_busy(interface_to_usbdev(desc->intf));
  497. if (rv < 0) {
  498. rv = -ERESTARTSYS;
  499. goto err;
  500. }
  501. spin_lock_irq(&desc->iuspin);
  502. if (desc->rerr) { /* read completed, error happened */
  503. rv = usb_translate_errors(desc->rerr);
  504. desc->rerr = 0;
  505. spin_unlock_irq(&desc->iuspin);
  506. goto err;
  507. }
  508. /*
  509. * recheck whether we've lost the race
  510. * against the completion handler
  511. */
  512. if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
  513. spin_unlock_irq(&desc->iuspin);
  514. goto retry;
  515. }
  516. cntr = desc->length;
  517. spin_unlock_irq(&desc->iuspin);
  518. }
  519. if (cntr > count)
  520. cntr = count;
  521. rv = copy_to_user(buffer, desc->ubuf, cntr);
  522. if (rv > 0) {
  523. rv = -EFAULT;
  524. goto err;
  525. }
  526. spin_lock_irq(&desc->iuspin);
  527. for (i = 0; i < desc->length - cntr; i++)
  528. desc->ubuf[i] = desc->ubuf[i + cntr];
  529. desc->length -= cntr;
  530. /* in case we had outstanding data */
  531. if (!desc->length) {
  532. clear_bit(WDM_READ, &desc->flags);
  533. service_outstanding_interrupt(desc);
  534. }
  535. spin_unlock_irq(&desc->iuspin);
  536. rv = cntr;
  537. err:
  538. mutex_unlock(&desc->rlock);
  539. return rv;
  540. }
  541. static int wdm_wait_for_response(struct file *file, long timeout)
  542. {
  543. struct wdm_device *desc = file->private_data;
  544. long rv; /* Use long here because (int) MAX_SCHEDULE_TIMEOUT < 0. */
  545. /*
  546. * Needs both flags. We cannot do with one because resetting it would
  547. * cause a race with write() yet we need to signal a disconnect.
  548. */
  549. rv = wait_event_interruptible_timeout(desc->wait,
  550. !test_bit(WDM_IN_USE, &desc->flags) ||
  551. test_bit(WDM_DISCONNECTING, &desc->flags),
  552. timeout);
  553. /*
  554. * To report the correct error. This is best effort.
  555. * We are inevitably racing with the hardware.
  556. */
  557. if (test_bit(WDM_DISCONNECTING, &desc->flags))
  558. return -ENODEV;
  559. if (!rv)
  560. return -EIO;
  561. if (rv < 0)
  562. return -EINTR;
  563. spin_lock_irq(&desc->iuspin);
  564. rv = desc->werr;
  565. desc->werr = 0;
  566. spin_unlock_irq(&desc->iuspin);
  567. return usb_translate_errors(rv);
  568. }
  569. /*
  570. * You need to send a signal when you react to malicious or defective hardware.
  571. * Also, don't abort when fsync() returned -EINVAL, for older kernels which do
  572. * not implement wdm_flush() will return -EINVAL.
  573. */
  574. static int wdm_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  575. {
  576. return wdm_wait_for_response(file, MAX_SCHEDULE_TIMEOUT);
  577. }
  578. /*
  579. * Same with wdm_fsync(), except it uses finite timeout in order to react to
  580. * malicious or defective hardware which ceased communication after close() was
  581. * implicitly called due to process termination.
  582. */
  583. static int wdm_flush(struct file *file, fl_owner_t id)
  584. {
  585. return wdm_wait_for_response(file, WDM_FLUSH_TIMEOUT);
  586. }
  587. static __poll_t wdm_poll(struct file *file, struct poll_table_struct *wait)
  588. {
  589. struct wdm_device *desc = file->private_data;
  590. unsigned long flags;
  591. __poll_t mask = 0;
  592. spin_lock_irqsave(&desc->iuspin, flags);
  593. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  594. mask = EPOLLHUP | EPOLLERR;
  595. spin_unlock_irqrestore(&desc->iuspin, flags);
  596. goto desc_out;
  597. }
  598. if (test_bit(WDM_READ, &desc->flags))
  599. mask = EPOLLIN | EPOLLRDNORM;
  600. if (desc->rerr || desc->werr)
  601. mask |= EPOLLERR;
  602. if (!test_bit(WDM_IN_USE, &desc->flags))
  603. mask |= EPOLLOUT | EPOLLWRNORM;
  604. spin_unlock_irqrestore(&desc->iuspin, flags);
  605. poll_wait(file, &desc->wait, wait);
  606. desc_out:
  607. return mask;
  608. }
  609. static int wdm_open(struct inode *inode, struct file *file)
  610. {
  611. int minor = iminor(inode);
  612. int rv = -ENODEV;
  613. struct usb_interface *intf;
  614. struct wdm_device *desc;
  615. mutex_lock(&wdm_mutex);
  616. desc = wdm_find_device_by_minor(minor);
  617. if (!desc)
  618. goto out;
  619. intf = desc->intf;
  620. if (test_bit(WDM_DISCONNECTING, &desc->flags))
  621. goto out;
  622. file->private_data = desc;
  623. if (test_bit(WDM_WWAN_IN_USE, &desc->flags)) {
  624. rv = -EBUSY;
  625. goto out;
  626. }
  627. smp_rmb(); /* ordered against wdm_wwan_port_stop() */
  628. rv = usb_autopm_get_interface(desc->intf);
  629. if (rv < 0) {
  630. dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
  631. goto out;
  632. }
  633. /* using write lock to protect desc->count */
  634. mutex_lock(&desc->wlock);
  635. if (!desc->count++) {
  636. desc->werr = 0;
  637. desc->rerr = 0;
  638. rv = usb_submit_urb(desc->validity, GFP_KERNEL);
  639. if (rv < 0) {
  640. desc->count--;
  641. dev_err(&desc->intf->dev,
  642. "Error submitting int urb - %d\n", rv);
  643. rv = usb_translate_errors(rv);
  644. }
  645. } else {
  646. rv = 0;
  647. }
  648. mutex_unlock(&desc->wlock);
  649. if (desc->count == 1)
  650. desc->manage_power(intf, 1);
  651. usb_autopm_put_interface(desc->intf);
  652. out:
  653. mutex_unlock(&wdm_mutex);
  654. return rv;
  655. }
  656. static int wdm_release(struct inode *inode, struct file *file)
  657. {
  658. struct wdm_device *desc = file->private_data;
  659. mutex_lock(&wdm_mutex);
  660. /* using write lock to protect desc->count */
  661. mutex_lock(&desc->wlock);
  662. desc->count--;
  663. mutex_unlock(&desc->wlock);
  664. if (!desc->count) {
  665. if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
  666. dev_dbg(&desc->intf->dev, "wdm_release: cleanup\n");
  667. poison_urbs(desc);
  668. spin_lock_irq(&desc->iuspin);
  669. desc->resp_count = 0;
  670. clear_bit(WDM_RESPONDING, &desc->flags);
  671. spin_unlock_irq(&desc->iuspin);
  672. desc->manage_power(desc->intf, 0);
  673. unpoison_urbs(desc);
  674. } else {
  675. /* must avoid dev_printk here as desc->intf is invalid */
  676. pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
  677. cleanup(desc);
  678. }
  679. }
  680. mutex_unlock(&wdm_mutex);
  681. return 0;
  682. }
  683. static long wdm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  684. {
  685. struct wdm_device *desc = file->private_data;
  686. int rv = 0;
  687. switch (cmd) {
  688. case IOCTL_WDM_MAX_COMMAND:
  689. if (copy_to_user((void __user *)arg, &desc->wMaxCommand, sizeof(desc->wMaxCommand)))
  690. rv = -EFAULT;
  691. break;
  692. default:
  693. rv = -ENOTTY;
  694. }
  695. return rv;
  696. }
  697. static const struct file_operations wdm_fops = {
  698. .owner = THIS_MODULE,
  699. .read = wdm_read,
  700. .write = wdm_write,
  701. .fsync = wdm_fsync,
  702. .open = wdm_open,
  703. .flush = wdm_flush,
  704. .release = wdm_release,
  705. .poll = wdm_poll,
  706. .unlocked_ioctl = wdm_ioctl,
  707. .compat_ioctl = compat_ptr_ioctl,
  708. .llseek = noop_llseek,
  709. };
  710. static struct usb_class_driver wdm_class = {
  711. .name = "cdc-wdm%d",
  712. .fops = &wdm_fops,
  713. .minor_base = WDM_MINOR_BASE,
  714. };
  715. /* --- WWAN framework integration --- */
  716. #ifdef CONFIG_WWAN
  717. static int wdm_wwan_port_start(struct wwan_port *port)
  718. {
  719. struct wdm_device *desc = wwan_port_get_drvdata(port);
  720. int rv;
  721. /* The interface is both exposed via the WWAN framework and as a
  722. * legacy usbmisc chardev. If chardev is already open, just fail
  723. * to prevent concurrent usage. Otherwise, switch to WWAN mode.
  724. */
  725. mutex_lock(&wdm_mutex);
  726. if (desc->count) {
  727. mutex_unlock(&wdm_mutex);
  728. return -EBUSY;
  729. }
  730. set_bit(WDM_WWAN_IN_USE, &desc->flags);
  731. mutex_unlock(&wdm_mutex);
  732. desc->manage_power(desc->intf, 1);
  733. /* tx is allowed */
  734. wwan_port_txon(port);
  735. /* Start getting events */
  736. rv = usb_submit_urb(desc->validity, GFP_KERNEL);
  737. if (rv < 0) {
  738. wwan_port_txoff(port);
  739. desc->manage_power(desc->intf, 0);
  740. /* this must be last lest we race with chardev open */
  741. clear_bit(WDM_WWAN_IN_USE, &desc->flags);
  742. }
  743. return rv;
  744. }
  745. static void wdm_wwan_port_stop(struct wwan_port *port)
  746. {
  747. struct wdm_device *desc = wwan_port_get_drvdata(port);
  748. /* Stop all transfers and disable WWAN mode */
  749. poison_urbs(desc);
  750. desc->manage_power(desc->intf, 0);
  751. clear_bit(WDM_READ, &desc->flags);
  752. unpoison_urbs(desc);
  753. smp_wmb(); /* ordered against wdm_open() */
  754. /* this must be last lest we open a poisoned device */
  755. clear_bit(WDM_WWAN_IN_USE, &desc->flags);
  756. }
  757. static void wdm_wwan_port_tx_complete(struct urb *urb)
  758. {
  759. struct sk_buff *skb = urb->context;
  760. struct wdm_device *desc = skb_shinfo(skb)->destructor_arg;
  761. usb_autopm_put_interface_async(desc->intf);
  762. wwan_port_txon(desc->wwanp);
  763. kfree_skb(skb);
  764. }
  765. static int wdm_wwan_port_tx(struct wwan_port *port, struct sk_buff *skb)
  766. {
  767. struct wdm_device *desc = wwan_port_get_drvdata(port);
  768. struct usb_interface *intf = desc->intf;
  769. struct usb_ctrlrequest *req = desc->orq;
  770. int rv;
  771. rv = usb_autopm_get_interface(intf);
  772. if (rv)
  773. return rv;
  774. usb_fill_control_urb(
  775. desc->command,
  776. interface_to_usbdev(intf),
  777. usb_sndctrlpipe(interface_to_usbdev(intf), 0),
  778. (unsigned char *)req,
  779. skb->data,
  780. skb->len,
  781. wdm_wwan_port_tx_complete,
  782. skb
  783. );
  784. req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
  785. req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
  786. req->wValue = 0;
  787. req->wIndex = desc->inum; /* already converted */
  788. req->wLength = cpu_to_le16(skb->len);
  789. skb_shinfo(skb)->destructor_arg = desc;
  790. rv = usb_submit_urb(desc->command, GFP_KERNEL);
  791. if (rv)
  792. usb_autopm_put_interface(intf);
  793. else /* One transfer at a time, stop TX until URB completion */
  794. wwan_port_txoff(port);
  795. return rv;
  796. }
  797. static const struct wwan_port_ops wdm_wwan_port_ops = {
  798. .start = wdm_wwan_port_start,
  799. .stop = wdm_wwan_port_stop,
  800. .tx = wdm_wwan_port_tx,
  801. };
  802. static void wdm_wwan_init(struct wdm_device *desc)
  803. {
  804. struct usb_interface *intf = desc->intf;
  805. struct wwan_port *port;
  806. /* Only register to WWAN core if protocol/type is known */
  807. if (desc->wwanp_type == WWAN_PORT_UNKNOWN) {
  808. dev_info(&intf->dev, "Unknown control protocol\n");
  809. return;
  810. }
  811. port = wwan_create_port(&intf->dev, desc->wwanp_type, &wdm_wwan_port_ops,
  812. NULL, desc);
  813. if (IS_ERR(port)) {
  814. dev_err(&intf->dev, "%s: Unable to create WWAN port\n",
  815. dev_name(intf->usb_dev));
  816. return;
  817. }
  818. desc->wwanp = port;
  819. }
  820. static void wdm_wwan_deinit(struct wdm_device *desc)
  821. {
  822. if (!desc->wwanp)
  823. return;
  824. wwan_remove_port(desc->wwanp);
  825. desc->wwanp = NULL;
  826. }
  827. static void wdm_wwan_rx(struct wdm_device *desc, int length)
  828. {
  829. struct wwan_port *port = desc->wwanp;
  830. struct sk_buff *skb;
  831. /* Forward data to WWAN port */
  832. skb = alloc_skb(length, GFP_ATOMIC);
  833. if (!skb)
  834. return;
  835. skb_put_data(skb, desc->inbuf, length);
  836. wwan_port_rx(port, skb);
  837. /* inbuf has been copied, it is safe to check for outstanding data */
  838. schedule_work(&desc->service_outs_intr);
  839. }
  840. #else /* CONFIG_WWAN */
  841. static void wdm_wwan_init(struct wdm_device *desc) {}
  842. static void wdm_wwan_deinit(struct wdm_device *desc) {}
  843. static void wdm_wwan_rx(struct wdm_device *desc, int length) {}
  844. #endif /* CONFIG_WWAN */
  845. /* --- error handling --- */
  846. static void wdm_rxwork(struct work_struct *work)
  847. {
  848. struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
  849. unsigned long flags;
  850. int rv = 0;
  851. int responding;
  852. spin_lock_irqsave(&desc->iuspin, flags);
  853. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  854. spin_unlock_irqrestore(&desc->iuspin, flags);
  855. } else {
  856. responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
  857. spin_unlock_irqrestore(&desc->iuspin, flags);
  858. if (!responding)
  859. rv = usb_submit_urb(desc->response, GFP_KERNEL);
  860. if (rv < 0 && rv != -EPERM) {
  861. spin_lock_irqsave(&desc->iuspin, flags);
  862. clear_bit(WDM_RESPONDING, &desc->flags);
  863. if (!test_bit(WDM_DISCONNECTING, &desc->flags))
  864. schedule_work(&desc->rxwork);
  865. spin_unlock_irqrestore(&desc->iuspin, flags);
  866. }
  867. }
  868. }
  869. static void service_interrupt_work(struct work_struct *work)
  870. {
  871. struct wdm_device *desc;
  872. desc = container_of(work, struct wdm_device, service_outs_intr);
  873. spin_lock_irq(&desc->iuspin);
  874. service_outstanding_interrupt(desc);
  875. if (!desc->resp_count && (desc->length || desc->rerr)) {
  876. set_bit(WDM_READ, &desc->flags);
  877. wake_up(&desc->wait);
  878. }
  879. spin_unlock_irq(&desc->iuspin);
  880. }
  881. /* --- hotplug --- */
  882. static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
  883. u16 bufsize, enum wwan_port_type type,
  884. int (*manage_power)(struct usb_interface *, int))
  885. {
  886. int rv = -ENOMEM;
  887. struct wdm_device *desc;
  888. desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
  889. if (!desc)
  890. goto out;
  891. INIT_LIST_HEAD(&desc->device_list);
  892. mutex_init(&desc->rlock);
  893. mutex_init(&desc->wlock);
  894. spin_lock_init(&desc->iuspin);
  895. init_waitqueue_head(&desc->wait);
  896. desc->wMaxCommand = bufsize;
  897. /* this will be expanded and needed in hardware endianness */
  898. desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
  899. desc->intf = intf;
  900. desc->wwanp_type = type;
  901. INIT_WORK(&desc->rxwork, wdm_rxwork);
  902. INIT_WORK(&desc->service_outs_intr, service_interrupt_work);
  903. if (!usb_endpoint_is_int_in(ep)) {
  904. rv = -EINVAL;
  905. goto err;
  906. }
  907. desc->wMaxPacketSize = usb_endpoint_maxp(ep);
  908. desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  909. if (!desc->orq)
  910. goto err;
  911. desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  912. if (!desc->irq)
  913. goto err;
  914. desc->validity = usb_alloc_urb(0, GFP_KERNEL);
  915. if (!desc->validity)
  916. goto err;
  917. desc->response = usb_alloc_urb(0, GFP_KERNEL);
  918. if (!desc->response)
  919. goto err;
  920. desc->command = usb_alloc_urb(0, GFP_KERNEL);
  921. if (!desc->command)
  922. goto err;
  923. desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
  924. if (!desc->ubuf)
  925. goto err;
  926. desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
  927. if (!desc->sbuf)
  928. goto err;
  929. desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
  930. if (!desc->inbuf)
  931. goto err;
  932. usb_fill_int_urb(
  933. desc->validity,
  934. interface_to_usbdev(intf),
  935. usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
  936. desc->sbuf,
  937. desc->wMaxPacketSize,
  938. wdm_int_callback,
  939. desc,
  940. ep->bInterval
  941. );
  942. desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
  943. desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
  944. desc->irq->wValue = 0;
  945. desc->irq->wIndex = desc->inum; /* already converted */
  946. desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
  947. usb_fill_control_urb(
  948. desc->response,
  949. interface_to_usbdev(intf),
  950. /* using common endpoint 0 */
  951. usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
  952. (unsigned char *)desc->irq,
  953. desc->inbuf,
  954. desc->wMaxCommand,
  955. wdm_in_callback,
  956. desc
  957. );
  958. desc->manage_power = manage_power;
  959. spin_lock(&wdm_device_list_lock);
  960. list_add(&desc->device_list, &wdm_device_list);
  961. spin_unlock(&wdm_device_list_lock);
  962. rv = usb_register_dev(intf, &wdm_class);
  963. if (rv < 0)
  964. goto err;
  965. else
  966. dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
  967. wdm_wwan_init(desc);
  968. out:
  969. return rv;
  970. err:
  971. spin_lock(&wdm_device_list_lock);
  972. list_del(&desc->device_list);
  973. spin_unlock(&wdm_device_list_lock);
  974. cleanup(desc);
  975. return rv;
  976. }
  977. static int wdm_manage_power(struct usb_interface *intf, int on)
  978. {
  979. /* need autopm_get/put here to ensure the usbcore sees the new value */
  980. int rv = usb_autopm_get_interface(intf);
  981. intf->needs_remote_wakeup = on;
  982. if (!rv)
  983. usb_autopm_put_interface(intf);
  984. return 0;
  985. }
  986. static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
  987. {
  988. int rv = -EINVAL;
  989. struct usb_host_interface *iface;
  990. struct usb_endpoint_descriptor *ep;
  991. struct usb_cdc_parsed_header hdr;
  992. u8 *buffer = intf->altsetting->extra;
  993. int buflen = intf->altsetting->extralen;
  994. u16 maxcom = WDM_DEFAULT_BUFSIZE;
  995. if (!buffer)
  996. goto err;
  997. cdc_parse_cdc_header(&hdr, intf, buffer, buflen);
  998. if (hdr.usb_cdc_dmm_desc)
  999. maxcom = le16_to_cpu(hdr.usb_cdc_dmm_desc->wMaxCommand);
  1000. iface = intf->cur_altsetting;
  1001. if (iface->desc.bNumEndpoints != 1)
  1002. goto err;
  1003. ep = &iface->endpoint[0].desc;
  1004. rv = wdm_create(intf, ep, maxcom, WWAN_PORT_UNKNOWN, &wdm_manage_power);
  1005. err:
  1006. return rv;
  1007. }
  1008. /**
  1009. * usb_cdc_wdm_register - register a WDM subdriver
  1010. * @intf: usb interface the subdriver will associate with
  1011. * @ep: interrupt endpoint to monitor for notifications
  1012. * @bufsize: maximum message size to support for read/write
  1013. * @type: Type/protocol of the transported data (MBIM, QMI...)
  1014. * @manage_power: call-back invoked during open and release to
  1015. * manage the device's power
  1016. * Create WDM usb class character device and associate it with intf
  1017. * without binding, allowing another driver to manage the interface.
  1018. *
  1019. * The subdriver will manage the given interrupt endpoint exclusively
  1020. * and will issue control requests referring to the given intf. It
  1021. * will otherwise avoid interferring, and in particular not do
  1022. * usb_set_intfdata/usb_get_intfdata on intf.
  1023. *
  1024. * The return value is a pointer to the subdriver's struct usb_driver.
  1025. * The registering driver is responsible for calling this subdriver's
  1026. * disconnect, suspend, resume, pre_reset and post_reset methods from
  1027. * its own.
  1028. */
  1029. struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
  1030. struct usb_endpoint_descriptor *ep,
  1031. int bufsize, enum wwan_port_type type,
  1032. int (*manage_power)(struct usb_interface *, int))
  1033. {
  1034. int rv;
  1035. rv = wdm_create(intf, ep, bufsize, type, manage_power);
  1036. if (rv < 0)
  1037. goto err;
  1038. return &wdm_driver;
  1039. err:
  1040. return ERR_PTR(rv);
  1041. }
  1042. EXPORT_SYMBOL(usb_cdc_wdm_register);
  1043. static void wdm_disconnect(struct usb_interface *intf)
  1044. {
  1045. struct wdm_device *desc;
  1046. unsigned long flags;
  1047. usb_deregister_dev(intf, &wdm_class);
  1048. desc = wdm_find_device(intf);
  1049. mutex_lock(&wdm_mutex);
  1050. wdm_wwan_deinit(desc);
  1051. /* the spinlock makes sure no new urbs are generated in the callbacks */
  1052. spin_lock_irqsave(&desc->iuspin, flags);
  1053. set_bit(WDM_DISCONNECTING, &desc->flags);
  1054. set_bit(WDM_READ, &desc->flags);
  1055. spin_unlock_irqrestore(&desc->iuspin, flags);
  1056. wake_up_all(&desc->wait);
  1057. mutex_lock(&desc->rlock);
  1058. mutex_lock(&desc->wlock);
  1059. poison_urbs(desc);
  1060. cancel_work_sync(&desc->rxwork);
  1061. cancel_work_sync(&desc->service_outs_intr);
  1062. mutex_unlock(&desc->wlock);
  1063. mutex_unlock(&desc->rlock);
  1064. /* the desc->intf pointer used as list key is now invalid */
  1065. spin_lock(&wdm_device_list_lock);
  1066. list_del(&desc->device_list);
  1067. spin_unlock(&wdm_device_list_lock);
  1068. if (!desc->count)
  1069. cleanup(desc);
  1070. else
  1071. dev_dbg(&intf->dev, "%d open files - postponing cleanup\n", desc->count);
  1072. mutex_unlock(&wdm_mutex);
  1073. }
  1074. #ifdef CONFIG_PM
  1075. static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
  1076. {
  1077. struct wdm_device *desc = wdm_find_device(intf);
  1078. int rv = 0;
  1079. dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
  1080. /* if this is an autosuspend the caller does the locking */
  1081. if (!PMSG_IS_AUTO(message)) {
  1082. mutex_lock(&desc->rlock);
  1083. mutex_lock(&desc->wlock);
  1084. }
  1085. spin_lock_irq(&desc->iuspin);
  1086. if (PMSG_IS_AUTO(message) &&
  1087. (test_bit(WDM_IN_USE, &desc->flags)
  1088. || test_bit(WDM_RESPONDING, &desc->flags))) {
  1089. spin_unlock_irq(&desc->iuspin);
  1090. rv = -EBUSY;
  1091. } else {
  1092. set_bit(WDM_SUSPENDING, &desc->flags);
  1093. spin_unlock_irq(&desc->iuspin);
  1094. /* callback submits work - order is essential */
  1095. poison_urbs(desc);
  1096. cancel_work_sync(&desc->rxwork);
  1097. cancel_work_sync(&desc->service_outs_intr);
  1098. unpoison_urbs(desc);
  1099. }
  1100. if (!PMSG_IS_AUTO(message)) {
  1101. mutex_unlock(&desc->wlock);
  1102. mutex_unlock(&desc->rlock);
  1103. }
  1104. return rv;
  1105. }
  1106. #endif
  1107. static int recover_from_urb_loss(struct wdm_device *desc)
  1108. {
  1109. int rv = 0;
  1110. if (desc->count) {
  1111. rv = usb_submit_urb(desc->validity, GFP_NOIO);
  1112. if (rv < 0)
  1113. dev_err(&desc->intf->dev,
  1114. "Error resume submitting int urb - %d\n", rv);
  1115. }
  1116. return rv;
  1117. }
  1118. #ifdef CONFIG_PM
  1119. static int wdm_resume(struct usb_interface *intf)
  1120. {
  1121. struct wdm_device *desc = wdm_find_device(intf);
  1122. int rv;
  1123. dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
  1124. clear_bit(WDM_SUSPENDING, &desc->flags);
  1125. rv = recover_from_urb_loss(desc);
  1126. return rv;
  1127. }
  1128. #endif
  1129. static int wdm_pre_reset(struct usb_interface *intf)
  1130. {
  1131. struct wdm_device *desc = wdm_find_device(intf);
  1132. /*
  1133. * we notify everybody using poll of
  1134. * an exceptional situation
  1135. * must be done before recovery lest a spontaneous
  1136. * message from the device is lost
  1137. */
  1138. spin_lock_irq(&desc->iuspin);
  1139. set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
  1140. set_bit(WDM_READ, &desc->flags); /* unblock read */
  1141. clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
  1142. desc->rerr = -EINTR;
  1143. spin_unlock_irq(&desc->iuspin);
  1144. wake_up_all(&desc->wait);
  1145. mutex_lock(&desc->rlock);
  1146. mutex_lock(&desc->wlock);
  1147. poison_urbs(desc);
  1148. cancel_work_sync(&desc->rxwork);
  1149. cancel_work_sync(&desc->service_outs_intr);
  1150. return 0;
  1151. }
  1152. static int wdm_post_reset(struct usb_interface *intf)
  1153. {
  1154. struct wdm_device *desc = wdm_find_device(intf);
  1155. int rv;
  1156. unpoison_urbs(desc);
  1157. clear_bit(WDM_OVERFLOW, &desc->flags);
  1158. clear_bit(WDM_RESETTING, &desc->flags);
  1159. rv = recover_from_urb_loss(desc);
  1160. mutex_unlock(&desc->wlock);
  1161. mutex_unlock(&desc->rlock);
  1162. return rv;
  1163. }
  1164. static struct usb_driver wdm_driver = {
  1165. .name = "cdc_wdm",
  1166. .probe = wdm_probe,
  1167. .disconnect = wdm_disconnect,
  1168. #ifdef CONFIG_PM
  1169. .suspend = wdm_suspend,
  1170. .resume = wdm_resume,
  1171. .reset_resume = wdm_resume,
  1172. #endif
  1173. .pre_reset = wdm_pre_reset,
  1174. .post_reset = wdm_post_reset,
  1175. .id_table = wdm_ids,
  1176. .supports_autosuspend = 1,
  1177. .disable_hub_initiated_lpm = 1,
  1178. };
  1179. module_usb_driver(wdm_driver);
  1180. MODULE_AUTHOR(DRIVER_AUTHOR);
  1181. MODULE_DESCRIPTION(DRIVER_DESC);
  1182. MODULE_LICENSE("GPL");