cdc-acm.c 54 KB

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