cdc-wdm.c 25 KB

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