cdc-wdm.c 24 KB

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