cdc-wdm.c 30 KB

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