cdc-wdm.c 27 KB

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