cdc-wdm.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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, !test_bit(WDM_IN_USE, &desc->flags));
  516. /* cannot dereference desc->intf if WDM_DISCONNECTING */
  517. if (desc->werr < 0 && !test_bit(WDM_DISCONNECTING, &desc->flags))
  518. dev_err(&desc->intf->dev, "Error in flush path: %d\n",
  519. desc->werr);
  520. return usb_translate_errors(desc->werr);
  521. }
  522. static __poll_t wdm_poll(struct file *file, struct poll_table_struct *wait)
  523. {
  524. struct wdm_device *desc = file->private_data;
  525. unsigned long flags;
  526. __poll_t mask = 0;
  527. spin_lock_irqsave(&desc->iuspin, flags);
  528. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  529. mask = EPOLLHUP | EPOLLERR;
  530. spin_unlock_irqrestore(&desc->iuspin, flags);
  531. goto desc_out;
  532. }
  533. if (test_bit(WDM_READ, &desc->flags))
  534. mask = EPOLLIN | EPOLLRDNORM;
  535. if (desc->rerr || desc->werr)
  536. mask |= EPOLLERR;
  537. if (!test_bit(WDM_IN_USE, &desc->flags))
  538. mask |= EPOLLOUT | EPOLLWRNORM;
  539. spin_unlock_irqrestore(&desc->iuspin, flags);
  540. poll_wait(file, &desc->wait, wait);
  541. desc_out:
  542. return mask;
  543. }
  544. static int wdm_open(struct inode *inode, struct file *file)
  545. {
  546. int minor = iminor(inode);
  547. int rv = -ENODEV;
  548. struct usb_interface *intf;
  549. struct wdm_device *desc;
  550. mutex_lock(&wdm_mutex);
  551. desc = wdm_find_device_by_minor(minor);
  552. if (!desc)
  553. goto out;
  554. intf = desc->intf;
  555. if (test_bit(WDM_DISCONNECTING, &desc->flags))
  556. goto out;
  557. file->private_data = desc;
  558. rv = usb_autopm_get_interface(desc->intf);
  559. if (rv < 0) {
  560. dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
  561. goto out;
  562. }
  563. /* using write lock to protect desc->count */
  564. mutex_lock(&desc->wlock);
  565. if (!desc->count++) {
  566. desc->werr = 0;
  567. desc->rerr = 0;
  568. rv = usb_submit_urb(desc->validity, GFP_KERNEL);
  569. if (rv < 0) {
  570. desc->count--;
  571. dev_err(&desc->intf->dev,
  572. "Error submitting int urb - %d\n", rv);
  573. rv = usb_translate_errors(rv);
  574. }
  575. } else {
  576. rv = 0;
  577. }
  578. mutex_unlock(&desc->wlock);
  579. if (desc->count == 1)
  580. desc->manage_power(intf, 1);
  581. usb_autopm_put_interface(desc->intf);
  582. out:
  583. mutex_unlock(&wdm_mutex);
  584. return rv;
  585. }
  586. static int wdm_release(struct inode *inode, struct file *file)
  587. {
  588. struct wdm_device *desc = file->private_data;
  589. mutex_lock(&wdm_mutex);
  590. /* using write lock to protect desc->count */
  591. mutex_lock(&desc->wlock);
  592. desc->count--;
  593. mutex_unlock(&desc->wlock);
  594. if (!desc->count) {
  595. if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
  596. dev_dbg(&desc->intf->dev, "wdm_release: cleanup\n");
  597. kill_urbs(desc);
  598. spin_lock_irq(&desc->iuspin);
  599. desc->resp_count = 0;
  600. spin_unlock_irq(&desc->iuspin);
  601. desc->manage_power(desc->intf, 0);
  602. } else {
  603. /* must avoid dev_printk here as desc->intf is invalid */
  604. pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
  605. cleanup(desc);
  606. }
  607. }
  608. mutex_unlock(&wdm_mutex);
  609. return 0;
  610. }
  611. static long wdm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  612. {
  613. struct wdm_device *desc = file->private_data;
  614. int rv = 0;
  615. switch (cmd) {
  616. case IOCTL_WDM_MAX_COMMAND:
  617. if (copy_to_user((void __user *)arg, &desc->wMaxCommand, sizeof(desc->wMaxCommand)))
  618. rv = -EFAULT;
  619. break;
  620. default:
  621. rv = -ENOTTY;
  622. }
  623. return rv;
  624. }
  625. static const struct file_operations wdm_fops = {
  626. .owner = THIS_MODULE,
  627. .read = wdm_read,
  628. .write = wdm_write,
  629. .open = wdm_open,
  630. .flush = wdm_flush,
  631. .release = wdm_release,
  632. .poll = wdm_poll,
  633. .unlocked_ioctl = wdm_ioctl,
  634. .compat_ioctl = wdm_ioctl,
  635. .llseek = noop_llseek,
  636. };
  637. static struct usb_class_driver wdm_class = {
  638. .name = "cdc-wdm%d",
  639. .fops = &wdm_fops,
  640. .minor_base = WDM_MINOR_BASE,
  641. };
  642. /* --- error handling --- */
  643. static void wdm_rxwork(struct work_struct *work)
  644. {
  645. struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
  646. unsigned long flags;
  647. int rv = 0;
  648. int responding;
  649. spin_lock_irqsave(&desc->iuspin, flags);
  650. if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
  651. spin_unlock_irqrestore(&desc->iuspin, flags);
  652. } else {
  653. responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
  654. spin_unlock_irqrestore(&desc->iuspin, flags);
  655. if (!responding)
  656. rv = usb_submit_urb(desc->response, GFP_KERNEL);
  657. if (rv < 0 && rv != -EPERM) {
  658. spin_lock_irqsave(&desc->iuspin, flags);
  659. clear_bit(WDM_RESPONDING, &desc->flags);
  660. if (!test_bit(WDM_DISCONNECTING, &desc->flags))
  661. schedule_work(&desc->rxwork);
  662. spin_unlock_irqrestore(&desc->iuspin, flags);
  663. }
  664. }
  665. }
  666. static void service_interrupt_work(struct work_struct *work)
  667. {
  668. struct wdm_device *desc;
  669. desc = container_of(work, struct wdm_device, service_outs_intr);
  670. spin_lock_irq(&desc->iuspin);
  671. service_outstanding_interrupt(desc);
  672. if (!desc->resp_count) {
  673. set_bit(WDM_READ, &desc->flags);
  674. wake_up(&desc->wait);
  675. }
  676. spin_unlock_irq(&desc->iuspin);
  677. }
  678. /* --- hotplug --- */
  679. static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
  680. u16 bufsize, int (*manage_power)(struct usb_interface *, int))
  681. {
  682. int rv = -ENOMEM;
  683. struct wdm_device *desc;
  684. desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
  685. if (!desc)
  686. goto out;
  687. INIT_LIST_HEAD(&desc->device_list);
  688. mutex_init(&desc->rlock);
  689. mutex_init(&desc->wlock);
  690. spin_lock_init(&desc->iuspin);
  691. init_waitqueue_head(&desc->wait);
  692. desc->wMaxCommand = bufsize;
  693. /* this will be expanded and needed in hardware endianness */
  694. desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
  695. desc->intf = intf;
  696. INIT_WORK(&desc->rxwork, wdm_rxwork);
  697. INIT_WORK(&desc->service_outs_intr, service_interrupt_work);
  698. rv = -EINVAL;
  699. if (!usb_endpoint_is_int_in(ep))
  700. goto err;
  701. desc->wMaxPacketSize = usb_endpoint_maxp(ep);
  702. desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  703. if (!desc->orq)
  704. goto err;
  705. desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  706. if (!desc->irq)
  707. goto err;
  708. desc->validity = usb_alloc_urb(0, GFP_KERNEL);
  709. if (!desc->validity)
  710. goto err;
  711. desc->response = usb_alloc_urb(0, GFP_KERNEL);
  712. if (!desc->response)
  713. goto err;
  714. desc->command = usb_alloc_urb(0, GFP_KERNEL);
  715. if (!desc->command)
  716. goto err;
  717. desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
  718. if (!desc->ubuf)
  719. goto err;
  720. desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
  721. if (!desc->sbuf)
  722. goto err;
  723. desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
  724. if (!desc->inbuf)
  725. goto err;
  726. usb_fill_int_urb(
  727. desc->validity,
  728. interface_to_usbdev(intf),
  729. usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
  730. desc->sbuf,
  731. desc->wMaxPacketSize,
  732. wdm_int_callback,
  733. desc,
  734. ep->bInterval
  735. );
  736. desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
  737. desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
  738. desc->irq->wValue = 0;
  739. desc->irq->wIndex = desc->inum; /* already converted */
  740. desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
  741. usb_fill_control_urb(
  742. desc->response,
  743. interface_to_usbdev(intf),
  744. /* using common endpoint 0 */
  745. usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
  746. (unsigned char *)desc->irq,
  747. desc->inbuf,
  748. desc->wMaxCommand,
  749. wdm_in_callback,
  750. desc
  751. );
  752. desc->manage_power = manage_power;
  753. spin_lock(&wdm_device_list_lock);
  754. list_add(&desc->device_list, &wdm_device_list);
  755. spin_unlock(&wdm_device_list_lock);
  756. rv = usb_register_dev(intf, &wdm_class);
  757. if (rv < 0)
  758. goto err;
  759. else
  760. dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
  761. out:
  762. return rv;
  763. err:
  764. spin_lock(&wdm_device_list_lock);
  765. list_del(&desc->device_list);
  766. spin_unlock(&wdm_device_list_lock);
  767. cleanup(desc);
  768. return rv;
  769. }
  770. static int wdm_manage_power(struct usb_interface *intf, int on)
  771. {
  772. /* need autopm_get/put here to ensure the usbcore sees the new value */
  773. int rv = usb_autopm_get_interface(intf);
  774. intf->needs_remote_wakeup = on;
  775. if (!rv)
  776. usb_autopm_put_interface(intf);
  777. return 0;
  778. }
  779. static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
  780. {
  781. int rv = -EINVAL;
  782. struct usb_host_interface *iface;
  783. struct usb_endpoint_descriptor *ep;
  784. struct usb_cdc_parsed_header hdr;
  785. u8 *buffer = intf->altsetting->extra;
  786. int buflen = intf->altsetting->extralen;
  787. u16 maxcom = WDM_DEFAULT_BUFSIZE;
  788. if (!buffer)
  789. goto err;
  790. cdc_parse_cdc_header(&hdr, intf, buffer, buflen);
  791. if (hdr.usb_cdc_dmm_desc)
  792. maxcom = le16_to_cpu(hdr.usb_cdc_dmm_desc->wMaxCommand);
  793. iface = intf->cur_altsetting;
  794. if (iface->desc.bNumEndpoints != 1)
  795. goto err;
  796. ep = &iface->endpoint[0].desc;
  797. rv = wdm_create(intf, ep, maxcom, &wdm_manage_power);
  798. err:
  799. return rv;
  800. }
  801. /**
  802. * usb_cdc_wdm_register - register a WDM subdriver
  803. * @intf: usb interface the subdriver will associate with
  804. * @ep: interrupt endpoint to monitor for notifications
  805. * @bufsize: maximum message size to support for read/write
  806. *
  807. * Create WDM usb class character device and associate it with intf
  808. * without binding, allowing another driver to manage the interface.
  809. *
  810. * The subdriver will manage the given interrupt endpoint exclusively
  811. * and will issue control requests referring to the given intf. It
  812. * will otherwise avoid interferring, and in particular not do
  813. * usb_set_intfdata/usb_get_intfdata on intf.
  814. *
  815. * The return value is a pointer to the subdriver's struct usb_driver.
  816. * The registering driver is responsible for calling this subdriver's
  817. * disconnect, suspend, resume, pre_reset and post_reset methods from
  818. * its own.
  819. */
  820. struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
  821. struct usb_endpoint_descriptor *ep,
  822. int bufsize,
  823. int (*manage_power)(struct usb_interface *, int))
  824. {
  825. int rv = -EINVAL;
  826. rv = wdm_create(intf, ep, bufsize, manage_power);
  827. if (rv < 0)
  828. goto err;
  829. return &wdm_driver;
  830. err:
  831. return ERR_PTR(rv);
  832. }
  833. EXPORT_SYMBOL(usb_cdc_wdm_register);
  834. static void wdm_disconnect(struct usb_interface *intf)
  835. {
  836. struct wdm_device *desc;
  837. unsigned long flags;
  838. usb_deregister_dev(intf, &wdm_class);
  839. desc = wdm_find_device(intf);
  840. mutex_lock(&wdm_mutex);
  841. /* the spinlock makes sure no new urbs are generated in the callbacks */
  842. spin_lock_irqsave(&desc->iuspin, flags);
  843. set_bit(WDM_DISCONNECTING, &desc->flags);
  844. set_bit(WDM_READ, &desc->flags);
  845. /* to terminate pending flushes */
  846. clear_bit(WDM_IN_USE, &desc->flags);
  847. spin_unlock_irqrestore(&desc->iuspin, flags);
  848. wake_up_all(&desc->wait);
  849. mutex_lock(&desc->rlock);
  850. mutex_lock(&desc->wlock);
  851. kill_urbs(desc);
  852. cancel_work_sync(&desc->rxwork);
  853. cancel_work_sync(&desc->service_outs_intr);
  854. mutex_unlock(&desc->wlock);
  855. mutex_unlock(&desc->rlock);
  856. /* the desc->intf pointer used as list key is now invalid */
  857. spin_lock(&wdm_device_list_lock);
  858. list_del(&desc->device_list);
  859. spin_unlock(&wdm_device_list_lock);
  860. if (!desc->count)
  861. cleanup(desc);
  862. else
  863. dev_dbg(&intf->dev, "%d open files - postponing cleanup\n", desc->count);
  864. mutex_unlock(&wdm_mutex);
  865. }
  866. #ifdef CONFIG_PM
  867. static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
  868. {
  869. struct wdm_device *desc = wdm_find_device(intf);
  870. int rv = 0;
  871. dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
  872. /* if this is an autosuspend the caller does the locking */
  873. if (!PMSG_IS_AUTO(message)) {
  874. mutex_lock(&desc->rlock);
  875. mutex_lock(&desc->wlock);
  876. }
  877. spin_lock_irq(&desc->iuspin);
  878. if (PMSG_IS_AUTO(message) &&
  879. (test_bit(WDM_IN_USE, &desc->flags)
  880. || test_bit(WDM_RESPONDING, &desc->flags))) {
  881. spin_unlock_irq(&desc->iuspin);
  882. rv = -EBUSY;
  883. } else {
  884. set_bit(WDM_SUSPENDING, &desc->flags);
  885. spin_unlock_irq(&desc->iuspin);
  886. /* callback submits work - order is essential */
  887. kill_urbs(desc);
  888. cancel_work_sync(&desc->rxwork);
  889. cancel_work_sync(&desc->service_outs_intr);
  890. }
  891. if (!PMSG_IS_AUTO(message)) {
  892. mutex_unlock(&desc->wlock);
  893. mutex_unlock(&desc->rlock);
  894. }
  895. return rv;
  896. }
  897. #endif
  898. static int recover_from_urb_loss(struct wdm_device *desc)
  899. {
  900. int rv = 0;
  901. if (desc->count) {
  902. rv = usb_submit_urb(desc->validity, GFP_NOIO);
  903. if (rv < 0)
  904. dev_err(&desc->intf->dev,
  905. "Error resume submitting int urb - %d\n", rv);
  906. }
  907. return rv;
  908. }
  909. #ifdef CONFIG_PM
  910. static int wdm_resume(struct usb_interface *intf)
  911. {
  912. struct wdm_device *desc = wdm_find_device(intf);
  913. int rv;
  914. dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
  915. clear_bit(WDM_SUSPENDING, &desc->flags);
  916. rv = recover_from_urb_loss(desc);
  917. return rv;
  918. }
  919. #endif
  920. static int wdm_pre_reset(struct usb_interface *intf)
  921. {
  922. struct wdm_device *desc = wdm_find_device(intf);
  923. /*
  924. * we notify everybody using poll of
  925. * an exceptional situation
  926. * must be done before recovery lest a spontaneous
  927. * message from the device is lost
  928. */
  929. spin_lock_irq(&desc->iuspin);
  930. set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
  931. set_bit(WDM_READ, &desc->flags); /* unblock read */
  932. clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
  933. desc->rerr = -EINTR;
  934. spin_unlock_irq(&desc->iuspin);
  935. wake_up_all(&desc->wait);
  936. mutex_lock(&desc->rlock);
  937. mutex_lock(&desc->wlock);
  938. kill_urbs(desc);
  939. cancel_work_sync(&desc->rxwork);
  940. cancel_work_sync(&desc->service_outs_intr);
  941. return 0;
  942. }
  943. static int wdm_post_reset(struct usb_interface *intf)
  944. {
  945. struct wdm_device *desc = wdm_find_device(intf);
  946. int rv;
  947. clear_bit(WDM_OVERFLOW, &desc->flags);
  948. clear_bit(WDM_RESETTING, &desc->flags);
  949. rv = recover_from_urb_loss(desc);
  950. mutex_unlock(&desc->wlock);
  951. mutex_unlock(&desc->rlock);
  952. return 0;
  953. }
  954. static struct usb_driver wdm_driver = {
  955. .name = "cdc_wdm",
  956. .probe = wdm_probe,
  957. .disconnect = wdm_disconnect,
  958. #ifdef CONFIG_PM
  959. .suspend = wdm_suspend,
  960. .resume = wdm_resume,
  961. .reset_resume = wdm_resume,
  962. #endif
  963. .pre_reset = wdm_pre_reset,
  964. .post_reset = wdm_post_reset,
  965. .id_table = wdm_ids,
  966. .supports_autosuspend = 1,
  967. .disable_hub_initiated_lpm = 1,
  968. };
  969. module_usb_driver(wdm_driver);
  970. MODULE_AUTHOR(DRIVER_AUTHOR);
  971. MODULE_DESCRIPTION(DRIVER_DESC);
  972. MODULE_LICENSE("GPL");