cdc-acm.c 56 KB

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