cdc-wdm.c 23 KB

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