cdc-acm.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * cdc-acm.c
  4. *
  5. * Copyright (c) 1999 Armin Fuerst <fuerst@in.tum.de>
  6. * Copyright (c) 1999 Pavel Machek <pavel@ucw.cz>
  7. * Copyright (c) 1999 Johannes Erdfelt <johannes@erdfelt.com>
  8. * Copyright (c) 2000 Vojtech Pavlik <vojtech@suse.cz>
  9. * Copyright (c) 2004 Oliver Neukum <oliver@neukum.name>
  10. * Copyright (c) 2005 David Kubicek <dave@awk.cz>
  11. * Copyright (c) 2011 Johan Hovold <jhovold@gmail.com>
  12. *
  13. * USB Abstract Control Model driver for USB modems and ISDN adapters
  14. *
  15. * Sponsored by SuSE
  16. */
  17. #undef DEBUG
  18. #undef VERBOSE_DEBUG
  19. #include <linux/kernel.h>
  20. #include <linux/sched/signal.h>
  21. #include <linux/errno.h>
  22. #include <linux/init.h>
  23. #include <linux/slab.h>
  24. #include <linux/log2.h>
  25. #include <linux/tty.h>
  26. #include <linux/serial.h>
  27. #include <linux/tty_driver.h>
  28. #include <linux/tty_flip.h>
  29. #include <linux/tty_ldisc.h>
  30. #include <linux/module.h>
  31. #include <linux/mutex.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/usb.h>
  34. #include <linux/usb/cdc.h>
  35. #include <asm/byteorder.h>
  36. #include <linux/unaligned.h>
  37. #include <linux/idr.h>
  38. #include <linux/list.h>
  39. #include "cdc-acm.h"
  40. #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek, Johan Hovold"
  41. #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
  42. static struct usb_driver acm_driver;
  43. static struct tty_driver *acm_tty_driver;
  44. static DEFINE_IDR(acm_minors);
  45. static DEFINE_MUTEX(acm_minors_lock);
  46. static void acm_tty_set_termios(struct tty_struct *tty,
  47. const struct ktermios *termios_old);
  48. /*
  49. * acm_minors accessors
  50. */
  51. /*
  52. * Look up an ACM structure by minor. If found and not disconnected, increment
  53. * its refcount and return it with its mutex held.
  54. */
  55. static struct acm *acm_get_by_minor(unsigned int minor)
  56. {
  57. struct acm *acm;
  58. mutex_lock(&acm_minors_lock);
  59. acm = idr_find(&acm_minors, minor);
  60. if (acm) {
  61. mutex_lock(&acm->mutex);
  62. if (acm->disconnected) {
  63. mutex_unlock(&acm->mutex);
  64. acm = NULL;
  65. } else {
  66. tty_port_get(&acm->port);
  67. mutex_unlock(&acm->mutex);
  68. }
  69. }
  70. mutex_unlock(&acm_minors_lock);
  71. return acm;
  72. }
  73. /*
  74. * Try to find an available minor number and if found, associate it with 'acm'.
  75. */
  76. static int acm_alloc_minor(struct acm *acm)
  77. {
  78. int minor;
  79. mutex_lock(&acm_minors_lock);
  80. minor = idr_alloc(&acm_minors, acm, 0, ACM_TTY_MINORS, GFP_KERNEL);
  81. mutex_unlock(&acm_minors_lock);
  82. return minor;
  83. }
  84. /* Release the minor number associated with 'acm'. */
  85. static void acm_release_minor(struct acm *acm)
  86. {
  87. mutex_lock(&acm_minors_lock);
  88. idr_remove(&acm_minors, acm->minor);
  89. mutex_unlock(&acm_minors_lock);
  90. }
  91. /*
  92. * Functions for ACM control messages.
  93. */
  94. static int acm_ctrl_msg(struct acm *acm, int request, int value,
  95. void *buf, int len)
  96. {
  97. int retval;
  98. retval = usb_autopm_get_interface(acm->control);
  99. if (retval)
  100. return retval;
  101. retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
  102. request, USB_RT_ACM, value,
  103. acm->control->altsetting[0].desc.bInterfaceNumber,
  104. buf, len, USB_CTRL_SET_TIMEOUT);
  105. dev_dbg(&acm->control->dev,
  106. "%s - rq 0x%02x, val %#x, len %#x, result %d\n",
  107. __func__, request, value, len, retval);
  108. usb_autopm_put_interface(acm->control);
  109. return retval < 0 ? retval : 0;
  110. }
  111. /* devices aren't required to support these requests.
  112. * the cdc acm descriptor tells whether they do...
  113. */
  114. static inline int acm_set_control(struct acm *acm, int control)
  115. {
  116. if (acm->quirks & QUIRK_CONTROL_LINE_STATE)
  117. return -EOPNOTSUPP;
  118. return acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE,
  119. control, NULL, 0);
  120. }
  121. #define acm_set_line(acm, line) \
  122. acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
  123. #define acm_send_break(acm, ms) \
  124. acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
  125. static void acm_poison_urbs(struct acm *acm)
  126. {
  127. int i;
  128. usb_poison_urb(acm->ctrlurb);
  129. for (i = 0; i < ACM_NW; i++)
  130. usb_poison_urb(acm->wb[i].urb);
  131. for (i = 0; i < acm->rx_buflimit; i++)
  132. usb_poison_urb(acm->read_urbs[i]);
  133. }
  134. static void acm_unpoison_urbs(struct acm *acm)
  135. {
  136. int i;
  137. for (i = 0; i < acm->rx_buflimit; i++)
  138. usb_unpoison_urb(acm->read_urbs[i]);
  139. for (i = 0; i < ACM_NW; i++)
  140. usb_unpoison_urb(acm->wb[i].urb);
  141. usb_unpoison_urb(acm->ctrlurb);
  142. }
  143. /*
  144. * Write buffer management.
  145. * All of these assume proper locks taken by the caller.
  146. */
  147. static int acm_wb_alloc(struct acm *acm)
  148. {
  149. int i, wbn;
  150. struct acm_wb *wb;
  151. wbn = 0;
  152. i = 0;
  153. for (;;) {
  154. wb = &acm->wb[wbn];
  155. if (!wb->use) {
  156. wb->use = true;
  157. wb->len = 0;
  158. return wbn;
  159. }
  160. wbn = (wbn + 1) % ACM_NW;
  161. if (++i >= ACM_NW)
  162. return -1;
  163. }
  164. }
  165. static int acm_wb_is_avail(struct acm *acm)
  166. {
  167. int i, n;
  168. unsigned long flags;
  169. n = ACM_NW;
  170. spin_lock_irqsave(&acm->write_lock, flags);
  171. for (i = 0; i < ACM_NW; i++)
  172. if(acm->wb[i].use)
  173. n--;
  174. spin_unlock_irqrestore(&acm->write_lock, flags);
  175. return n;
  176. }
  177. /*
  178. * Finish write. Caller must hold acm->write_lock
  179. */
  180. static void acm_write_done(struct acm *acm, struct acm_wb *wb)
  181. {
  182. wb->use = false;
  183. acm->transmitting--;
  184. usb_autopm_put_interface_async(acm->control);
  185. }
  186. /*
  187. * Poke write.
  188. *
  189. * the caller is responsible for locking
  190. */
  191. static int acm_start_wb(struct acm *acm, struct acm_wb *wb)
  192. {
  193. int rc;
  194. acm->transmitting++;
  195. wb->urb->transfer_buffer = wb->buf;
  196. wb->urb->transfer_dma = wb->dmah;
  197. wb->urb->transfer_buffer_length = wb->len;
  198. wb->urb->dev = acm->dev;
  199. rc = usb_submit_urb(wb->urb, GFP_ATOMIC);
  200. if (rc < 0) {
  201. if (rc != -EPERM)
  202. dev_err(&acm->data->dev,
  203. "%s - usb_submit_urb(write bulk) failed: %d\n",
  204. __func__, rc);
  205. acm_write_done(acm, wb);
  206. }
  207. return rc;
  208. }
  209. /*
  210. * attributes exported through sysfs
  211. */
  212. static ssize_t bmCapabilities_show
  213. (struct device *dev, struct device_attribute *attr, char *buf)
  214. {
  215. struct usb_interface *intf = to_usb_interface(dev);
  216. struct acm *acm = usb_get_intfdata(intf);
  217. return sprintf(buf, "%d", acm->ctrl_caps);
  218. }
  219. static DEVICE_ATTR_RO(bmCapabilities);
  220. static ssize_t wCountryCodes_show
  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. memcpy(buf, acm->country_codes, acm->country_code_size);
  226. return acm->country_code_size;
  227. }
  228. static DEVICE_ATTR_RO(wCountryCodes);
  229. static ssize_t iCountryCodeRelDate_show
  230. (struct device *dev, struct device_attribute *attr, char *buf)
  231. {
  232. struct usb_interface *intf = to_usb_interface(dev);
  233. struct acm *acm = usb_get_intfdata(intf);
  234. return sprintf(buf, "%d", acm->country_rel_date);
  235. }
  236. static DEVICE_ATTR_RO(iCountryCodeRelDate);
  237. /*
  238. * Interrupt handlers for various ACM device responses
  239. */
  240. static void acm_process_notification(struct acm *acm, unsigned char *buf)
  241. {
  242. int newctrl;
  243. int difference;
  244. unsigned long flags;
  245. struct usb_cdc_notification *dr = (struct usb_cdc_notification *)buf;
  246. unsigned char *data = buf + sizeof(struct usb_cdc_notification);
  247. switch (dr->bNotificationType) {
  248. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  249. dev_dbg(&acm->control->dev,
  250. "%s - network connection: %d\n", __func__, dr->wValue);
  251. break;
  252. case USB_CDC_NOTIFY_SERIAL_STATE:
  253. if (le16_to_cpu(dr->wLength) != 2) {
  254. dev_dbg(&acm->control->dev,
  255. "%s - malformed serial state\n", __func__);
  256. break;
  257. }
  258. newctrl = get_unaligned_le16(data);
  259. dev_dbg(&acm->control->dev,
  260. "%s - serial state: 0x%x\n", __func__, newctrl);
  261. if (!acm->clocal && (acm->ctrlin & ~newctrl & USB_CDC_SERIAL_STATE_DCD)) {
  262. dev_dbg(&acm->control->dev,
  263. "%s - calling hangup\n", __func__);
  264. tty_port_tty_hangup(&acm->port, false);
  265. }
  266. difference = acm->ctrlin ^ newctrl;
  267. if ((difference & USB_CDC_SERIAL_STATE_DCD) && acm->port.tty) {
  268. struct tty_ldisc *ld = tty_ldisc_ref(acm->port.tty);
  269. if (ld) {
  270. if (ld->ops->dcd_change)
  271. ld->ops->dcd_change(acm->port.tty, newctrl & USB_CDC_SERIAL_STATE_DCD);
  272. tty_ldisc_deref(ld);
  273. }
  274. }
  275. spin_lock_irqsave(&acm->read_lock, flags);
  276. acm->ctrlin = newctrl;
  277. acm->oldcount = acm->iocount;
  278. if (difference & USB_CDC_SERIAL_STATE_DSR)
  279. acm->iocount.dsr++;
  280. if (difference & USB_CDC_SERIAL_STATE_DCD)
  281. acm->iocount.dcd++;
  282. if (newctrl & USB_CDC_SERIAL_STATE_BREAK) {
  283. acm->iocount.brk++;
  284. tty_insert_flip_char(&acm->port, 0, TTY_BREAK);
  285. }
  286. if (newctrl & USB_CDC_SERIAL_STATE_RING_SIGNAL)
  287. acm->iocount.rng++;
  288. if (newctrl & USB_CDC_SERIAL_STATE_FRAMING)
  289. acm->iocount.frame++;
  290. if (newctrl & USB_CDC_SERIAL_STATE_PARITY)
  291. acm->iocount.parity++;
  292. if (newctrl & USB_CDC_SERIAL_STATE_OVERRUN)
  293. acm->iocount.overrun++;
  294. spin_unlock_irqrestore(&acm->read_lock, flags);
  295. if (newctrl & USB_CDC_SERIAL_STATE_BREAK)
  296. tty_flip_buffer_push(&acm->port);
  297. if (difference)
  298. wake_up_all(&acm->wioctl);
  299. break;
  300. default:
  301. dev_dbg(&acm->control->dev,
  302. "%s - unknown notification %d received: index %d len %d\n",
  303. __func__,
  304. dr->bNotificationType, dr->wIndex, dr->wLength);
  305. }
  306. }
  307. /* control interface reports status changes with "interrupt" transfers */
  308. static void acm_ctrl_irq(struct urb *urb)
  309. {
  310. struct acm *acm = urb->context;
  311. struct usb_cdc_notification *dr;
  312. unsigned int current_size = urb->actual_length;
  313. unsigned int expected_size, copy_size, alloc_size;
  314. int retval;
  315. int status = urb->status;
  316. switch (status) {
  317. case 0:
  318. /* success */
  319. break;
  320. case -ECONNRESET:
  321. case -ENOENT:
  322. case -ESHUTDOWN:
  323. /* this urb is terminated, clean up */
  324. dev_dbg(&acm->control->dev,
  325. "%s - urb shutting down with status: %d\n",
  326. __func__, status);
  327. return;
  328. default:
  329. dev_dbg(&acm->control->dev,
  330. "%s - nonzero urb status received: %d\n",
  331. __func__, status);
  332. goto exit;
  333. }
  334. usb_mark_last_busy(acm->dev);
  335. if (acm->nb_index == 0) {
  336. /*
  337. * The first chunk of a message must contain at least the
  338. * notification header with the length field, otherwise we
  339. * can't get an expected_size.
  340. */
  341. if (current_size < sizeof(struct usb_cdc_notification)) {
  342. dev_dbg(&acm->control->dev, "urb too short\n");
  343. goto exit;
  344. }
  345. dr = urb->transfer_buffer;
  346. } else {
  347. dr = (struct usb_cdc_notification *)acm->notification_buffer;
  348. }
  349. /* size = notification-header + (optional) data */
  350. expected_size = sizeof(struct usb_cdc_notification) +
  351. le16_to_cpu(dr->wLength);
  352. if (acm->nb_index != 0 || current_size < expected_size) {
  353. /* notification is transmitted fragmented, reassemble */
  354. if (acm->nb_size < expected_size) {
  355. u8 *new_buffer;
  356. alloc_size = roundup_pow_of_two(expected_size);
  357. /* Final freeing is done on disconnect. */
  358. new_buffer = krealloc(acm->notification_buffer,
  359. alloc_size, GFP_ATOMIC);
  360. if (!new_buffer) {
  361. acm->nb_index = 0;
  362. goto exit;
  363. }
  364. acm->notification_buffer = new_buffer;
  365. acm->nb_size = alloc_size;
  366. dr = (struct usb_cdc_notification *)acm->notification_buffer;
  367. }
  368. copy_size = min(current_size,
  369. expected_size - acm->nb_index);
  370. memcpy(&acm->notification_buffer[acm->nb_index],
  371. urb->transfer_buffer, copy_size);
  372. acm->nb_index += copy_size;
  373. current_size = acm->nb_index;
  374. }
  375. if (current_size >= expected_size) {
  376. /* notification complete */
  377. acm_process_notification(acm, (unsigned char *)dr);
  378. acm->nb_index = 0;
  379. }
  380. exit:
  381. retval = usb_submit_urb(urb, GFP_ATOMIC);
  382. if (retval && retval != -EPERM && retval != -ENODEV)
  383. dev_err(&acm->control->dev,
  384. "%s - usb_submit_urb failed: %d\n", __func__, retval);
  385. else
  386. dev_vdbg(&acm->control->dev,
  387. "control resubmission terminated %d\n", retval);
  388. }
  389. static int acm_submit_read_urb(struct acm *acm, int index, gfp_t mem_flags)
  390. {
  391. int res;
  392. if (!test_and_clear_bit(index, &acm->read_urbs_free))
  393. return 0;
  394. res = usb_submit_urb(acm->read_urbs[index], mem_flags);
  395. if (res) {
  396. if (res != -EPERM && res != -ENODEV) {
  397. dev_err(&acm->data->dev,
  398. "urb %d failed submission with %d\n",
  399. index, res);
  400. } else {
  401. dev_vdbg(&acm->data->dev, "intended failure %d\n", res);
  402. }
  403. set_bit(index, &acm->read_urbs_free);
  404. return res;
  405. } else {
  406. dev_vdbg(&acm->data->dev, "submitted urb %d\n", index);
  407. }
  408. return 0;
  409. }
  410. static int acm_submit_read_urbs(struct acm *acm, gfp_t mem_flags)
  411. {
  412. int res;
  413. int i;
  414. for (i = 0; i < acm->rx_buflimit; ++i) {
  415. res = acm_submit_read_urb(acm, i, mem_flags);
  416. if (res)
  417. return res;
  418. }
  419. return 0;
  420. }
  421. static void acm_process_read_urb(struct acm *acm, struct urb *urb)
  422. {
  423. unsigned long flags;
  424. if (!urb->actual_length)
  425. return;
  426. spin_lock_irqsave(&acm->read_lock, flags);
  427. tty_insert_flip_string(&acm->port, urb->transfer_buffer,
  428. urb->actual_length);
  429. spin_unlock_irqrestore(&acm->read_lock, flags);
  430. tty_flip_buffer_push(&acm->port);
  431. }
  432. static void acm_read_bulk_callback(struct urb *urb)
  433. {
  434. struct acm_rb *rb = urb->context;
  435. struct acm *acm = rb->instance;
  436. int status = urb->status;
  437. bool stopped = false;
  438. bool stalled = false;
  439. bool cooldown = false;
  440. dev_vdbg(&acm->data->dev, "got urb %d, len %d, status %d\n",
  441. rb->index, urb->actual_length, status);
  442. switch (status) {
  443. case 0:
  444. usb_mark_last_busy(acm->dev);
  445. acm_process_read_urb(acm, urb);
  446. break;
  447. case -EPIPE:
  448. set_bit(EVENT_RX_STALL, &acm->flags);
  449. stalled = true;
  450. break;
  451. case -ENOENT:
  452. case -ECONNRESET:
  453. case -ESHUTDOWN:
  454. dev_dbg(&acm->data->dev,
  455. "%s - urb shutting down with status: %d\n",
  456. __func__, status);
  457. stopped = true;
  458. break;
  459. case -EOVERFLOW:
  460. case -EPROTO:
  461. dev_dbg(&acm->data->dev,
  462. "%s - cooling babbling device\n", __func__);
  463. usb_mark_last_busy(acm->dev);
  464. set_bit(rb->index, &acm->urbs_in_error_delay);
  465. set_bit(ACM_ERROR_DELAY, &acm->flags);
  466. cooldown = true;
  467. break;
  468. default:
  469. dev_dbg(&acm->data->dev,
  470. "%s - nonzero urb status received: %d\n",
  471. __func__, status);
  472. break;
  473. }
  474. /*
  475. * Make sure URB processing is done before marking as free to avoid
  476. * racing with unthrottle() on another CPU. Matches the barriers
  477. * implied by the test_and_clear_bit() in acm_submit_read_urb().
  478. */
  479. smp_mb__before_atomic();
  480. set_bit(rb->index, &acm->read_urbs_free);
  481. /*
  482. * Make sure URB is marked as free before checking the throttled flag
  483. * to avoid racing with unthrottle() on another CPU. Matches the
  484. * smp_mb() in unthrottle().
  485. */
  486. smp_mb__after_atomic();
  487. if (stopped || stalled || cooldown) {
  488. if (stalled)
  489. schedule_delayed_work(&acm->dwork, 0);
  490. else if (cooldown)
  491. schedule_delayed_work(&acm->dwork, HZ / 2);
  492. return;
  493. }
  494. if (test_bit(ACM_THROTTLED, &acm->flags))
  495. return;
  496. acm_submit_read_urb(acm, rb->index, GFP_ATOMIC);
  497. }
  498. /* data interface wrote those outgoing bytes */
  499. static void acm_write_bulk(struct urb *urb)
  500. {
  501. struct acm_wb *wb = urb->context;
  502. struct acm *acm = wb->instance;
  503. unsigned long flags;
  504. int status = urb->status;
  505. if (status || (urb->actual_length != urb->transfer_buffer_length))
  506. dev_vdbg(&acm->data->dev, "wrote len %d/%d, status %d\n",
  507. urb->actual_length,
  508. urb->transfer_buffer_length,
  509. status);
  510. spin_lock_irqsave(&acm->write_lock, flags);
  511. acm_write_done(acm, wb);
  512. spin_unlock_irqrestore(&acm->write_lock, flags);
  513. set_bit(EVENT_TTY_WAKEUP, &acm->flags);
  514. schedule_delayed_work(&acm->dwork, 0);
  515. }
  516. static void acm_softint(struct work_struct *work)
  517. {
  518. int i;
  519. struct acm *acm = container_of(work, struct acm, dwork.work);
  520. if (test_bit(EVENT_RX_STALL, &acm->flags)) {
  521. smp_mb(); /* against acm_suspend() */
  522. if (!acm->susp_count) {
  523. for (i = 0; i < acm->rx_buflimit; i++)
  524. usb_kill_urb(acm->read_urbs[i]);
  525. usb_clear_halt(acm->dev, acm->in);
  526. acm_submit_read_urbs(acm, GFP_KERNEL);
  527. clear_bit(EVENT_RX_STALL, &acm->flags);
  528. }
  529. }
  530. if (test_and_clear_bit(ACM_ERROR_DELAY, &acm->flags)) {
  531. for (i = 0; i < acm->rx_buflimit; i++)
  532. if (test_and_clear_bit(i, &acm->urbs_in_error_delay))
  533. acm_submit_read_urb(acm, i, GFP_KERNEL);
  534. }
  535. if (test_and_clear_bit(EVENT_TTY_WAKEUP, &acm->flags))
  536. tty_port_tty_wakeup(&acm->port);
  537. }
  538. /*
  539. * TTY handlers
  540. */
  541. static int acm_tty_install(struct tty_driver *driver, struct tty_struct *tty)
  542. {
  543. struct acm *acm;
  544. int retval;
  545. acm = acm_get_by_minor(tty->index);
  546. if (!acm)
  547. return -ENODEV;
  548. retval = tty_standard_install(driver, tty);
  549. if (retval)
  550. goto error_init_termios;
  551. /*
  552. * Suppress initial echoing for some devices which might send data
  553. * immediately after acm driver has been installed.
  554. */
  555. if (acm->quirks & DISABLE_ECHO)
  556. tty->termios.c_lflag &= ~ECHO;
  557. tty->driver_data = acm;
  558. return 0;
  559. error_init_termios:
  560. tty_port_put(&acm->port);
  561. return retval;
  562. }
  563. static int acm_tty_open(struct tty_struct *tty, struct file *filp)
  564. {
  565. struct acm *acm = tty->driver_data;
  566. return tty_port_open(&acm->port, tty, filp);
  567. }
  568. static void acm_port_dtr_rts(struct tty_port *port, bool active)
  569. {
  570. struct acm *acm = container_of(port, struct acm, port);
  571. int val;
  572. int res;
  573. if (active)
  574. val = USB_CDC_CTRL_DTR | USB_CDC_CTRL_RTS;
  575. else
  576. val = 0;
  577. /* FIXME: add missing ctrlout locking throughout driver */
  578. acm->ctrlout = val;
  579. res = acm_set_control(acm, val);
  580. if (res && (acm->ctrl_caps & USB_CDC_CAP_LINE))
  581. /* This is broken in too many devices to spam the logs */
  582. dev_dbg(&acm->control->dev, "failed to set dtr/rts\n");
  583. }
  584. static int acm_port_activate(struct tty_port *port, struct tty_struct *tty)
  585. {
  586. struct acm *acm = container_of(port, struct acm, port);
  587. int retval = -ENODEV;
  588. int i;
  589. mutex_lock(&acm->mutex);
  590. if (acm->disconnected)
  591. goto disconnected;
  592. retval = usb_autopm_get_interface(acm->control);
  593. if (retval)
  594. goto error_get_interface;
  595. set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
  596. acm->control->needs_remote_wakeup = 1;
  597. acm->ctrlurb->dev = acm->dev;
  598. retval = usb_submit_urb(acm->ctrlurb, GFP_KERNEL);
  599. if (retval) {
  600. dev_err(&acm->control->dev,
  601. "%s - usb_submit_urb(ctrl irq) failed\n", __func__);
  602. goto error_submit_urb;
  603. }
  604. acm_tty_set_termios(tty, NULL);
  605. /*
  606. * Unthrottle device in case the TTY was closed while throttled.
  607. */
  608. clear_bit(ACM_THROTTLED, &acm->flags);
  609. retval = acm_submit_read_urbs(acm, GFP_KERNEL);
  610. if (retval)
  611. goto error_submit_read_urbs;
  612. usb_autopm_put_interface(acm->control);
  613. mutex_unlock(&acm->mutex);
  614. return 0;
  615. error_submit_read_urbs:
  616. for (i = 0; i < acm->rx_buflimit; i++)
  617. usb_kill_urb(acm->read_urbs[i]);
  618. usb_kill_urb(acm->ctrlurb);
  619. error_submit_urb:
  620. usb_autopm_put_interface(acm->control);
  621. error_get_interface:
  622. disconnected:
  623. mutex_unlock(&acm->mutex);
  624. return usb_translate_errors(retval);
  625. }
  626. static void acm_port_destruct(struct tty_port *port)
  627. {
  628. struct acm *acm = container_of(port, struct acm, port);
  629. if (acm->minor != ACM_MINOR_INVALID)
  630. acm_release_minor(acm);
  631. usb_put_intf(acm->control);
  632. kfree(acm->country_codes);
  633. kfree(acm);
  634. }
  635. static void acm_port_shutdown(struct tty_port *port)
  636. {
  637. struct acm *acm = container_of(port, struct acm, port);
  638. struct urb *urb;
  639. struct acm_wb *wb;
  640. /*
  641. * Need to grab write_lock to prevent race with resume, but no need to
  642. * hold it due to the tty-port initialised flag.
  643. */
  644. acm_poison_urbs(acm);
  645. spin_lock_irq(&acm->write_lock);
  646. spin_unlock_irq(&acm->write_lock);
  647. usb_autopm_get_interface_no_resume(acm->control);
  648. acm->control->needs_remote_wakeup = 0;
  649. usb_autopm_put_interface(acm->control);
  650. for (;;) {
  651. urb = usb_get_from_anchor(&acm->delayed);
  652. if (!urb)
  653. break;
  654. wb = urb->context;
  655. wb->use = false;
  656. usb_autopm_put_interface_async(acm->control);
  657. }
  658. acm_unpoison_urbs(acm);
  659. }
  660. static void acm_tty_cleanup(struct tty_struct *tty)
  661. {
  662. struct acm *acm = tty->driver_data;
  663. tty_port_put(&acm->port);
  664. }
  665. static void acm_tty_hangup(struct tty_struct *tty)
  666. {
  667. struct acm *acm = tty->driver_data;
  668. tty_port_hangup(&acm->port);
  669. }
  670. static void acm_tty_close(struct tty_struct *tty, struct file *filp)
  671. {
  672. struct acm *acm = tty->driver_data;
  673. tty_port_close(&acm->port, tty, filp);
  674. }
  675. static ssize_t acm_tty_write(struct tty_struct *tty, const u8 *buf,
  676. size_t count)
  677. {
  678. struct acm *acm = tty->driver_data;
  679. int stat;
  680. unsigned long flags;
  681. int wbn;
  682. struct acm_wb *wb;
  683. if (!count)
  684. return 0;
  685. dev_vdbg(&acm->data->dev, "%zu bytes from tty layer\n", count);
  686. spin_lock_irqsave(&acm->write_lock, flags);
  687. wbn = acm_wb_alloc(acm);
  688. if (wbn < 0) {
  689. spin_unlock_irqrestore(&acm->write_lock, flags);
  690. return 0;
  691. }
  692. wb = &acm->wb[wbn];
  693. if (!acm->dev) {
  694. wb->use = false;
  695. spin_unlock_irqrestore(&acm->write_lock, flags);
  696. return -ENODEV;
  697. }
  698. count = (count > acm->writesize) ? acm->writesize : count;
  699. dev_vdbg(&acm->data->dev, "writing %zu bytes\n", count);
  700. memcpy(wb->buf, buf, count);
  701. wb->len = count;
  702. stat = usb_autopm_get_interface_async(acm->control);
  703. if (stat) {
  704. wb->use = false;
  705. spin_unlock_irqrestore(&acm->write_lock, flags);
  706. return stat;
  707. }
  708. if (acm->susp_count) {
  709. usb_anchor_urb(wb->urb, &acm->delayed);
  710. spin_unlock_irqrestore(&acm->write_lock, flags);
  711. return count;
  712. }
  713. stat = acm_start_wb(acm, wb);
  714. spin_unlock_irqrestore(&acm->write_lock, flags);
  715. if (stat < 0)
  716. return stat;
  717. return count;
  718. }
  719. static unsigned int acm_tty_write_room(struct tty_struct *tty)
  720. {
  721. struct acm *acm = tty->driver_data;
  722. /*
  723. * Do not let the line discipline to know that we have a reserve,
  724. * or it might get too enthusiastic.
  725. */
  726. return acm_wb_is_avail(acm) ? acm->writesize : 0;
  727. }
  728. static void acm_tty_flush_buffer(struct tty_struct *tty)
  729. {
  730. struct acm *acm = tty->driver_data;
  731. unsigned long flags;
  732. int i;
  733. spin_lock_irqsave(&acm->write_lock, flags);
  734. for (i = 0; i < ACM_NW; i++)
  735. if (acm->wb[i].use)
  736. usb_unlink_urb(acm->wb[i].urb);
  737. spin_unlock_irqrestore(&acm->write_lock, flags);
  738. }
  739. static unsigned int acm_tty_chars_in_buffer(struct tty_struct *tty)
  740. {
  741. struct acm *acm = tty->driver_data;
  742. /*
  743. * if the device was unplugged then any remaining characters fell out
  744. * of the connector ;)
  745. */
  746. if (acm->disconnected)
  747. return 0;
  748. /*
  749. * This is inaccurate (overcounts), but it works.
  750. */
  751. return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
  752. }
  753. static void acm_tty_throttle(struct tty_struct *tty)
  754. {
  755. struct acm *acm = tty->driver_data;
  756. set_bit(ACM_THROTTLED, &acm->flags);
  757. }
  758. static void acm_tty_unthrottle(struct tty_struct *tty)
  759. {
  760. struct acm *acm = tty->driver_data;
  761. clear_bit(ACM_THROTTLED, &acm->flags);
  762. /* Matches the smp_mb__after_atomic() in acm_read_bulk_callback(). */
  763. smp_mb();
  764. acm_submit_read_urbs(acm, GFP_KERNEL);
  765. }
  766. static int acm_tty_break_ctl(struct tty_struct *tty, int state)
  767. {
  768. struct acm *acm = tty->driver_data;
  769. int retval;
  770. if (!(acm->ctrl_caps & USB_CDC_CAP_BRK))
  771. return -EOPNOTSUPP;
  772. retval = acm_send_break(acm, state ? 0xffff : 0);
  773. if (retval < 0)
  774. dev_dbg(&acm->control->dev,
  775. "%s - send break failed\n", __func__);
  776. return retval;
  777. }
  778. static int acm_tty_tiocmget(struct tty_struct *tty)
  779. {
  780. struct acm *acm = tty->driver_data;
  781. return (acm->ctrlout & USB_CDC_CTRL_DTR ? TIOCM_DTR : 0) |
  782. (acm->ctrlout & USB_CDC_CTRL_RTS ? TIOCM_RTS : 0) |
  783. (acm->ctrlin & USB_CDC_SERIAL_STATE_DSR ? TIOCM_DSR : 0) |
  784. (acm->ctrlin & USB_CDC_SERIAL_STATE_RING_SIGNAL ? TIOCM_RI : 0) |
  785. (acm->ctrlin & USB_CDC_SERIAL_STATE_DCD ? TIOCM_CD : 0) |
  786. TIOCM_CTS;
  787. }
  788. static int acm_tty_tiocmset(struct tty_struct *tty,
  789. unsigned int set, unsigned int clear)
  790. {
  791. struct acm *acm = tty->driver_data;
  792. unsigned int newctrl;
  793. newctrl = acm->ctrlout;
  794. set = (set & TIOCM_DTR ? USB_CDC_CTRL_DTR : 0) |
  795. (set & TIOCM_RTS ? USB_CDC_CTRL_RTS : 0);
  796. clear = (clear & TIOCM_DTR ? USB_CDC_CTRL_DTR : 0) |
  797. (clear & TIOCM_RTS ? USB_CDC_CTRL_RTS : 0);
  798. newctrl = (newctrl & ~clear) | set;
  799. if (acm->ctrlout == newctrl)
  800. return 0;
  801. return acm_set_control(acm, acm->ctrlout = newctrl);
  802. }
  803. static int get_serial_info(struct tty_struct *tty, struct serial_struct *ss)
  804. {
  805. struct acm *acm = tty->driver_data;
  806. ss->line = acm->minor;
  807. mutex_lock(&acm->port.mutex);
  808. ss->close_delay = jiffies_to_msecs(acm->port.close_delay) / 10;
  809. ss->closing_wait = acm->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
  810. ASYNC_CLOSING_WAIT_NONE :
  811. jiffies_to_msecs(acm->port.closing_wait) / 10;
  812. mutex_unlock(&acm->port.mutex);
  813. return 0;
  814. }
  815. static int set_serial_info(struct tty_struct *tty, struct serial_struct *ss)
  816. {
  817. struct acm *acm = tty->driver_data;
  818. unsigned int closing_wait, close_delay;
  819. int retval = 0;
  820. close_delay = msecs_to_jiffies(ss->close_delay * 10);
  821. closing_wait = ss->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
  822. ASYNC_CLOSING_WAIT_NONE :
  823. msecs_to_jiffies(ss->closing_wait * 10);
  824. mutex_lock(&acm->port.mutex);
  825. if (!capable(CAP_SYS_ADMIN)) {
  826. if ((close_delay != acm->port.close_delay) ||
  827. (closing_wait != acm->port.closing_wait))
  828. retval = -EPERM;
  829. } else {
  830. acm->port.close_delay = close_delay;
  831. acm->port.closing_wait = closing_wait;
  832. }
  833. mutex_unlock(&acm->port.mutex);
  834. return retval;
  835. }
  836. static int wait_serial_change(struct acm *acm, unsigned long arg)
  837. {
  838. int rv = 0;
  839. DECLARE_WAITQUEUE(wait, current);
  840. struct async_icount old, new;
  841. do {
  842. spin_lock_irq(&acm->read_lock);
  843. old = acm->oldcount;
  844. new = acm->iocount;
  845. acm->oldcount = new;
  846. spin_unlock_irq(&acm->read_lock);
  847. if ((arg & TIOCM_DSR) &&
  848. old.dsr != new.dsr)
  849. break;
  850. if ((arg & TIOCM_CD) &&
  851. old.dcd != new.dcd)
  852. break;
  853. if ((arg & TIOCM_RI) &&
  854. old.rng != new.rng)
  855. break;
  856. add_wait_queue(&acm->wioctl, &wait);
  857. set_current_state(TASK_INTERRUPTIBLE);
  858. schedule();
  859. remove_wait_queue(&acm->wioctl, &wait);
  860. if (acm->disconnected) {
  861. if (arg & TIOCM_CD)
  862. break;
  863. else
  864. rv = -ENODEV;
  865. } else {
  866. if (signal_pending(current))
  867. rv = -ERESTARTSYS;
  868. }
  869. } while (!rv);
  870. return rv;
  871. }
  872. static int acm_tty_get_icount(struct tty_struct *tty,
  873. struct serial_icounter_struct *icount)
  874. {
  875. struct acm *acm = tty->driver_data;
  876. icount->dsr = acm->iocount.dsr;
  877. icount->rng = acm->iocount.rng;
  878. icount->dcd = acm->iocount.dcd;
  879. icount->frame = acm->iocount.frame;
  880. icount->overrun = acm->iocount.overrun;
  881. icount->parity = acm->iocount.parity;
  882. icount->brk = acm->iocount.brk;
  883. return 0;
  884. }
  885. static int acm_tty_ioctl(struct tty_struct *tty,
  886. unsigned int cmd, unsigned long arg)
  887. {
  888. struct acm *acm = tty->driver_data;
  889. int rv = -ENOIOCTLCMD;
  890. switch (cmd) {
  891. case TIOCMIWAIT:
  892. rv = usb_autopm_get_interface(acm->control);
  893. if (rv < 0) {
  894. rv = -EIO;
  895. break;
  896. }
  897. rv = wait_serial_change(acm, arg);
  898. usb_autopm_put_interface(acm->control);
  899. break;
  900. }
  901. return rv;
  902. }
  903. static void acm_tty_set_termios(struct tty_struct *tty,
  904. const struct ktermios *termios_old)
  905. {
  906. struct acm *acm = tty->driver_data;
  907. struct ktermios *termios = &tty->termios;
  908. struct usb_cdc_line_coding newline;
  909. int newctrl = acm->ctrlout;
  910. newline.dwDTERate = cpu_to_le32(tty_get_baud_rate(tty));
  911. newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
  912. newline.bParityType = termios->c_cflag & PARENB ?
  913. (termios->c_cflag & PARODD ? 1 : 2) +
  914. (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
  915. newline.bDataBits = tty_get_char_size(termios->c_cflag);
  916. /* FIXME: Needs to clear unsupported bits in the termios */
  917. acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
  918. if (C_BAUD(tty) == B0) {
  919. newline.dwDTERate = acm->line.dwDTERate;
  920. newctrl &= ~USB_CDC_CTRL_DTR;
  921. } else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
  922. newctrl |= USB_CDC_CTRL_DTR;
  923. }
  924. if (newctrl != acm->ctrlout)
  925. acm_set_control(acm, acm->ctrlout = newctrl);
  926. if (memcmp(&acm->line, &newline, sizeof newline)) {
  927. memcpy(&acm->line, &newline, sizeof newline);
  928. dev_dbg(&acm->control->dev, "%s - set line: %d %d %d %d\n",
  929. __func__,
  930. le32_to_cpu(newline.dwDTERate),
  931. newline.bCharFormat, newline.bParityType,
  932. newline.bDataBits);
  933. acm_set_line(acm, &acm->line);
  934. }
  935. }
  936. static const struct tty_port_operations acm_port_ops = {
  937. .dtr_rts = acm_port_dtr_rts,
  938. .shutdown = acm_port_shutdown,
  939. .activate = acm_port_activate,
  940. .destruct = acm_port_destruct,
  941. };
  942. /*
  943. * USB probe and disconnect routines.
  944. */
  945. /* Little helpers: write/read buffers free */
  946. static void acm_write_buffers_free(struct acm *acm)
  947. {
  948. int i;
  949. struct acm_wb *wb;
  950. for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++)
  951. usb_free_coherent(acm->dev, acm->writesize, wb->buf, wb->dmah);
  952. }
  953. static void acm_read_buffers_free(struct acm *acm)
  954. {
  955. int i;
  956. for (i = 0; i < acm->rx_buflimit; i++)
  957. usb_free_coherent(acm->dev, acm->readsize,
  958. acm->read_buffers[i].base, acm->read_buffers[i].dma);
  959. }
  960. /* Little helper: write buffers allocate */
  961. static int acm_write_buffers_alloc(struct acm *acm)
  962. {
  963. int i;
  964. struct acm_wb *wb;
  965. for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
  966. wb->buf = usb_alloc_coherent(acm->dev, acm->writesize, GFP_KERNEL,
  967. &wb->dmah);
  968. if (!wb->buf) {
  969. while (i != 0) {
  970. --i;
  971. --wb;
  972. usb_free_coherent(acm->dev, acm->writesize,
  973. wb->buf, wb->dmah);
  974. }
  975. return -ENOMEM;
  976. }
  977. }
  978. return 0;
  979. }
  980. static int acm_probe(struct usb_interface *intf,
  981. const struct usb_device_id *id)
  982. {
  983. struct usb_cdc_union_desc *union_header = NULL;
  984. struct usb_cdc_call_mgmt_descriptor *cmgmd = NULL;
  985. unsigned char *buffer = intf->altsetting->extra;
  986. int buflen = intf->altsetting->extralen;
  987. struct usb_interface *control_interface;
  988. struct usb_interface *data_interface;
  989. struct usb_endpoint_descriptor *epctrl = NULL;
  990. struct usb_endpoint_descriptor *epread = NULL;
  991. struct usb_endpoint_descriptor *epwrite = NULL;
  992. struct usb_device *usb_dev = interface_to_usbdev(intf);
  993. struct usb_cdc_parsed_header h;
  994. struct acm *acm;
  995. int minor;
  996. int ctrlsize, readsize;
  997. u8 *buf;
  998. int call_intf_num = -1;
  999. int data_intf_num = -1;
  1000. unsigned long quirks;
  1001. int num_rx_buf;
  1002. int i;
  1003. int combined_interfaces = 0;
  1004. struct device *tty_dev;
  1005. int rv = -ENOMEM;
  1006. int res;
  1007. /* normal quirks */
  1008. quirks = (unsigned long)id->driver_info;
  1009. if (quirks == IGNORE_DEVICE)
  1010. return -ENODEV;
  1011. memset(&h, 0x00, sizeof(struct usb_cdc_parsed_header));
  1012. num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
  1013. /* handle quirks deadly to normal probing*/
  1014. if (quirks == NO_UNION_NORMAL) {
  1015. data_interface = usb_ifnum_to_if(usb_dev, 1);
  1016. control_interface = usb_ifnum_to_if(usb_dev, 0);
  1017. /* we would crash */
  1018. if (!data_interface || !control_interface)
  1019. return -ENODEV;
  1020. goto skip_normal_probe;
  1021. } else if (quirks == NO_UNION_12) {
  1022. data_interface = usb_ifnum_to_if(usb_dev, 2);
  1023. control_interface = usb_ifnum_to_if(usb_dev, 1);
  1024. if (!data_interface || !control_interface)
  1025. return -ENODEV;
  1026. goto skip_normal_probe;
  1027. }
  1028. /* normal probing*/
  1029. if (!buffer) {
  1030. dev_err(&intf->dev, "Weird descriptor references\n");
  1031. return -EINVAL;
  1032. }
  1033. if (!buflen) {
  1034. if (intf->cur_altsetting->endpoint &&
  1035. intf->cur_altsetting->endpoint->extralen &&
  1036. intf->cur_altsetting->endpoint->extra) {
  1037. dev_dbg(&intf->dev,
  1038. "Seeking extra descriptors on endpoint\n");
  1039. buflen = intf->cur_altsetting->endpoint->extralen;
  1040. buffer = intf->cur_altsetting->endpoint->extra;
  1041. } else {
  1042. dev_err(&intf->dev,
  1043. "Zero length descriptor references\n");
  1044. return -EINVAL;
  1045. }
  1046. }
  1047. cdc_parse_cdc_header(&h, intf, buffer, buflen);
  1048. union_header = h.usb_cdc_union_desc;
  1049. cmgmd = h.usb_cdc_call_mgmt_descriptor;
  1050. if (cmgmd)
  1051. call_intf_num = cmgmd->bDataInterface;
  1052. if (!union_header) {
  1053. if (intf->cur_altsetting->desc.bNumEndpoints == 3) {
  1054. dev_dbg(&intf->dev, "No union descriptor, assuming single interface\n");
  1055. combined_interfaces = 1;
  1056. control_interface = data_interface = intf;
  1057. goto look_for_collapsed_interface;
  1058. } else if (call_intf_num > 0) {
  1059. dev_dbg(&intf->dev, "No union descriptor, using call management descriptor\n");
  1060. data_intf_num = call_intf_num;
  1061. data_interface = usb_ifnum_to_if(usb_dev, data_intf_num);
  1062. control_interface = intf;
  1063. } else {
  1064. dev_dbg(&intf->dev, "No union descriptor, giving up\n");
  1065. return -ENODEV;
  1066. }
  1067. } else {
  1068. int class = -1;
  1069. data_intf_num = union_header->bSlaveInterface0;
  1070. control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
  1071. data_interface = usb_ifnum_to_if(usb_dev, data_intf_num);
  1072. if (control_interface)
  1073. class = control_interface->cur_altsetting->desc.bInterfaceClass;
  1074. if (class != USB_CLASS_COMM && class != USB_CLASS_CDC_DATA) {
  1075. dev_dbg(&intf->dev, "Broken union descriptor, assuming single interface\n");
  1076. combined_interfaces = 1;
  1077. control_interface = data_interface = intf;
  1078. goto look_for_collapsed_interface;
  1079. }
  1080. }
  1081. if (!control_interface || !data_interface) {
  1082. dev_dbg(&intf->dev, "no interfaces\n");
  1083. return -ENODEV;
  1084. }
  1085. if (data_intf_num != call_intf_num)
  1086. dev_dbg(&intf->dev, "Separate call control interface. That is not fully supported.\n");
  1087. if (control_interface == data_interface) {
  1088. /* some broken devices designed for windows work this way */
  1089. dev_warn(&intf->dev,"Control and data interfaces are not separated!\n");
  1090. combined_interfaces = 1;
  1091. /* a popular other OS doesn't use it */
  1092. quirks |= NO_CAP_LINE;
  1093. if (data_interface->cur_altsetting->desc.bNumEndpoints != 3) {
  1094. dev_err(&intf->dev, "This needs exactly 3 endpoints\n");
  1095. return -EINVAL;
  1096. }
  1097. look_for_collapsed_interface:
  1098. res = usb_find_common_endpoints(data_interface->cur_altsetting,
  1099. &epread, &epwrite, &epctrl, NULL);
  1100. if (res)
  1101. return res;
  1102. goto made_compressed_probe;
  1103. }
  1104. skip_normal_probe:
  1105. /*workaround for switched interfaces */
  1106. if (data_interface->cur_altsetting->desc.bInterfaceClass != USB_CLASS_CDC_DATA) {
  1107. if (control_interface->cur_altsetting->desc.bInterfaceClass == USB_CLASS_CDC_DATA) {
  1108. dev_dbg(&intf->dev,
  1109. "Your device has switched interfaces.\n");
  1110. swap(control_interface, data_interface);
  1111. } else {
  1112. return -EINVAL;
  1113. }
  1114. }
  1115. /* Accept probe requests only for the control interface */
  1116. if (!combined_interfaces && intf != control_interface)
  1117. return -ENODEV;
  1118. if (data_interface->cur_altsetting->desc.bNumEndpoints < 2 ||
  1119. control_interface->cur_altsetting->desc.bNumEndpoints == 0)
  1120. return -EINVAL;
  1121. epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
  1122. epread = &data_interface->cur_altsetting->endpoint[0].desc;
  1123. epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
  1124. /* workaround for switched endpoints */
  1125. if (!usb_endpoint_dir_in(epread)) {
  1126. /* descriptors are swapped */
  1127. dev_dbg(&intf->dev,
  1128. "The data interface has switched endpoints\n");
  1129. swap(epread, epwrite);
  1130. }
  1131. made_compressed_probe:
  1132. dev_dbg(&intf->dev, "interfaces are valid\n");
  1133. acm = kzalloc_obj(struct acm);
  1134. if (!acm)
  1135. return -ENOMEM;
  1136. tty_port_init(&acm->port);
  1137. acm->port.ops = &acm_port_ops;
  1138. ctrlsize = usb_endpoint_maxp(epctrl);
  1139. readsize = usb_endpoint_maxp(epread) *
  1140. (quirks == SINGLE_RX_URB ? 1 : 2);
  1141. acm->combined_interfaces = combined_interfaces;
  1142. acm->writesize = usb_endpoint_maxp(epwrite) * 20;
  1143. acm->control = control_interface;
  1144. acm->data = data_interface;
  1145. usb_get_intf(acm->control); /* undone in destruct() */
  1146. minor = acm_alloc_minor(acm);
  1147. if (minor < 0) {
  1148. acm->minor = ACM_MINOR_INVALID;
  1149. goto err_put_port;
  1150. }
  1151. acm->minor = minor;
  1152. acm->dev = usb_dev;
  1153. if (h.usb_cdc_acm_descriptor)
  1154. acm->ctrl_caps = h.usb_cdc_acm_descriptor->bmCapabilities;
  1155. if (quirks & NO_CAP_LINE)
  1156. acm->ctrl_caps &= ~USB_CDC_CAP_LINE;
  1157. if (quirks & MISSING_CAP_BRK)
  1158. acm->ctrl_caps |= USB_CDC_CAP_BRK;
  1159. acm->ctrlsize = ctrlsize;
  1160. acm->readsize = readsize;
  1161. acm->rx_buflimit = num_rx_buf;
  1162. INIT_DELAYED_WORK(&acm->dwork, acm_softint);
  1163. init_waitqueue_head(&acm->wioctl);
  1164. spin_lock_init(&acm->write_lock);
  1165. spin_lock_init(&acm->read_lock);
  1166. mutex_init(&acm->mutex);
  1167. if (usb_endpoint_xfer_int(epread)) {
  1168. acm->bInterval = epread->bInterval;
  1169. acm->in = usb_rcvintpipe(usb_dev, epread->bEndpointAddress);
  1170. } else {
  1171. acm->in = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
  1172. }
  1173. if (usb_endpoint_xfer_int(epwrite))
  1174. acm->out = usb_sndintpipe(usb_dev, epwrite->bEndpointAddress);
  1175. else
  1176. acm->out = usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress);
  1177. init_usb_anchor(&acm->delayed);
  1178. acm->quirks = quirks;
  1179. buf = usb_alloc_coherent(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
  1180. if (!buf)
  1181. goto err_put_port;
  1182. acm->ctrl_buffer = buf;
  1183. if (acm_write_buffers_alloc(acm) < 0)
  1184. goto err_free_ctrl_buffer;
  1185. acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
  1186. if (!acm->ctrlurb)
  1187. goto err_free_write_buffers;
  1188. for (i = 0; i < num_rx_buf; i++) {
  1189. struct acm_rb *rb = &(acm->read_buffers[i]);
  1190. struct urb *urb;
  1191. rb->base = usb_alloc_coherent(acm->dev, readsize, GFP_KERNEL,
  1192. &rb->dma);
  1193. if (!rb->base)
  1194. goto err_free_read_urbs;
  1195. rb->index = i;
  1196. rb->instance = acm;
  1197. urb = usb_alloc_urb(0, GFP_KERNEL);
  1198. if (!urb)
  1199. goto err_free_read_urbs;
  1200. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1201. urb->transfer_dma = rb->dma;
  1202. if (usb_endpoint_xfer_int(epread))
  1203. usb_fill_int_urb(urb, acm->dev, acm->in, rb->base,
  1204. acm->readsize,
  1205. acm_read_bulk_callback, rb,
  1206. acm->bInterval);
  1207. else
  1208. usb_fill_bulk_urb(urb, acm->dev, acm->in, rb->base,
  1209. acm->readsize,
  1210. acm_read_bulk_callback, rb);
  1211. acm->read_urbs[i] = urb;
  1212. __set_bit(i, &acm->read_urbs_free);
  1213. }
  1214. for (i = 0; i < ACM_NW; i++) {
  1215. struct acm_wb *snd = &(acm->wb[i]);
  1216. snd->urb = usb_alloc_urb(0, GFP_KERNEL);
  1217. if (!snd->urb)
  1218. goto err_free_write_urbs;
  1219. if (usb_endpoint_xfer_int(epwrite))
  1220. usb_fill_int_urb(snd->urb, usb_dev, acm->out,
  1221. NULL, acm->writesize, acm_write_bulk, snd, epwrite->bInterval);
  1222. else
  1223. usb_fill_bulk_urb(snd->urb, usb_dev, acm->out,
  1224. NULL, acm->writesize, acm_write_bulk, snd);
  1225. snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1226. if (quirks & SEND_ZERO_PACKET)
  1227. snd->urb->transfer_flags |= URB_ZERO_PACKET;
  1228. snd->instance = acm;
  1229. }
  1230. usb_set_intfdata(intf, acm);
  1231. i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
  1232. if (i < 0)
  1233. goto err_free_write_urbs;
  1234. if (h.usb_cdc_country_functional_desc) { /* export the country data */
  1235. struct usb_cdc_country_functional_desc * cfd =
  1236. h.usb_cdc_country_functional_desc;
  1237. acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
  1238. if (!acm->country_codes)
  1239. goto skip_countries;
  1240. acm->country_code_size = cfd->bLength - 4;
  1241. memcpy(acm->country_codes, cfd->wCountryCodes,
  1242. cfd->bLength - 4);
  1243. acm->country_rel_date = cfd->iCountryCodeRelDate;
  1244. i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
  1245. if (i < 0) {
  1246. kfree(acm->country_codes);
  1247. acm->country_codes = NULL;
  1248. acm->country_code_size = 0;
  1249. goto skip_countries;
  1250. }
  1251. i = device_create_file(&intf->dev,
  1252. &dev_attr_iCountryCodeRelDate);
  1253. if (i < 0) {
  1254. device_remove_file(&intf->dev, &dev_attr_wCountryCodes);
  1255. kfree(acm->country_codes);
  1256. acm->country_codes = NULL;
  1257. acm->country_code_size = 0;
  1258. goto skip_countries;
  1259. }
  1260. }
  1261. skip_countries:
  1262. usb_fill_int_urb(acm->ctrlurb, usb_dev,
  1263. usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
  1264. acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm,
  1265. /* works around buggy devices */
  1266. epctrl->bInterval ? epctrl->bInterval : 16);
  1267. acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1268. acm->ctrlurb->transfer_dma = acm->ctrl_dma;
  1269. acm->notification_buffer = NULL;
  1270. acm->nb_index = 0;
  1271. acm->nb_size = 0;
  1272. acm->line.dwDTERate = cpu_to_le32(9600);
  1273. acm->line.bDataBits = 8;
  1274. acm_set_line(acm, &acm->line);
  1275. if (!acm->combined_interfaces) {
  1276. rv = usb_driver_claim_interface(&acm_driver, data_interface, acm);
  1277. if (rv)
  1278. goto err_remove_files;
  1279. }
  1280. if (quirks & CLEAR_HALT_CONDITIONS) {
  1281. /* errors intentionally ignored */
  1282. usb_clear_halt(usb_dev, acm->in);
  1283. usb_clear_halt(usb_dev, acm->out);
  1284. }
  1285. tty_dev = tty_port_register_device(&acm->port, acm_tty_driver, minor,
  1286. &control_interface->dev);
  1287. if (IS_ERR(tty_dev)) {
  1288. rv = PTR_ERR(tty_dev);
  1289. goto err_release_data_interface;
  1290. }
  1291. dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
  1292. return 0;
  1293. err_release_data_interface:
  1294. if (!acm->combined_interfaces) {
  1295. /* Clear driver data so that disconnect() returns early. */
  1296. usb_set_intfdata(data_interface, NULL);
  1297. usb_driver_release_interface(&acm_driver, data_interface);
  1298. }
  1299. err_remove_files:
  1300. if (acm->country_codes) {
  1301. device_remove_file(&acm->control->dev,
  1302. &dev_attr_wCountryCodes);
  1303. device_remove_file(&acm->control->dev,
  1304. &dev_attr_iCountryCodeRelDate);
  1305. }
  1306. device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
  1307. err_free_write_urbs:
  1308. for (i = 0; i < ACM_NW; i++)
  1309. usb_free_urb(acm->wb[i].urb);
  1310. err_free_read_urbs:
  1311. for (i = 0; i < num_rx_buf; i++)
  1312. usb_free_urb(acm->read_urbs[i]);
  1313. acm_read_buffers_free(acm);
  1314. usb_free_urb(acm->ctrlurb);
  1315. err_free_write_buffers:
  1316. acm_write_buffers_free(acm);
  1317. err_free_ctrl_buffer:
  1318. usb_free_coherent(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
  1319. err_put_port:
  1320. tty_port_put(&acm->port);
  1321. return rv;
  1322. }
  1323. static void acm_disconnect(struct usb_interface *intf)
  1324. {
  1325. struct acm *acm = usb_get_intfdata(intf);
  1326. int i;
  1327. /* sibling interface is already cleaning up */
  1328. if (!acm)
  1329. return;
  1330. acm->disconnected = true;
  1331. /*
  1332. * there is a circular dependency. acm_softint() can resubmit
  1333. * the URBs in error handling so we need to block any
  1334. * submission right away
  1335. */
  1336. acm_poison_urbs(acm);
  1337. mutex_lock(&acm->mutex);
  1338. if (acm->country_codes) {
  1339. device_remove_file(&acm->control->dev,
  1340. &dev_attr_wCountryCodes);
  1341. device_remove_file(&acm->control->dev,
  1342. &dev_attr_iCountryCodeRelDate);
  1343. }
  1344. wake_up_all(&acm->wioctl);
  1345. device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
  1346. usb_set_intfdata(acm->control, NULL);
  1347. usb_set_intfdata(acm->data, NULL);
  1348. mutex_unlock(&acm->mutex);
  1349. tty_port_tty_vhangup(&acm->port);
  1350. cancel_delayed_work_sync(&acm->dwork);
  1351. tty_unregister_device(acm_tty_driver, acm->minor);
  1352. usb_free_urb(acm->ctrlurb);
  1353. for (i = 0; i < ACM_NW; i++)
  1354. usb_free_urb(acm->wb[i].urb);
  1355. for (i = 0; i < acm->rx_buflimit; i++)
  1356. usb_free_urb(acm->read_urbs[i]);
  1357. acm_write_buffers_free(acm);
  1358. usb_free_coherent(acm->dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
  1359. acm_read_buffers_free(acm);
  1360. kfree(acm->notification_buffer);
  1361. if (!acm->combined_interfaces)
  1362. usb_driver_release_interface(&acm_driver, intf == acm->control ?
  1363. acm->data : acm->control);
  1364. tty_port_put(&acm->port);
  1365. }
  1366. #ifdef CONFIG_PM
  1367. static int acm_suspend(struct usb_interface *intf, pm_message_t message)
  1368. {
  1369. struct acm *acm = usb_get_intfdata(intf);
  1370. int cnt;
  1371. spin_lock_irq(&acm->write_lock);
  1372. if (PMSG_IS_AUTO(message)) {
  1373. if (acm->transmitting) {
  1374. spin_unlock_irq(&acm->write_lock);
  1375. return -EBUSY;
  1376. }
  1377. }
  1378. cnt = acm->susp_count++;
  1379. spin_unlock_irq(&acm->write_lock);
  1380. if (cnt)
  1381. return 0;
  1382. acm_poison_urbs(acm);
  1383. cancel_delayed_work_sync(&acm->dwork);
  1384. acm->urbs_in_error_delay = 0;
  1385. return 0;
  1386. }
  1387. static int acm_resume(struct usb_interface *intf)
  1388. {
  1389. struct acm *acm = usb_get_intfdata(intf);
  1390. struct urb *urb;
  1391. int rv = 0;
  1392. spin_lock_irq(&acm->write_lock);
  1393. if (--acm->susp_count)
  1394. goto out;
  1395. acm_unpoison_urbs(acm);
  1396. if (tty_port_initialized(&acm->port)) {
  1397. rv = usb_submit_urb(acm->ctrlurb, GFP_ATOMIC);
  1398. for (;;) {
  1399. urb = usb_get_from_anchor(&acm->delayed);
  1400. if (!urb)
  1401. break;
  1402. acm_start_wb(acm, urb->context);
  1403. }
  1404. /*
  1405. * delayed error checking because we must
  1406. * do the write path at all cost
  1407. */
  1408. if (rv < 0)
  1409. goto out;
  1410. rv = acm_submit_read_urbs(acm, GFP_ATOMIC);
  1411. }
  1412. out:
  1413. spin_unlock_irq(&acm->write_lock);
  1414. return rv;
  1415. }
  1416. static int acm_reset_resume(struct usb_interface *intf)
  1417. {
  1418. struct acm *acm = usb_get_intfdata(intf);
  1419. if (tty_port_initialized(&acm->port))
  1420. tty_port_tty_hangup(&acm->port, false);
  1421. return acm_resume(intf);
  1422. }
  1423. #endif /* CONFIG_PM */
  1424. static int acm_pre_reset(struct usb_interface *intf)
  1425. {
  1426. struct acm *acm = usb_get_intfdata(intf);
  1427. clear_bit(EVENT_RX_STALL, &acm->flags);
  1428. acm->nb_index = 0; /* pending control transfers are lost */
  1429. return 0;
  1430. }
  1431. #define NOKIA_PCSUITE_ACM_INFO(x) \
  1432. USB_DEVICE_AND_INTERFACE_INFO(0x0421, x, \
  1433. USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
  1434. USB_CDC_ACM_PROTO_VENDOR)
  1435. #define SAMSUNG_PCSUITE_ACM_INFO(x) \
  1436. USB_DEVICE_AND_INTERFACE_INFO(0x04e7, x, \
  1437. USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
  1438. USB_CDC_ACM_PROTO_VENDOR)
  1439. /*
  1440. * USB driver structure.
  1441. */
  1442. static const struct usb_device_id acm_ids[] = {
  1443. /* quirky and broken devices */
  1444. { USB_DEVICE(0x0424, 0x274e), /* Microchip Technology, Inc. (formerly SMSC) */
  1445. .driver_info = DISABLE_ECHO, }, /* DISABLE ECHO in termios flag */
  1446. { USB_DEVICE(0x076d, 0x0006), /* Denso Cradle CU-321 */
  1447. .driver_info = NO_UNION_NORMAL, },/* has no union descriptor */
  1448. { USB_DEVICE(0x17ef, 0x7000), /* Lenovo USB modem */
  1449. .driver_info = NO_UNION_NORMAL, },/* has no union descriptor */
  1450. { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
  1451. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1452. },
  1453. { USB_DEVICE(0x045b, 0x023c), /* Renesas R-Car H3 USB Download mode */
  1454. .driver_info = DISABLE_ECHO, /* Don't echo banner */
  1455. },
  1456. { USB_DEVICE(0x045b, 0x0247), /* Renesas R-Car D3 USB Download mode */
  1457. .driver_info = DISABLE_ECHO, /* Don't echo banner */
  1458. },
  1459. { USB_DEVICE(0x045b, 0x0248), /* Renesas R-Car M3-N USB Download mode */
  1460. .driver_info = DISABLE_ECHO, /* Don't echo banner */
  1461. },
  1462. { USB_DEVICE(0x045b, 0x024D), /* Renesas R-Car E3 USB Download mode */
  1463. .driver_info = DISABLE_ECHO, /* Don't echo banner */
  1464. },
  1465. { USB_DEVICE(0x04b8, 0x0d12), /* EPSON HMD Com&Sens */
  1466. .driver_info = NO_UNION_12, /* union descriptor is garbage */
  1467. },
  1468. { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
  1469. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1470. },
  1471. { USB_DEVICE(0x0e8d, 0x2000), /* MediaTek Inc Preloader */
  1472. .driver_info = DISABLE_ECHO, /* DISABLE ECHO in termios flag */
  1473. },
  1474. { USB_DEVICE(0x0e8d, 0x3329), /* MediaTek Inc GPS */
  1475. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1476. },
  1477. { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
  1478. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1479. },
  1480. { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
  1481. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1482. },
  1483. { USB_DEVICE(0x0ace, 0x1602), /* ZyDAS 56K USB MODEM */
  1484. .driver_info = SINGLE_RX_URB,
  1485. },
  1486. { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
  1487. .driver_info = SINGLE_RX_URB, /* firmware bug */
  1488. },
  1489. { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
  1490. .driver_info = SINGLE_RX_URB, /* firmware bug */
  1491. },
  1492. { USB_DEVICE(0x11ca, 0x0201), /* VeriFone Mx870 Gadget Serial */
  1493. .driver_info = SINGLE_RX_URB,
  1494. },
  1495. { USB_DEVICE(0x1901, 0x0006), /* GE Healthcare Patient Monitor UI Controller */
  1496. .driver_info = DISABLE_ECHO, /* DISABLE ECHO in termios flag */
  1497. },
  1498. { USB_DEVICE(0x1965, 0x0018), /* Uniden UBC125XLT */
  1499. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1500. },
  1501. { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
  1502. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1503. },
  1504. { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */
  1505. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1506. },
  1507. { USB_DEVICE(0x0572, 0x1321), /* Conexant USB MODEM CX93010 */
  1508. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1509. },
  1510. { USB_DEVICE(0x0572, 0x1324), /* Conexant USB MODEM RD02-D400 */
  1511. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1512. },
  1513. { USB_DEVICE(0x0572, 0x1328), /* Shiro / Aztech USB MODEM UM-3100 */
  1514. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1515. },
  1516. { USB_DEVICE(0x0572, 0x1349), /* Hiro (Conexant) USB MODEM H50228 */
  1517. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1518. },
  1519. { USB_DEVICE(0x20df, 0x0001), /* Simtec Electronics Entropy Key */
  1520. .driver_info = QUIRK_CONTROL_LINE_STATE, },
  1521. { USB_DEVICE(0x2184, 0x001c) }, /* GW Instek AFG-2225 */
  1522. { USB_DEVICE(0x2184, 0x0036) }, /* GW Instek AFG-125 */
  1523. { USB_DEVICE(0x22b8, 0x6425), /* Motorola MOTOMAGX phones */
  1524. },
  1525. /* Motorola H24 HSPA module: */
  1526. { USB_DEVICE(0x22b8, 0x2d91) }, /* modem */
  1527. { USB_DEVICE(0x22b8, 0x2d92), /* modem + diagnostics */
  1528. .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
  1529. },
  1530. { USB_DEVICE(0x22b8, 0x2d93), /* modem + AT port */
  1531. .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
  1532. },
  1533. { USB_DEVICE(0x22b8, 0x2d95), /* modem + AT port + diagnostics */
  1534. .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
  1535. },
  1536. { USB_DEVICE(0x22b8, 0x2d96), /* modem + NMEA */
  1537. .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
  1538. },
  1539. { USB_DEVICE(0x22b8, 0x2d97), /* modem + diagnostics + NMEA */
  1540. .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
  1541. },
  1542. { USB_DEVICE(0x22b8, 0x2d99), /* modem + AT port + NMEA */
  1543. .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
  1544. },
  1545. { USB_DEVICE(0x22b8, 0x2d9a), /* modem + AT port + diagnostics + NMEA */
  1546. .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
  1547. },
  1548. { USB_DEVICE(0x0572, 0x1329), /* Hummingbird huc56s (Conexant) */
  1549. .driver_info = NO_UNION_NORMAL, /* union descriptor misplaced on
  1550. data interface instead of
  1551. communications interface.
  1552. Maybe we should define a new
  1553. quirk for this. */
  1554. },
  1555. { USB_DEVICE(0x0572, 0x1340), /* Conexant CX93010-2x UCMxx */
  1556. .driver_info = NO_UNION_NORMAL,
  1557. },
  1558. { USB_DEVICE(0x05f9, 0x4002), /* PSC Scanning, Magellan 800i */
  1559. .driver_info = NO_UNION_NORMAL,
  1560. },
  1561. { USB_DEVICE(0x1bbb, 0x0003), /* Alcatel OT-I650 */
  1562. .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
  1563. },
  1564. { USB_DEVICE(0x1576, 0x03b1), /* Maretron USB100 */
  1565. .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
  1566. },
  1567. { USB_DEVICE(0xfff0, 0x0100), /* DATECS FP-2000 */
  1568. .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
  1569. },
  1570. { USB_DEVICE(0x09d8, 0x0320), /* Elatec GmbH TWN3 */
  1571. .driver_info = NO_UNION_NORMAL, /* has misplaced union descriptor */
  1572. },
  1573. { USB_DEVICE(0x0c26, 0x0020), /* Icom ICF3400 Serie */
  1574. .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
  1575. },
  1576. { USB_DEVICE(0x0ca6, 0xa050), /* Castles VEGA3000 */
  1577. .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
  1578. },
  1579. { USB_DEVICE(0x2912, 0x0001), /* ATOL FPrint */
  1580. .driver_info = CLEAR_HALT_CONDITIONS,
  1581. },
  1582. /* Nokia S60 phones expose two ACM channels. The first is
  1583. * a modem and is picked up by the standard AT-command
  1584. * information below. The second is 'vendor-specific' but
  1585. * is treated as a serial device at the S60 end, so we want
  1586. * to expose it on Linux too. */
  1587. { NOKIA_PCSUITE_ACM_INFO(0x042D), }, /* Nokia 3250 */
  1588. { NOKIA_PCSUITE_ACM_INFO(0x04D8), }, /* Nokia 5500 Sport */
  1589. { NOKIA_PCSUITE_ACM_INFO(0x04C9), }, /* Nokia E50 */
  1590. { NOKIA_PCSUITE_ACM_INFO(0x0419), }, /* Nokia E60 */
  1591. { NOKIA_PCSUITE_ACM_INFO(0x044D), }, /* Nokia E61 */
  1592. { NOKIA_PCSUITE_ACM_INFO(0x0001), }, /* Nokia E61i */
  1593. { NOKIA_PCSUITE_ACM_INFO(0x0475), }, /* Nokia E62 */
  1594. { NOKIA_PCSUITE_ACM_INFO(0x0508), }, /* Nokia E65 */
  1595. { NOKIA_PCSUITE_ACM_INFO(0x0418), }, /* Nokia E70 */
  1596. { NOKIA_PCSUITE_ACM_INFO(0x0425), }, /* Nokia N71 */
  1597. { NOKIA_PCSUITE_ACM_INFO(0x0486), }, /* Nokia N73 */
  1598. { NOKIA_PCSUITE_ACM_INFO(0x04DF), }, /* Nokia N75 */
  1599. { NOKIA_PCSUITE_ACM_INFO(0x000e), }, /* Nokia N77 */
  1600. { NOKIA_PCSUITE_ACM_INFO(0x0445), }, /* Nokia N80 */
  1601. { NOKIA_PCSUITE_ACM_INFO(0x042F), }, /* Nokia N91 & N91 8GB */
  1602. { NOKIA_PCSUITE_ACM_INFO(0x048E), }, /* Nokia N92 */
  1603. { NOKIA_PCSUITE_ACM_INFO(0x0420), }, /* Nokia N93 */
  1604. { NOKIA_PCSUITE_ACM_INFO(0x04E6), }, /* Nokia N93i */
  1605. { NOKIA_PCSUITE_ACM_INFO(0x04B2), }, /* Nokia 5700 XpressMusic */
  1606. { NOKIA_PCSUITE_ACM_INFO(0x0134), }, /* Nokia 6110 Navigator (China) */
  1607. { NOKIA_PCSUITE_ACM_INFO(0x046E), }, /* Nokia 6110 Navigator */
  1608. { NOKIA_PCSUITE_ACM_INFO(0x002f), }, /* Nokia 6120 classic & */
  1609. { NOKIA_PCSUITE_ACM_INFO(0x0088), }, /* Nokia 6121 classic */
  1610. { NOKIA_PCSUITE_ACM_INFO(0x00fc), }, /* Nokia 6124 classic */
  1611. { NOKIA_PCSUITE_ACM_INFO(0x0042), }, /* Nokia E51 */
  1612. { NOKIA_PCSUITE_ACM_INFO(0x00b0), }, /* Nokia E66 */
  1613. { NOKIA_PCSUITE_ACM_INFO(0x00ab), }, /* Nokia E71 */
  1614. { NOKIA_PCSUITE_ACM_INFO(0x0481), }, /* Nokia N76 */
  1615. { NOKIA_PCSUITE_ACM_INFO(0x0007), }, /* Nokia N81 & N81 8GB */
  1616. { NOKIA_PCSUITE_ACM_INFO(0x0071), }, /* Nokia N82 */
  1617. { NOKIA_PCSUITE_ACM_INFO(0x04F0), }, /* Nokia N95 & N95-3 NAM */
  1618. { NOKIA_PCSUITE_ACM_INFO(0x0070), }, /* Nokia N95 8GB */
  1619. { NOKIA_PCSUITE_ACM_INFO(0x0099), }, /* Nokia 6210 Navigator, RM-367 */
  1620. { NOKIA_PCSUITE_ACM_INFO(0x0128), }, /* Nokia 6210 Navigator, RM-419 */
  1621. { NOKIA_PCSUITE_ACM_INFO(0x008f), }, /* Nokia 6220 Classic */
  1622. { NOKIA_PCSUITE_ACM_INFO(0x00a0), }, /* Nokia 6650 */
  1623. { NOKIA_PCSUITE_ACM_INFO(0x007b), }, /* Nokia N78 */
  1624. { NOKIA_PCSUITE_ACM_INFO(0x0094), }, /* Nokia N85 */
  1625. { NOKIA_PCSUITE_ACM_INFO(0x003a), }, /* Nokia N96 & N96-3 */
  1626. { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
  1627. { NOKIA_PCSUITE_ACM_INFO(0x0108), }, /* Nokia 5320 XpressMusic 2G */
  1628. { NOKIA_PCSUITE_ACM_INFO(0x01f5), }, /* Nokia N97, RM-505 */
  1629. { NOKIA_PCSUITE_ACM_INFO(0x02e3), }, /* Nokia 5230, RM-588 */
  1630. { NOKIA_PCSUITE_ACM_INFO(0x0178), }, /* Nokia E63 */
  1631. { NOKIA_PCSUITE_ACM_INFO(0x010e), }, /* Nokia E75 */
  1632. { NOKIA_PCSUITE_ACM_INFO(0x02d9), }, /* Nokia 6760 Slide */
  1633. { NOKIA_PCSUITE_ACM_INFO(0x01d0), }, /* Nokia E52 */
  1634. { NOKIA_PCSUITE_ACM_INFO(0x0223), }, /* Nokia E72 */
  1635. { NOKIA_PCSUITE_ACM_INFO(0x0275), }, /* Nokia X6 */
  1636. { NOKIA_PCSUITE_ACM_INFO(0x026c), }, /* Nokia N97 Mini */
  1637. { NOKIA_PCSUITE_ACM_INFO(0x0154), }, /* Nokia 5800 XpressMusic */
  1638. { NOKIA_PCSUITE_ACM_INFO(0x04ce), }, /* Nokia E90 */
  1639. { NOKIA_PCSUITE_ACM_INFO(0x01d4), }, /* Nokia E55 */
  1640. { NOKIA_PCSUITE_ACM_INFO(0x0302), }, /* Nokia N8 */
  1641. { NOKIA_PCSUITE_ACM_INFO(0x0335), }, /* Nokia E7 */
  1642. { NOKIA_PCSUITE_ACM_INFO(0x03cd), }, /* Nokia C7 */
  1643. { SAMSUNG_PCSUITE_ACM_INFO(0x6651), }, /* Samsung GTi8510 (INNOV8) */
  1644. /* Support for Owen devices */
  1645. { USB_DEVICE(0x03eb, 0x0030), }, /* Owen SI30 */
  1646. /* NOTE: non-Nokia COMM/ACM/0xff is likely MSFT RNDIS... NOT a modem! */
  1647. #if IS_ENABLED(CONFIG_INPUT_IMS_PCU)
  1648. { USB_DEVICE(0x04d8, 0x0082), /* Application mode */
  1649. .driver_info = IGNORE_DEVICE,
  1650. },
  1651. { USB_DEVICE(0x04d8, 0x0083), /* Bootloader mode */
  1652. .driver_info = IGNORE_DEVICE,
  1653. },
  1654. #endif
  1655. #if IS_ENABLED(CONFIG_IR_TOY)
  1656. { USB_DEVICE(0x04d8, 0xfd08),
  1657. .driver_info = IGNORE_DEVICE,
  1658. },
  1659. { USB_DEVICE(0x04d8, 0xf58b),
  1660. .driver_info = IGNORE_DEVICE,
  1661. },
  1662. #endif
  1663. #if IS_ENABLED(CONFIG_USB_SERIAL_XR)
  1664. { USB_DEVICE(0x04e2, 0x1400), .driver_info = IGNORE_DEVICE },
  1665. { USB_DEVICE(0x04e2, 0x1401), .driver_info = IGNORE_DEVICE },
  1666. { USB_DEVICE(0x04e2, 0x1402), .driver_info = IGNORE_DEVICE },
  1667. { USB_DEVICE(0x04e2, 0x1403), .driver_info = IGNORE_DEVICE },
  1668. { USB_DEVICE(0x04e2, 0x1410), .driver_info = IGNORE_DEVICE },
  1669. { USB_DEVICE(0x04e2, 0x1411), .driver_info = IGNORE_DEVICE },
  1670. { USB_DEVICE(0x04e2, 0x1412), .driver_info = IGNORE_DEVICE },
  1671. { USB_DEVICE(0x04e2, 0x1414), .driver_info = IGNORE_DEVICE },
  1672. { USB_DEVICE(0x04e2, 0x1420), .driver_info = IGNORE_DEVICE },
  1673. { USB_DEVICE(0x04e2, 0x1422), .driver_info = IGNORE_DEVICE },
  1674. { USB_DEVICE(0x04e2, 0x1424), .driver_info = IGNORE_DEVICE },
  1675. #endif
  1676. /*Samsung phone in firmware update mode */
  1677. { USB_DEVICE(0x04e8, 0x685d),
  1678. .driver_info = IGNORE_DEVICE,
  1679. },
  1680. /* Exclude Infineon Flash Loader utility */
  1681. { USB_DEVICE(0x058b, 0x0041),
  1682. .driver_info = IGNORE_DEVICE,
  1683. },
  1684. /* Exclude ETAS ES58x */
  1685. { USB_DEVICE(0x108c, 0x0159), /* ES581.4 */
  1686. .driver_info = IGNORE_DEVICE,
  1687. },
  1688. { USB_DEVICE(0x108c, 0x0168), /* ES582.1 */
  1689. .driver_info = IGNORE_DEVICE,
  1690. },
  1691. { USB_DEVICE(0x108c, 0x0169), /* ES584.1 */
  1692. .driver_info = IGNORE_DEVICE,
  1693. },
  1694. { USB_DEVICE(0x1bc7, 0x0021), /* Telit 3G ACM only composition */
  1695. .driver_info = SEND_ZERO_PACKET,
  1696. },
  1697. { USB_DEVICE(0x1bc7, 0x0023), /* Telit 3G ACM + ECM composition */
  1698. .driver_info = SEND_ZERO_PACKET,
  1699. },
  1700. /* Exclude Goodix Fingerprint Reader */
  1701. { USB_DEVICE(0x27c6, 0x5395),
  1702. .driver_info = IGNORE_DEVICE,
  1703. },
  1704. /* Exclude Heimann Sensor GmbH USB appset demo */
  1705. { USB_DEVICE(0x32a7, 0x0000),
  1706. .driver_info = IGNORE_DEVICE,
  1707. },
  1708. /* CH343 supports CAP_BRK, but doesn't advertise it */
  1709. { USB_DEVICE(0x1a86, 0x55d3), .driver_info = MISSING_CAP_BRK, },
  1710. /* control interfaces without any protocol set */
  1711. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1712. USB_CDC_PROTO_NONE) },
  1713. /* control interfaces with various AT-command sets */
  1714. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1715. USB_CDC_ACM_PROTO_AT_V25TER) },
  1716. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1717. USB_CDC_ACM_PROTO_AT_PCCA101) },
  1718. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1719. USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
  1720. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1721. USB_CDC_ACM_PROTO_AT_GSM) },
  1722. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1723. USB_CDC_ACM_PROTO_AT_3G) },
  1724. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1725. USB_CDC_ACM_PROTO_AT_CDMA) },
  1726. { USB_DEVICE(0x1519, 0x0452), /* Intel 7260 modem */
  1727. .driver_info = SEND_ZERO_PACKET,
  1728. },
  1729. { }
  1730. };
  1731. MODULE_DEVICE_TABLE(usb, acm_ids);
  1732. static struct usb_driver acm_driver = {
  1733. .name = "cdc_acm",
  1734. .probe = acm_probe,
  1735. .disconnect = acm_disconnect,
  1736. #ifdef CONFIG_PM
  1737. .suspend = acm_suspend,
  1738. .resume = acm_resume,
  1739. .reset_resume = acm_reset_resume,
  1740. #endif
  1741. .pre_reset = acm_pre_reset,
  1742. .id_table = acm_ids,
  1743. #ifdef CONFIG_PM
  1744. .supports_autosuspend = 1,
  1745. #endif
  1746. .disable_hub_initiated_lpm = 1,
  1747. };
  1748. /*
  1749. * TTY driver structures.
  1750. */
  1751. static const struct tty_operations acm_ops = {
  1752. .install = acm_tty_install,
  1753. .open = acm_tty_open,
  1754. .close = acm_tty_close,
  1755. .cleanup = acm_tty_cleanup,
  1756. .hangup = acm_tty_hangup,
  1757. .write = acm_tty_write,
  1758. .write_room = acm_tty_write_room,
  1759. .flush_buffer = acm_tty_flush_buffer,
  1760. .ioctl = acm_tty_ioctl,
  1761. .throttle = acm_tty_throttle,
  1762. .unthrottle = acm_tty_unthrottle,
  1763. .chars_in_buffer = acm_tty_chars_in_buffer,
  1764. .break_ctl = acm_tty_break_ctl,
  1765. .set_termios = acm_tty_set_termios,
  1766. .tiocmget = acm_tty_tiocmget,
  1767. .tiocmset = acm_tty_tiocmset,
  1768. .get_serial = get_serial_info,
  1769. .set_serial = set_serial_info,
  1770. .get_icount = acm_tty_get_icount,
  1771. };
  1772. /*
  1773. * Init / exit.
  1774. */
  1775. static int __init acm_init(void)
  1776. {
  1777. int retval;
  1778. acm_tty_driver = tty_alloc_driver(ACM_TTY_MINORS, TTY_DRIVER_REAL_RAW |
  1779. TTY_DRIVER_DYNAMIC_DEV);
  1780. if (IS_ERR(acm_tty_driver))
  1781. return PTR_ERR(acm_tty_driver);
  1782. acm_tty_driver->driver_name = "acm",
  1783. acm_tty_driver->name = "ttyACM",
  1784. acm_tty_driver->major = ACM_TTY_MAJOR,
  1785. acm_tty_driver->minor_start = 0,
  1786. acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
  1787. acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
  1788. acm_tty_driver->init_termios = tty_std_termios;
  1789. acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD |
  1790. HUPCL | CLOCAL;
  1791. tty_set_operations(acm_tty_driver, &acm_ops);
  1792. retval = tty_register_driver(acm_tty_driver);
  1793. if (retval) {
  1794. tty_driver_kref_put(acm_tty_driver);
  1795. return retval;
  1796. }
  1797. retval = usb_register(&acm_driver);
  1798. if (retval) {
  1799. tty_unregister_driver(acm_tty_driver);
  1800. tty_driver_kref_put(acm_tty_driver);
  1801. return retval;
  1802. }
  1803. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
  1804. return 0;
  1805. }
  1806. static void __exit acm_exit(void)
  1807. {
  1808. usb_deregister(&acm_driver);
  1809. tty_unregister_driver(acm_tty_driver);
  1810. tty_driver_kref_put(acm_tty_driver);
  1811. idr_destroy(&acm_minors);
  1812. }
  1813. module_init(acm_init);
  1814. module_exit(acm_exit);
  1815. MODULE_AUTHOR(DRIVER_AUTHOR);
  1816. MODULE_DESCRIPTION(DRIVER_DESC);
  1817. MODULE_LICENSE("GPL");
  1818. MODULE_ALIAS_CHARDEV_MAJOR(ACM_TTY_MAJOR);