cdc-acm.c 40 KB

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