cdc-wdm.c 26 KB

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