cdc-acm.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  1. /*
  2. * cdc-acm.c
  3. *
  4. * Copyright (c) 1999 Armin Fuerst <fuerst@in.tum.de>
  5. * Copyright (c) 1999 Pavel Machek <pavel@suse.cz>
  6. * Copyright (c) 1999 Johannes Erdfelt <johannes@erdfelt.com>
  7. * Copyright (c) 2000 Vojtech Pavlik <vojtech@suse.cz>
  8. * Copyright (c) 2004 Oliver Neukum <oliver@neukum.name>
  9. * Copyright (c) 2005 David Kubicek <dave@awk.cz>
  10. *
  11. * USB Abstract Control Model driver for USB modems and ISDN adapters
  12. *
  13. * Sponsored by SuSE
  14. *
  15. * ChangeLog:
  16. * v0.9 - thorough cleaning, URBification, almost a rewrite
  17. * v0.10 - some more cleanups
  18. * v0.11 - fixed flow control, read error doesn't stop reads
  19. * v0.12 - added TIOCM ioctls, added break handling, made struct acm kmalloced
  20. * v0.13 - added termios, added hangup
  21. * v0.14 - sized down struct acm
  22. * v0.15 - fixed flow control again - characters could be lost
  23. * v0.16 - added code for modems with swapped data and control interfaces
  24. * v0.17 - added new style probing
  25. * v0.18 - fixed new style probing for devices with more configurations
  26. * v0.19 - fixed CLOCAL handling (thanks to Richard Shih-Ping Chan)
  27. * v0.20 - switched to probing on interface (rather than device) class
  28. * v0.21 - revert to probing on device for devices with multiple configs
  29. * v0.22 - probe only the control interface. if usbcore doesn't choose the
  30. * config we want, sysadmin changes bConfigurationValue in sysfs.
  31. * v0.23 - use softirq for rx processing, as needed by tty layer
  32. * v0.24 - change probe method to evaluate CDC union descriptor
  33. * v0.25 - downstream tasks paralelized to maximize throughput
  34. */
  35. /*
  36. * This program is free software; you can redistribute it and/or modify
  37. * it under the terms of the GNU General Public License as published by
  38. * the Free Software Foundation; either version 2 of the License, or
  39. * (at your option) any later version.
  40. *
  41. * This program is distributed in the hope that it will be useful,
  42. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  43. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  44. * GNU General Public License for more details.
  45. *
  46. * You should have received a copy of the GNU General Public License
  47. * along with this program; if not, write to the Free Software
  48. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  49. */
  50. #undef DEBUG
  51. #include <linux/kernel.h>
  52. #include <linux/errno.h>
  53. #include <linux/init.h>
  54. #include <linux/slab.h>
  55. #include <linux/tty.h>
  56. #include <linux/tty_driver.h>
  57. #include <linux/tty_flip.h>
  58. #include <linux/module.h>
  59. #include <linux/mutex.h>
  60. #include <asm/uaccess.h>
  61. #include <linux/usb.h>
  62. #include <linux/usb/cdc.h>
  63. #include <asm/byteorder.h>
  64. #include <asm/unaligned.h>
  65. #include <linux/list.h>
  66. #include "cdc-acm.h"
  67. /*
  68. * Version Information
  69. */
  70. #define DRIVER_VERSION "v0.25"
  71. #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek"
  72. #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
  73. static struct usb_driver acm_driver;
  74. static struct tty_driver *acm_tty_driver;
  75. static struct acm *acm_table[ACM_TTY_MINORS];
  76. static DEFINE_MUTEX(open_mutex);
  77. #define ACM_READY(acm) (acm && acm->dev && acm->used)
  78. /*
  79. * Functions for ACM control messages.
  80. */
  81. static int acm_ctrl_msg(struct acm *acm, int request, int value, void *buf, int len)
  82. {
  83. int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
  84. request, USB_RT_ACM, value,
  85. acm->control->altsetting[0].desc.bInterfaceNumber,
  86. buf, len, 5000);
  87. dbg("acm_control_msg: rq: 0x%02x val: %#x len: %#x result: %d", request, value, len, retval);
  88. return retval < 0 ? retval : 0;
  89. }
  90. /* devices aren't required to support these requests.
  91. * the cdc acm descriptor tells whether they do...
  92. */
  93. #define acm_set_control(acm, control) \
  94. acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0)
  95. #define acm_set_line(acm, line) \
  96. acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
  97. #define acm_send_break(acm, ms) \
  98. acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
  99. /*
  100. * Write buffer management.
  101. * All of these assume proper locks taken by the caller.
  102. */
  103. static int acm_wb_alloc(struct acm *acm)
  104. {
  105. int i, wbn;
  106. struct acm_wb *wb;
  107. wbn = acm->write_current;
  108. i = 0;
  109. for (;;) {
  110. wb = &acm->wb[wbn];
  111. if (!wb->use) {
  112. wb->use = 1;
  113. return wbn;
  114. }
  115. wbn = (wbn + 1) % ACM_NW;
  116. if (++i >= ACM_NW)
  117. return -1;
  118. }
  119. }
  120. static void acm_wb_free(struct acm *acm, int wbn)
  121. {
  122. acm->wb[wbn].use = 0;
  123. }
  124. static int acm_wb_is_avail(struct acm *acm)
  125. {
  126. int i, n;
  127. n = ACM_NW;
  128. for (i = 0; i < ACM_NW; i++) {
  129. n -= acm->wb[i].use;
  130. }
  131. return n;
  132. }
  133. static inline int acm_wb_is_used(struct acm *acm, int wbn)
  134. {
  135. return acm->wb[wbn].use;
  136. }
  137. /*
  138. * Finish write.
  139. */
  140. static void acm_write_done(struct acm *acm)
  141. {
  142. unsigned long flags;
  143. int wbn;
  144. spin_lock_irqsave(&acm->write_lock, flags);
  145. acm->write_ready = 1;
  146. wbn = acm->write_current;
  147. acm_wb_free(acm, wbn);
  148. acm->write_current = (wbn + 1) % ACM_NW;
  149. spin_unlock_irqrestore(&acm->write_lock, flags);
  150. }
  151. /*
  152. * Poke write.
  153. */
  154. static int acm_write_start(struct acm *acm)
  155. {
  156. unsigned long flags;
  157. int wbn;
  158. struct acm_wb *wb;
  159. int rc;
  160. spin_lock_irqsave(&acm->write_lock, flags);
  161. if (!acm->dev) {
  162. spin_unlock_irqrestore(&acm->write_lock, flags);
  163. return -ENODEV;
  164. }
  165. if (!acm->write_ready) {
  166. spin_unlock_irqrestore(&acm->write_lock, flags);
  167. return 0; /* A white lie */
  168. }
  169. wbn = acm->write_current;
  170. if (!acm_wb_is_used(acm, wbn)) {
  171. spin_unlock_irqrestore(&acm->write_lock, flags);
  172. return 0;
  173. }
  174. wb = &acm->wb[wbn];
  175. acm->write_ready = 0;
  176. spin_unlock_irqrestore(&acm->write_lock, flags);
  177. acm->writeurb->transfer_buffer = wb->buf;
  178. acm->writeurb->transfer_dma = wb->dmah;
  179. acm->writeurb->transfer_buffer_length = wb->len;
  180. acm->writeurb->dev = acm->dev;
  181. if ((rc = usb_submit_urb(acm->writeurb, GFP_ATOMIC)) < 0) {
  182. dbg("usb_submit_urb(write bulk) failed: %d", rc);
  183. acm_write_done(acm);
  184. }
  185. return rc;
  186. }
  187. /*
  188. * attributes exported through sysfs
  189. */
  190. static ssize_t show_caps
  191. (struct device *dev, struct device_attribute *attr, char *buf)
  192. {
  193. struct usb_interface *intf = to_usb_interface(dev);
  194. struct acm *acm = usb_get_intfdata(intf);
  195. return sprintf(buf, "%d", acm->ctrl_caps);
  196. }
  197. static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
  198. static ssize_t show_country_codes
  199. (struct device *dev, struct device_attribute *attr, char *buf)
  200. {
  201. struct usb_interface *intf = to_usb_interface(dev);
  202. struct acm *acm = usb_get_intfdata(intf);
  203. memcpy(buf, acm->country_codes, acm->country_code_size);
  204. return acm->country_code_size;
  205. }
  206. static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
  207. static ssize_t show_country_rel_date
  208. (struct device *dev, struct device_attribute *attr, char *buf)
  209. {
  210. struct usb_interface *intf = to_usb_interface(dev);
  211. struct acm *acm = usb_get_intfdata(intf);
  212. return sprintf(buf, "%d", acm->country_rel_date);
  213. }
  214. static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
  215. /*
  216. * Interrupt handlers for various ACM device responses
  217. */
  218. /* control interface reports status changes with "interrupt" transfers */
  219. static void acm_ctrl_irq(struct urb *urb)
  220. {
  221. struct acm *acm = urb->context;
  222. struct usb_cdc_notification *dr = urb->transfer_buffer;
  223. unsigned char *data;
  224. int newctrl;
  225. int retval;
  226. int status = urb->status;
  227. switch (status) {
  228. case 0:
  229. /* success */
  230. break;
  231. case -ECONNRESET:
  232. case -ENOENT:
  233. case -ESHUTDOWN:
  234. /* this urb is terminated, clean up */
  235. dbg("%s - urb shutting down with status: %d", __FUNCTION__, status);
  236. return;
  237. default:
  238. dbg("%s - nonzero urb status received: %d", __FUNCTION__, status);
  239. goto exit;
  240. }
  241. if (!ACM_READY(acm))
  242. goto exit;
  243. data = (unsigned char *)(dr + 1);
  244. switch (dr->bNotificationType) {
  245. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  246. dbg("%s network", dr->wValue ? "connected to" : "disconnected from");
  247. break;
  248. case USB_CDC_NOTIFY_SERIAL_STATE:
  249. newctrl = le16_to_cpu(get_unaligned((__le16 *) data));
  250. if (acm->tty && !acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
  251. dbg("calling hangup");
  252. tty_hangup(acm->tty);
  253. }
  254. acm->ctrlin = newctrl;
  255. dbg("input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c",
  256. acm->ctrlin & ACM_CTRL_DCD ? '+' : '-', acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
  257. acm->ctrlin & ACM_CTRL_BRK ? '+' : '-', acm->ctrlin & ACM_CTRL_RI ? '+' : '-',
  258. acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-', acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
  259. acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
  260. break;
  261. default:
  262. dbg("unknown notification %d received: index %d len %d data0 %d data1 %d",
  263. dr->bNotificationType, dr->wIndex,
  264. dr->wLength, data[0], data[1]);
  265. break;
  266. }
  267. exit:
  268. retval = usb_submit_urb (urb, GFP_ATOMIC);
  269. if (retval)
  270. err ("%s - usb_submit_urb failed with result %d",
  271. __FUNCTION__, retval);
  272. }
  273. /* data interface returns incoming bytes, or we got unthrottled */
  274. static void acm_read_bulk(struct urb *urb)
  275. {
  276. struct acm_rb *buf;
  277. struct acm_ru *rcv = urb->context;
  278. struct acm *acm = rcv->instance;
  279. int status = urb->status;
  280. dbg("Entering acm_read_bulk with status %d", status);
  281. if (!ACM_READY(acm))
  282. return;
  283. if (status)
  284. dev_dbg(&acm->data->dev, "bulk rx status %d\n", status);
  285. buf = rcv->buffer;
  286. buf->size = urb->actual_length;
  287. if (likely(status == 0)) {
  288. spin_lock(&acm->read_lock);
  289. list_add_tail(&rcv->list, &acm->spare_read_urbs);
  290. list_add_tail(&buf->list, &acm->filled_read_bufs);
  291. spin_unlock(&acm->read_lock);
  292. } else {
  293. /* we drop the buffer due to an error */
  294. spin_lock(&acm->read_lock);
  295. list_add_tail(&rcv->list, &acm->spare_read_urbs);
  296. list_add(&buf->list, &acm->spare_read_bufs);
  297. spin_unlock(&acm->read_lock);
  298. /* nevertheless the tasklet must be kicked unconditionally
  299. so the queue cannot dry up */
  300. }
  301. tasklet_schedule(&acm->urb_task);
  302. }
  303. static void acm_rx_tasklet(unsigned long _acm)
  304. {
  305. struct acm *acm = (void *)_acm;
  306. struct acm_rb *buf;
  307. struct tty_struct *tty = acm->tty;
  308. struct acm_ru *rcv;
  309. unsigned long flags;
  310. unsigned char throttled;
  311. dbg("Entering acm_rx_tasklet");
  312. if (!ACM_READY(acm))
  313. return;
  314. spin_lock_irqsave(&acm->throttle_lock, flags);
  315. throttled = acm->throttle;
  316. spin_unlock_irqrestore(&acm->throttle_lock, flags);
  317. if (throttled)
  318. return;
  319. next_buffer:
  320. spin_lock_irqsave(&acm->read_lock, flags);
  321. if (list_empty(&acm->filled_read_bufs)) {
  322. spin_unlock_irqrestore(&acm->read_lock, flags);
  323. goto urbs;
  324. }
  325. buf = list_entry(acm->filled_read_bufs.next,
  326. struct acm_rb, list);
  327. list_del(&buf->list);
  328. spin_unlock_irqrestore(&acm->read_lock, flags);
  329. dbg("acm_rx_tasklet: procesing buf 0x%p, size = %d", buf, buf->size);
  330. tty_buffer_request_room(tty, buf->size);
  331. spin_lock_irqsave(&acm->throttle_lock, flags);
  332. throttled = acm->throttle;
  333. spin_unlock_irqrestore(&acm->throttle_lock, flags);
  334. if (!throttled)
  335. tty_insert_flip_string(tty, buf->base, buf->size);
  336. tty_flip_buffer_push(tty);
  337. if (throttled) {
  338. dbg("Throttling noticed");
  339. spin_lock_irqsave(&acm->read_lock, flags);
  340. list_add(&buf->list, &acm->filled_read_bufs);
  341. spin_unlock_irqrestore(&acm->read_lock, flags);
  342. return;
  343. }
  344. spin_lock_irqsave(&acm->read_lock, flags);
  345. list_add(&buf->list, &acm->spare_read_bufs);
  346. spin_unlock_irqrestore(&acm->read_lock, flags);
  347. goto next_buffer;
  348. urbs:
  349. while (!list_empty(&acm->spare_read_bufs)) {
  350. spin_lock_irqsave(&acm->read_lock, flags);
  351. if (list_empty(&acm->spare_read_urbs)) {
  352. spin_unlock_irqrestore(&acm->read_lock, flags);
  353. return;
  354. }
  355. rcv = list_entry(acm->spare_read_urbs.next,
  356. struct acm_ru, list);
  357. list_del(&rcv->list);
  358. spin_unlock_irqrestore(&acm->read_lock, flags);
  359. buf = list_entry(acm->spare_read_bufs.next,
  360. struct acm_rb, list);
  361. list_del(&buf->list);
  362. rcv->buffer = buf;
  363. usb_fill_bulk_urb(rcv->urb, acm->dev,
  364. acm->rx_endpoint,
  365. buf->base,
  366. acm->readsize,
  367. acm_read_bulk, rcv);
  368. rcv->urb->transfer_dma = buf->dma;
  369. rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  370. dbg("acm_rx_tasklet: sending urb 0x%p, rcv 0x%p, buf 0x%p", rcv->urb, rcv, buf);
  371. /* This shouldn't kill the driver as unsuccessful URBs are returned to the
  372. free-urbs-pool and resubmited ASAP */
  373. if (usb_submit_urb(rcv->urb, GFP_ATOMIC) < 0) {
  374. list_add(&buf->list, &acm->spare_read_bufs);
  375. spin_lock_irqsave(&acm->read_lock, flags);
  376. list_add(&rcv->list, &acm->spare_read_urbs);
  377. spin_unlock_irqrestore(&acm->read_lock, flags);
  378. return;
  379. }
  380. }
  381. }
  382. /* data interface wrote those outgoing bytes */
  383. static void acm_write_bulk(struct urb *urb)
  384. {
  385. struct acm *acm = (struct acm *)urb->context;
  386. dbg("Entering acm_write_bulk with status %d", urb->status);
  387. acm_write_done(acm);
  388. acm_write_start(acm);
  389. if (ACM_READY(acm))
  390. schedule_work(&acm->work);
  391. }
  392. static void acm_softint(struct work_struct *work)
  393. {
  394. struct acm *acm = container_of(work, struct acm, work);
  395. dbg("Entering acm_softint.");
  396. if (!ACM_READY(acm))
  397. return;
  398. tty_wakeup(acm->tty);
  399. }
  400. /*
  401. * TTY handlers
  402. */
  403. static int acm_tty_open(struct tty_struct *tty, struct file *filp)
  404. {
  405. struct acm *acm;
  406. int rv = -EINVAL;
  407. int i;
  408. dbg("Entering acm_tty_open.");
  409. mutex_lock(&open_mutex);
  410. acm = acm_table[tty->index];
  411. if (!acm || !acm->dev)
  412. goto err_out;
  413. else
  414. rv = 0;
  415. tty->driver_data = acm;
  416. acm->tty = tty;
  417. /* force low_latency on so that our tty_push actually forces the data through,
  418. otherwise it is scheduled, and with high data rates data can get lost. */
  419. tty->low_latency = 1;
  420. if (usb_autopm_get_interface(acm->control)) {
  421. mutex_unlock(&open_mutex);
  422. return -EIO;
  423. }
  424. mutex_lock(&acm->mutex);
  425. mutex_unlock(&open_mutex);
  426. if (acm->used++) {
  427. usb_autopm_put_interface(acm->control);
  428. goto done;
  429. }
  430. acm->ctrlurb->dev = acm->dev;
  431. if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
  432. dbg("usb_submit_urb(ctrl irq) failed");
  433. goto bail_out;
  434. }
  435. if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS) &&
  436. (acm->ctrl_caps & USB_CDC_CAP_LINE))
  437. goto full_bailout;
  438. INIT_LIST_HEAD(&acm->spare_read_urbs);
  439. INIT_LIST_HEAD(&acm->spare_read_bufs);
  440. INIT_LIST_HEAD(&acm->filled_read_bufs);
  441. for (i = 0; i < acm->rx_buflimit; i++) {
  442. list_add(&(acm->ru[i].list), &acm->spare_read_urbs);
  443. }
  444. for (i = 0; i < acm->rx_buflimit; i++) {
  445. list_add(&(acm->rb[i].list), &acm->spare_read_bufs);
  446. }
  447. acm->throttle = 0;
  448. tasklet_schedule(&acm->urb_task);
  449. done:
  450. err_out:
  451. mutex_unlock(&acm->mutex);
  452. return rv;
  453. full_bailout:
  454. usb_kill_urb(acm->ctrlurb);
  455. bail_out:
  456. usb_autopm_put_interface(acm->control);
  457. acm->used--;
  458. mutex_unlock(&acm->mutex);
  459. return -EIO;
  460. }
  461. static void acm_tty_unregister(struct acm *acm)
  462. {
  463. int i,nr;
  464. nr = acm->rx_buflimit;
  465. tty_unregister_device(acm_tty_driver, acm->minor);
  466. usb_put_intf(acm->control);
  467. acm_table[acm->minor] = NULL;
  468. usb_free_urb(acm->ctrlurb);
  469. usb_free_urb(acm->writeurb);
  470. for (i = 0; i < nr; i++)
  471. usb_free_urb(acm->ru[i].urb);
  472. kfree(acm->country_codes);
  473. kfree(acm);
  474. }
  475. static void acm_tty_close(struct tty_struct *tty, struct file *filp)
  476. {
  477. struct acm *acm = tty->driver_data;
  478. int i,nr;
  479. if (!acm || !acm->used)
  480. return;
  481. nr = acm->rx_buflimit;
  482. mutex_lock(&open_mutex);
  483. if (!--acm->used) {
  484. if (acm->dev) {
  485. acm_set_control(acm, acm->ctrlout = 0);
  486. usb_kill_urb(acm->ctrlurb);
  487. usb_kill_urb(acm->writeurb);
  488. for (i = 0; i < nr; i++)
  489. usb_kill_urb(acm->ru[i].urb);
  490. usb_autopm_put_interface(acm->control);
  491. } else
  492. acm_tty_unregister(acm);
  493. }
  494. mutex_unlock(&open_mutex);
  495. }
  496. static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
  497. {
  498. struct acm *acm = tty->driver_data;
  499. int stat;
  500. unsigned long flags;
  501. int wbn;
  502. struct acm_wb *wb;
  503. dbg("Entering acm_tty_write to write %d bytes,", count);
  504. if (!ACM_READY(acm))
  505. return -EINVAL;
  506. if (!count)
  507. return 0;
  508. spin_lock_irqsave(&acm->write_lock, flags);
  509. if ((wbn = acm_wb_alloc(acm)) < 0) {
  510. spin_unlock_irqrestore(&acm->write_lock, flags);
  511. acm_write_start(acm);
  512. return 0;
  513. }
  514. wb = &acm->wb[wbn];
  515. count = (count > acm->writesize) ? acm->writesize : count;
  516. dbg("Get %d bytes...", count);
  517. memcpy(wb->buf, buf, count);
  518. wb->len = count;
  519. spin_unlock_irqrestore(&acm->write_lock, flags);
  520. if ((stat = acm_write_start(acm)) < 0)
  521. return stat;
  522. return count;
  523. }
  524. static int acm_tty_write_room(struct tty_struct *tty)
  525. {
  526. struct acm *acm = tty->driver_data;
  527. if (!ACM_READY(acm))
  528. return -EINVAL;
  529. /*
  530. * Do not let the line discipline to know that we have a reserve,
  531. * or it might get too enthusiastic.
  532. */
  533. return (acm->write_ready && acm_wb_is_avail(acm)) ? acm->writesize : 0;
  534. }
  535. static int acm_tty_chars_in_buffer(struct tty_struct *tty)
  536. {
  537. struct acm *acm = tty->driver_data;
  538. if (!ACM_READY(acm))
  539. return -EINVAL;
  540. /*
  541. * This is inaccurate (overcounts), but it works.
  542. */
  543. return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
  544. }
  545. static void acm_tty_throttle(struct tty_struct *tty)
  546. {
  547. struct acm *acm = tty->driver_data;
  548. if (!ACM_READY(acm))
  549. return;
  550. spin_lock_bh(&acm->throttle_lock);
  551. acm->throttle = 1;
  552. spin_unlock_bh(&acm->throttle_lock);
  553. }
  554. static void acm_tty_unthrottle(struct tty_struct *tty)
  555. {
  556. struct acm *acm = tty->driver_data;
  557. if (!ACM_READY(acm))
  558. return;
  559. spin_lock_bh(&acm->throttle_lock);
  560. acm->throttle = 0;
  561. spin_unlock_bh(&acm->throttle_lock);
  562. tasklet_schedule(&acm->urb_task);
  563. }
  564. static void acm_tty_break_ctl(struct tty_struct *tty, int state)
  565. {
  566. struct acm *acm = tty->driver_data;
  567. if (!ACM_READY(acm))
  568. return;
  569. if (acm_send_break(acm, state ? 0xffff : 0))
  570. dbg("send break failed");
  571. }
  572. static int acm_tty_tiocmget(struct tty_struct *tty, struct file *file)
  573. {
  574. struct acm *acm = tty->driver_data;
  575. if (!ACM_READY(acm))
  576. return -EINVAL;
  577. return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
  578. (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
  579. (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
  580. (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
  581. (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
  582. TIOCM_CTS;
  583. }
  584. static int acm_tty_tiocmset(struct tty_struct *tty, struct file *file,
  585. unsigned int set, unsigned int clear)
  586. {
  587. struct acm *acm = tty->driver_data;
  588. unsigned int newctrl;
  589. if (!ACM_READY(acm))
  590. return -EINVAL;
  591. newctrl = acm->ctrlout;
  592. set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
  593. clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
  594. newctrl = (newctrl & ~clear) | set;
  595. if (acm->ctrlout == newctrl)
  596. return 0;
  597. return acm_set_control(acm, acm->ctrlout = newctrl);
  598. }
  599. static int acm_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
  600. {
  601. struct acm *acm = tty->driver_data;
  602. if (!ACM_READY(acm))
  603. return -EINVAL;
  604. return -ENOIOCTLCMD;
  605. }
  606. static const __u32 acm_tty_speed[] = {
  607. 0, 50, 75, 110, 134, 150, 200, 300, 600,
  608. 1200, 1800, 2400, 4800, 9600, 19200, 38400,
  609. 57600, 115200, 230400, 460800, 500000, 576000,
  610. 921600, 1000000, 1152000, 1500000, 2000000,
  611. 2500000, 3000000, 3500000, 4000000
  612. };
  613. static const __u8 acm_tty_size[] = {
  614. 5, 6, 7, 8
  615. };
  616. static void acm_tty_set_termios(struct tty_struct *tty, struct ktermios *termios_old)
  617. {
  618. struct acm *acm = tty->driver_data;
  619. struct ktermios *termios = tty->termios;
  620. struct usb_cdc_line_coding newline;
  621. int newctrl = acm->ctrlout;
  622. if (!ACM_READY(acm))
  623. return;
  624. newline.dwDTERate = cpu_to_le32p(acm_tty_speed +
  625. (termios->c_cflag & CBAUD & ~CBAUDEX) + (termios->c_cflag & CBAUDEX ? 15 : 0));
  626. newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
  627. newline.bParityType = termios->c_cflag & PARENB ?
  628. (termios->c_cflag & PARODD ? 1 : 2) + (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
  629. newline.bDataBits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4];
  630. acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
  631. if (!newline.dwDTERate) {
  632. newline.dwDTERate = acm->line.dwDTERate;
  633. newctrl &= ~ACM_CTRL_DTR;
  634. } else newctrl |= ACM_CTRL_DTR;
  635. if (newctrl != acm->ctrlout)
  636. acm_set_control(acm, acm->ctrlout = newctrl);
  637. if (memcmp(&acm->line, &newline, sizeof newline)) {
  638. memcpy(&acm->line, &newline, sizeof newline);
  639. dbg("set line: %d %d %d %d", le32_to_cpu(newline.dwDTERate),
  640. newline.bCharFormat, newline.bParityType,
  641. newline.bDataBits);
  642. acm_set_line(acm, &acm->line);
  643. }
  644. }
  645. /*
  646. * USB probe and disconnect routines.
  647. */
  648. /* Little helper: write buffers free */
  649. static void acm_write_buffers_free(struct acm *acm)
  650. {
  651. int i;
  652. struct acm_wb *wb;
  653. for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
  654. usb_buffer_free(acm->dev, acm->writesize, wb->buf, wb->dmah);
  655. }
  656. }
  657. /* Little helper: write buffers allocate */
  658. static int acm_write_buffers_alloc(struct acm *acm)
  659. {
  660. int i;
  661. struct acm_wb *wb;
  662. for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
  663. wb->buf = usb_buffer_alloc(acm->dev, acm->writesize, GFP_KERNEL,
  664. &wb->dmah);
  665. if (!wb->buf) {
  666. while (i != 0) {
  667. --i;
  668. --wb;
  669. usb_buffer_free(acm->dev, acm->writesize,
  670. wb->buf, wb->dmah);
  671. }
  672. return -ENOMEM;
  673. }
  674. }
  675. return 0;
  676. }
  677. static int acm_probe (struct usb_interface *intf,
  678. const struct usb_device_id *id)
  679. {
  680. struct usb_cdc_union_desc *union_header = NULL;
  681. struct usb_cdc_country_functional_desc *cfd = NULL;
  682. char *buffer = intf->altsetting->extra;
  683. int buflen = intf->altsetting->extralen;
  684. struct usb_interface *control_interface;
  685. struct usb_interface *data_interface;
  686. struct usb_endpoint_descriptor *epctrl;
  687. struct usb_endpoint_descriptor *epread;
  688. struct usb_endpoint_descriptor *epwrite;
  689. struct usb_device *usb_dev = interface_to_usbdev(intf);
  690. struct acm *acm;
  691. int minor;
  692. int ctrlsize,readsize;
  693. u8 *buf;
  694. u8 ac_management_function = 0;
  695. u8 call_management_function = 0;
  696. int call_interface_num = -1;
  697. int data_interface_num;
  698. unsigned long quirks;
  699. int num_rx_buf;
  700. int i;
  701. /* normal quirks */
  702. quirks = (unsigned long)id->driver_info;
  703. num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
  704. /* handle quirks deadly to normal probing*/
  705. if (quirks == NO_UNION_NORMAL) {
  706. data_interface = usb_ifnum_to_if(usb_dev, 1);
  707. control_interface = usb_ifnum_to_if(usb_dev, 0);
  708. goto skip_normal_probe;
  709. }
  710. /* normal probing*/
  711. if (!buffer) {
  712. err("Weird descriptor references\n");
  713. return -EINVAL;
  714. }
  715. if (!buflen) {
  716. if (intf->cur_altsetting->endpoint->extralen && intf->cur_altsetting->endpoint->extra) {
  717. dev_dbg(&intf->dev,"Seeking extra descriptors on endpoint\n");
  718. buflen = intf->cur_altsetting->endpoint->extralen;
  719. buffer = intf->cur_altsetting->endpoint->extra;
  720. } else {
  721. err("Zero length descriptor references\n");
  722. return -EINVAL;
  723. }
  724. }
  725. while (buflen > 0) {
  726. if (buffer [1] != USB_DT_CS_INTERFACE) {
  727. err("skipping garbage\n");
  728. goto next_desc;
  729. }
  730. switch (buffer [2]) {
  731. case USB_CDC_UNION_TYPE: /* we've found it */
  732. if (union_header) {
  733. err("More than one union descriptor, skipping ...");
  734. goto next_desc;
  735. }
  736. union_header = (struct usb_cdc_union_desc *)
  737. buffer;
  738. break;
  739. case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/
  740. cfd = (struct usb_cdc_country_functional_desc *)buffer;
  741. break;
  742. case USB_CDC_HEADER_TYPE: /* maybe check version */
  743. break; /* for now we ignore it */
  744. case USB_CDC_ACM_TYPE:
  745. ac_management_function = buffer[3];
  746. break;
  747. case USB_CDC_CALL_MANAGEMENT_TYPE:
  748. call_management_function = buffer[3];
  749. call_interface_num = buffer[4];
  750. if ((call_management_function & 3) != 3)
  751. err("This device cannot do calls on its own. It is no modem.");
  752. break;
  753. default:
  754. err("Ignoring extra header, type %d, length %d", buffer[2], buffer[0]);
  755. break;
  756. }
  757. next_desc:
  758. buflen -= buffer[0];
  759. buffer += buffer[0];
  760. }
  761. if (!union_header) {
  762. if (call_interface_num > 0) {
  763. dev_dbg(&intf->dev,"No union descriptor, using call management descriptor\n");
  764. data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
  765. control_interface = intf;
  766. } else {
  767. dev_dbg(&intf->dev,"No union descriptor, giving up\n");
  768. return -ENODEV;
  769. }
  770. } else {
  771. control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
  772. data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
  773. if (!control_interface || !data_interface) {
  774. dev_dbg(&intf->dev,"no interfaces\n");
  775. return -ENODEV;
  776. }
  777. }
  778. if (data_interface_num != call_interface_num)
  779. dev_dbg(&intf->dev,"Seperate call control interface. That is not fully supported.\n");
  780. skip_normal_probe:
  781. /*workaround for switched interfaces */
  782. if (data_interface->cur_altsetting->desc.bInterfaceClass != CDC_DATA_INTERFACE_TYPE) {
  783. if (control_interface->cur_altsetting->desc.bInterfaceClass == CDC_DATA_INTERFACE_TYPE) {
  784. struct usb_interface *t;
  785. dev_dbg(&intf->dev,"Your device has switched interfaces.\n");
  786. t = control_interface;
  787. control_interface = data_interface;
  788. data_interface = t;
  789. } else {
  790. return -EINVAL;
  791. }
  792. }
  793. /* Accept probe requests only for the control interface */
  794. if (intf != control_interface)
  795. return -ENODEV;
  796. if (usb_interface_claimed(data_interface)) { /* valid in this context */
  797. dev_dbg(&intf->dev,"The data interface isn't available\n");
  798. return -EBUSY;
  799. }
  800. if (data_interface->cur_altsetting->desc.bNumEndpoints < 2)
  801. return -EINVAL;
  802. epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
  803. epread = &data_interface->cur_altsetting->endpoint[0].desc;
  804. epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
  805. /* workaround for switched endpoints */
  806. if (!usb_endpoint_dir_in(epread)) {
  807. /* descriptors are swapped */
  808. struct usb_endpoint_descriptor *t;
  809. dev_dbg(&intf->dev,"The data interface has switched endpoints\n");
  810. t = epread;
  811. epread = epwrite;
  812. epwrite = t;
  813. }
  814. dbg("interfaces are valid");
  815. for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++);
  816. if (minor == ACM_TTY_MINORS) {
  817. err("no more free acm devices");
  818. return -ENODEV;
  819. }
  820. if (!(acm = kzalloc(sizeof(struct acm), GFP_KERNEL))) {
  821. dev_dbg(&intf->dev, "out of memory (acm kzalloc)\n");
  822. goto alloc_fail;
  823. }
  824. ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize);
  825. readsize = le16_to_cpu(epread->wMaxPacketSize)* ( quirks == SINGLE_RX_URB ? 1 : 2);
  826. acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize);
  827. acm->control = control_interface;
  828. acm->data = data_interface;
  829. acm->minor = minor;
  830. acm->dev = usb_dev;
  831. acm->ctrl_caps = ac_management_function;
  832. acm->ctrlsize = ctrlsize;
  833. acm->readsize = readsize;
  834. acm->rx_buflimit = num_rx_buf;
  835. acm->urb_task.func = acm_rx_tasklet;
  836. acm->urb_task.data = (unsigned long) acm;
  837. INIT_WORK(&acm->work, acm_softint);
  838. spin_lock_init(&acm->throttle_lock);
  839. spin_lock_init(&acm->write_lock);
  840. spin_lock_init(&acm->read_lock);
  841. mutex_init(&acm->mutex);
  842. acm->write_ready = 1;
  843. acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
  844. buf = usb_buffer_alloc(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
  845. if (!buf) {
  846. dev_dbg(&intf->dev, "out of memory (ctrl buffer alloc)\n");
  847. goto alloc_fail2;
  848. }
  849. acm->ctrl_buffer = buf;
  850. if (acm_write_buffers_alloc(acm) < 0) {
  851. dev_dbg(&intf->dev, "out of memory (write buffer alloc)\n");
  852. goto alloc_fail4;
  853. }
  854. acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
  855. if (!acm->ctrlurb) {
  856. dev_dbg(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
  857. goto alloc_fail5;
  858. }
  859. for (i = 0; i < num_rx_buf; i++) {
  860. struct acm_ru *rcv = &(acm->ru[i]);
  861. if (!(rcv->urb = usb_alloc_urb(0, GFP_KERNEL))) {
  862. dev_dbg(&intf->dev, "out of memory (read urbs usb_alloc_urb)\n");
  863. goto alloc_fail7;
  864. }
  865. rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  866. rcv->instance = acm;
  867. }
  868. for (i = 0; i < num_rx_buf; i++) {
  869. struct acm_rb *buf = &(acm->rb[i]);
  870. if (!(buf->base = usb_buffer_alloc(acm->dev, readsize, GFP_KERNEL, &buf->dma))) {
  871. dev_dbg(&intf->dev, "out of memory (read bufs usb_buffer_alloc)\n");
  872. goto alloc_fail7;
  873. }
  874. }
  875. acm->writeurb = usb_alloc_urb(0, GFP_KERNEL);
  876. if (!acm->writeurb) {
  877. dev_dbg(&intf->dev, "out of memory (writeurb kmalloc)\n");
  878. goto alloc_fail7;
  879. }
  880. usb_set_intfdata (intf, acm);
  881. i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
  882. if (i < 0)
  883. goto alloc_fail8;
  884. if (cfd) { /* export the country data */
  885. acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
  886. if (!acm->country_codes)
  887. goto skip_countries;
  888. acm->country_code_size = cfd->bLength - 4;
  889. memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0, cfd->bLength - 4);
  890. acm->country_rel_date = cfd->iCountryCodeRelDate;
  891. i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
  892. if (i < 0) {
  893. kfree(acm->country_codes);
  894. goto skip_countries;
  895. }
  896. i = device_create_file(&intf->dev, &dev_attr_iCountryCodeRelDate);
  897. if (i < 0) {
  898. kfree(acm->country_codes);
  899. goto skip_countries;
  900. }
  901. }
  902. skip_countries:
  903. usb_fill_int_urb(acm->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
  904. acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm, epctrl->bInterval);
  905. acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  906. acm->ctrlurb->transfer_dma = acm->ctrl_dma;
  907. usb_fill_bulk_urb(acm->writeurb, usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
  908. NULL, acm->writesize, acm_write_bulk, acm);
  909. acm->writeurb->transfer_flags |= URB_NO_FSBR | URB_NO_TRANSFER_DMA_MAP;
  910. dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
  911. acm_set_control(acm, acm->ctrlout);
  912. acm->line.dwDTERate = cpu_to_le32(9600);
  913. acm->line.bDataBits = 8;
  914. acm_set_line(acm, &acm->line);
  915. usb_driver_claim_interface(&acm_driver, data_interface, acm);
  916. usb_get_intf(control_interface);
  917. tty_register_device(acm_tty_driver, minor, &control_interface->dev);
  918. acm_table[minor] = acm;
  919. return 0;
  920. alloc_fail8:
  921. usb_free_urb(acm->writeurb);
  922. alloc_fail7:
  923. for (i = 0; i < num_rx_buf; i++)
  924. usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma);
  925. for (i = 0; i < num_rx_buf; i++)
  926. usb_free_urb(acm->ru[i].urb);
  927. usb_free_urb(acm->ctrlurb);
  928. alloc_fail5:
  929. acm_write_buffers_free(acm);
  930. alloc_fail4:
  931. usb_buffer_free(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
  932. alloc_fail2:
  933. kfree(acm);
  934. alloc_fail:
  935. return -ENOMEM;
  936. }
  937. static void stop_data_traffic(struct acm *acm)
  938. {
  939. int i;
  940. tasklet_disable(&acm->urb_task);
  941. usb_kill_urb(acm->ctrlurb);
  942. usb_kill_urb(acm->writeurb);
  943. for (i = 0; i < acm->rx_buflimit; i++)
  944. usb_kill_urb(acm->ru[i].urb);
  945. INIT_LIST_HEAD(&acm->filled_read_bufs);
  946. INIT_LIST_HEAD(&acm->spare_read_bufs);
  947. tasklet_enable(&acm->urb_task);
  948. cancel_work_sync(&acm->work);
  949. }
  950. static void acm_disconnect(struct usb_interface *intf)
  951. {
  952. struct acm *acm = usb_get_intfdata(intf);
  953. struct usb_device *usb_dev = interface_to_usbdev(intf);
  954. int i;
  955. if (!acm || !acm->dev) {
  956. dbg("disconnect on nonexisting interface");
  957. return;
  958. }
  959. mutex_lock(&open_mutex);
  960. if (!usb_get_intfdata(intf)) {
  961. mutex_unlock(&open_mutex);
  962. return;
  963. }
  964. if (acm->country_codes){
  965. device_remove_file(&acm->control->dev,
  966. &dev_attr_wCountryCodes);
  967. device_remove_file(&acm->control->dev,
  968. &dev_attr_iCountryCodeRelDate);
  969. }
  970. device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
  971. acm->dev = NULL;
  972. usb_set_intfdata(acm->control, NULL);
  973. usb_set_intfdata(acm->data, NULL);
  974. stop_data_traffic(acm);
  975. acm_write_buffers_free(acm);
  976. usb_buffer_free(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
  977. for (i = 0; i < acm->rx_buflimit; i++)
  978. usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma);
  979. usb_driver_release_interface(&acm_driver, intf == acm->control ? acm->data : intf);
  980. if (!acm->used) {
  981. acm_tty_unregister(acm);
  982. mutex_unlock(&open_mutex);
  983. return;
  984. }
  985. mutex_unlock(&open_mutex);
  986. if (acm->tty)
  987. tty_hangup(acm->tty);
  988. }
  989. static int acm_suspend(struct usb_interface *intf, pm_message_t message)
  990. {
  991. struct acm *acm = usb_get_intfdata(intf);
  992. if (acm->susp_count++)
  993. return 0;
  994. /*
  995. we treat opened interfaces differently,
  996. we must guard against open
  997. */
  998. mutex_lock(&acm->mutex);
  999. if (acm->used)
  1000. stop_data_traffic(acm);
  1001. mutex_unlock(&acm->mutex);
  1002. return 0;
  1003. }
  1004. static int acm_resume(struct usb_interface *intf)
  1005. {
  1006. struct acm *acm = usb_get_intfdata(intf);
  1007. int rv = 0;
  1008. if (--acm->susp_count)
  1009. return 0;
  1010. mutex_lock(&acm->mutex);
  1011. if (acm->used) {
  1012. rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO);
  1013. if (rv < 0)
  1014. goto err_out;
  1015. tasklet_schedule(&acm->urb_task);
  1016. }
  1017. err_out:
  1018. mutex_unlock(&acm->mutex);
  1019. return rv;
  1020. }
  1021. /*
  1022. * USB driver structure.
  1023. */
  1024. static struct usb_device_id acm_ids[] = {
  1025. /* quirky and broken devices */
  1026. { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
  1027. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1028. },
  1029. { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
  1030. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1031. },
  1032. { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
  1033. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1034. },
  1035. { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
  1036. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1037. },
  1038. { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
  1039. .driver_info = SINGLE_RX_URB, /* firmware bug */
  1040. },
  1041. { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
  1042. .driver_info = SINGLE_RX_URB, /* firmware bug */
  1043. },
  1044. { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
  1045. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1046. },
  1047. /* control interfaces with various AT-command sets */
  1048. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1049. USB_CDC_ACM_PROTO_AT_V25TER) },
  1050. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1051. USB_CDC_ACM_PROTO_AT_PCCA101) },
  1052. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1053. USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
  1054. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1055. USB_CDC_ACM_PROTO_AT_GSM) },
  1056. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1057. USB_CDC_ACM_PROTO_AT_3G ) },
  1058. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1059. USB_CDC_ACM_PROTO_AT_CDMA) },
  1060. /* NOTE: COMM/ACM/0xff is likely MSFT RNDIS ... NOT a modem!! */
  1061. { }
  1062. };
  1063. MODULE_DEVICE_TABLE (usb, acm_ids);
  1064. static struct usb_driver acm_driver = {
  1065. .name = "cdc_acm",
  1066. .probe = acm_probe,
  1067. .disconnect = acm_disconnect,
  1068. .suspend = acm_suspend,
  1069. .resume = acm_resume,
  1070. .id_table = acm_ids,
  1071. .supports_autosuspend = 1,
  1072. };
  1073. /*
  1074. * TTY driver structures.
  1075. */
  1076. static const struct tty_operations acm_ops = {
  1077. .open = acm_tty_open,
  1078. .close = acm_tty_close,
  1079. .write = acm_tty_write,
  1080. .write_room = acm_tty_write_room,
  1081. .ioctl = acm_tty_ioctl,
  1082. .throttle = acm_tty_throttle,
  1083. .unthrottle = acm_tty_unthrottle,
  1084. .chars_in_buffer = acm_tty_chars_in_buffer,
  1085. .break_ctl = acm_tty_break_ctl,
  1086. .set_termios = acm_tty_set_termios,
  1087. .tiocmget = acm_tty_tiocmget,
  1088. .tiocmset = acm_tty_tiocmset,
  1089. };
  1090. /*
  1091. * Init / exit.
  1092. */
  1093. static int __init acm_init(void)
  1094. {
  1095. int retval;
  1096. acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
  1097. if (!acm_tty_driver)
  1098. return -ENOMEM;
  1099. acm_tty_driver->owner = THIS_MODULE,
  1100. acm_tty_driver->driver_name = "acm",
  1101. acm_tty_driver->name = "ttyACM",
  1102. acm_tty_driver->major = ACM_TTY_MAJOR,
  1103. acm_tty_driver->minor_start = 0,
  1104. acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
  1105. acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
  1106. acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  1107. acm_tty_driver->init_termios = tty_std_termios;
  1108. acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1109. tty_set_operations(acm_tty_driver, &acm_ops);
  1110. retval = tty_register_driver(acm_tty_driver);
  1111. if (retval) {
  1112. put_tty_driver(acm_tty_driver);
  1113. return retval;
  1114. }
  1115. retval = usb_register(&acm_driver);
  1116. if (retval) {
  1117. tty_unregister_driver(acm_tty_driver);
  1118. put_tty_driver(acm_tty_driver);
  1119. return retval;
  1120. }
  1121. info(DRIVER_VERSION ":" DRIVER_DESC);
  1122. return 0;
  1123. }
  1124. static void __exit acm_exit(void)
  1125. {
  1126. usb_deregister(&acm_driver);
  1127. tty_unregister_driver(acm_tty_driver);
  1128. put_tty_driver(acm_tty_driver);
  1129. }
  1130. module_init(acm_init);
  1131. module_exit(acm_exit);
  1132. MODULE_AUTHOR( DRIVER_AUTHOR );
  1133. MODULE_DESCRIPTION( DRIVER_DESC );
  1134. MODULE_LICENSE("GPL");