cdc-wdm.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  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. le16_to_cpu(dr->wIndex), le16_to_cpu(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. case USB_CDC_NOTIFY_SPEED_CHANGE:
  224. dev_dbg(&desc->intf->dev, "SPEED_CHANGE received (len %u)",
  225. urb->actual_length);
  226. goto exit;
  227. default:
  228. clear_bit(WDM_POLL_RUNNING, &desc->flags);
  229. dev_err(&desc->intf->dev,
  230. "unknown notification %d received: index %d len %d\n",
  231. dr->bNotificationType,
  232. le16_to_cpu(dr->wIndex),
  233. le16_to_cpu(dr->wLength));
  234. goto exit;
  235. }
  236. spin_lock(&desc->iuspin);
  237. responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
  238. if (!desc->resp_count++ && !responding
  239. && !test_bit(WDM_DISCONNECTING, &desc->flags)
  240. && !test_bit(WDM_SUSPENDING, &desc->flags)) {
  241. rv = usb_submit_urb(desc->response, GFP_ATOMIC);
  242. dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
  243. __func__, rv);
  244. }
  245. spin_unlock(&desc->iuspin);
  246. if (rv < 0) {
  247. clear_bit(WDM_RESPONDING, &desc->flags);
  248. if (rv == -EPERM)
  249. return;
  250. if (rv == -ENOMEM) {
  251. sw:
  252. rv = schedule_work(&desc->rxwork);
  253. if (rv)
  254. dev_err(&desc->intf->dev,
  255. "Cannot schedule work\n");
  256. }
  257. }
  258. exit:
  259. rv = usb_submit_urb(urb, GFP_ATOMIC);
  260. if (rv)
  261. dev_err(&desc->intf->dev,
  262. "%s - usb_submit_urb failed with result %d\n",
  263. __func__, rv);
  264. }
  265. static void kill_urbs(struct wdm_device *desc)
  266. {
  267. /* the order here is essential */
  268. usb_kill_urb(desc->command);
  269. usb_kill_urb(desc->validity);
  270. usb_kill_urb(desc->response);
  271. }
  272. static void free_urbs(struct wdm_device *desc)
  273. {
  274. usb_free_urb(desc->validity);
  275. usb_free_urb(desc->response);
  276. usb_free_urb(desc->command);
  277. }
  278. static void cleanup(struct wdm_device *desc)
  279. {
  280. kfree(desc->sbuf);
  281. kfree(desc->inbuf);
  282. kfree(desc->orq);
  283. kfree(desc->irq);
  284. kfree(desc->ubuf);
  285. free_urbs(desc);
  286. kfree(desc);
  287. }
  288. static ssize_t wdm_write
  289. (struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  290. {
  291. u8 *buf;
  292. int rv = -EMSGSIZE, r, we;
  293. struct wdm_device *desc = file->private_data;
  294. struct usb_ctrlrequest *req;
  295. if (count > desc->wMaxCommand)
  296. count = desc->wMaxCommand;
  297. spin_lock_irq(&desc->iuspin);
  298. we = desc->werr;
  299. desc->werr = 0;
  300. spin_unlock_irq(&desc->iuspin);
  301. if (we < 0)
  302. return usb_translate_errors(we);
  303. buf = kmalloc(count, GFP_KERNEL);
  304. if (!buf) {
  305. rv = -ENOMEM;
  306. goto outnl;
  307. }
  308. r = copy_from_user(buf, buffer, count);
  309. if (r > 0) {
  310. rv = -EFAULT;
  311. goto out_free_mem;
  312. }
  313. /* concurrent writes and disconnect */
  314. r = mutex_lock_interruptible(&desc->wlock);
  315. rv = -ERESTARTSYS;
  316. if (r)
  317. goto out_free_mem;
  318. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  319. rv = -ENODEV;
  320. goto out_free_mem_lock;
  321. }
  322. r = usb_autopm_get_interface(desc->intf);
  323. if (r < 0) {
  324. rv = usb_translate_errors(r);
  325. goto out_free_mem_lock;
  326. }
  327. if (!(file->f_flags & O_NONBLOCK))
  328. r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
  329. &desc->flags));
  330. else
  331. if (test_bit(WDM_IN_USE, &desc->flags))
  332. r = -EAGAIN;
  333. if (test_bit(WDM_RESETTING, &desc->flags))
  334. r = -EIO;
  335. if (r < 0) {
  336. rv = r;
  337. goto out_free_mem_pm;
  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; /* already converted */
  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. desc->outbuf = NULL;
  362. clear_bit(WDM_IN_USE, &desc->flags);
  363. dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
  364. rv = usb_translate_errors(rv);
  365. goto out_free_mem_pm;
  366. } else {
  367. dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
  368. le16_to_cpu(req->wIndex));
  369. }
  370. usb_autopm_put_interface(desc->intf);
  371. mutex_unlock(&desc->wlock);
  372. outnl:
  373. return rv < 0 ? rv : count;
  374. out_free_mem_pm:
  375. usb_autopm_put_interface(desc->intf);
  376. out_free_mem_lock:
  377. mutex_unlock(&desc->wlock);
  378. out_free_mem:
  379. kfree(buf);
  380. return rv;
  381. }
  382. /*
  383. * clear WDM_READ flag and possibly submit the read urb if resp_count
  384. * is non-zero.
  385. *
  386. * Called with desc->iuspin locked
  387. */
  388. static int clear_wdm_read_flag(struct wdm_device *desc)
  389. {
  390. int rv = 0;
  391. clear_bit(WDM_READ, &desc->flags);
  392. /* submit read urb only if the device is waiting for it */
  393. if (!desc->resp_count || !--desc->resp_count)
  394. goto out;
  395. set_bit(WDM_RESPONDING, &desc->flags);
  396. spin_unlock_irq(&desc->iuspin);
  397. rv = usb_submit_urb(desc->response, GFP_KERNEL);
  398. spin_lock_irq(&desc->iuspin);
  399. if (rv) {
  400. dev_err(&desc->intf->dev,
  401. "usb_submit_urb failed with result %d\n", rv);
  402. /* make sure the next notification trigger a submit */
  403. clear_bit(WDM_RESPONDING, &desc->flags);
  404. desc->resp_count = 0;
  405. }
  406. out:
  407. return rv;
  408. }
  409. static ssize_t wdm_read
  410. (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  411. {
  412. int rv, cntr;
  413. int i = 0;
  414. struct wdm_device *desc = file->private_data;
  415. rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
  416. if (rv < 0)
  417. return -ERESTARTSYS;
  418. cntr = ACCESS_ONCE(desc->length);
  419. if (cntr == 0) {
  420. desc->read = 0;
  421. retry:
  422. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  423. rv = -ENODEV;
  424. goto err;
  425. }
  426. if (test_bit(WDM_OVERFLOW, &desc->flags)) {
  427. clear_bit(WDM_OVERFLOW, &desc->flags);
  428. rv = -ENOBUFS;
  429. goto err;
  430. }
  431. i++;
  432. if (file->f_flags & O_NONBLOCK) {
  433. if (!test_bit(WDM_READ, &desc->flags)) {
  434. rv = cntr ? cntr : -EAGAIN;
  435. goto err;
  436. }
  437. rv = 0;
  438. } else {
  439. rv = wait_event_interruptible(desc->wait,
  440. test_bit(WDM_READ, &desc->flags));
  441. }
  442. /* may have happened while we slept */
  443. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  444. rv = -ENODEV;
  445. goto err;
  446. }
  447. if (test_bit(WDM_RESETTING, &desc->flags)) {
  448. rv = -EIO;
  449. goto err;
  450. }
  451. usb_mark_last_busy(interface_to_usbdev(desc->intf));
  452. if (rv < 0) {
  453. rv = -ERESTARTSYS;
  454. goto err;
  455. }
  456. spin_lock_irq(&desc->iuspin);
  457. if (desc->rerr) { /* read completed, error happened */
  458. rv = usb_translate_errors(desc->rerr);
  459. desc->rerr = 0;
  460. spin_unlock_irq(&desc->iuspin);
  461. goto err;
  462. }
  463. /*
  464. * recheck whether we've lost the race
  465. * against the completion handler
  466. */
  467. if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
  468. spin_unlock_irq(&desc->iuspin);
  469. goto retry;
  470. }
  471. if (!desc->reslength) { /* zero length read */
  472. dev_dbg(&desc->intf->dev, "%s: zero length - clearing WDM_READ\n", __func__);
  473. rv = clear_wdm_read_flag(desc);
  474. spin_unlock_irq(&desc->iuspin);
  475. if (rv < 0)
  476. goto err;
  477. goto retry;
  478. }
  479. cntr = desc->length;
  480. spin_unlock_irq(&desc->iuspin);
  481. }
  482. if (cntr > count)
  483. cntr = count;
  484. rv = copy_to_user(buffer, desc->ubuf, cntr);
  485. if (rv > 0) {
  486. rv = -EFAULT;
  487. goto err;
  488. }
  489. spin_lock_irq(&desc->iuspin);
  490. for (i = 0; i < desc->length - cntr; i++)
  491. desc->ubuf[i] = desc->ubuf[i + cntr];
  492. desc->length -= cntr;
  493. /* in case we had outstanding data */
  494. if (!desc->length)
  495. clear_wdm_read_flag(desc);
  496. spin_unlock_irq(&desc->iuspin);
  497. rv = cntr;
  498. err:
  499. mutex_unlock(&desc->rlock);
  500. return rv;
  501. }
  502. static int wdm_flush(struct file *file, fl_owner_t id)
  503. {
  504. struct wdm_device *desc = file->private_data;
  505. wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
  506. /* cannot dereference desc->intf if WDM_DISCONNECTING */
  507. if (desc->werr < 0 && !test_bit(WDM_DISCONNECTING, &desc->flags))
  508. dev_err(&desc->intf->dev, "Error in flush path: %d\n",
  509. desc->werr);
  510. return usb_translate_errors(desc->werr);
  511. }
  512. static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
  513. {
  514. struct wdm_device *desc = file->private_data;
  515. unsigned long flags;
  516. unsigned int mask = 0;
  517. spin_lock_irqsave(&desc->iuspin, flags);
  518. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  519. mask = POLLHUP | POLLERR;
  520. spin_unlock_irqrestore(&desc->iuspin, flags);
  521. goto desc_out;
  522. }
  523. if (test_bit(WDM_READ, &desc->flags))
  524. mask = POLLIN | POLLRDNORM;
  525. if (desc->rerr || desc->werr)
  526. mask |= POLLERR;
  527. if (!test_bit(WDM_IN_USE, &desc->flags))
  528. mask |= POLLOUT | POLLWRNORM;
  529. spin_unlock_irqrestore(&desc->iuspin, flags);
  530. poll_wait(file, &desc->wait, wait);
  531. desc_out:
  532. return mask;
  533. }
  534. static int wdm_open(struct inode *inode, struct file *file)
  535. {
  536. int minor = iminor(inode);
  537. int rv = -ENODEV;
  538. struct usb_interface *intf;
  539. struct wdm_device *desc;
  540. mutex_lock(&wdm_mutex);
  541. desc = wdm_find_device_by_minor(minor);
  542. if (!desc)
  543. goto out;
  544. intf = desc->intf;
  545. if (test_bit(WDM_DISCONNECTING, &desc->flags))
  546. goto out;
  547. file->private_data = desc;
  548. rv = usb_autopm_get_interface(desc->intf);
  549. if (rv < 0) {
  550. dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
  551. goto out;
  552. }
  553. /* using write lock to protect desc->count */
  554. mutex_lock(&desc->wlock);
  555. if (!desc->count++) {
  556. desc->werr = 0;
  557. desc->rerr = 0;
  558. rv = usb_submit_urb(desc->validity, GFP_KERNEL);
  559. if (rv < 0) {
  560. desc->count--;
  561. dev_err(&desc->intf->dev,
  562. "Error submitting int urb - %d\n", rv);
  563. rv = usb_translate_errors(rv);
  564. }
  565. } else {
  566. rv = 0;
  567. }
  568. mutex_unlock(&desc->wlock);
  569. if (desc->count == 1)
  570. desc->manage_power(intf, 1);
  571. usb_autopm_put_interface(desc->intf);
  572. out:
  573. mutex_unlock(&wdm_mutex);
  574. return rv;
  575. }
  576. static int wdm_release(struct inode *inode, struct file *file)
  577. {
  578. struct wdm_device *desc = file->private_data;
  579. mutex_lock(&wdm_mutex);
  580. /* using write lock to protect desc->count */
  581. mutex_lock(&desc->wlock);
  582. desc->count--;
  583. mutex_unlock(&desc->wlock);
  584. if (!desc->count) {
  585. if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
  586. dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
  587. kill_urbs(desc);
  588. spin_lock_irq(&desc->iuspin);
  589. desc->resp_count = 0;
  590. spin_unlock_irq(&desc->iuspin);
  591. desc->manage_power(desc->intf, 0);
  592. } else {
  593. /* must avoid dev_printk here as desc->intf is invalid */
  594. pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
  595. cleanup(desc);
  596. }
  597. }
  598. mutex_unlock(&wdm_mutex);
  599. return 0;
  600. }
  601. static long wdm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  602. {
  603. struct wdm_device *desc = file->private_data;
  604. int rv = 0;
  605. switch (cmd) {
  606. case IOCTL_WDM_MAX_COMMAND:
  607. if (copy_to_user((void __user *)arg, &desc->wMaxCommand, sizeof(desc->wMaxCommand)))
  608. rv = -EFAULT;
  609. break;
  610. default:
  611. rv = -ENOTTY;
  612. }
  613. return rv;
  614. }
  615. static const struct file_operations wdm_fops = {
  616. .owner = THIS_MODULE,
  617. .read = wdm_read,
  618. .write = wdm_write,
  619. .open = wdm_open,
  620. .flush = wdm_flush,
  621. .release = wdm_release,
  622. .poll = wdm_poll,
  623. .unlocked_ioctl = wdm_ioctl,
  624. .compat_ioctl = wdm_ioctl,
  625. .llseek = noop_llseek,
  626. };
  627. static struct usb_class_driver wdm_class = {
  628. .name = "cdc-wdm%d",
  629. .fops = &wdm_fops,
  630. .minor_base = WDM_MINOR_BASE,
  631. };
  632. /* --- error handling --- */
  633. static void wdm_rxwork(struct work_struct *work)
  634. {
  635. struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
  636. unsigned long flags;
  637. int rv = 0;
  638. int responding;
  639. spin_lock_irqsave(&desc->iuspin, flags);
  640. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  641. spin_unlock_irqrestore(&desc->iuspin, flags);
  642. } else {
  643. responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
  644. spin_unlock_irqrestore(&desc->iuspin, flags);
  645. if (!responding)
  646. rv = usb_submit_urb(desc->response, GFP_KERNEL);
  647. if (rv < 0 && rv != -EPERM) {
  648. spin_lock_irqsave(&desc->iuspin, flags);
  649. clear_bit(WDM_RESPONDING, &desc->flags);
  650. if (!test_bit(WDM_DISCONNECTING, &desc->flags))
  651. schedule_work(&desc->rxwork);
  652. spin_unlock_irqrestore(&desc->iuspin, flags);
  653. }
  654. }
  655. }
  656. /* --- hotplug --- */
  657. static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
  658. u16 bufsize, int (*manage_power)(struct usb_interface *, int))
  659. {
  660. int rv = -ENOMEM;
  661. struct wdm_device *desc;
  662. desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
  663. if (!desc)
  664. goto out;
  665. INIT_LIST_HEAD(&desc->device_list);
  666. mutex_init(&desc->rlock);
  667. mutex_init(&desc->wlock);
  668. spin_lock_init(&desc->iuspin);
  669. init_waitqueue_head(&desc->wait);
  670. desc->wMaxCommand = bufsize;
  671. /* this will be expanded and needed in hardware endianness */
  672. desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
  673. desc->intf = intf;
  674. INIT_WORK(&desc->rxwork, wdm_rxwork);
  675. rv = -EINVAL;
  676. if (!usb_endpoint_is_int_in(ep))
  677. goto err;
  678. desc->wMaxPacketSize = usb_endpoint_maxp(ep);
  679. desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  680. if (!desc->orq)
  681. goto err;
  682. desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  683. if (!desc->irq)
  684. goto err;
  685. desc->validity = usb_alloc_urb(0, GFP_KERNEL);
  686. if (!desc->validity)
  687. goto err;
  688. desc->response = usb_alloc_urb(0, GFP_KERNEL);
  689. if (!desc->response)
  690. goto err;
  691. desc->command = usb_alloc_urb(0, GFP_KERNEL);
  692. if (!desc->command)
  693. goto err;
  694. desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
  695. if (!desc->ubuf)
  696. goto err;
  697. desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
  698. if (!desc->sbuf)
  699. goto err;
  700. desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
  701. if (!desc->inbuf)
  702. goto err;
  703. usb_fill_int_urb(
  704. desc->validity,
  705. interface_to_usbdev(intf),
  706. usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
  707. desc->sbuf,
  708. desc->wMaxPacketSize,
  709. wdm_int_callback,
  710. desc,
  711. ep->bInterval
  712. );
  713. desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
  714. desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
  715. desc->irq->wValue = 0;
  716. desc->irq->wIndex = desc->inum; /* already converted */
  717. desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
  718. usb_fill_control_urb(
  719. desc->response,
  720. interface_to_usbdev(intf),
  721. /* using common endpoint 0 */
  722. usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
  723. (unsigned char *)desc->irq,
  724. desc->inbuf,
  725. desc->wMaxCommand,
  726. wdm_in_callback,
  727. desc
  728. );
  729. desc->manage_power = manage_power;
  730. spin_lock(&wdm_device_list_lock);
  731. list_add(&desc->device_list, &wdm_device_list);
  732. spin_unlock(&wdm_device_list_lock);
  733. rv = usb_register_dev(intf, &wdm_class);
  734. if (rv < 0)
  735. goto err;
  736. else
  737. dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
  738. out:
  739. return rv;
  740. err:
  741. spin_lock(&wdm_device_list_lock);
  742. list_del(&desc->device_list);
  743. spin_unlock(&wdm_device_list_lock);
  744. cleanup(desc);
  745. return rv;
  746. }
  747. static int wdm_manage_power(struct usb_interface *intf, int on)
  748. {
  749. /* need autopm_get/put here to ensure the usbcore sees the new value */
  750. int rv = usb_autopm_get_interface(intf);
  751. intf->needs_remote_wakeup = on;
  752. if (!rv)
  753. usb_autopm_put_interface(intf);
  754. return 0;
  755. }
  756. static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
  757. {
  758. int rv = -EINVAL;
  759. struct usb_host_interface *iface;
  760. struct usb_endpoint_descriptor *ep;
  761. struct usb_cdc_parsed_header hdr;
  762. u8 *buffer = intf->altsetting->extra;
  763. int buflen = intf->altsetting->extralen;
  764. u16 maxcom = WDM_DEFAULT_BUFSIZE;
  765. if (!buffer)
  766. goto err;
  767. cdc_parse_cdc_header(&hdr, intf, buffer, buflen);
  768. if (hdr.usb_cdc_dmm_desc)
  769. maxcom = le16_to_cpu(hdr.usb_cdc_dmm_desc->wMaxCommand);
  770. iface = intf->cur_altsetting;
  771. if (iface->desc.bNumEndpoints != 1)
  772. goto err;
  773. ep = &iface->endpoint[0].desc;
  774. rv = wdm_create(intf, ep, maxcom, &wdm_manage_power);
  775. err:
  776. return rv;
  777. }
  778. /**
  779. * usb_cdc_wdm_register - register a WDM subdriver
  780. * @intf: usb interface the subdriver will associate with
  781. * @ep: interrupt endpoint to monitor for notifications
  782. * @bufsize: maximum message size to support for read/write
  783. *
  784. * Create WDM usb class character device and associate it with intf
  785. * without binding, allowing another driver to manage the interface.
  786. *
  787. * The subdriver will manage the given interrupt endpoint exclusively
  788. * and will issue control requests referring to the given intf. It
  789. * will otherwise avoid interferring, and in particular not do
  790. * usb_set_intfdata/usb_get_intfdata on intf.
  791. *
  792. * The return value is a pointer to the subdriver's struct usb_driver.
  793. * The registering driver is responsible for calling this subdriver's
  794. * disconnect, suspend, resume, pre_reset and post_reset methods from
  795. * its own.
  796. */
  797. struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
  798. struct usb_endpoint_descriptor *ep,
  799. int bufsize,
  800. int (*manage_power)(struct usb_interface *, int))
  801. {
  802. int rv = -EINVAL;
  803. rv = wdm_create(intf, ep, bufsize, manage_power);
  804. if (rv < 0)
  805. goto err;
  806. return &wdm_driver;
  807. err:
  808. return ERR_PTR(rv);
  809. }
  810. EXPORT_SYMBOL(usb_cdc_wdm_register);
  811. static void wdm_disconnect(struct usb_interface *intf)
  812. {
  813. struct wdm_device *desc;
  814. unsigned long flags;
  815. usb_deregister_dev(intf, &wdm_class);
  816. desc = wdm_find_device(intf);
  817. mutex_lock(&wdm_mutex);
  818. /* the spinlock makes sure no new urbs are generated in the callbacks */
  819. spin_lock_irqsave(&desc->iuspin, flags);
  820. set_bit(WDM_DISCONNECTING, &desc->flags);
  821. set_bit(WDM_READ, &desc->flags);
  822. /* to terminate pending flushes */
  823. clear_bit(WDM_IN_USE, &desc->flags);
  824. spin_unlock_irqrestore(&desc->iuspin, flags);
  825. wake_up_all(&desc->wait);
  826. mutex_lock(&desc->rlock);
  827. mutex_lock(&desc->wlock);
  828. kill_urbs(desc);
  829. cancel_work_sync(&desc->rxwork);
  830. mutex_unlock(&desc->wlock);
  831. mutex_unlock(&desc->rlock);
  832. /* the desc->intf pointer used as list key is now invalid */
  833. spin_lock(&wdm_device_list_lock);
  834. list_del(&desc->device_list);
  835. spin_unlock(&wdm_device_list_lock);
  836. if (!desc->count)
  837. cleanup(desc);
  838. else
  839. dev_dbg(&intf->dev, "%s: %d open files - postponing cleanup\n", __func__, desc->count);
  840. mutex_unlock(&wdm_mutex);
  841. }
  842. #ifdef CONFIG_PM
  843. static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
  844. {
  845. struct wdm_device *desc = wdm_find_device(intf);
  846. int rv = 0;
  847. dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
  848. /* if this is an autosuspend the caller does the locking */
  849. if (!PMSG_IS_AUTO(message)) {
  850. mutex_lock(&desc->rlock);
  851. mutex_lock(&desc->wlock);
  852. }
  853. spin_lock_irq(&desc->iuspin);
  854. if (PMSG_IS_AUTO(message) &&
  855. (test_bit(WDM_IN_USE, &desc->flags)
  856. || test_bit(WDM_RESPONDING, &desc->flags))) {
  857. spin_unlock_irq(&desc->iuspin);
  858. rv = -EBUSY;
  859. } else {
  860. set_bit(WDM_SUSPENDING, &desc->flags);
  861. spin_unlock_irq(&desc->iuspin);
  862. /* callback submits work - order is essential */
  863. kill_urbs(desc);
  864. cancel_work_sync(&desc->rxwork);
  865. }
  866. if (!PMSG_IS_AUTO(message)) {
  867. mutex_unlock(&desc->wlock);
  868. mutex_unlock(&desc->rlock);
  869. }
  870. return rv;
  871. }
  872. #endif
  873. static int recover_from_urb_loss(struct wdm_device *desc)
  874. {
  875. int rv = 0;
  876. if (desc->count) {
  877. rv = usb_submit_urb(desc->validity, GFP_NOIO);
  878. if (rv < 0)
  879. dev_err(&desc->intf->dev,
  880. "Error resume submitting int urb - %d\n", rv);
  881. }
  882. return rv;
  883. }
  884. #ifdef CONFIG_PM
  885. static int wdm_resume(struct usb_interface *intf)
  886. {
  887. struct wdm_device *desc = wdm_find_device(intf);
  888. int rv;
  889. dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
  890. clear_bit(WDM_SUSPENDING, &desc->flags);
  891. rv = recover_from_urb_loss(desc);
  892. return rv;
  893. }
  894. #endif
  895. static int wdm_pre_reset(struct usb_interface *intf)
  896. {
  897. struct wdm_device *desc = wdm_find_device(intf);
  898. /*
  899. * we notify everybody using poll of
  900. * an exceptional situation
  901. * must be done before recovery lest a spontaneous
  902. * message from the device is lost
  903. */
  904. spin_lock_irq(&desc->iuspin);
  905. set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
  906. set_bit(WDM_READ, &desc->flags); /* unblock read */
  907. clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
  908. desc->rerr = -EINTR;
  909. spin_unlock_irq(&desc->iuspin);
  910. wake_up_all(&desc->wait);
  911. mutex_lock(&desc->rlock);
  912. mutex_lock(&desc->wlock);
  913. kill_urbs(desc);
  914. cancel_work_sync(&desc->rxwork);
  915. return 0;
  916. }
  917. static int wdm_post_reset(struct usb_interface *intf)
  918. {
  919. struct wdm_device *desc = wdm_find_device(intf);
  920. int rv;
  921. clear_bit(WDM_OVERFLOW, &desc->flags);
  922. clear_bit(WDM_RESETTING, &desc->flags);
  923. rv = recover_from_urb_loss(desc);
  924. mutex_unlock(&desc->wlock);
  925. mutex_unlock(&desc->rlock);
  926. return 0;
  927. }
  928. static struct usb_driver wdm_driver = {
  929. .name = "cdc_wdm",
  930. .probe = wdm_probe,
  931. .disconnect = wdm_disconnect,
  932. #ifdef CONFIG_PM
  933. .suspend = wdm_suspend,
  934. .resume = wdm_resume,
  935. .reset_resume = wdm_resume,
  936. #endif
  937. .pre_reset = wdm_pre_reset,
  938. .post_reset = wdm_post_reset,
  939. .id_table = wdm_ids,
  940. .supports_autosuspend = 1,
  941. .disable_hub_initiated_lpm = 1,
  942. };
  943. module_usb_driver(wdm_driver);
  944. MODULE_AUTHOR(DRIVER_AUTHOR);
  945. MODULE_DESCRIPTION(DRIVER_DESC);
  946. MODULE_LICENSE("GPL");