cdc-wdm.c 26 KB

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