cdc-wdm.c 24 KB

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