cdc-wdm.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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. 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_MAX 16
  54. /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
  55. #define WDM_DEFAULT_BUFSIZE 256
  56. static DEFINE_MUTEX(wdm_mutex);
  57. static DEFINE_SPINLOCK(wdm_device_list_lock);
  58. static LIST_HEAD(wdm_device_list);
  59. /* --- method tables --- */
  60. struct wdm_device {
  61. u8 *inbuf; /* buffer for response */
  62. u8 *outbuf; /* buffer for command */
  63. u8 *sbuf; /* buffer for status */
  64. u8 *ubuf; /* buffer for copy to user space */
  65. struct urb *command;
  66. struct urb *response;
  67. struct urb *validity;
  68. struct usb_interface *intf;
  69. struct usb_ctrlrequest *orq;
  70. struct usb_ctrlrequest *irq;
  71. spinlock_t iuspin;
  72. unsigned long flags;
  73. u16 bufsize;
  74. u16 wMaxCommand;
  75. u16 wMaxPacketSize;
  76. __le16 inum;
  77. int reslength;
  78. int length;
  79. int read;
  80. int count;
  81. dma_addr_t shandle;
  82. dma_addr_t ihandle;
  83. struct mutex wlock;
  84. struct mutex rlock;
  85. wait_queue_head_t wait;
  86. struct work_struct rxwork;
  87. int werr;
  88. int rerr;
  89. struct list_head device_list;
  90. int (*manage_power)(struct usb_interface *, int);
  91. };
  92. static struct usb_driver wdm_driver;
  93. /* return intfdata if we own the interface, else look up intf in the list */
  94. static struct wdm_device *wdm_find_device(struct usb_interface *intf)
  95. {
  96. struct wdm_device *desc = NULL;
  97. spin_lock(&wdm_device_list_lock);
  98. list_for_each_entry(desc, &wdm_device_list, device_list)
  99. if (desc->intf == intf)
  100. break;
  101. spin_unlock(&wdm_device_list_lock);
  102. return desc;
  103. }
  104. static struct wdm_device *wdm_find_device_by_minor(int minor)
  105. {
  106. struct wdm_device *desc = NULL;
  107. spin_lock(&wdm_device_list_lock);
  108. list_for_each_entry(desc, &wdm_device_list, device_list)
  109. if (desc->intf->minor == minor)
  110. break;
  111. spin_unlock(&wdm_device_list_lock);
  112. return desc;
  113. }
  114. /* --- callbacks --- */
  115. static void wdm_out_callback(struct urb *urb)
  116. {
  117. struct wdm_device *desc;
  118. desc = urb->context;
  119. spin_lock(&desc->iuspin);
  120. desc->werr = urb->status;
  121. spin_unlock(&desc->iuspin);
  122. kfree(desc->outbuf);
  123. desc->outbuf = NULL;
  124. clear_bit(WDM_IN_USE, &desc->flags);
  125. wake_up(&desc->wait);
  126. }
  127. static void wdm_in_callback(struct urb *urb)
  128. {
  129. struct wdm_device *desc = urb->context;
  130. int status = urb->status;
  131. spin_lock(&desc->iuspin);
  132. clear_bit(WDM_RESPONDING, &desc->flags);
  133. if (status) {
  134. switch (status) {
  135. case -ENOENT:
  136. dev_dbg(&desc->intf->dev,
  137. "nonzero urb status received: -ENOENT");
  138. goto skip_error;
  139. case -ECONNRESET:
  140. dev_dbg(&desc->intf->dev,
  141. "nonzero urb status received: -ECONNRESET");
  142. goto skip_error;
  143. case -ESHUTDOWN:
  144. dev_dbg(&desc->intf->dev,
  145. "nonzero urb status received: -ESHUTDOWN");
  146. goto skip_error;
  147. case -EPIPE:
  148. dev_err(&desc->intf->dev,
  149. "nonzero urb status received: -EPIPE\n");
  150. break;
  151. default:
  152. dev_err(&desc->intf->dev,
  153. "Unexpected error %d\n", status);
  154. break;
  155. }
  156. }
  157. desc->rerr = status;
  158. desc->reslength = urb->actual_length;
  159. memmove(desc->ubuf + desc->length, desc->inbuf, desc->reslength);
  160. desc->length += desc->reslength;
  161. skip_error:
  162. wake_up(&desc->wait);
  163. set_bit(WDM_READ, &desc->flags);
  164. spin_unlock(&desc->iuspin);
  165. }
  166. static void wdm_int_callback(struct urb *urb)
  167. {
  168. int rv = 0;
  169. int status = urb->status;
  170. struct wdm_device *desc;
  171. struct usb_cdc_notification *dr;
  172. desc = urb->context;
  173. dr = (struct usb_cdc_notification *)desc->sbuf;
  174. if (status) {
  175. switch (status) {
  176. case -ESHUTDOWN:
  177. case -ENOENT:
  178. case -ECONNRESET:
  179. return; /* unplug */
  180. case -EPIPE:
  181. set_bit(WDM_INT_STALL, &desc->flags);
  182. dev_err(&desc->intf->dev, "Stall on int endpoint\n");
  183. goto sw; /* halt is cleared in work */
  184. default:
  185. dev_err(&desc->intf->dev,
  186. "nonzero urb status received: %d\n", status);
  187. break;
  188. }
  189. }
  190. if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
  191. dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
  192. urb->actual_length);
  193. goto exit;
  194. }
  195. switch (dr->bNotificationType) {
  196. case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
  197. dev_dbg(&desc->intf->dev,
  198. "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
  199. dr->wIndex, dr->wLength);
  200. break;
  201. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  202. dev_dbg(&desc->intf->dev,
  203. "NOTIFY_NETWORK_CONNECTION %s network",
  204. dr->wValue ? "connected to" : "disconnected from");
  205. goto exit;
  206. default:
  207. clear_bit(WDM_POLL_RUNNING, &desc->flags);
  208. dev_err(&desc->intf->dev,
  209. "unknown notification %d received: index %d len %d\n",
  210. dr->bNotificationType, dr->wIndex, dr->wLength);
  211. goto exit;
  212. }
  213. spin_lock(&desc->iuspin);
  214. clear_bit(WDM_READ, &desc->flags);
  215. set_bit(WDM_RESPONDING, &desc->flags);
  216. if (!test_bit(WDM_DISCONNECTING, &desc->flags)
  217. && !test_bit(WDM_SUSPENDING, &desc->flags)) {
  218. rv = usb_submit_urb(desc->response, GFP_ATOMIC);
  219. dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
  220. __func__, rv);
  221. }
  222. spin_unlock(&desc->iuspin);
  223. if (rv < 0) {
  224. clear_bit(WDM_RESPONDING, &desc->flags);
  225. if (rv == -EPERM)
  226. return;
  227. if (rv == -ENOMEM) {
  228. sw:
  229. rv = schedule_work(&desc->rxwork);
  230. if (rv)
  231. dev_err(&desc->intf->dev,
  232. "Cannot schedule work\n");
  233. }
  234. }
  235. exit:
  236. rv = usb_submit_urb(urb, GFP_ATOMIC);
  237. if (rv)
  238. dev_err(&desc->intf->dev,
  239. "%s - usb_submit_urb failed with result %d\n",
  240. __func__, rv);
  241. }
  242. static void kill_urbs(struct wdm_device *desc)
  243. {
  244. /* the order here is essential */
  245. usb_kill_urb(desc->command);
  246. usb_kill_urb(desc->validity);
  247. usb_kill_urb(desc->response);
  248. }
  249. static void free_urbs(struct wdm_device *desc)
  250. {
  251. usb_free_urb(desc->validity);
  252. usb_free_urb(desc->response);
  253. usb_free_urb(desc->command);
  254. }
  255. static void cleanup(struct wdm_device *desc)
  256. {
  257. kfree(desc->sbuf);
  258. kfree(desc->inbuf);
  259. kfree(desc->orq);
  260. kfree(desc->irq);
  261. kfree(desc->ubuf);
  262. free_urbs(desc);
  263. kfree(desc);
  264. }
  265. static ssize_t wdm_write
  266. (struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  267. {
  268. u8 *buf;
  269. int rv = -EMSGSIZE, r, we;
  270. struct wdm_device *desc = file->private_data;
  271. struct usb_ctrlrequest *req;
  272. if (count > desc->wMaxCommand)
  273. count = desc->wMaxCommand;
  274. spin_lock_irq(&desc->iuspin);
  275. we = desc->werr;
  276. desc->werr = 0;
  277. spin_unlock_irq(&desc->iuspin);
  278. if (we < 0)
  279. return -EIO;
  280. buf = kmalloc(count, GFP_KERNEL);
  281. if (!buf) {
  282. rv = -ENOMEM;
  283. goto outnl;
  284. }
  285. r = copy_from_user(buf, buffer, count);
  286. if (r > 0) {
  287. kfree(buf);
  288. rv = -EFAULT;
  289. goto outnl;
  290. }
  291. /* concurrent writes and disconnect */
  292. r = mutex_lock_interruptible(&desc->wlock);
  293. rv = -ERESTARTSYS;
  294. if (r) {
  295. kfree(buf);
  296. goto outnl;
  297. }
  298. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  299. kfree(buf);
  300. rv = -ENODEV;
  301. goto outnp;
  302. }
  303. r = usb_autopm_get_interface(desc->intf);
  304. if (r < 0) {
  305. kfree(buf);
  306. rv = usb_translate_errors(r);
  307. goto outnp;
  308. }
  309. if (!(file->f_flags & O_NONBLOCK))
  310. r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
  311. &desc->flags));
  312. else
  313. if (test_bit(WDM_IN_USE, &desc->flags))
  314. r = -EAGAIN;
  315. if (test_bit(WDM_RESETTING, &desc->flags))
  316. r = -EIO;
  317. if (r < 0) {
  318. kfree(buf);
  319. rv = r;
  320. goto out;
  321. }
  322. req = desc->orq;
  323. usb_fill_control_urb(
  324. desc->command,
  325. interface_to_usbdev(desc->intf),
  326. /* using common endpoint 0 */
  327. usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
  328. (unsigned char *)req,
  329. buf,
  330. count,
  331. wdm_out_callback,
  332. desc
  333. );
  334. req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
  335. USB_RECIP_INTERFACE);
  336. req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
  337. req->wValue = 0;
  338. req->wIndex = desc->inum;
  339. req->wLength = cpu_to_le16(count);
  340. set_bit(WDM_IN_USE, &desc->flags);
  341. desc->outbuf = buf;
  342. rv = usb_submit_urb(desc->command, GFP_KERNEL);
  343. if (rv < 0) {
  344. kfree(buf);
  345. desc->outbuf = NULL;
  346. clear_bit(WDM_IN_USE, &desc->flags);
  347. dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
  348. rv = usb_translate_errors(rv);
  349. } else {
  350. dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
  351. req->wIndex);
  352. }
  353. out:
  354. usb_autopm_put_interface(desc->intf);
  355. outnp:
  356. mutex_unlock(&desc->wlock);
  357. outnl:
  358. return rv < 0 ? rv : count;
  359. }
  360. static ssize_t wdm_read
  361. (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  362. {
  363. int rv, cntr;
  364. int i = 0;
  365. struct wdm_device *desc = file->private_data;
  366. rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
  367. if (rv < 0)
  368. return -ERESTARTSYS;
  369. cntr = ACCESS_ONCE(desc->length);
  370. if (cntr == 0) {
  371. desc->read = 0;
  372. retry:
  373. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  374. rv = -ENODEV;
  375. goto err;
  376. }
  377. i++;
  378. if (file->f_flags & O_NONBLOCK) {
  379. if (!test_bit(WDM_READ, &desc->flags)) {
  380. rv = cntr ? cntr : -EAGAIN;
  381. goto err;
  382. }
  383. rv = 0;
  384. } else {
  385. rv = wait_event_interruptible(desc->wait,
  386. test_bit(WDM_READ, &desc->flags));
  387. }
  388. /* may have happened while we slept */
  389. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  390. rv = -ENODEV;
  391. goto err;
  392. }
  393. if (test_bit(WDM_RESETTING, &desc->flags)) {
  394. rv = -EIO;
  395. goto err;
  396. }
  397. usb_mark_last_busy(interface_to_usbdev(desc->intf));
  398. if (rv < 0) {
  399. rv = -ERESTARTSYS;
  400. goto err;
  401. }
  402. spin_lock_irq(&desc->iuspin);
  403. if (desc->rerr) { /* read completed, error happened */
  404. desc->rerr = 0;
  405. spin_unlock_irq(&desc->iuspin);
  406. rv = -EIO;
  407. goto err;
  408. }
  409. /*
  410. * recheck whether we've lost the race
  411. * against the completion handler
  412. */
  413. if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
  414. spin_unlock_irq(&desc->iuspin);
  415. goto retry;
  416. }
  417. if (!desc->reslength) { /* zero length read */
  418. dev_dbg(&desc->intf->dev, "%s: zero length - clearing WDM_READ\n", __func__);
  419. clear_bit(WDM_READ, &desc->flags);
  420. spin_unlock_irq(&desc->iuspin);
  421. goto retry;
  422. }
  423. cntr = desc->length;
  424. spin_unlock_irq(&desc->iuspin);
  425. }
  426. if (cntr > count)
  427. cntr = count;
  428. rv = copy_to_user(buffer, desc->ubuf, cntr);
  429. if (rv > 0) {
  430. rv = -EFAULT;
  431. goto err;
  432. }
  433. spin_lock_irq(&desc->iuspin);
  434. for (i = 0; i < desc->length - cntr; i++)
  435. desc->ubuf[i] = desc->ubuf[i + cntr];
  436. desc->length -= cntr;
  437. /* in case we had outstanding data */
  438. if (!desc->length)
  439. clear_bit(WDM_READ, &desc->flags);
  440. spin_unlock_irq(&desc->iuspin);
  441. rv = cntr;
  442. err:
  443. mutex_unlock(&desc->rlock);
  444. return rv;
  445. }
  446. static int wdm_flush(struct file *file, fl_owner_t id)
  447. {
  448. struct wdm_device *desc = file->private_data;
  449. wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
  450. /* cannot dereference desc->intf if WDM_DISCONNECTING */
  451. if (desc->werr < 0 && !test_bit(WDM_DISCONNECTING, &desc->flags))
  452. dev_err(&desc->intf->dev, "Error in flush path: %d\n",
  453. desc->werr);
  454. return usb_translate_errors(desc->werr);
  455. }
  456. static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
  457. {
  458. struct wdm_device *desc = file->private_data;
  459. unsigned long flags;
  460. unsigned int mask = 0;
  461. spin_lock_irqsave(&desc->iuspin, flags);
  462. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  463. mask = POLLHUP | POLLERR;
  464. spin_unlock_irqrestore(&desc->iuspin, flags);
  465. goto desc_out;
  466. }
  467. if (test_bit(WDM_READ, &desc->flags))
  468. mask = POLLIN | POLLRDNORM;
  469. if (desc->rerr || desc->werr)
  470. mask |= POLLERR;
  471. if (!test_bit(WDM_IN_USE, &desc->flags))
  472. mask |= POLLOUT | POLLWRNORM;
  473. spin_unlock_irqrestore(&desc->iuspin, flags);
  474. poll_wait(file, &desc->wait, wait);
  475. desc_out:
  476. return mask;
  477. }
  478. static int wdm_open(struct inode *inode, struct file *file)
  479. {
  480. int minor = iminor(inode);
  481. int rv = -ENODEV;
  482. struct usb_interface *intf;
  483. struct wdm_device *desc;
  484. mutex_lock(&wdm_mutex);
  485. desc = wdm_find_device_by_minor(minor);
  486. if (!desc)
  487. goto out;
  488. intf = desc->intf;
  489. if (test_bit(WDM_DISCONNECTING, &desc->flags))
  490. goto out;
  491. file->private_data = desc;
  492. rv = usb_autopm_get_interface(desc->intf);
  493. if (rv < 0) {
  494. dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
  495. goto out;
  496. }
  497. /* using write lock to protect desc->count */
  498. mutex_lock(&desc->wlock);
  499. if (!desc->count++) {
  500. desc->werr = 0;
  501. desc->rerr = 0;
  502. rv = usb_submit_urb(desc->validity, GFP_KERNEL);
  503. if (rv < 0) {
  504. desc->count--;
  505. dev_err(&desc->intf->dev,
  506. "Error submitting int urb - %d\n", rv);
  507. rv = usb_translate_errors(rv);
  508. }
  509. } else {
  510. rv = 0;
  511. }
  512. mutex_unlock(&desc->wlock);
  513. if (desc->count == 1)
  514. desc->manage_power(intf, 1);
  515. usb_autopm_put_interface(desc->intf);
  516. out:
  517. mutex_unlock(&wdm_mutex);
  518. return rv;
  519. }
  520. static int wdm_release(struct inode *inode, struct file *file)
  521. {
  522. struct wdm_device *desc = file->private_data;
  523. mutex_lock(&wdm_mutex);
  524. /* using write lock to protect desc->count */
  525. mutex_lock(&desc->wlock);
  526. desc->count--;
  527. mutex_unlock(&desc->wlock);
  528. if (!desc->count) {
  529. if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
  530. dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
  531. kill_urbs(desc);
  532. desc->manage_power(desc->intf, 0);
  533. } else {
  534. /* must avoid dev_printk here as desc->intf is invalid */
  535. pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
  536. cleanup(desc);
  537. }
  538. }
  539. mutex_unlock(&wdm_mutex);
  540. return 0;
  541. }
  542. static const struct file_operations wdm_fops = {
  543. .owner = THIS_MODULE,
  544. .read = wdm_read,
  545. .write = wdm_write,
  546. .open = wdm_open,
  547. .flush = wdm_flush,
  548. .release = wdm_release,
  549. .poll = wdm_poll,
  550. .llseek = noop_llseek,
  551. };
  552. static struct usb_class_driver wdm_class = {
  553. .name = "cdc-wdm%d",
  554. .fops = &wdm_fops,
  555. .minor_base = WDM_MINOR_BASE,
  556. };
  557. /* --- error handling --- */
  558. static void wdm_rxwork(struct work_struct *work)
  559. {
  560. struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
  561. unsigned long flags;
  562. int rv;
  563. spin_lock_irqsave(&desc->iuspin, flags);
  564. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  565. spin_unlock_irqrestore(&desc->iuspin, flags);
  566. } else {
  567. spin_unlock_irqrestore(&desc->iuspin, flags);
  568. rv = usb_submit_urb(desc->response, GFP_KERNEL);
  569. if (rv < 0 && rv != -EPERM) {
  570. spin_lock_irqsave(&desc->iuspin, flags);
  571. if (!test_bit(WDM_DISCONNECTING, &desc->flags))
  572. schedule_work(&desc->rxwork);
  573. spin_unlock_irqrestore(&desc->iuspin, flags);
  574. }
  575. }
  576. }
  577. /* --- hotplug --- */
  578. static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
  579. u16 bufsize, int (*manage_power)(struct usb_interface *, int))
  580. {
  581. int rv = -ENOMEM;
  582. struct wdm_device *desc;
  583. desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
  584. if (!desc)
  585. goto out;
  586. INIT_LIST_HEAD(&desc->device_list);
  587. mutex_init(&desc->rlock);
  588. mutex_init(&desc->wlock);
  589. spin_lock_init(&desc->iuspin);
  590. init_waitqueue_head(&desc->wait);
  591. desc->wMaxCommand = bufsize;
  592. /* this will be expanded and needed in hardware endianness */
  593. desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
  594. desc->intf = intf;
  595. INIT_WORK(&desc->rxwork, wdm_rxwork);
  596. rv = -EINVAL;
  597. if (!usb_endpoint_is_int_in(ep))
  598. goto err;
  599. desc->wMaxPacketSize = usb_endpoint_maxp(ep);
  600. desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  601. if (!desc->orq)
  602. goto err;
  603. desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  604. if (!desc->irq)
  605. goto err;
  606. desc->validity = usb_alloc_urb(0, GFP_KERNEL);
  607. if (!desc->validity)
  608. goto err;
  609. desc->response = usb_alloc_urb(0, GFP_KERNEL);
  610. if (!desc->response)
  611. goto err;
  612. desc->command = usb_alloc_urb(0, GFP_KERNEL);
  613. if (!desc->command)
  614. goto err;
  615. desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
  616. if (!desc->ubuf)
  617. goto err;
  618. desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
  619. if (!desc->sbuf)
  620. goto err;
  621. desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
  622. if (!desc->inbuf)
  623. goto err;
  624. usb_fill_int_urb(
  625. desc->validity,
  626. interface_to_usbdev(intf),
  627. usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
  628. desc->sbuf,
  629. desc->wMaxPacketSize,
  630. wdm_int_callback,
  631. desc,
  632. ep->bInterval
  633. );
  634. desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
  635. desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
  636. desc->irq->wValue = 0;
  637. desc->irq->wIndex = desc->inum;
  638. desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
  639. usb_fill_control_urb(
  640. desc->response,
  641. interface_to_usbdev(intf),
  642. /* using common endpoint 0 */
  643. usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
  644. (unsigned char *)desc->irq,
  645. desc->inbuf,
  646. desc->wMaxCommand,
  647. wdm_in_callback,
  648. desc
  649. );
  650. desc->manage_power = manage_power;
  651. spin_lock(&wdm_device_list_lock);
  652. list_add(&desc->device_list, &wdm_device_list);
  653. spin_unlock(&wdm_device_list_lock);
  654. rv = usb_register_dev(intf, &wdm_class);
  655. if (rv < 0)
  656. goto err;
  657. else
  658. dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
  659. out:
  660. return rv;
  661. err:
  662. spin_lock(&wdm_device_list_lock);
  663. list_del(&desc->device_list);
  664. spin_unlock(&wdm_device_list_lock);
  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. /* the desc->intf pointer used as list key is now invalid */
  777. spin_lock(&wdm_device_list_lock);
  778. list_del(&desc->device_list);
  779. spin_unlock(&wdm_device_list_lock);
  780. if (!desc->count)
  781. cleanup(desc);
  782. else
  783. dev_dbg(&intf->dev, "%s: %d open files - postponing cleanup\n", __func__, desc->count);
  784. mutex_unlock(&wdm_mutex);
  785. }
  786. #ifdef CONFIG_PM
  787. static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
  788. {
  789. struct wdm_device *desc = wdm_find_device(intf);
  790. int rv = 0;
  791. dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
  792. /* if this is an autosuspend the caller does the locking */
  793. if (!PMSG_IS_AUTO(message)) {
  794. mutex_lock(&desc->rlock);
  795. mutex_lock(&desc->wlock);
  796. }
  797. spin_lock_irq(&desc->iuspin);
  798. if (PMSG_IS_AUTO(message) &&
  799. (test_bit(WDM_IN_USE, &desc->flags)
  800. || test_bit(WDM_RESPONDING, &desc->flags))) {
  801. spin_unlock_irq(&desc->iuspin);
  802. rv = -EBUSY;
  803. } else {
  804. set_bit(WDM_SUSPENDING, &desc->flags);
  805. spin_unlock_irq(&desc->iuspin);
  806. /* callback submits work - order is essential */
  807. kill_urbs(desc);
  808. cancel_work_sync(&desc->rxwork);
  809. }
  810. if (!PMSG_IS_AUTO(message)) {
  811. mutex_unlock(&desc->wlock);
  812. mutex_unlock(&desc->rlock);
  813. }
  814. return rv;
  815. }
  816. #endif
  817. static int recover_from_urb_loss(struct wdm_device *desc)
  818. {
  819. int rv = 0;
  820. if (desc->count) {
  821. rv = usb_submit_urb(desc->validity, GFP_NOIO);
  822. if (rv < 0)
  823. dev_err(&desc->intf->dev,
  824. "Error resume submitting int urb - %d\n", rv);
  825. }
  826. return rv;
  827. }
  828. #ifdef CONFIG_PM
  829. static int wdm_resume(struct usb_interface *intf)
  830. {
  831. struct wdm_device *desc = wdm_find_device(intf);
  832. int rv;
  833. dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
  834. clear_bit(WDM_SUSPENDING, &desc->flags);
  835. rv = recover_from_urb_loss(desc);
  836. return rv;
  837. }
  838. #endif
  839. static int wdm_pre_reset(struct usb_interface *intf)
  840. {
  841. struct wdm_device *desc = wdm_find_device(intf);
  842. /*
  843. * we notify everybody using poll of
  844. * an exceptional situation
  845. * must be done before recovery lest a spontaneous
  846. * message from the device is lost
  847. */
  848. spin_lock_irq(&desc->iuspin);
  849. set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
  850. set_bit(WDM_READ, &desc->flags); /* unblock read */
  851. clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
  852. desc->rerr = -EINTR;
  853. spin_unlock_irq(&desc->iuspin);
  854. wake_up_all(&desc->wait);
  855. mutex_lock(&desc->rlock);
  856. mutex_lock(&desc->wlock);
  857. kill_urbs(desc);
  858. cancel_work_sync(&desc->rxwork);
  859. return 0;
  860. }
  861. static int wdm_post_reset(struct usb_interface *intf)
  862. {
  863. struct wdm_device *desc = wdm_find_device(intf);
  864. int rv;
  865. clear_bit(WDM_RESETTING, &desc->flags);
  866. rv = recover_from_urb_loss(desc);
  867. mutex_unlock(&desc->wlock);
  868. mutex_unlock(&desc->rlock);
  869. return 0;
  870. }
  871. static struct usb_driver wdm_driver = {
  872. .name = "cdc_wdm",
  873. .probe = wdm_probe,
  874. .disconnect = wdm_disconnect,
  875. #ifdef CONFIG_PM
  876. .suspend = wdm_suspend,
  877. .resume = wdm_resume,
  878. .reset_resume = wdm_resume,
  879. #endif
  880. .pre_reset = wdm_pre_reset,
  881. .post_reset = wdm_post_reset,
  882. .id_table = wdm_ids,
  883. .supports_autosuspend = 1,
  884. .disable_hub_initiated_lpm = 1,
  885. };
  886. module_usb_driver(wdm_driver);
  887. MODULE_AUTHOR(DRIVER_AUTHOR);
  888. MODULE_DESCRIPTION(DRIVER_DESC);
  889. MODULE_LICENSE("GPL");