cdc-acm.c 56 KB

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