cdc-acm.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  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/list.h>
  48. #include "cdc-acm.h"
  49. #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek, Johan Hovold"
  50. #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
  51. static struct usb_driver acm_driver;
  52. static struct tty_driver *acm_tty_driver;
  53. static struct acm *acm_table[ACM_TTY_MINORS];
  54. static DEFINE_MUTEX(open_mutex);
  55. #define ACM_READY(acm) (acm && acm->dev && acm->port.count)
  56. static const struct tty_port_operations acm_port_ops = {
  57. };
  58. /*
  59. * Functions for ACM control messages.
  60. */
  61. static int acm_ctrl_msg(struct acm *acm, int request, int value,
  62. void *buf, int len)
  63. {
  64. int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
  65. request, USB_RT_ACM, value,
  66. acm->control->altsetting[0].desc.bInterfaceNumber,
  67. buf, len, 5000);
  68. dev_dbg(&acm->control->dev,
  69. "%s - rq 0x%02x, val %#x, len %#x, result %d\n",
  70. __func__, request, value, len, retval);
  71. return retval < 0 ? retval : 0;
  72. }
  73. /* devices aren't required to support these requests.
  74. * the cdc acm descriptor tells whether they do...
  75. */
  76. #define acm_set_control(acm, control) \
  77. acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0)
  78. #define acm_set_line(acm, line) \
  79. acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
  80. #define acm_send_break(acm, ms) \
  81. acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
  82. /*
  83. * Write buffer management.
  84. * All of these assume proper locks taken by the caller.
  85. */
  86. static int acm_wb_alloc(struct acm *acm)
  87. {
  88. int i, wbn;
  89. struct acm_wb *wb;
  90. wbn = 0;
  91. i = 0;
  92. for (;;) {
  93. wb = &acm->wb[wbn];
  94. if (!wb->use) {
  95. wb->use = 1;
  96. return wbn;
  97. }
  98. wbn = (wbn + 1) % ACM_NW;
  99. if (++i >= ACM_NW)
  100. return -1;
  101. }
  102. }
  103. static int acm_wb_is_avail(struct acm *acm)
  104. {
  105. int i, n;
  106. unsigned long flags;
  107. n = ACM_NW;
  108. spin_lock_irqsave(&acm->write_lock, flags);
  109. for (i = 0; i < ACM_NW; i++)
  110. n -= acm->wb[i].use;
  111. spin_unlock_irqrestore(&acm->write_lock, flags);
  112. return n;
  113. }
  114. /*
  115. * Finish write. Caller must hold acm->write_lock
  116. */
  117. static void acm_write_done(struct acm *acm, struct acm_wb *wb)
  118. {
  119. wb->use = 0;
  120. acm->transmitting--;
  121. usb_autopm_put_interface_async(acm->control);
  122. }
  123. /*
  124. * Poke write.
  125. *
  126. * the caller is responsible for locking
  127. */
  128. static int acm_start_wb(struct acm *acm, struct acm_wb *wb)
  129. {
  130. int rc;
  131. acm->transmitting++;
  132. wb->urb->transfer_buffer = wb->buf;
  133. wb->urb->transfer_dma = wb->dmah;
  134. wb->urb->transfer_buffer_length = wb->len;
  135. wb->urb->dev = acm->dev;
  136. rc = usb_submit_urb(wb->urb, GFP_ATOMIC);
  137. if (rc < 0) {
  138. dev_err(&acm->data->dev,
  139. "%s - usb_submit_urb(write bulk) failed: %d\n",
  140. __func__, rc);
  141. acm_write_done(acm, wb);
  142. }
  143. return rc;
  144. }
  145. static int acm_write_start(struct acm *acm, int wbn)
  146. {
  147. unsigned long flags;
  148. struct acm_wb *wb = &acm->wb[wbn];
  149. int rc;
  150. spin_lock_irqsave(&acm->write_lock, flags);
  151. if (!acm->dev) {
  152. wb->use = 0;
  153. spin_unlock_irqrestore(&acm->write_lock, flags);
  154. return -ENODEV;
  155. }
  156. dev_vdbg(&acm->data->dev, "%s - susp_count %d\n", __func__,
  157. acm->susp_count);
  158. usb_autopm_get_interface_async(acm->control);
  159. if (acm->susp_count) {
  160. if (!acm->delayed_wb)
  161. acm->delayed_wb = wb;
  162. else
  163. usb_autopm_put_interface_async(acm->control);
  164. spin_unlock_irqrestore(&acm->write_lock, flags);
  165. return 0; /* A white lie */
  166. }
  167. usb_mark_last_busy(acm->dev);
  168. rc = acm_start_wb(acm, wb);
  169. spin_unlock_irqrestore(&acm->write_lock, flags);
  170. return rc;
  171. }
  172. /*
  173. * attributes exported through sysfs
  174. */
  175. static ssize_t show_caps
  176. (struct device *dev, struct device_attribute *attr, char *buf)
  177. {
  178. struct usb_interface *intf = to_usb_interface(dev);
  179. struct acm *acm = usb_get_intfdata(intf);
  180. return sprintf(buf, "%d", acm->ctrl_caps);
  181. }
  182. static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
  183. static ssize_t show_country_codes
  184. (struct device *dev, struct device_attribute *attr, char *buf)
  185. {
  186. struct usb_interface *intf = to_usb_interface(dev);
  187. struct acm *acm = usb_get_intfdata(intf);
  188. memcpy(buf, acm->country_codes, acm->country_code_size);
  189. return acm->country_code_size;
  190. }
  191. static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
  192. static ssize_t show_country_rel_date
  193. (struct device *dev, struct device_attribute *attr, char *buf)
  194. {
  195. struct usb_interface *intf = to_usb_interface(dev);
  196. struct acm *acm = usb_get_intfdata(intf);
  197. return sprintf(buf, "%d", acm->country_rel_date);
  198. }
  199. static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
  200. /*
  201. * Interrupt handlers for various ACM device responses
  202. */
  203. /* control interface reports status changes with "interrupt" transfers */
  204. static void acm_ctrl_irq(struct urb *urb)
  205. {
  206. struct acm *acm = urb->context;
  207. struct usb_cdc_notification *dr = urb->transfer_buffer;
  208. struct tty_struct *tty;
  209. unsigned char *data;
  210. int newctrl;
  211. int retval;
  212. int status = urb->status;
  213. switch (status) {
  214. case 0:
  215. /* success */
  216. break;
  217. case -ECONNRESET:
  218. case -ENOENT:
  219. case -ESHUTDOWN:
  220. /* this urb is terminated, clean up */
  221. dev_dbg(&acm->control->dev,
  222. "%s - urb shutting down with status: %d\n",
  223. __func__, status);
  224. return;
  225. default:
  226. dev_dbg(&acm->control->dev,
  227. "%s - nonzero urb status received: %d\n",
  228. __func__, status);
  229. goto exit;
  230. }
  231. if (!ACM_READY(acm))
  232. goto exit;
  233. usb_mark_last_busy(acm->dev);
  234. data = (unsigned char *)(dr + 1);
  235. switch (dr->bNotificationType) {
  236. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  237. dev_dbg(&acm->control->dev, "%s - network connection: %d\n",
  238. __func__, dr->wValue);
  239. break;
  240. case USB_CDC_NOTIFY_SERIAL_STATE:
  241. tty = tty_port_tty_get(&acm->port);
  242. newctrl = get_unaligned_le16(data);
  243. if (tty) {
  244. if (!acm->clocal &&
  245. (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
  246. dev_dbg(&acm->control->dev,
  247. "%s - calling hangup\n", __func__);
  248. tty_hangup(tty);
  249. }
  250. tty_kref_put(tty);
  251. }
  252. acm->ctrlin = newctrl;
  253. dev_dbg(&acm->control->dev,
  254. "%s - input control lines: dcd%c dsr%c break%c "
  255. "ring%c framing%c parity%c overrun%c\n",
  256. __func__,
  257. acm->ctrlin & ACM_CTRL_DCD ? '+' : '-',
  258. acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
  259. acm->ctrlin & ACM_CTRL_BRK ? '+' : '-',
  260. acm->ctrlin & ACM_CTRL_RI ? '+' : '-',
  261. acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-',
  262. acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
  263. acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
  264. break;
  265. default:
  266. dev_dbg(&acm->control->dev,
  267. "%s - unknown notification %d received: index %d "
  268. "len %d data0 %d data1 %d\n",
  269. __func__,
  270. dr->bNotificationType, dr->wIndex,
  271. dr->wLength, data[0], data[1]);
  272. break;
  273. }
  274. exit:
  275. retval = usb_submit_urb(urb, GFP_ATOMIC);
  276. if (retval)
  277. dev_err(&acm->control->dev, "%s - usb_submit_urb failed: %d\n",
  278. __func__, retval);
  279. }
  280. static int acm_submit_read_urb(struct acm *acm, int index, gfp_t mem_flags)
  281. {
  282. int res;
  283. if (!test_and_clear_bit(index, &acm->read_urbs_free))
  284. return 0;
  285. dev_vdbg(&acm->data->dev, "%s - urb %d\n", __func__, index);
  286. res = usb_submit_urb(acm->read_urbs[index], mem_flags);
  287. if (res) {
  288. if (res != -EPERM) {
  289. dev_err(&acm->data->dev,
  290. "%s - usb_submit_urb failed: %d\n",
  291. __func__, res);
  292. }
  293. set_bit(index, &acm->read_urbs_free);
  294. return res;
  295. }
  296. return 0;
  297. }
  298. static int acm_submit_read_urbs(struct acm *acm, gfp_t mem_flags)
  299. {
  300. int res;
  301. int i;
  302. for (i = 0; i < acm->rx_buflimit; ++i) {
  303. res = acm_submit_read_urb(acm, i, mem_flags);
  304. if (res)
  305. return res;
  306. }
  307. return 0;
  308. }
  309. static void acm_process_read_urb(struct acm *acm, struct urb *urb)
  310. {
  311. struct tty_struct *tty;
  312. if (!urb->actual_length)
  313. return;
  314. tty = tty_port_tty_get(&acm->port);
  315. if (!tty)
  316. return;
  317. tty_insert_flip_string(tty, urb->transfer_buffer, urb->actual_length);
  318. tty_flip_buffer_push(tty);
  319. tty_kref_put(tty);
  320. }
  321. static void acm_read_bulk_callback(struct urb *urb)
  322. {
  323. struct acm_rb *rb = urb->context;
  324. struct acm *acm = rb->instance;
  325. unsigned long flags;
  326. dev_vdbg(&acm->data->dev, "%s - urb %d, len %d\n", __func__,
  327. rb->index, urb->actual_length);
  328. set_bit(rb->index, &acm->read_urbs_free);
  329. if (!acm->dev) {
  330. dev_dbg(&acm->data->dev, "%s - disconnected\n", __func__);
  331. return;
  332. }
  333. usb_mark_last_busy(acm->dev);
  334. if (urb->status) {
  335. dev_dbg(&acm->data->dev, "%s - non-zero urb status: %d\n",
  336. __func__, urb->status);
  337. return;
  338. }
  339. acm_process_read_urb(acm, urb);
  340. /* throttle device if requested by tty */
  341. spin_lock_irqsave(&acm->read_lock, flags);
  342. acm->throttled = acm->throttle_req;
  343. if (!acm->throttled && !acm->susp_count) {
  344. spin_unlock_irqrestore(&acm->read_lock, flags);
  345. acm_submit_read_urb(acm, rb->index, GFP_ATOMIC);
  346. } else {
  347. spin_unlock_irqrestore(&acm->read_lock, flags);
  348. }
  349. }
  350. /* data interface wrote those outgoing bytes */
  351. static void acm_write_bulk(struct urb *urb)
  352. {
  353. struct acm_wb *wb = urb->context;
  354. struct acm *acm = wb->instance;
  355. unsigned long flags;
  356. if (urb->status || (urb->actual_length != urb->transfer_buffer_length))
  357. dev_vdbg(&acm->data->dev, "%s - len %d/%d, status %d\n",
  358. __func__,
  359. urb->actual_length,
  360. urb->transfer_buffer_length,
  361. urb->status);
  362. spin_lock_irqsave(&acm->write_lock, flags);
  363. acm_write_done(acm, wb);
  364. spin_unlock_irqrestore(&acm->write_lock, flags);
  365. if (ACM_READY(acm))
  366. schedule_work(&acm->work);
  367. }
  368. static void acm_softint(struct work_struct *work)
  369. {
  370. struct acm *acm = container_of(work, struct acm, work);
  371. struct tty_struct *tty;
  372. dev_vdbg(&acm->data->dev, "%s\n", __func__);
  373. if (!ACM_READY(acm))
  374. return;
  375. tty = tty_port_tty_get(&acm->port);
  376. if (!tty)
  377. return;
  378. tty_wakeup(tty);
  379. tty_kref_put(tty);
  380. }
  381. /*
  382. * TTY handlers
  383. */
  384. static int acm_tty_open(struct tty_struct *tty, struct file *filp)
  385. {
  386. struct acm *acm;
  387. int rv = -ENODEV;
  388. mutex_lock(&open_mutex);
  389. acm = acm_table[tty->index];
  390. if (!acm || !acm->dev)
  391. goto out;
  392. else
  393. rv = 0;
  394. dev_dbg(&acm->control->dev, "%s\n", __func__);
  395. set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
  396. tty->driver_data = acm;
  397. tty_port_tty_set(&acm->port, tty);
  398. if (usb_autopm_get_interface(acm->control) < 0)
  399. goto early_bail;
  400. else
  401. acm->control->needs_remote_wakeup = 1;
  402. mutex_lock(&acm->mutex);
  403. if (acm->port.count++) {
  404. mutex_unlock(&acm->mutex);
  405. usb_autopm_put_interface(acm->control);
  406. goto out;
  407. }
  408. acm->ctrlurb->dev = acm->dev;
  409. if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
  410. dev_err(&acm->control->dev,
  411. "%s - usb_submit_urb(ctrl irq) failed\n", __func__);
  412. goto bail_out;
  413. }
  414. if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS) &&
  415. (acm->ctrl_caps & USB_CDC_CAP_LINE))
  416. goto bail_out;
  417. usb_autopm_put_interface(acm->control);
  418. if (acm_submit_read_urbs(acm, GFP_KERNEL))
  419. goto bail_out;
  420. set_bit(ASYNCB_INITIALIZED, &acm->port.flags);
  421. rv = tty_port_block_til_ready(&acm->port, tty, filp);
  422. mutex_unlock(&acm->mutex);
  423. out:
  424. mutex_unlock(&open_mutex);
  425. return rv;
  426. bail_out:
  427. acm->port.count--;
  428. mutex_unlock(&acm->mutex);
  429. usb_autopm_put_interface(acm->control);
  430. early_bail:
  431. mutex_unlock(&open_mutex);
  432. tty_port_tty_set(&acm->port, NULL);
  433. return -EIO;
  434. }
  435. static void acm_tty_unregister(struct acm *acm)
  436. {
  437. int i;
  438. tty_unregister_device(acm_tty_driver, acm->minor);
  439. usb_put_intf(acm->control);
  440. acm_table[acm->minor] = NULL;
  441. usb_free_urb(acm->ctrlurb);
  442. for (i = 0; i < ACM_NW; i++)
  443. usb_free_urb(acm->wb[i].urb);
  444. for (i = 0; i < acm->rx_buflimit; i++)
  445. usb_free_urb(acm->read_urbs[i]);
  446. kfree(acm->country_codes);
  447. kfree(acm);
  448. }
  449. static void acm_port_down(struct acm *acm)
  450. {
  451. int i;
  452. mutex_lock(&open_mutex);
  453. if (acm->dev) {
  454. usb_autopm_get_interface(acm->control);
  455. acm_set_control(acm, acm->ctrlout = 0);
  456. usb_kill_urb(acm->ctrlurb);
  457. for (i = 0; i < ACM_NW; i++)
  458. usb_kill_urb(acm->wb[i].urb);
  459. for (i = 0; i < acm->rx_buflimit; i++)
  460. usb_kill_urb(acm->read_urbs[i]);
  461. acm->control->needs_remote_wakeup = 0;
  462. usb_autopm_put_interface(acm->control);
  463. }
  464. mutex_unlock(&open_mutex);
  465. }
  466. static void acm_tty_hangup(struct tty_struct *tty)
  467. {
  468. struct acm *acm = tty->driver_data;
  469. tty_port_hangup(&acm->port);
  470. acm_port_down(acm);
  471. }
  472. static void acm_tty_close(struct tty_struct *tty, struct file *filp)
  473. {
  474. struct acm *acm = tty->driver_data;
  475. /* Perform the closing process and see if we need to do the hardware
  476. shutdown */
  477. if (!acm)
  478. return;
  479. if (tty_port_close_start(&acm->port, tty, filp) == 0) {
  480. mutex_lock(&open_mutex);
  481. if (!acm->dev) {
  482. tty_port_tty_set(&acm->port, NULL);
  483. acm_tty_unregister(acm);
  484. tty->driver_data = NULL;
  485. }
  486. mutex_unlock(&open_mutex);
  487. return;
  488. }
  489. acm_port_down(acm);
  490. tty_port_close_end(&acm->port, tty);
  491. tty_port_tty_set(&acm->port, NULL);
  492. }
  493. static int acm_tty_write(struct tty_struct *tty,
  494. const unsigned char *buf, int count)
  495. {
  496. struct acm *acm = tty->driver_data;
  497. int stat;
  498. unsigned long flags;
  499. int wbn;
  500. struct acm_wb *wb;
  501. if (!ACM_READY(acm))
  502. return -EINVAL;
  503. if (!count)
  504. return 0;
  505. dev_vdbg(&acm->data->dev, "%s - count %d\n", __func__, count);
  506. spin_lock_irqsave(&acm->write_lock, flags);
  507. wbn = acm_wb_alloc(acm);
  508. if (wbn < 0) {
  509. spin_unlock_irqrestore(&acm->write_lock, flags);
  510. return 0;
  511. }
  512. wb = &acm->wb[wbn];
  513. count = (count > acm->writesize) ? acm->writesize : count;
  514. dev_vdbg(&acm->data->dev, "%s - write %d\n", __func__, count);
  515. memcpy(wb->buf, buf, count);
  516. wb->len = count;
  517. spin_unlock_irqrestore(&acm->write_lock, flags);
  518. stat = acm_write_start(acm, wbn);
  519. if (stat < 0)
  520. return stat;
  521. return count;
  522. }
  523. static int acm_tty_write_room(struct tty_struct *tty)
  524. {
  525. struct acm *acm = tty->driver_data;
  526. if (!ACM_READY(acm))
  527. return -EINVAL;
  528. /*
  529. * Do not let the line discipline to know that we have a reserve,
  530. * or it might get too enthusiastic.
  531. */
  532. return acm_wb_is_avail(acm) ? acm->writesize : 0;
  533. }
  534. static int acm_tty_chars_in_buffer(struct tty_struct *tty)
  535. {
  536. struct acm *acm = tty->driver_data;
  537. if (!ACM_READY(acm))
  538. return 0;
  539. /*
  540. * This is inaccurate (overcounts), but it works.
  541. */
  542. return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
  543. }
  544. static void acm_tty_throttle(struct tty_struct *tty)
  545. {
  546. struct acm *acm = tty->driver_data;
  547. if (!ACM_READY(acm))
  548. return;
  549. spin_lock_irq(&acm->read_lock);
  550. acm->throttle_req = 1;
  551. spin_unlock_irq(&acm->read_lock);
  552. }
  553. static void acm_tty_unthrottle(struct tty_struct *tty)
  554. {
  555. struct acm *acm = tty->driver_data;
  556. unsigned int was_throttled;
  557. if (!ACM_READY(acm))
  558. return;
  559. spin_lock_irq(&acm->read_lock);
  560. was_throttled = acm->throttled;
  561. acm->throttled = 0;
  562. acm->throttle_req = 0;
  563. spin_unlock_irq(&acm->read_lock);
  564. if (was_throttled)
  565. acm_submit_read_urbs(acm, GFP_KERNEL);
  566. }
  567. static int acm_tty_break_ctl(struct tty_struct *tty, int state)
  568. {
  569. struct acm *acm = tty->driver_data;
  570. int retval;
  571. if (!ACM_READY(acm))
  572. return -EINVAL;
  573. retval = acm_send_break(acm, state ? 0xffff : 0);
  574. if (retval < 0)
  575. dev_dbg(&acm->control->dev, "%s - send break failed\n",
  576. __func__);
  577. return retval;
  578. }
  579. static int acm_tty_tiocmget(struct tty_struct *tty)
  580. {
  581. struct acm *acm = tty->driver_data;
  582. if (!ACM_READY(acm))
  583. return -EINVAL;
  584. return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
  585. (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
  586. (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
  587. (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
  588. (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
  589. TIOCM_CTS;
  590. }
  591. static int acm_tty_tiocmset(struct tty_struct *tty,
  592. unsigned int set, unsigned int clear)
  593. {
  594. struct acm *acm = tty->driver_data;
  595. unsigned int newctrl;
  596. if (!ACM_READY(acm))
  597. return -EINVAL;
  598. newctrl = acm->ctrlout;
  599. set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
  600. (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
  601. clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
  602. (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
  603. newctrl = (newctrl & ~clear) | set;
  604. if (acm->ctrlout == newctrl)
  605. return 0;
  606. return acm_set_control(acm, acm->ctrlout = newctrl);
  607. }
  608. static int acm_tty_ioctl(struct tty_struct *tty,
  609. unsigned int cmd, unsigned long arg)
  610. {
  611. struct acm *acm = tty->driver_data;
  612. if (!ACM_READY(acm))
  613. return -EINVAL;
  614. return -ENOIOCTLCMD;
  615. }
  616. static const __u32 acm_tty_speed[] = {
  617. 0, 50, 75, 110, 134, 150, 200, 300, 600,
  618. 1200, 1800, 2400, 4800, 9600, 19200, 38400,
  619. 57600, 115200, 230400, 460800, 500000, 576000,
  620. 921600, 1000000, 1152000, 1500000, 2000000,
  621. 2500000, 3000000, 3500000, 4000000
  622. };
  623. static const __u8 acm_tty_size[] = {
  624. 5, 6, 7, 8
  625. };
  626. static void acm_tty_set_termios(struct tty_struct *tty,
  627. struct ktermios *termios_old)
  628. {
  629. struct acm *acm = tty->driver_data;
  630. struct ktermios *termios = tty->termios;
  631. struct usb_cdc_line_coding newline;
  632. int newctrl = acm->ctrlout;
  633. if (!ACM_READY(acm))
  634. return;
  635. newline.dwDTERate = cpu_to_le32(tty_get_baud_rate(tty));
  636. newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
  637. newline.bParityType = termios->c_cflag & PARENB ?
  638. (termios->c_cflag & PARODD ? 1 : 2) +
  639. (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
  640. newline.bDataBits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4];
  641. /* FIXME: Needs to clear unsupported bits in the termios */
  642. acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
  643. if (!newline.dwDTERate) {
  644. newline.dwDTERate = acm->line.dwDTERate;
  645. newctrl &= ~ACM_CTRL_DTR;
  646. } else
  647. newctrl |= ACM_CTRL_DTR;
  648. if (newctrl != acm->ctrlout)
  649. acm_set_control(acm, acm->ctrlout = newctrl);
  650. if (memcmp(&acm->line, &newline, sizeof newline)) {
  651. memcpy(&acm->line, &newline, sizeof newline);
  652. dev_dbg(&acm->control->dev, "%s - set line: %d %d %d %d\n",
  653. __func__,
  654. le32_to_cpu(newline.dwDTERate),
  655. newline.bCharFormat, newline.bParityType,
  656. newline.bDataBits);
  657. acm_set_line(acm, &acm->line);
  658. }
  659. }
  660. /*
  661. * USB probe and disconnect routines.
  662. */
  663. /* Little helpers: write/read buffers free */
  664. static void acm_write_buffers_free(struct acm *acm)
  665. {
  666. int i;
  667. struct acm_wb *wb;
  668. struct usb_device *usb_dev = interface_to_usbdev(acm->control);
  669. for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++)
  670. usb_free_coherent(usb_dev, acm->writesize, wb->buf, wb->dmah);
  671. }
  672. static void acm_read_buffers_free(struct acm *acm)
  673. {
  674. struct usb_device *usb_dev = interface_to_usbdev(acm->control);
  675. int i;
  676. for (i = 0; i < acm->rx_buflimit; i++)
  677. usb_free_coherent(usb_dev, acm->readsize,
  678. acm->read_buffers[i].base, acm->read_buffers[i].dma);
  679. }
  680. /* Little helper: write buffers allocate */
  681. static int acm_write_buffers_alloc(struct acm *acm)
  682. {
  683. int i;
  684. struct acm_wb *wb;
  685. for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
  686. wb->buf = usb_alloc_coherent(acm->dev, acm->writesize, GFP_KERNEL,
  687. &wb->dmah);
  688. if (!wb->buf) {
  689. while (i != 0) {
  690. --i;
  691. --wb;
  692. usb_free_coherent(acm->dev, acm->writesize,
  693. wb->buf, wb->dmah);
  694. }
  695. return -ENOMEM;
  696. }
  697. }
  698. return 0;
  699. }
  700. static int acm_probe(struct usb_interface *intf,
  701. const struct usb_device_id *id)
  702. {
  703. struct usb_cdc_union_desc *union_header = NULL;
  704. struct usb_cdc_country_functional_desc *cfd = NULL;
  705. unsigned char *buffer = intf->altsetting->extra;
  706. int buflen = intf->altsetting->extralen;
  707. struct usb_interface *control_interface;
  708. struct usb_interface *data_interface;
  709. struct usb_endpoint_descriptor *epctrl = NULL;
  710. struct usb_endpoint_descriptor *epread = NULL;
  711. struct usb_endpoint_descriptor *epwrite = NULL;
  712. struct usb_device *usb_dev = interface_to_usbdev(intf);
  713. struct acm *acm;
  714. int minor;
  715. int ctrlsize, readsize;
  716. u8 *buf;
  717. u8 ac_management_function = 0;
  718. u8 call_management_function = 0;
  719. int call_interface_num = -1;
  720. int data_interface_num = -1;
  721. unsigned long quirks;
  722. int num_rx_buf;
  723. int i;
  724. int combined_interfaces = 0;
  725. /* normal quirks */
  726. quirks = (unsigned long)id->driver_info;
  727. num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
  728. /* handle quirks deadly to normal probing*/
  729. if (quirks == NO_UNION_NORMAL) {
  730. data_interface = usb_ifnum_to_if(usb_dev, 1);
  731. control_interface = usb_ifnum_to_if(usb_dev, 0);
  732. goto skip_normal_probe;
  733. }
  734. /* normal probing*/
  735. if (!buffer) {
  736. dev_err(&intf->dev, "Weird descriptor references\n");
  737. return -EINVAL;
  738. }
  739. if (!buflen) {
  740. if (intf->cur_altsetting->endpoint &&
  741. intf->cur_altsetting->endpoint->extralen &&
  742. intf->cur_altsetting->endpoint->extra) {
  743. dev_dbg(&intf->dev,
  744. "Seeking extra descriptors on endpoint\n");
  745. buflen = intf->cur_altsetting->endpoint->extralen;
  746. buffer = intf->cur_altsetting->endpoint->extra;
  747. } else {
  748. dev_err(&intf->dev,
  749. "Zero length descriptor references\n");
  750. return -EINVAL;
  751. }
  752. }
  753. while (buflen > 0) {
  754. if (buffer[1] != USB_DT_CS_INTERFACE) {
  755. dev_err(&intf->dev, "skipping garbage\n");
  756. goto next_desc;
  757. }
  758. switch (buffer[2]) {
  759. case USB_CDC_UNION_TYPE: /* we've found it */
  760. if (union_header) {
  761. dev_err(&intf->dev, "More than one "
  762. "union descriptor, skipping ...\n");
  763. goto next_desc;
  764. }
  765. union_header = (struct usb_cdc_union_desc *)buffer;
  766. break;
  767. case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/
  768. cfd = (struct usb_cdc_country_functional_desc *)buffer;
  769. break;
  770. case USB_CDC_HEADER_TYPE: /* maybe check version */
  771. break; /* for now we ignore it */
  772. case USB_CDC_ACM_TYPE:
  773. ac_management_function = buffer[3];
  774. break;
  775. case USB_CDC_CALL_MANAGEMENT_TYPE:
  776. call_management_function = buffer[3];
  777. call_interface_num = buffer[4];
  778. if ( (quirks & NOT_A_MODEM) == 0 && (call_management_function & 3) != 3)
  779. dev_err(&intf->dev, "This device cannot do calls on its own. It is not a modem.\n");
  780. break;
  781. default:
  782. /* there are LOTS more CDC descriptors that
  783. * could legitimately be found here.
  784. */
  785. dev_dbg(&intf->dev, "Ignoring descriptor: "
  786. "type %02x, length %d\n",
  787. buffer[2], buffer[0]);
  788. break;
  789. }
  790. next_desc:
  791. buflen -= buffer[0];
  792. buffer += buffer[0];
  793. }
  794. if (!union_header) {
  795. if (call_interface_num > 0) {
  796. dev_dbg(&intf->dev, "No union descriptor, using call management descriptor\n");
  797. /* quirks for Droids MuIn LCD */
  798. if (quirks & NO_DATA_INTERFACE)
  799. data_interface = usb_ifnum_to_if(usb_dev, 0);
  800. else
  801. data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
  802. control_interface = intf;
  803. } else {
  804. if (intf->cur_altsetting->desc.bNumEndpoints != 3) {
  805. dev_dbg(&intf->dev,"No union descriptor, giving up\n");
  806. return -ENODEV;
  807. } else {
  808. dev_warn(&intf->dev,"No union descriptor, testing for castrated device\n");
  809. combined_interfaces = 1;
  810. control_interface = data_interface = intf;
  811. goto look_for_collapsed_interface;
  812. }
  813. }
  814. } else {
  815. control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
  816. data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
  817. if (!control_interface || !data_interface) {
  818. dev_dbg(&intf->dev, "no interfaces\n");
  819. return -ENODEV;
  820. }
  821. }
  822. if (data_interface_num != call_interface_num)
  823. dev_dbg(&intf->dev, "Separate call control interface. That is not fully supported.\n");
  824. if (control_interface == data_interface) {
  825. /* some broken devices designed for windows work this way */
  826. dev_warn(&intf->dev,"Control and data interfaces are not separated!\n");
  827. combined_interfaces = 1;
  828. /* a popular other OS doesn't use it */
  829. quirks |= NO_CAP_LINE;
  830. if (data_interface->cur_altsetting->desc.bNumEndpoints != 3) {
  831. dev_err(&intf->dev, "This needs exactly 3 endpoints\n");
  832. return -EINVAL;
  833. }
  834. look_for_collapsed_interface:
  835. for (i = 0; i < 3; i++) {
  836. struct usb_endpoint_descriptor *ep;
  837. ep = &data_interface->cur_altsetting->endpoint[i].desc;
  838. if (usb_endpoint_is_int_in(ep))
  839. epctrl = ep;
  840. else if (usb_endpoint_is_bulk_out(ep))
  841. epwrite = ep;
  842. else if (usb_endpoint_is_bulk_in(ep))
  843. epread = ep;
  844. else
  845. return -EINVAL;
  846. }
  847. if (!epctrl || !epread || !epwrite)
  848. return -ENODEV;
  849. else
  850. goto made_compressed_probe;
  851. }
  852. skip_normal_probe:
  853. /*workaround for switched interfaces */
  854. if (data_interface->cur_altsetting->desc.bInterfaceClass
  855. != CDC_DATA_INTERFACE_TYPE) {
  856. if (control_interface->cur_altsetting->desc.bInterfaceClass
  857. == CDC_DATA_INTERFACE_TYPE) {
  858. struct usb_interface *t;
  859. dev_dbg(&intf->dev,
  860. "Your device has switched interfaces.\n");
  861. t = control_interface;
  862. control_interface = data_interface;
  863. data_interface = t;
  864. } else {
  865. return -EINVAL;
  866. }
  867. }
  868. /* Accept probe requests only for the control interface */
  869. if (!combined_interfaces && intf != control_interface)
  870. return -ENODEV;
  871. if (!combined_interfaces && usb_interface_claimed(data_interface)) {
  872. /* valid in this context */
  873. dev_dbg(&intf->dev, "The data interface isn't available\n");
  874. return -EBUSY;
  875. }
  876. if (data_interface->cur_altsetting->desc.bNumEndpoints < 2)
  877. return -EINVAL;
  878. epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
  879. epread = &data_interface->cur_altsetting->endpoint[0].desc;
  880. epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
  881. /* workaround for switched endpoints */
  882. if (!usb_endpoint_dir_in(epread)) {
  883. /* descriptors are swapped */
  884. struct usb_endpoint_descriptor *t;
  885. dev_dbg(&intf->dev,
  886. "The data interface has switched endpoints\n");
  887. t = epread;
  888. epread = epwrite;
  889. epwrite = t;
  890. }
  891. made_compressed_probe:
  892. dev_dbg(&intf->dev, "interfaces are valid\n");
  893. for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++);
  894. if (minor == ACM_TTY_MINORS) {
  895. dev_err(&intf->dev, "no more free acm devices\n");
  896. return -ENODEV;
  897. }
  898. acm = kzalloc(sizeof(struct acm), GFP_KERNEL);
  899. if (acm == NULL) {
  900. dev_err(&intf->dev, "out of memory (acm kzalloc)\n");
  901. goto alloc_fail;
  902. }
  903. ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize);
  904. readsize = le16_to_cpu(epread->wMaxPacketSize) *
  905. (quirks == SINGLE_RX_URB ? 1 : 2);
  906. acm->combined_interfaces = combined_interfaces;
  907. acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize) * 20;
  908. acm->control = control_interface;
  909. acm->data = data_interface;
  910. acm->minor = minor;
  911. acm->dev = usb_dev;
  912. acm->ctrl_caps = ac_management_function;
  913. if (quirks & NO_CAP_LINE)
  914. acm->ctrl_caps &= ~USB_CDC_CAP_LINE;
  915. acm->ctrlsize = ctrlsize;
  916. acm->readsize = readsize;
  917. acm->rx_buflimit = num_rx_buf;
  918. INIT_WORK(&acm->work, acm_softint);
  919. spin_lock_init(&acm->write_lock);
  920. spin_lock_init(&acm->read_lock);
  921. mutex_init(&acm->mutex);
  922. acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
  923. acm->is_int_ep = usb_endpoint_xfer_int(epread);
  924. if (acm->is_int_ep)
  925. acm->bInterval = epread->bInterval;
  926. tty_port_init(&acm->port);
  927. acm->port.ops = &acm_port_ops;
  928. buf = usb_alloc_coherent(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
  929. if (!buf) {
  930. dev_err(&intf->dev, "out of memory (ctrl buffer alloc)\n");
  931. goto alloc_fail2;
  932. }
  933. acm->ctrl_buffer = buf;
  934. if (acm_write_buffers_alloc(acm) < 0) {
  935. dev_err(&intf->dev, "out of memory (write buffer alloc)\n");
  936. goto alloc_fail4;
  937. }
  938. acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
  939. if (!acm->ctrlurb) {
  940. dev_err(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
  941. goto alloc_fail5;
  942. }
  943. for (i = 0; i < num_rx_buf; i++) {
  944. struct acm_rb *rb = &(acm->read_buffers[i]);
  945. struct urb *urb;
  946. rb->base = usb_alloc_coherent(acm->dev, readsize, GFP_KERNEL,
  947. &rb->dma);
  948. if (!rb->base) {
  949. dev_err(&intf->dev, "out of memory "
  950. "(read bufs usb_alloc_coherent)\n");
  951. goto alloc_fail6;
  952. }
  953. rb->index = i;
  954. rb->instance = acm;
  955. urb = usb_alloc_urb(0, GFP_KERNEL);
  956. if (!urb) {
  957. dev_err(&intf->dev,
  958. "out of memory (read urbs usb_alloc_urb)\n");
  959. goto alloc_fail6;
  960. }
  961. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  962. urb->transfer_dma = rb->dma;
  963. if (acm->is_int_ep) {
  964. usb_fill_int_urb(urb, acm->dev,
  965. acm->rx_endpoint,
  966. rb->base,
  967. acm->readsize,
  968. acm_read_bulk_callback, rb,
  969. acm->bInterval);
  970. } else {
  971. usb_fill_bulk_urb(urb, acm->dev,
  972. acm->rx_endpoint,
  973. rb->base,
  974. acm->readsize,
  975. acm_read_bulk_callback, rb);
  976. }
  977. acm->read_urbs[i] = urb;
  978. __set_bit(i, &acm->read_urbs_free);
  979. }
  980. for (i = 0; i < ACM_NW; i++) {
  981. struct acm_wb *snd = &(acm->wb[i]);
  982. snd->urb = usb_alloc_urb(0, GFP_KERNEL);
  983. if (snd->urb == NULL) {
  984. dev_err(&intf->dev,
  985. "out of memory (write urbs usb_alloc_urb)\n");
  986. goto alloc_fail7;
  987. }
  988. if (usb_endpoint_xfer_int(epwrite))
  989. usb_fill_int_urb(snd->urb, usb_dev,
  990. usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
  991. NULL, acm->writesize, acm_write_bulk, snd, epwrite->bInterval);
  992. else
  993. usb_fill_bulk_urb(snd->urb, usb_dev,
  994. usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
  995. NULL, acm->writesize, acm_write_bulk, snd);
  996. snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  997. snd->instance = acm;
  998. }
  999. usb_set_intfdata(intf, acm);
  1000. i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
  1001. if (i < 0)
  1002. goto alloc_fail7;
  1003. if (cfd) { /* export the country data */
  1004. acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
  1005. if (!acm->country_codes)
  1006. goto skip_countries;
  1007. acm->country_code_size = cfd->bLength - 4;
  1008. memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0,
  1009. cfd->bLength - 4);
  1010. acm->country_rel_date = cfd->iCountryCodeRelDate;
  1011. i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
  1012. if (i < 0) {
  1013. kfree(acm->country_codes);
  1014. goto skip_countries;
  1015. }
  1016. i = device_create_file(&intf->dev,
  1017. &dev_attr_iCountryCodeRelDate);
  1018. if (i < 0) {
  1019. device_remove_file(&intf->dev, &dev_attr_wCountryCodes);
  1020. kfree(acm->country_codes);
  1021. goto skip_countries;
  1022. }
  1023. }
  1024. skip_countries:
  1025. usb_fill_int_urb(acm->ctrlurb, usb_dev,
  1026. usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
  1027. acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm,
  1028. /* works around buggy devices */
  1029. epctrl->bInterval ? epctrl->bInterval : 0xff);
  1030. acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  1031. acm->ctrlurb->transfer_dma = acm->ctrl_dma;
  1032. dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
  1033. acm_set_control(acm, acm->ctrlout);
  1034. acm->line.dwDTERate = cpu_to_le32(9600);
  1035. acm->line.bDataBits = 8;
  1036. acm_set_line(acm, &acm->line);
  1037. usb_driver_claim_interface(&acm_driver, data_interface, acm);
  1038. usb_set_intfdata(data_interface, acm);
  1039. usb_get_intf(control_interface);
  1040. tty_register_device(acm_tty_driver, minor, &control_interface->dev);
  1041. acm_table[minor] = acm;
  1042. return 0;
  1043. alloc_fail7:
  1044. for (i = 0; i < ACM_NW; i++)
  1045. usb_free_urb(acm->wb[i].urb);
  1046. alloc_fail6:
  1047. for (i = 0; i < num_rx_buf; i++)
  1048. usb_free_urb(acm->read_urbs[i]);
  1049. acm_read_buffers_free(acm);
  1050. usb_free_urb(acm->ctrlurb);
  1051. alloc_fail5:
  1052. acm_write_buffers_free(acm);
  1053. alloc_fail4:
  1054. usb_free_coherent(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
  1055. alloc_fail2:
  1056. kfree(acm);
  1057. alloc_fail:
  1058. return -ENOMEM;
  1059. }
  1060. static void stop_data_traffic(struct acm *acm)
  1061. {
  1062. int i;
  1063. dev_dbg(&acm->control->dev, "%s\n", __func__);
  1064. usb_kill_urb(acm->ctrlurb);
  1065. for (i = 0; i < ACM_NW; i++)
  1066. usb_kill_urb(acm->wb[i].urb);
  1067. for (i = 0; i < acm->rx_buflimit; i++)
  1068. usb_kill_urb(acm->read_urbs[i]);
  1069. cancel_work_sync(&acm->work);
  1070. }
  1071. static void acm_disconnect(struct usb_interface *intf)
  1072. {
  1073. struct acm *acm = usb_get_intfdata(intf);
  1074. struct usb_device *usb_dev = interface_to_usbdev(intf);
  1075. struct tty_struct *tty;
  1076. /* sibling interface is already cleaning up */
  1077. if (!acm)
  1078. return;
  1079. mutex_lock(&open_mutex);
  1080. if (acm->country_codes) {
  1081. device_remove_file(&acm->control->dev,
  1082. &dev_attr_wCountryCodes);
  1083. device_remove_file(&acm->control->dev,
  1084. &dev_attr_iCountryCodeRelDate);
  1085. }
  1086. device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
  1087. acm->dev = NULL;
  1088. usb_set_intfdata(acm->control, NULL);
  1089. usb_set_intfdata(acm->data, NULL);
  1090. stop_data_traffic(acm);
  1091. acm_write_buffers_free(acm);
  1092. usb_free_coherent(usb_dev, acm->ctrlsize, acm->ctrl_buffer,
  1093. acm->ctrl_dma);
  1094. acm_read_buffers_free(acm);
  1095. if (!acm->combined_interfaces)
  1096. usb_driver_release_interface(&acm_driver, intf == acm->control ?
  1097. acm->data : acm->control);
  1098. if (acm->port.count == 0) {
  1099. acm_tty_unregister(acm);
  1100. mutex_unlock(&open_mutex);
  1101. return;
  1102. }
  1103. mutex_unlock(&open_mutex);
  1104. tty = tty_port_tty_get(&acm->port);
  1105. if (tty) {
  1106. tty_hangup(tty);
  1107. tty_kref_put(tty);
  1108. }
  1109. }
  1110. #ifdef CONFIG_PM
  1111. static int acm_suspend(struct usb_interface *intf, pm_message_t message)
  1112. {
  1113. struct acm *acm = usb_get_intfdata(intf);
  1114. int cnt;
  1115. if (message.event & PM_EVENT_AUTO) {
  1116. int b;
  1117. spin_lock_irq(&acm->write_lock);
  1118. b = acm->transmitting;
  1119. spin_unlock_irq(&acm->write_lock);
  1120. if (b)
  1121. return -EBUSY;
  1122. }
  1123. spin_lock_irq(&acm->read_lock);
  1124. spin_lock(&acm->write_lock);
  1125. cnt = acm->susp_count++;
  1126. spin_unlock(&acm->write_lock);
  1127. spin_unlock_irq(&acm->read_lock);
  1128. if (cnt)
  1129. return 0;
  1130. /*
  1131. we treat opened interfaces differently,
  1132. we must guard against open
  1133. */
  1134. mutex_lock(&acm->mutex);
  1135. if (acm->port.count)
  1136. stop_data_traffic(acm);
  1137. mutex_unlock(&acm->mutex);
  1138. return 0;
  1139. }
  1140. static int acm_resume(struct usb_interface *intf)
  1141. {
  1142. struct acm *acm = usb_get_intfdata(intf);
  1143. struct acm_wb *wb;
  1144. int rv = 0;
  1145. int cnt;
  1146. spin_lock_irq(&acm->read_lock);
  1147. acm->susp_count -= 1;
  1148. cnt = acm->susp_count;
  1149. spin_unlock_irq(&acm->read_lock);
  1150. if (cnt)
  1151. return 0;
  1152. mutex_lock(&acm->mutex);
  1153. if (acm->port.count) {
  1154. rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO);
  1155. spin_lock_irq(&acm->write_lock);
  1156. if (acm->delayed_wb) {
  1157. wb = acm->delayed_wb;
  1158. acm->delayed_wb = NULL;
  1159. spin_unlock_irq(&acm->write_lock);
  1160. acm_start_wb(acm, wb);
  1161. } else {
  1162. spin_unlock_irq(&acm->write_lock);
  1163. }
  1164. /*
  1165. * delayed error checking because we must
  1166. * do the write path at all cost
  1167. */
  1168. if (rv < 0)
  1169. goto err_out;
  1170. rv = acm_submit_read_urbs(acm, GFP_NOIO);
  1171. }
  1172. err_out:
  1173. mutex_unlock(&acm->mutex);
  1174. return rv;
  1175. }
  1176. static int acm_reset_resume(struct usb_interface *intf)
  1177. {
  1178. struct acm *acm = usb_get_intfdata(intf);
  1179. struct tty_struct *tty;
  1180. mutex_lock(&acm->mutex);
  1181. if (acm->port.count) {
  1182. tty = tty_port_tty_get(&acm->port);
  1183. if (tty) {
  1184. tty_hangup(tty);
  1185. tty_kref_put(tty);
  1186. }
  1187. }
  1188. mutex_unlock(&acm->mutex);
  1189. return acm_resume(intf);
  1190. }
  1191. #endif /* CONFIG_PM */
  1192. #define NOKIA_PCSUITE_ACM_INFO(x) \
  1193. USB_DEVICE_AND_INTERFACE_INFO(0x0421, x, \
  1194. USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
  1195. USB_CDC_ACM_PROTO_VENDOR)
  1196. #define SAMSUNG_PCSUITE_ACM_INFO(x) \
  1197. USB_DEVICE_AND_INTERFACE_INFO(0x04e7, x, \
  1198. USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
  1199. USB_CDC_ACM_PROTO_VENDOR)
  1200. /*
  1201. * USB driver structure.
  1202. */
  1203. static const struct usb_device_id acm_ids[] = {
  1204. /* quirky and broken devices */
  1205. { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
  1206. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1207. },
  1208. { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
  1209. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1210. },
  1211. { USB_DEVICE(0x0e8d, 0x3329), /* MediaTek Inc GPS */
  1212. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1213. },
  1214. { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
  1215. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1216. },
  1217. { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
  1218. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1219. },
  1220. { USB_DEVICE(0x0ace, 0x1602), /* ZyDAS 56K USB MODEM */
  1221. .driver_info = SINGLE_RX_URB,
  1222. },
  1223. { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
  1224. .driver_info = SINGLE_RX_URB, /* firmware bug */
  1225. },
  1226. { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
  1227. .driver_info = SINGLE_RX_URB, /* firmware bug */
  1228. },
  1229. { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
  1230. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1231. },
  1232. { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */
  1233. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1234. },
  1235. { USB_DEVICE(0x0572, 0x1321), /* Conexant USB MODEM CX93010 */
  1236. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1237. },
  1238. { USB_DEVICE(0x0572, 0x1324), /* Conexant USB MODEM RD02-D400 */
  1239. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1240. },
  1241. { USB_DEVICE(0x0572, 0x1328), /* Shiro / Aztech USB MODEM UM-3100 */
  1242. .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
  1243. },
  1244. { USB_DEVICE(0x22b8, 0x6425), /* Motorola MOTOMAGX phones */
  1245. },
  1246. { USB_DEVICE(0x0572, 0x1329), /* Hummingbird huc56s (Conexant) */
  1247. .driver_info = NO_UNION_NORMAL, /* union descriptor misplaced on
  1248. data interface instead of
  1249. communications interface.
  1250. Maybe we should define a new
  1251. quirk for this. */
  1252. },
  1253. { USB_DEVICE(0x1bbb, 0x0003), /* Alcatel OT-I650 */
  1254. .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
  1255. },
  1256. { USB_DEVICE(0x1576, 0x03b1), /* Maretron USB100 */
  1257. .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
  1258. },
  1259. /* Nokia S60 phones expose two ACM channels. The first is
  1260. * a modem and is picked up by the standard AT-command
  1261. * information below. The second is 'vendor-specific' but
  1262. * is treated as a serial device at the S60 end, so we want
  1263. * to expose it on Linux too. */
  1264. { NOKIA_PCSUITE_ACM_INFO(0x042D), }, /* Nokia 3250 */
  1265. { NOKIA_PCSUITE_ACM_INFO(0x04D8), }, /* Nokia 5500 Sport */
  1266. { NOKIA_PCSUITE_ACM_INFO(0x04C9), }, /* Nokia E50 */
  1267. { NOKIA_PCSUITE_ACM_INFO(0x0419), }, /* Nokia E60 */
  1268. { NOKIA_PCSUITE_ACM_INFO(0x044D), }, /* Nokia E61 */
  1269. { NOKIA_PCSUITE_ACM_INFO(0x0001), }, /* Nokia E61i */
  1270. { NOKIA_PCSUITE_ACM_INFO(0x0475), }, /* Nokia E62 */
  1271. { NOKIA_PCSUITE_ACM_INFO(0x0508), }, /* Nokia E65 */
  1272. { NOKIA_PCSUITE_ACM_INFO(0x0418), }, /* Nokia E70 */
  1273. { NOKIA_PCSUITE_ACM_INFO(0x0425), }, /* Nokia N71 */
  1274. { NOKIA_PCSUITE_ACM_INFO(0x0486), }, /* Nokia N73 */
  1275. { NOKIA_PCSUITE_ACM_INFO(0x04DF), }, /* Nokia N75 */
  1276. { NOKIA_PCSUITE_ACM_INFO(0x000e), }, /* Nokia N77 */
  1277. { NOKIA_PCSUITE_ACM_INFO(0x0445), }, /* Nokia N80 */
  1278. { NOKIA_PCSUITE_ACM_INFO(0x042F), }, /* Nokia N91 & N91 8GB */
  1279. { NOKIA_PCSUITE_ACM_INFO(0x048E), }, /* Nokia N92 */
  1280. { NOKIA_PCSUITE_ACM_INFO(0x0420), }, /* Nokia N93 */
  1281. { NOKIA_PCSUITE_ACM_INFO(0x04E6), }, /* Nokia N93i */
  1282. { NOKIA_PCSUITE_ACM_INFO(0x04B2), }, /* Nokia 5700 XpressMusic */
  1283. { NOKIA_PCSUITE_ACM_INFO(0x0134), }, /* Nokia 6110 Navigator (China) */
  1284. { NOKIA_PCSUITE_ACM_INFO(0x046E), }, /* Nokia 6110 Navigator */
  1285. { NOKIA_PCSUITE_ACM_INFO(0x002f), }, /* Nokia 6120 classic & */
  1286. { NOKIA_PCSUITE_ACM_INFO(0x0088), }, /* Nokia 6121 classic */
  1287. { NOKIA_PCSUITE_ACM_INFO(0x00fc), }, /* Nokia 6124 classic */
  1288. { NOKIA_PCSUITE_ACM_INFO(0x0042), }, /* Nokia E51 */
  1289. { NOKIA_PCSUITE_ACM_INFO(0x00b0), }, /* Nokia E66 */
  1290. { NOKIA_PCSUITE_ACM_INFO(0x00ab), }, /* Nokia E71 */
  1291. { NOKIA_PCSUITE_ACM_INFO(0x0481), }, /* Nokia N76 */
  1292. { NOKIA_PCSUITE_ACM_INFO(0x0007), }, /* Nokia N81 & N81 8GB */
  1293. { NOKIA_PCSUITE_ACM_INFO(0x0071), }, /* Nokia N82 */
  1294. { NOKIA_PCSUITE_ACM_INFO(0x04F0), }, /* Nokia N95 & N95-3 NAM */
  1295. { NOKIA_PCSUITE_ACM_INFO(0x0070), }, /* Nokia N95 8GB */
  1296. { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
  1297. { NOKIA_PCSUITE_ACM_INFO(0x0099), }, /* Nokia 6210 Navigator, RM-367 */
  1298. { NOKIA_PCSUITE_ACM_INFO(0x0128), }, /* Nokia 6210 Navigator, RM-419 */
  1299. { NOKIA_PCSUITE_ACM_INFO(0x008f), }, /* Nokia 6220 Classic */
  1300. { NOKIA_PCSUITE_ACM_INFO(0x00a0), }, /* Nokia 6650 */
  1301. { NOKIA_PCSUITE_ACM_INFO(0x007b), }, /* Nokia N78 */
  1302. { NOKIA_PCSUITE_ACM_INFO(0x0094), }, /* Nokia N85 */
  1303. { NOKIA_PCSUITE_ACM_INFO(0x003a), }, /* Nokia N96 & N96-3 */
  1304. { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
  1305. { NOKIA_PCSUITE_ACM_INFO(0x0108), }, /* Nokia 5320 XpressMusic 2G */
  1306. { NOKIA_PCSUITE_ACM_INFO(0x01f5), }, /* Nokia N97, RM-505 */
  1307. { NOKIA_PCSUITE_ACM_INFO(0x02e3), }, /* Nokia 5230, RM-588 */
  1308. { NOKIA_PCSUITE_ACM_INFO(0x0178), }, /* Nokia E63 */
  1309. { NOKIA_PCSUITE_ACM_INFO(0x010e), }, /* Nokia E75 */
  1310. { NOKIA_PCSUITE_ACM_INFO(0x02d9), }, /* Nokia 6760 Slide */
  1311. { NOKIA_PCSUITE_ACM_INFO(0x01d0), }, /* Nokia E52 */
  1312. { NOKIA_PCSUITE_ACM_INFO(0x0223), }, /* Nokia E72 */
  1313. { NOKIA_PCSUITE_ACM_INFO(0x0275), }, /* Nokia X6 */
  1314. { NOKIA_PCSUITE_ACM_INFO(0x026c), }, /* Nokia N97 Mini */
  1315. { NOKIA_PCSUITE_ACM_INFO(0x0154), }, /* Nokia 5800 XpressMusic */
  1316. { NOKIA_PCSUITE_ACM_INFO(0x04ce), }, /* Nokia E90 */
  1317. { NOKIA_PCSUITE_ACM_INFO(0x01d4), }, /* Nokia E55 */
  1318. { NOKIA_PCSUITE_ACM_INFO(0x0302), }, /* Nokia N8 */
  1319. { NOKIA_PCSUITE_ACM_INFO(0x0335), }, /* Nokia E7 */
  1320. { NOKIA_PCSUITE_ACM_INFO(0x03cd), }, /* Nokia C7 */
  1321. { SAMSUNG_PCSUITE_ACM_INFO(0x6651), }, /* Samsung GTi8510 (INNOV8) */
  1322. /* NOTE: non-Nokia COMM/ACM/0xff is likely MSFT RNDIS... NOT a modem! */
  1323. /* Support Lego NXT using pbLua firmware */
  1324. { USB_DEVICE(0x0694, 0xff00),
  1325. .driver_info = NOT_A_MODEM,
  1326. },
  1327. /* Support for Droids MuIn LCD */
  1328. { USB_DEVICE(0x04d8, 0x000b),
  1329. .driver_info = NO_DATA_INTERFACE,
  1330. },
  1331. /* control interfaces without any protocol set */
  1332. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1333. USB_CDC_PROTO_NONE) },
  1334. /* control interfaces with various AT-command sets */
  1335. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1336. USB_CDC_ACM_PROTO_AT_V25TER) },
  1337. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1338. USB_CDC_ACM_PROTO_AT_PCCA101) },
  1339. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1340. USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
  1341. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1342. USB_CDC_ACM_PROTO_AT_GSM) },
  1343. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1344. USB_CDC_ACM_PROTO_AT_3G) },
  1345. { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
  1346. USB_CDC_ACM_PROTO_AT_CDMA) },
  1347. { }
  1348. };
  1349. MODULE_DEVICE_TABLE(usb, acm_ids);
  1350. static struct usb_driver acm_driver = {
  1351. .name = "cdc_acm",
  1352. .probe = acm_probe,
  1353. .disconnect = acm_disconnect,
  1354. #ifdef CONFIG_PM
  1355. .suspend = acm_suspend,
  1356. .resume = acm_resume,
  1357. .reset_resume = acm_reset_resume,
  1358. #endif
  1359. .id_table = acm_ids,
  1360. #ifdef CONFIG_PM
  1361. .supports_autosuspend = 1,
  1362. #endif
  1363. };
  1364. /*
  1365. * TTY driver structures.
  1366. */
  1367. static const struct tty_operations acm_ops = {
  1368. .open = acm_tty_open,
  1369. .close = acm_tty_close,
  1370. .hangup = acm_tty_hangup,
  1371. .write = acm_tty_write,
  1372. .write_room = acm_tty_write_room,
  1373. .ioctl = acm_tty_ioctl,
  1374. .throttle = acm_tty_throttle,
  1375. .unthrottle = acm_tty_unthrottle,
  1376. .chars_in_buffer = acm_tty_chars_in_buffer,
  1377. .break_ctl = acm_tty_break_ctl,
  1378. .set_termios = acm_tty_set_termios,
  1379. .tiocmget = acm_tty_tiocmget,
  1380. .tiocmset = acm_tty_tiocmset,
  1381. };
  1382. /*
  1383. * Init / exit.
  1384. */
  1385. static int __init acm_init(void)
  1386. {
  1387. int retval;
  1388. acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
  1389. if (!acm_tty_driver)
  1390. return -ENOMEM;
  1391. acm_tty_driver->owner = THIS_MODULE,
  1392. acm_tty_driver->driver_name = "acm",
  1393. acm_tty_driver->name = "ttyACM",
  1394. acm_tty_driver->major = ACM_TTY_MAJOR,
  1395. acm_tty_driver->minor_start = 0,
  1396. acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
  1397. acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
  1398. acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  1399. acm_tty_driver->init_termios = tty_std_termios;
  1400. acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD |
  1401. HUPCL | CLOCAL;
  1402. tty_set_operations(acm_tty_driver, &acm_ops);
  1403. retval = tty_register_driver(acm_tty_driver);
  1404. if (retval) {
  1405. put_tty_driver(acm_tty_driver);
  1406. return retval;
  1407. }
  1408. retval = usb_register(&acm_driver);
  1409. if (retval) {
  1410. tty_unregister_driver(acm_tty_driver);
  1411. put_tty_driver(acm_tty_driver);
  1412. return retval;
  1413. }
  1414. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
  1415. return 0;
  1416. }
  1417. static void __exit acm_exit(void)
  1418. {
  1419. usb_deregister(&acm_driver);
  1420. tty_unregister_driver(acm_tty_driver);
  1421. put_tty_driver(acm_tty_driver);
  1422. }
  1423. module_init(acm_init);
  1424. module_exit(acm_exit);
  1425. MODULE_AUTHOR(DRIVER_AUTHOR);
  1426. MODULE_DESCRIPTION(DRIVER_DESC);
  1427. MODULE_LICENSE("GPL");
  1428. MODULE_ALIAS_CHARDEV_MAJOR(ACM_TTY_MAJOR);