cdc-acm.c 38 KB

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