cdc-acm.c 54 KB

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