cdc-wdm.c 23 KB

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