cdc-wdm.c 24 KB

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