cdc-wdm.c 26 KB

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