cdc-wdm.c 24 KB

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