cdc-acm.c 53 KB

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