cdc-wdm.c 24 KB

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