cdc-acm.c 35 KB

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