cdc-wdm.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. /*
  2. * cdc-wdm.c
  3. *
  4. * This driver supports USB CDC WCM Device Management.
  5. *
  6. * Copyright (c) 2007-2009 Oliver Neukum
  7. *
  8. * Some code taken from cdc-acm.c
  9. *
  10. * Released under the GPLv2.
  11. *
  12. * Many thanks to Carl Nordbeck
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/errno.h>
  16. #include <linux/slab.h>
  17. #include <linux/module.h>
  18. #include <linux/mutex.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/bitops.h>
  21. #include <linux/poll.h>
  22. #include <linux/usb.h>
  23. #include <linux/usb/cdc.h>
  24. #include <asm/byteorder.h>
  25. #include <asm/unaligned.h>
  26. #include <linux/usb/cdc-wdm.h>
  27. /*
  28. * Version Information
  29. */
  30. #define DRIVER_VERSION "v0.03"
  31. #define DRIVER_AUTHOR "Oliver Neukum"
  32. #define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management"
  33. #define HUAWEI_VENDOR_ID 0x12D1
  34. static const struct usb_device_id wdm_ids[] = {
  35. {
  36. .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
  37. USB_DEVICE_ID_MATCH_INT_SUBCLASS,
  38. .bInterfaceClass = USB_CLASS_COMM,
  39. .bInterfaceSubClass = USB_CDC_SUBCLASS_DMM
  40. },
  41. {
  42. /*
  43. * Huawei E392, E398 and possibly other Qualcomm based modems
  44. * embed the Qualcomm QMI protocol inside CDC on CDC ECM like
  45. * control interfaces. Userspace access to this is required
  46. * to configure the accompanying data interface
  47. */
  48. .match_flags = USB_DEVICE_ID_MATCH_VENDOR |
  49. USB_DEVICE_ID_MATCH_INT_INFO,
  50. .idVendor = HUAWEI_VENDOR_ID,
  51. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  52. .bInterfaceSubClass = 1,
  53. .bInterfaceProtocol = 9, /* NOTE: CDC ECM control interface! */
  54. },
  55. { }
  56. };
  57. MODULE_DEVICE_TABLE (usb, wdm_ids);
  58. #define WDM_MINOR_BASE 176
  59. #define WDM_IN_USE 1
  60. #define WDM_DISCONNECTING 2
  61. #define WDM_RESULT 3
  62. #define WDM_READ 4
  63. #define WDM_INT_STALL 5
  64. #define WDM_POLL_RUNNING 6
  65. #define WDM_RESPONDING 7
  66. #define WDM_SUSPENDING 8
  67. #define WDM_RESETTING 9
  68. #define WDM_MAX 16
  69. /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
  70. #define WDM_DEFAULT_BUFSIZE 256
  71. static DEFINE_MUTEX(wdm_mutex);
  72. static DEFINE_SPINLOCK(wdm_device_list_lock);
  73. static LIST_HEAD(wdm_device_list);
  74. /* --- method tables --- */
  75. struct wdm_device {
  76. u8 *inbuf; /* buffer for response */
  77. u8 *outbuf; /* buffer for command */
  78. u8 *sbuf; /* buffer for status */
  79. u8 *ubuf; /* buffer for copy to user space */
  80. struct urb *command;
  81. struct urb *response;
  82. struct urb *validity;
  83. struct usb_interface *intf;
  84. struct usb_ctrlrequest *orq;
  85. struct usb_ctrlrequest *irq;
  86. spinlock_t iuspin;
  87. unsigned long flags;
  88. u16 bufsize;
  89. u16 wMaxCommand;
  90. u16 wMaxPacketSize;
  91. __le16 inum;
  92. int reslength;
  93. int length;
  94. int read;
  95. int count;
  96. dma_addr_t shandle;
  97. dma_addr_t ihandle;
  98. struct mutex wlock;
  99. struct mutex rlock;
  100. wait_queue_head_t wait;
  101. struct work_struct rxwork;
  102. int werr;
  103. int rerr;
  104. struct list_head device_list;
  105. int (*manage_power)(struct usb_interface *, int);
  106. };
  107. static struct usb_driver wdm_driver;
  108. /* return intfdata if we own the interface, else look up intf in the list */
  109. static struct wdm_device *wdm_find_device(struct usb_interface *intf)
  110. {
  111. struct wdm_device *desc = NULL;
  112. spin_lock(&wdm_device_list_lock);
  113. list_for_each_entry(desc, &wdm_device_list, device_list)
  114. if (desc->intf == intf)
  115. break;
  116. spin_unlock(&wdm_device_list_lock);
  117. return desc;
  118. }
  119. static struct wdm_device *wdm_find_device_by_minor(int minor)
  120. {
  121. struct wdm_device *desc = NULL;
  122. spin_lock(&wdm_device_list_lock);
  123. list_for_each_entry(desc, &wdm_device_list, device_list)
  124. if (desc->intf->minor == minor)
  125. break;
  126. spin_unlock(&wdm_device_list_lock);
  127. return desc;
  128. }
  129. /* --- callbacks --- */
  130. static void wdm_out_callback(struct urb *urb)
  131. {
  132. struct wdm_device *desc;
  133. desc = urb->context;
  134. spin_lock(&desc->iuspin);
  135. desc->werr = urb->status;
  136. spin_unlock(&desc->iuspin);
  137. kfree(desc->outbuf);
  138. desc->outbuf = NULL;
  139. clear_bit(WDM_IN_USE, &desc->flags);
  140. wake_up(&desc->wait);
  141. }
  142. static void wdm_in_callback(struct urb *urb)
  143. {
  144. struct wdm_device *desc = urb->context;
  145. int status = urb->status;
  146. spin_lock(&desc->iuspin);
  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");
  153. goto skip_error;
  154. case -ECONNRESET:
  155. dev_dbg(&desc->intf->dev,
  156. "nonzero urb status received: -ECONNRESET");
  157. goto skip_error;
  158. case -ESHUTDOWN:
  159. dev_dbg(&desc->intf->dev,
  160. "nonzero urb status received: -ESHUTDOWN");
  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. desc->rerr = status;
  173. desc->reslength = urb->actual_length;
  174. memmove(desc->ubuf + desc->length, desc->inbuf, desc->reslength);
  175. desc->length += desc->reslength;
  176. skip_error:
  177. wake_up(&desc->wait);
  178. set_bit(WDM_READ, &desc->flags);
  179. spin_unlock(&desc->iuspin);
  180. }
  181. static void wdm_int_callback(struct urb *urb)
  182. {
  183. int rv = 0;
  184. int status = urb->status;
  185. struct wdm_device *desc;
  186. struct usb_cdc_notification *dr;
  187. desc = urb->context;
  188. dr = (struct usb_cdc_notification *)desc->sbuf;
  189. if (status) {
  190. switch (status) {
  191. case -ESHUTDOWN:
  192. case -ENOENT:
  193. case -ECONNRESET:
  194. return; /* unplug */
  195. case -EPIPE:
  196. set_bit(WDM_INT_STALL, &desc->flags);
  197. dev_err(&desc->intf->dev, "Stall on int endpoint\n");
  198. goto sw; /* halt is cleared in work */
  199. default:
  200. dev_err(&desc->intf->dev,
  201. "nonzero urb status received: %d\n", status);
  202. break;
  203. }
  204. }
  205. if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
  206. dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
  207. urb->actual_length);
  208. goto exit;
  209. }
  210. switch (dr->bNotificationType) {
  211. case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
  212. dev_dbg(&desc->intf->dev,
  213. "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
  214. dr->wIndex, dr->wLength);
  215. break;
  216. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  217. dev_dbg(&desc->intf->dev,
  218. "NOTIFY_NETWORK_CONNECTION %s network",
  219. dr->wValue ? "connected to" : "disconnected from");
  220. goto exit;
  221. default:
  222. clear_bit(WDM_POLL_RUNNING, &desc->flags);
  223. dev_err(&desc->intf->dev,
  224. "unknown notification %d received: index %d len %d\n",
  225. dr->bNotificationType, dr->wIndex, dr->wLength);
  226. goto exit;
  227. }
  228. spin_lock(&desc->iuspin);
  229. clear_bit(WDM_READ, &desc->flags);
  230. set_bit(WDM_RESPONDING, &desc->flags);
  231. if (!test_bit(WDM_DISCONNECTING, &desc->flags)
  232. && !test_bit(WDM_SUSPENDING, &desc->flags)) {
  233. rv = usb_submit_urb(desc->response, GFP_ATOMIC);
  234. dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
  235. __func__, rv);
  236. }
  237. spin_unlock(&desc->iuspin);
  238. if (rv < 0) {
  239. clear_bit(WDM_RESPONDING, &desc->flags);
  240. if (rv == -EPERM)
  241. return;
  242. if (rv == -ENOMEM) {
  243. sw:
  244. rv = schedule_work(&desc->rxwork);
  245. if (rv)
  246. dev_err(&desc->intf->dev,
  247. "Cannot schedule work\n");
  248. }
  249. }
  250. exit:
  251. rv = usb_submit_urb(urb, GFP_ATOMIC);
  252. if (rv)
  253. dev_err(&desc->intf->dev,
  254. "%s - usb_submit_urb failed with result %d\n",
  255. __func__, rv);
  256. }
  257. static void kill_urbs(struct wdm_device *desc)
  258. {
  259. /* the order here is essential */
  260. usb_kill_urb(desc->command);
  261. usb_kill_urb(desc->validity);
  262. usb_kill_urb(desc->response);
  263. }
  264. static void free_urbs(struct wdm_device *desc)
  265. {
  266. usb_free_urb(desc->validity);
  267. usb_free_urb(desc->response);
  268. usb_free_urb(desc->command);
  269. }
  270. static void cleanup(struct wdm_device *desc)
  271. {
  272. spin_lock(&wdm_device_list_lock);
  273. list_del(&desc->device_list);
  274. spin_unlock(&wdm_device_list_lock);
  275. kfree(desc->sbuf);
  276. kfree(desc->inbuf);
  277. kfree(desc->orq);
  278. kfree(desc->irq);
  279. kfree(desc->ubuf);
  280. free_urbs(desc);
  281. kfree(desc);
  282. }
  283. static ssize_t wdm_write
  284. (struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  285. {
  286. u8 *buf;
  287. int rv = -EMSGSIZE, r, we;
  288. struct wdm_device *desc = file->private_data;
  289. struct usb_ctrlrequest *req;
  290. if (count > desc->wMaxCommand)
  291. count = desc->wMaxCommand;
  292. spin_lock_irq(&desc->iuspin);
  293. we = desc->werr;
  294. desc->werr = 0;
  295. spin_unlock_irq(&desc->iuspin);
  296. if (we < 0)
  297. return -EIO;
  298. buf = kmalloc(count, GFP_KERNEL);
  299. if (!buf) {
  300. rv = -ENOMEM;
  301. goto outnl;
  302. }
  303. r = copy_from_user(buf, buffer, count);
  304. if (r > 0) {
  305. kfree(buf);
  306. rv = -EFAULT;
  307. goto outnl;
  308. }
  309. /* concurrent writes and disconnect */
  310. r = mutex_lock_interruptible(&desc->wlock);
  311. rv = -ERESTARTSYS;
  312. if (r) {
  313. kfree(buf);
  314. goto outnl;
  315. }
  316. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  317. kfree(buf);
  318. rv = -ENODEV;
  319. goto outnp;
  320. }
  321. r = usb_autopm_get_interface(desc->intf);
  322. if (r < 0) {
  323. kfree(buf);
  324. goto outnp;
  325. }
  326. if (!(file->f_flags & O_NONBLOCK))
  327. r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
  328. &desc->flags));
  329. else
  330. if (test_bit(WDM_IN_USE, &desc->flags))
  331. r = -EAGAIN;
  332. if (test_bit(WDM_RESETTING, &desc->flags))
  333. r = -EIO;
  334. if (r < 0) {
  335. kfree(buf);
  336. goto out;
  337. }
  338. req = desc->orq;
  339. usb_fill_control_urb(
  340. desc->command,
  341. interface_to_usbdev(desc->intf),
  342. /* using common endpoint 0 */
  343. usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
  344. (unsigned char *)req,
  345. buf,
  346. count,
  347. wdm_out_callback,
  348. desc
  349. );
  350. req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
  351. USB_RECIP_INTERFACE);
  352. req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
  353. req->wValue = 0;
  354. req->wIndex = desc->inum;
  355. req->wLength = cpu_to_le16(count);
  356. set_bit(WDM_IN_USE, &desc->flags);
  357. desc->outbuf = buf;
  358. rv = usb_submit_urb(desc->command, GFP_KERNEL);
  359. if (rv < 0) {
  360. kfree(buf);
  361. desc->outbuf = NULL;
  362. clear_bit(WDM_IN_USE, &desc->flags);
  363. dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
  364. } else {
  365. dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
  366. req->wIndex);
  367. }
  368. out:
  369. usb_autopm_put_interface(desc->intf);
  370. outnp:
  371. mutex_unlock(&desc->wlock);
  372. outnl:
  373. return rv < 0 ? rv : count;
  374. }
  375. static ssize_t wdm_read
  376. (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  377. {
  378. int rv, cntr;
  379. int i = 0;
  380. struct wdm_device *desc = file->private_data;
  381. rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
  382. if (rv < 0)
  383. return -ERESTARTSYS;
  384. cntr = ACCESS_ONCE(desc->length);
  385. if (cntr == 0) {
  386. desc->read = 0;
  387. retry:
  388. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  389. rv = -ENODEV;
  390. goto err;
  391. }
  392. i++;
  393. if (file->f_flags & O_NONBLOCK) {
  394. if (!test_bit(WDM_READ, &desc->flags)) {
  395. rv = cntr ? cntr : -EAGAIN;
  396. goto err;
  397. }
  398. rv = 0;
  399. } else {
  400. rv = wait_event_interruptible(desc->wait,
  401. test_bit(WDM_READ, &desc->flags));
  402. }
  403. /* may have happened while we slept */
  404. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  405. rv = -ENODEV;
  406. goto err;
  407. }
  408. if (test_bit(WDM_RESETTING, &desc->flags)) {
  409. rv = -EIO;
  410. goto err;
  411. }
  412. usb_mark_last_busy(interface_to_usbdev(desc->intf));
  413. if (rv < 0) {
  414. rv = -ERESTARTSYS;
  415. goto err;
  416. }
  417. spin_lock_irq(&desc->iuspin);
  418. if (desc->rerr) { /* read completed, error happened */
  419. desc->rerr = 0;
  420. spin_unlock_irq(&desc->iuspin);
  421. rv = -EIO;
  422. goto err;
  423. }
  424. /*
  425. * recheck whether we've lost the race
  426. * against the completion handler
  427. */
  428. if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
  429. spin_unlock_irq(&desc->iuspin);
  430. goto retry;
  431. }
  432. if (!desc->reslength) { /* zero length read */
  433. spin_unlock_irq(&desc->iuspin);
  434. goto retry;
  435. }
  436. cntr = desc->length;
  437. spin_unlock_irq(&desc->iuspin);
  438. }
  439. if (cntr > count)
  440. cntr = count;
  441. rv = copy_to_user(buffer, desc->ubuf, cntr);
  442. if (rv > 0) {
  443. rv = -EFAULT;
  444. goto err;
  445. }
  446. spin_lock_irq(&desc->iuspin);
  447. for (i = 0; i < desc->length - cntr; i++)
  448. desc->ubuf[i] = desc->ubuf[i + cntr];
  449. desc->length -= cntr;
  450. /* in case we had outstanding data */
  451. if (!desc->length)
  452. clear_bit(WDM_READ, &desc->flags);
  453. spin_unlock_irq(&desc->iuspin);
  454. rv = cntr;
  455. err:
  456. mutex_unlock(&desc->rlock);
  457. return rv;
  458. }
  459. static int wdm_flush(struct file *file, fl_owner_t id)
  460. {
  461. struct wdm_device *desc = file->private_data;
  462. wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
  463. if (desc->werr < 0)
  464. dev_err(&desc->intf->dev, "Error in flush path: %d\n",
  465. desc->werr);
  466. return desc->werr;
  467. }
  468. static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
  469. {
  470. struct wdm_device *desc = file->private_data;
  471. unsigned long flags;
  472. unsigned int mask = 0;
  473. spin_lock_irqsave(&desc->iuspin, flags);
  474. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  475. mask = POLLERR;
  476. spin_unlock_irqrestore(&desc->iuspin, flags);
  477. goto desc_out;
  478. }
  479. if (test_bit(WDM_READ, &desc->flags))
  480. mask = POLLIN | POLLRDNORM;
  481. if (desc->rerr || desc->werr)
  482. mask |= POLLERR;
  483. if (!test_bit(WDM_IN_USE, &desc->flags))
  484. mask |= POLLOUT | POLLWRNORM;
  485. spin_unlock_irqrestore(&desc->iuspin, flags);
  486. poll_wait(file, &desc->wait, wait);
  487. desc_out:
  488. return mask;
  489. }
  490. static int wdm_open(struct inode *inode, struct file *file)
  491. {
  492. int minor = iminor(inode);
  493. int rv = -ENODEV;
  494. struct usb_interface *intf;
  495. struct wdm_device *desc;
  496. mutex_lock(&wdm_mutex);
  497. desc = wdm_find_device_by_minor(minor);
  498. if (!desc)
  499. goto out;
  500. intf = desc->intf;
  501. if (test_bit(WDM_DISCONNECTING, &desc->flags))
  502. goto out;
  503. file->private_data = desc;
  504. rv = usb_autopm_get_interface(desc->intf);
  505. if (rv < 0) {
  506. dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
  507. goto out;
  508. }
  509. /* using write lock to protect desc->count */
  510. mutex_lock(&desc->wlock);
  511. if (!desc->count++) {
  512. desc->werr = 0;
  513. desc->rerr = 0;
  514. rv = usb_submit_urb(desc->validity, GFP_KERNEL);
  515. if (rv < 0) {
  516. desc->count--;
  517. dev_err(&desc->intf->dev,
  518. "Error submitting int urb - %d\n", rv);
  519. }
  520. } else {
  521. rv = 0;
  522. }
  523. mutex_unlock(&desc->wlock);
  524. if (desc->count == 1)
  525. desc->manage_power(intf, 1);
  526. usb_autopm_put_interface(desc->intf);
  527. out:
  528. mutex_unlock(&wdm_mutex);
  529. return rv;
  530. }
  531. static int wdm_release(struct inode *inode, struct file *file)
  532. {
  533. struct wdm_device *desc = file->private_data;
  534. mutex_lock(&wdm_mutex);
  535. /* using write lock to protect desc->count */
  536. mutex_lock(&desc->wlock);
  537. desc->count--;
  538. mutex_unlock(&desc->wlock);
  539. if (!desc->count) {
  540. dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
  541. kill_urbs(desc);
  542. if (!test_bit(WDM_DISCONNECTING, &desc->flags))
  543. desc->manage_power(desc->intf, 0);
  544. }
  545. mutex_unlock(&wdm_mutex);
  546. return 0;
  547. }
  548. static const struct file_operations wdm_fops = {
  549. .owner = THIS_MODULE,
  550. .read = wdm_read,
  551. .write = wdm_write,
  552. .open = wdm_open,
  553. .flush = wdm_flush,
  554. .release = wdm_release,
  555. .poll = wdm_poll,
  556. .llseek = noop_llseek,
  557. };
  558. static struct usb_class_driver wdm_class = {
  559. .name = "cdc-wdm%d",
  560. .fops = &wdm_fops,
  561. .minor_base = WDM_MINOR_BASE,
  562. };
  563. /* --- error handling --- */
  564. static void wdm_rxwork(struct work_struct *work)
  565. {
  566. struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
  567. unsigned long flags;
  568. int rv;
  569. spin_lock_irqsave(&desc->iuspin, flags);
  570. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  571. spin_unlock_irqrestore(&desc->iuspin, flags);
  572. } else {
  573. spin_unlock_irqrestore(&desc->iuspin, flags);
  574. rv = usb_submit_urb(desc->response, GFP_KERNEL);
  575. if (rv < 0 && rv != -EPERM) {
  576. spin_lock_irqsave(&desc->iuspin, flags);
  577. if (!test_bit(WDM_DISCONNECTING, &desc->flags))
  578. schedule_work(&desc->rxwork);
  579. spin_unlock_irqrestore(&desc->iuspin, flags);
  580. }
  581. }
  582. }
  583. /* --- hotplug --- */
  584. static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
  585. u16 bufsize, int (*manage_power)(struct usb_interface *, int))
  586. {
  587. int rv = -ENOMEM;
  588. struct wdm_device *desc;
  589. desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
  590. if (!desc)
  591. goto out;
  592. INIT_LIST_HEAD(&desc->device_list);
  593. mutex_init(&desc->rlock);
  594. mutex_init(&desc->wlock);
  595. spin_lock_init(&desc->iuspin);
  596. init_waitqueue_head(&desc->wait);
  597. desc->wMaxCommand = bufsize;
  598. /* this will be expanded and needed in hardware endianness */
  599. desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
  600. desc->intf = intf;
  601. INIT_WORK(&desc->rxwork, wdm_rxwork);
  602. rv = -EINVAL;
  603. if (!usb_endpoint_is_int_in(ep))
  604. goto err;
  605. desc->wMaxPacketSize = usb_endpoint_maxp(ep);
  606. desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  607. if (!desc->orq)
  608. goto err;
  609. desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  610. if (!desc->irq)
  611. goto err;
  612. desc->validity = usb_alloc_urb(0, GFP_KERNEL);
  613. if (!desc->validity)
  614. goto err;
  615. desc->response = usb_alloc_urb(0, GFP_KERNEL);
  616. if (!desc->response)
  617. goto err;
  618. desc->command = usb_alloc_urb(0, GFP_KERNEL);
  619. if (!desc->command)
  620. goto err;
  621. desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
  622. if (!desc->ubuf)
  623. goto err;
  624. desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
  625. if (!desc->sbuf)
  626. goto err;
  627. desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
  628. if (!desc->inbuf)
  629. goto err;
  630. usb_fill_int_urb(
  631. desc->validity,
  632. interface_to_usbdev(intf),
  633. usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
  634. desc->sbuf,
  635. desc->wMaxPacketSize,
  636. wdm_int_callback,
  637. desc,
  638. ep->bInterval
  639. );
  640. desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
  641. desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
  642. desc->irq->wValue = 0;
  643. desc->irq->wIndex = desc->inum;
  644. desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
  645. usb_fill_control_urb(
  646. desc->response,
  647. interface_to_usbdev(intf),
  648. /* using common endpoint 0 */
  649. usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
  650. (unsigned char *)desc->irq,
  651. desc->inbuf,
  652. desc->wMaxCommand,
  653. wdm_in_callback,
  654. desc
  655. );
  656. desc->manage_power = manage_power;
  657. spin_lock(&wdm_device_list_lock);
  658. list_add(&desc->device_list, &wdm_device_list);
  659. spin_unlock(&wdm_device_list_lock);
  660. rv = usb_register_dev(intf, &wdm_class);
  661. if (rv < 0)
  662. goto err;
  663. else
  664. dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
  665. out:
  666. return rv;
  667. err:
  668. cleanup(desc);
  669. return rv;
  670. }
  671. static int wdm_manage_power(struct usb_interface *intf, int on)
  672. {
  673. /* need autopm_get/put here to ensure the usbcore sees the new value */
  674. int rv = usb_autopm_get_interface(intf);
  675. if (rv < 0)
  676. goto err;
  677. intf->needs_remote_wakeup = on;
  678. usb_autopm_put_interface(intf);
  679. err:
  680. return rv;
  681. }
  682. static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
  683. {
  684. int rv = -EINVAL;
  685. struct usb_host_interface *iface;
  686. struct usb_endpoint_descriptor *ep;
  687. struct usb_cdc_dmm_desc *dmhd;
  688. u8 *buffer = intf->altsetting->extra;
  689. int buflen = intf->altsetting->extralen;
  690. u16 maxcom = WDM_DEFAULT_BUFSIZE;
  691. if (!buffer)
  692. goto err;
  693. while (buflen > 2) {
  694. if (buffer[1] != USB_DT_CS_INTERFACE) {
  695. dev_err(&intf->dev, "skipping garbage\n");
  696. goto next_desc;
  697. }
  698. switch (buffer[2]) {
  699. case USB_CDC_HEADER_TYPE:
  700. break;
  701. case USB_CDC_DMM_TYPE:
  702. dmhd = (struct usb_cdc_dmm_desc *)buffer;
  703. maxcom = le16_to_cpu(dmhd->wMaxCommand);
  704. dev_dbg(&intf->dev,
  705. "Finding maximum buffer length: %d", maxcom);
  706. break;
  707. default:
  708. dev_err(&intf->dev,
  709. "Ignoring extra header, type %d, length %d\n",
  710. buffer[2], buffer[0]);
  711. break;
  712. }
  713. next_desc:
  714. buflen -= buffer[0];
  715. buffer += buffer[0];
  716. }
  717. iface = intf->cur_altsetting;
  718. if (iface->desc.bNumEndpoints != 1)
  719. goto err;
  720. ep = &iface->endpoint[0].desc;
  721. rv = wdm_create(intf, ep, maxcom, &wdm_manage_power);
  722. err:
  723. return rv;
  724. }
  725. /**
  726. * usb_cdc_wdm_register - register a WDM subdriver
  727. * @intf: usb interface the subdriver will associate with
  728. * @ep: interrupt endpoint to monitor for notifications
  729. * @bufsize: maximum message size to support for read/write
  730. *
  731. * Create WDM usb class character device and associate it with intf
  732. * without binding, allowing another driver to manage the interface.
  733. *
  734. * The subdriver will manage the given interrupt endpoint exclusively
  735. * and will issue control requests referring to the given intf. It
  736. * will otherwise avoid interferring, and in particular not do
  737. * usb_set_intfdata/usb_get_intfdata on intf.
  738. *
  739. * The return value is a pointer to the subdriver's struct usb_driver.
  740. * The registering driver is responsible for calling this subdriver's
  741. * disconnect, suspend, resume, pre_reset and post_reset methods from
  742. * its own.
  743. */
  744. struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
  745. struct usb_endpoint_descriptor *ep,
  746. int bufsize,
  747. int (*manage_power)(struct usb_interface *, int))
  748. {
  749. int rv = -EINVAL;
  750. rv = wdm_create(intf, ep, bufsize, manage_power);
  751. if (rv < 0)
  752. goto err;
  753. return &wdm_driver;
  754. err:
  755. return ERR_PTR(rv);
  756. }
  757. EXPORT_SYMBOL(usb_cdc_wdm_register);
  758. static void wdm_disconnect(struct usb_interface *intf)
  759. {
  760. struct wdm_device *desc;
  761. unsigned long flags;
  762. usb_deregister_dev(intf, &wdm_class);
  763. desc = wdm_find_device(intf);
  764. mutex_lock(&wdm_mutex);
  765. /* the spinlock makes sure no new urbs are generated in the callbacks */
  766. spin_lock_irqsave(&desc->iuspin, flags);
  767. set_bit(WDM_DISCONNECTING, &desc->flags);
  768. set_bit(WDM_READ, &desc->flags);
  769. /* to terminate pending flushes */
  770. clear_bit(WDM_IN_USE, &desc->flags);
  771. spin_unlock_irqrestore(&desc->iuspin, flags);
  772. wake_up_all(&desc->wait);
  773. mutex_lock(&desc->rlock);
  774. mutex_lock(&desc->wlock);
  775. kill_urbs(desc);
  776. cancel_work_sync(&desc->rxwork);
  777. mutex_unlock(&desc->wlock);
  778. mutex_unlock(&desc->rlock);
  779. if (!desc->count)
  780. cleanup(desc);
  781. mutex_unlock(&wdm_mutex);
  782. }
  783. #ifdef CONFIG_PM
  784. static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
  785. {
  786. struct wdm_device *desc = wdm_find_device(intf);
  787. int rv = 0;
  788. dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
  789. /* if this is an autosuspend the caller does the locking */
  790. if (!PMSG_IS_AUTO(message)) {
  791. mutex_lock(&desc->rlock);
  792. mutex_lock(&desc->wlock);
  793. }
  794. spin_lock_irq(&desc->iuspin);
  795. if (PMSG_IS_AUTO(message) &&
  796. (test_bit(WDM_IN_USE, &desc->flags)
  797. || test_bit(WDM_RESPONDING, &desc->flags))) {
  798. spin_unlock_irq(&desc->iuspin);
  799. rv = -EBUSY;
  800. } else {
  801. set_bit(WDM_SUSPENDING, &desc->flags);
  802. spin_unlock_irq(&desc->iuspin);
  803. /* callback submits work - order is essential */
  804. kill_urbs(desc);
  805. cancel_work_sync(&desc->rxwork);
  806. }
  807. if (!PMSG_IS_AUTO(message)) {
  808. mutex_unlock(&desc->wlock);
  809. mutex_unlock(&desc->rlock);
  810. }
  811. return rv;
  812. }
  813. #endif
  814. static int recover_from_urb_loss(struct wdm_device *desc)
  815. {
  816. int rv = 0;
  817. if (desc->count) {
  818. rv = usb_submit_urb(desc->validity, GFP_NOIO);
  819. if (rv < 0)
  820. dev_err(&desc->intf->dev,
  821. "Error resume submitting int urb - %d\n", rv);
  822. }
  823. return rv;
  824. }
  825. #ifdef CONFIG_PM
  826. static int wdm_resume(struct usb_interface *intf)
  827. {
  828. struct wdm_device *desc = wdm_find_device(intf);
  829. int rv;
  830. dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
  831. clear_bit(WDM_SUSPENDING, &desc->flags);
  832. rv = recover_from_urb_loss(desc);
  833. return rv;
  834. }
  835. #endif
  836. static int wdm_pre_reset(struct usb_interface *intf)
  837. {
  838. struct wdm_device *desc = wdm_find_device(intf);
  839. /*
  840. * we notify everybody using poll of
  841. * an exceptional situation
  842. * must be done before recovery lest a spontaneous
  843. * message from the device is lost
  844. */
  845. spin_lock_irq(&desc->iuspin);
  846. set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
  847. set_bit(WDM_READ, &desc->flags); /* unblock read */
  848. clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
  849. desc->rerr = -EINTR;
  850. spin_unlock_irq(&desc->iuspin);
  851. wake_up_all(&desc->wait);
  852. mutex_lock(&desc->rlock);
  853. mutex_lock(&desc->wlock);
  854. kill_urbs(desc);
  855. cancel_work_sync(&desc->rxwork);
  856. return 0;
  857. }
  858. static int wdm_post_reset(struct usb_interface *intf)
  859. {
  860. struct wdm_device *desc = wdm_find_device(intf);
  861. int rv;
  862. clear_bit(WDM_RESETTING, &desc->flags);
  863. rv = recover_from_urb_loss(desc);
  864. mutex_unlock(&desc->wlock);
  865. mutex_unlock(&desc->rlock);
  866. return 0;
  867. }
  868. static struct usb_driver wdm_driver = {
  869. .name = "cdc_wdm",
  870. .probe = wdm_probe,
  871. .disconnect = wdm_disconnect,
  872. #ifdef CONFIG_PM
  873. .suspend = wdm_suspend,
  874. .resume = wdm_resume,
  875. .reset_resume = wdm_resume,
  876. #endif
  877. .pre_reset = wdm_pre_reset,
  878. .post_reset = wdm_post_reset,
  879. .id_table = wdm_ids,
  880. .supports_autosuspend = 1,
  881. };
  882. module_usb_driver(wdm_driver);
  883. MODULE_AUTHOR(DRIVER_AUTHOR);
  884. MODULE_DESCRIPTION(DRIVER_DESC);
  885. MODULE_LICENSE("GPL");