cdc-acm.c 53 KB

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