cdc-acm.c 53 KB

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