cdc-wdm.c 26 KB

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