cdc-acm.c 53 KB

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