hso.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836
  1. /******************************************************************************
  2. *
  3. * Driver for Option High Speed Mobile Devices.
  4. *
  5. * Copyright (C) 2008 Option International
  6. * Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd)
  7. * <ajb@spheresystems.co.uk>
  8. * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
  9. * Copyright (C) 2008 Novell, Inc.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  23. * USA
  24. *
  25. *
  26. *****************************************************************************/
  27. /******************************************************************************
  28. *
  29. * Description of the device:
  30. *
  31. * Interface 0: Contains the IP network interface on the bulk end points.
  32. * The multiplexed serial ports are using the interrupt and
  33. * control endpoints.
  34. * Interrupt contains a bitmap telling which multiplexed
  35. * serialport needs servicing.
  36. *
  37. * Interface 1: Diagnostics port, uses bulk only, do not submit urbs until the
  38. * port is opened, as this have a huge impact on the network port
  39. * throughput.
  40. *
  41. * Interface 2: Standard modem interface - circuit switched interface, should
  42. * not be used.
  43. *
  44. *****************************************************************************/
  45. #include <linux/sched.h>
  46. #include <linux/slab.h>
  47. #include <linux/init.h>
  48. #include <linux/delay.h>
  49. #include <linux/netdevice.h>
  50. #include <linux/module.h>
  51. #include <linux/ethtool.h>
  52. #include <linux/usb.h>
  53. #include <linux/timer.h>
  54. #include <linux/tty.h>
  55. #include <linux/tty_driver.h>
  56. #include <linux/tty_flip.h>
  57. #include <linux/kmod.h>
  58. #include <linux/rfkill.h>
  59. #include <linux/ip.h>
  60. #include <linux/uaccess.h>
  61. #include <linux/usb/cdc.h>
  62. #include <net/arp.h>
  63. #include <asm/byteorder.h>
  64. #define DRIVER_VERSION "1.2"
  65. #define MOD_AUTHOR "Option Wireless"
  66. #define MOD_DESCRIPTION "USB High Speed Option driver"
  67. #define MOD_LICENSE "GPL"
  68. #define HSO_MAX_NET_DEVICES 10
  69. #define HSO__MAX_MTU 2048
  70. #define DEFAULT_MTU 1500
  71. #define DEFAULT_MRU 1500
  72. #define CTRL_URB_RX_SIZE 1024
  73. #define CTRL_URB_TX_SIZE 64
  74. #define BULK_URB_RX_SIZE 4096
  75. #define BULK_URB_TX_SIZE 8192
  76. #define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
  77. #define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
  78. #define MUX_BULK_RX_BUF_COUNT 4
  79. #define USB_TYPE_OPTION_VENDOR 0x20
  80. /* These definitions are used with the struct hso_net flags element */
  81. /* - use *_bit operations on it. (bit indices not values.) */
  82. #define HSO_NET_RUNNING 0
  83. #define HSO_NET_TX_TIMEOUT (HZ*10)
  84. /* Serial port defines and structs. */
  85. #define HSO_SERIAL_FLAG_RX_SENT 0
  86. #define HSO_SERIAL_MAGIC 0x48534f31
  87. /* Number of ttys to handle */
  88. #define HSO_SERIAL_TTY_MINORS 256
  89. #define MAX_RX_URBS 2
  90. #define get_serial_by_tty(x) \
  91. (x ? (struct hso_serial *)x->driver_data : NULL)
  92. /*****************************************************************************/
  93. /* Debugging functions */
  94. /*****************************************************************************/
  95. #define D__(lvl_, fmt, arg...) \
  96. do { \
  97. printk(lvl_ "[%d:%s]: " fmt "\n", \
  98. __LINE__, __func__, ## arg); \
  99. } while (0)
  100. #define D_(lvl, args...) \
  101. do { \
  102. if (lvl & debug) \
  103. D__(KERN_INFO, args); \
  104. } while (0)
  105. #define D1(args...) D_(0x01, ##args)
  106. #define D2(args...) D_(0x02, ##args)
  107. #define D3(args...) D_(0x04, ##args)
  108. #define D4(args...) D_(0x08, ##args)
  109. #define D5(args...) D_(0x10, ##args)
  110. /*****************************************************************************/
  111. /* Enumerators */
  112. /*****************************************************************************/
  113. enum pkt_parse_state {
  114. WAIT_IP,
  115. WAIT_DATA,
  116. WAIT_SYNC
  117. };
  118. /*****************************************************************************/
  119. /* Structs */
  120. /*****************************************************************************/
  121. struct hso_shared_int {
  122. struct usb_endpoint_descriptor *intr_endp;
  123. void *shared_intr_buf;
  124. struct urb *shared_intr_urb;
  125. struct usb_device *usb;
  126. int use_count;
  127. int ref_count;
  128. struct mutex shared_int_lock;
  129. };
  130. struct hso_net {
  131. struct hso_device *parent;
  132. struct net_device *net;
  133. struct rfkill *rfkill;
  134. struct usb_endpoint_descriptor *in_endp;
  135. struct usb_endpoint_descriptor *out_endp;
  136. struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
  137. struct urb *mux_bulk_tx_urb;
  138. void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
  139. void *mux_bulk_tx_buf;
  140. struct sk_buff *skb_rx_buf;
  141. struct sk_buff *skb_tx_buf;
  142. enum pkt_parse_state rx_parse_state;
  143. spinlock_t net_lock;
  144. unsigned short rx_buf_size;
  145. unsigned short rx_buf_missing;
  146. struct iphdr rx_ip_hdr;
  147. unsigned long flags;
  148. };
  149. struct hso_serial {
  150. struct hso_device *parent;
  151. int magic;
  152. u8 minor;
  153. struct hso_shared_int *shared_int;
  154. /* rx/tx urb could be either a bulk urb or a control urb depending
  155. on which serial port it is used on. */
  156. struct urb *rx_urb[MAX_RX_URBS];
  157. u8 num_rx_urbs;
  158. u8 *rx_data[MAX_RX_URBS];
  159. u16 rx_data_length; /* should contain allocated length */
  160. struct urb *tx_urb;
  161. u8 *tx_data;
  162. u8 *tx_buffer;
  163. u16 tx_data_length; /* should contain allocated length */
  164. u16 tx_data_count;
  165. u16 tx_buffer_count;
  166. struct usb_ctrlrequest ctrl_req_tx;
  167. struct usb_ctrlrequest ctrl_req_rx;
  168. struct usb_endpoint_descriptor *in_endp;
  169. struct usb_endpoint_descriptor *out_endp;
  170. unsigned long flags;
  171. u8 rts_state;
  172. u8 dtr_state;
  173. unsigned tx_urb_used:1;
  174. /* from usb_serial_port */
  175. struct tty_struct *tty;
  176. int open_count;
  177. spinlock_t serial_lock;
  178. int (*write_data) (struct hso_serial *serial);
  179. };
  180. struct hso_device {
  181. union {
  182. struct hso_serial *dev_serial;
  183. struct hso_net *dev_net;
  184. } port_data;
  185. u32 port_spec;
  186. u8 is_active;
  187. u8 usb_gone;
  188. struct work_struct async_get_intf;
  189. struct work_struct async_put_intf;
  190. struct usb_device *usb;
  191. struct usb_interface *interface;
  192. struct device *dev;
  193. struct kref ref;
  194. struct mutex mutex;
  195. };
  196. /* Type of interface */
  197. #define HSO_INTF_MASK 0xFF00
  198. #define HSO_INTF_MUX 0x0100
  199. #define HSO_INTF_BULK 0x0200
  200. /* Type of port */
  201. #define HSO_PORT_MASK 0xFF
  202. #define HSO_PORT_NO_PORT 0x0
  203. #define HSO_PORT_CONTROL 0x1
  204. #define HSO_PORT_APP 0x2
  205. #define HSO_PORT_GPS 0x3
  206. #define HSO_PORT_PCSC 0x4
  207. #define HSO_PORT_APP2 0x5
  208. #define HSO_PORT_GPS_CONTROL 0x6
  209. #define HSO_PORT_MSD 0x7
  210. #define HSO_PORT_VOICE 0x8
  211. #define HSO_PORT_DIAG2 0x9
  212. #define HSO_PORT_DIAG 0x10
  213. #define HSO_PORT_MODEM 0x11
  214. #define HSO_PORT_NETWORK 0x12
  215. /* Additional device info */
  216. #define HSO_INFO_MASK 0xFF000000
  217. #define HSO_INFO_CRC_BUG 0x01000000
  218. /*****************************************************************************/
  219. /* Prototypes */
  220. /*****************************************************************************/
  221. /* Serial driver functions */
  222. static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
  223. unsigned int set, unsigned int clear);
  224. static void ctrl_callback(struct urb *urb);
  225. static void put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
  226. static void hso_kick_transmit(struct hso_serial *serial);
  227. /* Helper functions */
  228. static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
  229. struct usb_device *usb, gfp_t gfp);
  230. static void log_usb_status(int status, const char *function);
  231. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  232. int type, int dir);
  233. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
  234. static void hso_free_interface(struct usb_interface *intf);
  235. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
  236. static int hso_stop_serial_device(struct hso_device *hso_dev);
  237. static int hso_start_net_device(struct hso_device *hso_dev);
  238. static void hso_free_shared_int(struct hso_shared_int *shared_int);
  239. static int hso_stop_net_device(struct hso_device *hso_dev);
  240. static void hso_serial_ref_free(struct kref *ref);
  241. static void async_get_intf(struct work_struct *data);
  242. static void async_put_intf(struct work_struct *data);
  243. static int hso_put_activity(struct hso_device *hso_dev);
  244. static int hso_get_activity(struct hso_device *hso_dev);
  245. /*****************************************************************************/
  246. /* Helping functions */
  247. /*****************************************************************************/
  248. /* #define DEBUG */
  249. #define dev2net(x) (x->port_data.dev_net)
  250. #define dev2ser(x) (x->port_data.dev_serial)
  251. /* Debugging functions */
  252. #ifdef DEBUG
  253. static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
  254. unsigned int len)
  255. {
  256. u8 i = 0;
  257. printk(KERN_DEBUG "[%d:%s]: len %d", line_count, func_name, len);
  258. for (i = 0; i < len; i++) {
  259. if (!(i % 16))
  260. printk("\n 0x%03x: ", i);
  261. printk("%02x ", (unsigned char)buf[i]);
  262. }
  263. printk("\n");
  264. }
  265. #define DUMP(buf_, len_) \
  266. dbg_dump(__LINE__, __func__, buf_, len_)
  267. #define DUMP1(buf_, len_) \
  268. do { \
  269. if (0x01 & debug) \
  270. DUMP(buf_, len_); \
  271. } while (0)
  272. #else
  273. #define DUMP(buf_, len_)
  274. #define DUMP1(buf_, len_)
  275. #endif
  276. /* module parameters */
  277. static int debug;
  278. static int tty_major;
  279. static int disable_net;
  280. /* driver info */
  281. static const char driver_name[] = "hso";
  282. static const char tty_filename[] = "ttyHS";
  283. static const char *version = __FILE__ ": " DRIVER_VERSION " " MOD_AUTHOR;
  284. /* the usb driver itself (registered in hso_init) */
  285. static struct usb_driver hso_driver;
  286. /* serial structures */
  287. static struct tty_driver *tty_drv;
  288. static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
  289. static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
  290. static spinlock_t serial_table_lock;
  291. static struct ktermios *hso_serial_termios[HSO_SERIAL_TTY_MINORS];
  292. static struct ktermios *hso_serial_termios_locked[HSO_SERIAL_TTY_MINORS];
  293. static const s32 default_port_spec[] = {
  294. HSO_INTF_MUX | HSO_PORT_NETWORK,
  295. HSO_INTF_BULK | HSO_PORT_DIAG,
  296. HSO_INTF_BULK | HSO_PORT_MODEM,
  297. 0
  298. };
  299. static const s32 icon321_port_spec[] = {
  300. HSO_INTF_MUX | HSO_PORT_NETWORK,
  301. HSO_INTF_BULK | HSO_PORT_DIAG2,
  302. HSO_INTF_BULK | HSO_PORT_MODEM,
  303. HSO_INTF_BULK | HSO_PORT_DIAG,
  304. 0
  305. };
  306. #define default_port_device(vendor, product) \
  307. USB_DEVICE(vendor, product), \
  308. .driver_info = (kernel_ulong_t)default_port_spec
  309. #define icon321_port_device(vendor, product) \
  310. USB_DEVICE(vendor, product), \
  311. .driver_info = (kernel_ulong_t)icon321_port_spec
  312. /* list of devices we support */
  313. static const struct usb_device_id hso_ids[] = {
  314. {default_port_device(0x0af0, 0x6711)},
  315. {default_port_device(0x0af0, 0x6731)},
  316. {default_port_device(0x0af0, 0x6751)},
  317. {default_port_device(0x0af0, 0x6771)},
  318. {default_port_device(0x0af0, 0x6791)},
  319. {default_port_device(0x0af0, 0x6811)},
  320. {default_port_device(0x0af0, 0x6911)},
  321. {default_port_device(0x0af0, 0x6951)},
  322. {default_port_device(0x0af0, 0x6971)},
  323. {default_port_device(0x0af0, 0x7011)},
  324. {default_port_device(0x0af0, 0x7031)},
  325. {default_port_device(0x0af0, 0x7051)},
  326. {default_port_device(0x0af0, 0x7071)},
  327. {default_port_device(0x0af0, 0x7111)},
  328. {default_port_device(0x0af0, 0x7211)},
  329. {default_port_device(0x0af0, 0x7251)},
  330. {default_port_device(0x0af0, 0x7271)},
  331. {default_port_device(0x0af0, 0x7311)},
  332. {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
  333. {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
  334. {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
  335. {default_port_device(0x0af0, 0xd033)}, /* Icon-322 */
  336. {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
  337. {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
  338. {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
  339. {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
  340. {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
  341. {}
  342. };
  343. MODULE_DEVICE_TABLE(usb, hso_ids);
  344. /* Sysfs attribute */
  345. static ssize_t hso_sysfs_show_porttype(struct device *dev,
  346. struct device_attribute *attr,
  347. char *buf)
  348. {
  349. struct hso_device *hso_dev = dev->driver_data;
  350. char *port_name;
  351. if (!hso_dev)
  352. return 0;
  353. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  354. case HSO_PORT_CONTROL:
  355. port_name = "Control";
  356. break;
  357. case HSO_PORT_APP:
  358. port_name = "Application";
  359. break;
  360. case HSO_PORT_APP2:
  361. port_name = "Application2";
  362. break;
  363. case HSO_PORT_GPS:
  364. port_name = "GPS";
  365. break;
  366. case HSO_PORT_GPS_CONTROL:
  367. port_name = "GPS Control";
  368. break;
  369. case HSO_PORT_PCSC:
  370. port_name = "PCSC";
  371. break;
  372. case HSO_PORT_DIAG:
  373. port_name = "Diagnostic";
  374. break;
  375. case HSO_PORT_DIAG2:
  376. port_name = "Diagnostic2";
  377. break;
  378. case HSO_PORT_MODEM:
  379. port_name = "Modem";
  380. break;
  381. case HSO_PORT_NETWORK:
  382. port_name = "Network";
  383. break;
  384. default:
  385. port_name = "Unknown";
  386. break;
  387. }
  388. return sprintf(buf, "%s\n", port_name);
  389. }
  390. static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
  391. /* converts mux value to a port spec value */
  392. static u32 hso_mux_to_port(int mux)
  393. {
  394. u32 result;
  395. switch (mux) {
  396. case 0x1:
  397. result = HSO_PORT_CONTROL;
  398. break;
  399. case 0x2:
  400. result = HSO_PORT_APP;
  401. break;
  402. case 0x4:
  403. result = HSO_PORT_PCSC;
  404. break;
  405. case 0x8:
  406. result = HSO_PORT_GPS;
  407. break;
  408. case 0x10:
  409. result = HSO_PORT_APP2;
  410. break;
  411. default:
  412. result = HSO_PORT_NO_PORT;
  413. }
  414. return result;
  415. }
  416. /* converts port spec value to a mux value */
  417. static u32 hso_port_to_mux(int port)
  418. {
  419. u32 result;
  420. switch (port & HSO_PORT_MASK) {
  421. case HSO_PORT_CONTROL:
  422. result = 0x0;
  423. break;
  424. case HSO_PORT_APP:
  425. result = 0x1;
  426. break;
  427. case HSO_PORT_PCSC:
  428. result = 0x2;
  429. break;
  430. case HSO_PORT_GPS:
  431. result = 0x3;
  432. break;
  433. case HSO_PORT_APP2:
  434. result = 0x4;
  435. break;
  436. default:
  437. result = 0x0;
  438. }
  439. return result;
  440. }
  441. static struct hso_serial *get_serial_by_shared_int_and_type(
  442. struct hso_shared_int *shared_int,
  443. int mux)
  444. {
  445. int i, port;
  446. port = hso_mux_to_port(mux);
  447. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  448. if (serial_table[i]
  449. && (dev2ser(serial_table[i])->shared_int == shared_int)
  450. && ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
  451. return dev2ser(serial_table[i]);
  452. }
  453. }
  454. return NULL;
  455. }
  456. static struct hso_serial *get_serial_by_index(unsigned index)
  457. {
  458. struct hso_serial *serial;
  459. unsigned long flags;
  460. if (!serial_table[index])
  461. return NULL;
  462. spin_lock_irqsave(&serial_table_lock, flags);
  463. serial = dev2ser(serial_table[index]);
  464. spin_unlock_irqrestore(&serial_table_lock, flags);
  465. return serial;
  466. }
  467. static int get_free_serial_index(void)
  468. {
  469. int index;
  470. unsigned long flags;
  471. spin_lock_irqsave(&serial_table_lock, flags);
  472. for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
  473. if (serial_table[index] == NULL) {
  474. spin_unlock_irqrestore(&serial_table_lock, flags);
  475. return index;
  476. }
  477. }
  478. spin_unlock_irqrestore(&serial_table_lock, flags);
  479. printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
  480. return -1;
  481. }
  482. static void set_serial_by_index(unsigned index, struct hso_serial *serial)
  483. {
  484. unsigned long flags;
  485. spin_lock_irqsave(&serial_table_lock, flags);
  486. if (serial)
  487. serial_table[index] = serial->parent;
  488. else
  489. serial_table[index] = NULL;
  490. spin_unlock_irqrestore(&serial_table_lock, flags);
  491. }
  492. /* log a meaningfull explanation of an USB status */
  493. static void log_usb_status(int status, const char *function)
  494. {
  495. char *explanation;
  496. switch (status) {
  497. case -ENODEV:
  498. explanation = "no device";
  499. break;
  500. case -ENOENT:
  501. explanation = "endpoint not enabled";
  502. break;
  503. case -EPIPE:
  504. explanation = "endpoint stalled";
  505. break;
  506. case -ENOSPC:
  507. explanation = "not enough bandwidth";
  508. break;
  509. case -ESHUTDOWN:
  510. explanation = "device disabled";
  511. break;
  512. case -EHOSTUNREACH:
  513. explanation = "device suspended";
  514. break;
  515. case -EINVAL:
  516. case -EAGAIN:
  517. case -EFBIG:
  518. case -EMSGSIZE:
  519. explanation = "internal error";
  520. break;
  521. default:
  522. explanation = "unknown status";
  523. break;
  524. }
  525. D1("%s: received USB status - %s (%d)", function, explanation, status);
  526. }
  527. /* Network interface functions */
  528. /* called when net interface is brought up by ifconfig */
  529. static int hso_net_open(struct net_device *net)
  530. {
  531. struct hso_net *odev = netdev_priv(net);
  532. unsigned long flags = 0;
  533. if (!odev) {
  534. dev_err(&net->dev, "No net device !\n");
  535. return -ENODEV;
  536. }
  537. odev->skb_tx_buf = NULL;
  538. /* setup environment */
  539. spin_lock_irqsave(&odev->net_lock, flags);
  540. odev->rx_parse_state = WAIT_IP;
  541. odev->rx_buf_size = 0;
  542. odev->rx_buf_missing = sizeof(struct iphdr);
  543. spin_unlock_irqrestore(&odev->net_lock, flags);
  544. hso_start_net_device(odev->parent);
  545. /* We are up and running. */
  546. set_bit(HSO_NET_RUNNING, &odev->flags);
  547. /* Tell the kernel we are ready to start receiving from it */
  548. netif_start_queue(net);
  549. return 0;
  550. }
  551. /* called when interface is brought down by ifconfig */
  552. static int hso_net_close(struct net_device *net)
  553. {
  554. struct hso_net *odev = netdev_priv(net);
  555. /* we don't need the queue anymore */
  556. netif_stop_queue(net);
  557. /* no longer running */
  558. clear_bit(HSO_NET_RUNNING, &odev->flags);
  559. hso_stop_net_device(odev->parent);
  560. /* done */
  561. return 0;
  562. }
  563. /* USB tells is xmit done, we should start the netqueue again */
  564. static void write_bulk_callback(struct urb *urb)
  565. {
  566. struct hso_net *odev = urb->context;
  567. int status = urb->status;
  568. /* Sanity check */
  569. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  570. dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
  571. return;
  572. }
  573. /* Do we still have a valid kernel network device? */
  574. if (!netif_device_present(odev->net)) {
  575. dev_err(&urb->dev->dev, "%s: net device not present\n",
  576. __func__);
  577. return;
  578. }
  579. /* log status, but don't act on it, we don't need to resubmit anything
  580. * anyhow */
  581. if (status)
  582. log_usb_status(status, __func__);
  583. hso_put_activity(odev->parent);
  584. /* Tell the network interface we are ready for another frame */
  585. netif_wake_queue(odev->net);
  586. }
  587. /* called by kernel when we need to transmit a packet */
  588. static int hso_net_start_xmit(struct sk_buff *skb, struct net_device *net)
  589. {
  590. struct hso_net *odev = netdev_priv(net);
  591. int result;
  592. /* Tell the kernel, "No more frames 'til we are done with this one." */
  593. netif_stop_queue(net);
  594. if (hso_get_activity(odev->parent) == -EAGAIN) {
  595. odev->skb_tx_buf = skb;
  596. return 0;
  597. }
  598. /* log if asked */
  599. DUMP1(skb->data, skb->len);
  600. /* Copy it from kernel memory to OUR memory */
  601. memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
  602. D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
  603. /* Fill in the URB for shipping it out. */
  604. usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
  605. odev->parent->usb,
  606. usb_sndbulkpipe(odev->parent->usb,
  607. odev->out_endp->
  608. bEndpointAddress & 0x7F),
  609. odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
  610. odev);
  611. /* Deal with the Zero Length packet problem, I hope */
  612. odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
  613. /* Send the URB on its merry way. */
  614. result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
  615. if (result) {
  616. dev_warn(&odev->parent->interface->dev,
  617. "failed mux_bulk_tx_urb %d", result);
  618. net->stats.tx_errors++;
  619. netif_start_queue(net);
  620. } else {
  621. net->stats.tx_packets++;
  622. net->stats.tx_bytes += skb->len;
  623. /* And tell the kernel when the last transmit started. */
  624. net->trans_start = jiffies;
  625. }
  626. dev_kfree_skb(skb);
  627. /* we're done */
  628. return result;
  629. }
  630. static void hso_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
  631. {
  632. struct hso_net *odev = netdev_priv(net);
  633. strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
  634. strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
  635. usb_make_path(odev->parent->usb, info->bus_info, sizeof info->bus_info);
  636. }
  637. static struct ethtool_ops ops = {
  638. .get_drvinfo = hso_get_drvinfo,
  639. .get_link = ethtool_op_get_link
  640. };
  641. /* called when a packet did not ack after watchdogtimeout */
  642. static void hso_net_tx_timeout(struct net_device *net)
  643. {
  644. struct hso_net *odev = netdev_priv(net);
  645. if (!odev)
  646. return;
  647. /* Tell syslog we are hosed. */
  648. dev_warn(&net->dev, "Tx timed out.\n");
  649. /* Tear the waiting frame off the list */
  650. if (odev->mux_bulk_tx_urb
  651. && (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
  652. usb_unlink_urb(odev->mux_bulk_tx_urb);
  653. /* Update statistics */
  654. net->stats.tx_errors++;
  655. }
  656. /* make a real packet from the received USB buffer */
  657. static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
  658. unsigned int count, unsigned char is_eop)
  659. {
  660. unsigned short temp_bytes;
  661. unsigned short buffer_offset = 0;
  662. unsigned short frame_len;
  663. unsigned char *tmp_rx_buf;
  664. /* log if needed */
  665. D1("Rx %d bytes", count);
  666. DUMP(ip_pkt, min(128, (int)count));
  667. while (count) {
  668. switch (odev->rx_parse_state) {
  669. case WAIT_IP:
  670. /* waiting for IP header. */
  671. /* wanted bytes - size of ip header */
  672. temp_bytes =
  673. (count <
  674. odev->rx_buf_missing) ? count : odev->
  675. rx_buf_missing;
  676. memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
  677. odev->rx_buf_size, ip_pkt + buffer_offset,
  678. temp_bytes);
  679. odev->rx_buf_size += temp_bytes;
  680. buffer_offset += temp_bytes;
  681. odev->rx_buf_missing -= temp_bytes;
  682. count -= temp_bytes;
  683. if (!odev->rx_buf_missing) {
  684. /* header is complete allocate an sk_buffer and
  685. * continue to WAIT_DATA */
  686. frame_len = ntohs(odev->rx_ip_hdr.tot_len);
  687. if ((frame_len > DEFAULT_MRU) ||
  688. (frame_len < sizeof(struct iphdr))) {
  689. dev_err(&odev->net->dev,
  690. "Invalid frame (%d) length\n",
  691. frame_len);
  692. odev->rx_parse_state = WAIT_SYNC;
  693. continue;
  694. }
  695. /* Allocate an sk_buff */
  696. odev->skb_rx_buf = dev_alloc_skb(frame_len);
  697. if (!odev->skb_rx_buf) {
  698. /* We got no receive buffer. */
  699. D1("could not allocate memory");
  700. odev->rx_parse_state = WAIT_SYNC;
  701. return;
  702. }
  703. /* Here's where it came from */
  704. odev->skb_rx_buf->dev = odev->net;
  705. /* Copy what we got so far. make room for iphdr
  706. * after tail. */
  707. tmp_rx_buf =
  708. skb_put(odev->skb_rx_buf,
  709. sizeof(struct iphdr));
  710. memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
  711. sizeof(struct iphdr));
  712. /* ETH_HLEN */
  713. odev->rx_buf_size = sizeof(struct iphdr);
  714. /* Filip actually use .tot_len */
  715. odev->rx_buf_missing =
  716. frame_len - sizeof(struct iphdr);
  717. odev->rx_parse_state = WAIT_DATA;
  718. }
  719. break;
  720. case WAIT_DATA:
  721. temp_bytes = (count < odev->rx_buf_missing)
  722. ? count : odev->rx_buf_missing;
  723. /* Copy the rest of the bytes that are left in the
  724. * buffer into the waiting sk_buf. */
  725. /* Make room for temp_bytes after tail. */
  726. tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
  727. memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
  728. odev->rx_buf_missing -= temp_bytes;
  729. count -= temp_bytes;
  730. buffer_offset += temp_bytes;
  731. odev->rx_buf_size += temp_bytes;
  732. if (!odev->rx_buf_missing) {
  733. /* Packet is complete. Inject into stack. */
  734. /* We have IP packet here */
  735. odev->skb_rx_buf->protocol =
  736. __constant_htons(ETH_P_IP);
  737. /* don't check it */
  738. odev->skb_rx_buf->ip_summed =
  739. CHECKSUM_UNNECESSARY;
  740. skb_reset_mac_header(odev->skb_rx_buf);
  741. /* Ship it off to the kernel */
  742. netif_rx(odev->skb_rx_buf);
  743. /* No longer our buffer. */
  744. odev->skb_rx_buf = NULL;
  745. /* update out statistics */
  746. odev->net->stats.rx_packets++;
  747. odev->net->stats.rx_bytes += odev->rx_buf_size;
  748. odev->rx_buf_size = 0;
  749. odev->rx_buf_missing = sizeof(struct iphdr);
  750. odev->rx_parse_state = WAIT_IP;
  751. }
  752. break;
  753. case WAIT_SYNC:
  754. D1(" W_S");
  755. count = 0;
  756. break;
  757. default:
  758. D1(" ");
  759. count--;
  760. break;
  761. }
  762. }
  763. /* Recovery mechanism for WAIT_SYNC state. */
  764. if (is_eop) {
  765. if (odev->rx_parse_state == WAIT_SYNC) {
  766. odev->rx_parse_state = WAIT_IP;
  767. odev->rx_buf_size = 0;
  768. odev->rx_buf_missing = sizeof(struct iphdr);
  769. }
  770. }
  771. }
  772. /* Moving data from usb to kernel (in interrupt state) */
  773. static void read_bulk_callback(struct urb *urb)
  774. {
  775. struct hso_net *odev = urb->context;
  776. struct net_device *net;
  777. int result;
  778. int status = urb->status;
  779. /* is al ok? (Filip: Who's Al ?) */
  780. if (status) {
  781. log_usb_status(status, __func__);
  782. return;
  783. }
  784. /* Sanity check */
  785. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  786. D1("BULK IN callback but driver is not active!");
  787. return;
  788. }
  789. usb_mark_last_busy(urb->dev);
  790. net = odev->net;
  791. if (!netif_device_present(net)) {
  792. /* Somebody killed our network interface... */
  793. return;
  794. }
  795. if (odev->parent->port_spec & HSO_INFO_CRC_BUG) {
  796. u32 rest;
  797. u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  798. rest = urb->actual_length % odev->in_endp->wMaxPacketSize;
  799. if (((rest == 5) || (rest == 6))
  800. && !memcmp(((u8 *) urb->transfer_buffer) +
  801. urb->actual_length - 4, crc_check, 4)) {
  802. urb->actual_length -= 4;
  803. }
  804. }
  805. /* do we even have a packet? */
  806. if (urb->actual_length) {
  807. /* Handle the IP stream, add header and push it onto network
  808. * stack if the packet is complete. */
  809. spin_lock(&odev->net_lock);
  810. packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
  811. (urb->transfer_buffer_length >
  812. urb->actual_length) ? 1 : 0);
  813. spin_unlock(&odev->net_lock);
  814. }
  815. /* We are done with this URB, resubmit it. Prep the USB to wait for
  816. * another frame. Reuse same as received. */
  817. usb_fill_bulk_urb(urb,
  818. odev->parent->usb,
  819. usb_rcvbulkpipe(odev->parent->usb,
  820. odev->in_endp->
  821. bEndpointAddress & 0x7F),
  822. urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
  823. read_bulk_callback, odev);
  824. /* Give this to the USB subsystem so it can tell us when more data
  825. * arrives. */
  826. result = usb_submit_urb(urb, GFP_ATOMIC);
  827. if (result)
  828. dev_warn(&odev->parent->interface->dev,
  829. "%s failed submit mux_bulk_rx_urb %d", __func__,
  830. result);
  831. }
  832. /* Serial driver functions */
  833. static void _hso_serial_set_termios(struct tty_struct *tty,
  834. struct ktermios *old)
  835. {
  836. struct hso_serial *serial = get_serial_by_tty(tty);
  837. struct ktermios *termios;
  838. if ((!tty) || (!tty->termios) || (!serial)) {
  839. printk(KERN_ERR "%s: no tty structures", __func__);
  840. return;
  841. }
  842. D4("port %d", serial->minor);
  843. /*
  844. * The default requirements for this device are:
  845. */
  846. termios = tty->termios;
  847. termios->c_iflag &=
  848. ~(IGNBRK /* disable ignore break */
  849. | BRKINT /* disable break causes interrupt */
  850. | PARMRK /* disable mark parity errors */
  851. | ISTRIP /* disable clear high bit of input characters */
  852. | INLCR /* disable translate NL to CR */
  853. | IGNCR /* disable ignore CR */
  854. | ICRNL /* disable translate CR to NL */
  855. | IXON); /* disable enable XON/XOFF flow control */
  856. /* disable postprocess output characters */
  857. termios->c_oflag &= ~OPOST;
  858. termios->c_lflag &=
  859. ~(ECHO /* disable echo input characters */
  860. | ECHONL /* disable echo new line */
  861. | ICANON /* disable erase, kill, werase, and rprnt
  862. special characters */
  863. | ISIG /* disable interrupt, quit, and suspend special
  864. characters */
  865. | IEXTEN); /* disable non-POSIX special characters */
  866. termios->c_cflag &=
  867. ~(CSIZE /* no size */
  868. | PARENB /* disable parity bit */
  869. | CBAUD /* clear current baud rate */
  870. | CBAUDEX); /* clear current buad rate */
  871. termios->c_cflag |= CS8; /* character size 8 bits */
  872. /* baud rate 115200 */
  873. tty_encode_baud_rate(serial->tty, 115200, 115200);
  874. /*
  875. * Force low_latency on; otherwise the pushes are scheduled;
  876. * this is bad as it opens up the possibility of dropping bytes
  877. * on the floor. We don't want to drop bytes on the floor. :)
  878. */
  879. serial->tty->low_latency = 1;
  880. return;
  881. }
  882. /* open the requested serial port */
  883. static int hso_serial_open(struct tty_struct *tty, struct file *filp)
  884. {
  885. struct hso_serial *serial = get_serial_by_index(tty->index);
  886. int result;
  887. /* sanity check */
  888. if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
  889. tty->driver_data = NULL;
  890. D1("Failed to open port");
  891. return -ENODEV;
  892. }
  893. mutex_lock(&serial->parent->mutex);
  894. result = usb_autopm_get_interface(serial->parent->interface);
  895. if (result < 0)
  896. goto err_out;
  897. D1("Opening %d", serial->minor);
  898. kref_get(&serial->parent->ref);
  899. /* setup */
  900. tty->driver_data = serial;
  901. serial->tty = tty;
  902. /* check for port allready opened, if not set the termios */
  903. serial->open_count++;
  904. if (serial->open_count == 1) {
  905. tty->low_latency = 1;
  906. serial->flags = 0;
  907. /* Force default termio settings */
  908. _hso_serial_set_termios(tty, NULL);
  909. result = hso_start_serial_device(serial->parent, GFP_KERNEL);
  910. if (result) {
  911. hso_stop_serial_device(serial->parent);
  912. serial->open_count--;
  913. kref_put(&serial->parent->ref, hso_serial_ref_free);
  914. }
  915. } else {
  916. D1("Port was already open");
  917. }
  918. usb_autopm_put_interface(serial->parent->interface);
  919. /* done */
  920. if (result)
  921. hso_serial_tiocmset(tty, NULL, TIOCM_RTS | TIOCM_DTR, 0);
  922. err_out:
  923. mutex_unlock(&serial->parent->mutex);
  924. return result;
  925. }
  926. /* close the requested serial port */
  927. static void hso_serial_close(struct tty_struct *tty, struct file *filp)
  928. {
  929. struct hso_serial *serial = tty->driver_data;
  930. u8 usb_gone;
  931. D1("Closing serial port");
  932. mutex_lock(&serial->parent->mutex);
  933. usb_gone = serial->parent->usb_gone;
  934. if (!usb_gone)
  935. usb_autopm_get_interface(serial->parent->interface);
  936. /* reset the rts and dtr */
  937. /* do the actual close */
  938. serial->open_count--;
  939. if (serial->open_count <= 0) {
  940. kref_put(&serial->parent->ref, hso_serial_ref_free);
  941. serial->open_count = 0;
  942. if (serial->tty) {
  943. serial->tty->driver_data = NULL;
  944. serial->tty = NULL;
  945. }
  946. if (!usb_gone)
  947. hso_stop_serial_device(serial->parent);
  948. }
  949. if (!usb_gone)
  950. usb_autopm_put_interface(serial->parent->interface);
  951. mutex_unlock(&serial->parent->mutex);
  952. }
  953. /* close the requested serial port */
  954. static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
  955. int count)
  956. {
  957. struct hso_serial *serial = get_serial_by_tty(tty);
  958. int space, tx_bytes;
  959. unsigned long flags;
  960. /* sanity check */
  961. if (serial == NULL) {
  962. printk(KERN_ERR "%s: serial is NULL\n", __func__);
  963. return -ENODEV;
  964. }
  965. spin_lock_irqsave(&serial->serial_lock, flags);
  966. space = serial->tx_data_length - serial->tx_buffer_count;
  967. tx_bytes = (count < space) ? count : space;
  968. if (!tx_bytes)
  969. goto out;
  970. memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
  971. serial->tx_buffer_count += tx_bytes;
  972. out:
  973. spin_unlock_irqrestore(&serial->serial_lock, flags);
  974. hso_kick_transmit(serial);
  975. /* done */
  976. return tx_bytes;
  977. }
  978. /* how much room is there for writing */
  979. static int hso_serial_write_room(struct tty_struct *tty)
  980. {
  981. struct hso_serial *serial = get_serial_by_tty(tty);
  982. int room;
  983. unsigned long flags;
  984. spin_lock_irqsave(&serial->serial_lock, flags);
  985. room = serial->tx_data_length - serial->tx_buffer_count;
  986. spin_unlock_irqrestore(&serial->serial_lock, flags);
  987. /* return free room */
  988. return room;
  989. }
  990. /* setup the term */
  991. static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
  992. {
  993. struct hso_serial *serial = get_serial_by_tty(tty);
  994. unsigned long flags;
  995. if (old)
  996. D5("Termios called with: cflags new[%d] - old[%d]",
  997. tty->termios->c_cflag, old->c_cflag);
  998. /* the actual setup */
  999. spin_lock_irqsave(&serial->serial_lock, flags);
  1000. if (serial->open_count)
  1001. _hso_serial_set_termios(tty, old);
  1002. else
  1003. tty->termios = old;
  1004. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1005. /* done */
  1006. return;
  1007. }
  1008. /* how many characters in the buffer */
  1009. static int hso_serial_chars_in_buffer(struct tty_struct *tty)
  1010. {
  1011. struct hso_serial *serial = get_serial_by_tty(tty);
  1012. int chars;
  1013. unsigned long flags;
  1014. /* sanity check */
  1015. if (serial == NULL)
  1016. return 0;
  1017. spin_lock_irqsave(&serial->serial_lock, flags);
  1018. chars = serial->tx_buffer_count;
  1019. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1020. return chars;
  1021. }
  1022. static int hso_serial_tiocmget(struct tty_struct *tty, struct file *file)
  1023. {
  1024. unsigned int value;
  1025. struct hso_serial *serial = get_serial_by_tty(tty);
  1026. unsigned long flags;
  1027. /* sanity check */
  1028. if (!serial) {
  1029. D1("no tty structures");
  1030. return -EINVAL;
  1031. }
  1032. spin_lock_irqsave(&serial->serial_lock, flags);
  1033. value = ((serial->rts_state) ? TIOCM_RTS : 0) |
  1034. ((serial->dtr_state) ? TIOCM_DTR : 0);
  1035. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1036. return value;
  1037. }
  1038. static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
  1039. unsigned int set, unsigned int clear)
  1040. {
  1041. int val = 0;
  1042. unsigned long flags;
  1043. int if_num;
  1044. struct hso_serial *serial = get_serial_by_tty(tty);
  1045. /* sanity check */
  1046. if (!serial) {
  1047. D1("no tty structures");
  1048. return -EINVAL;
  1049. }
  1050. if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
  1051. spin_lock_irqsave(&serial->serial_lock, flags);
  1052. if (set & TIOCM_RTS)
  1053. serial->rts_state = 1;
  1054. if (set & TIOCM_DTR)
  1055. serial->dtr_state = 1;
  1056. if (clear & TIOCM_RTS)
  1057. serial->rts_state = 0;
  1058. if (clear & TIOCM_DTR)
  1059. serial->dtr_state = 0;
  1060. if (serial->dtr_state)
  1061. val |= 0x01;
  1062. if (serial->rts_state)
  1063. val |= 0x02;
  1064. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1065. return usb_control_msg(serial->parent->usb,
  1066. usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
  1067. 0x21, val, if_num, NULL, 0,
  1068. USB_CTRL_SET_TIMEOUT);
  1069. }
  1070. /* starts a transmit */
  1071. static void hso_kick_transmit(struct hso_serial *serial)
  1072. {
  1073. u8 *temp;
  1074. unsigned long flags;
  1075. int res;
  1076. spin_lock_irqsave(&serial->serial_lock, flags);
  1077. if (!serial->tx_buffer_count)
  1078. goto out;
  1079. if (serial->tx_urb_used)
  1080. goto out;
  1081. /* Wakeup USB interface if necessary */
  1082. if (hso_get_activity(serial->parent) == -EAGAIN)
  1083. goto out;
  1084. /* Switch pointers around to avoid memcpy */
  1085. temp = serial->tx_buffer;
  1086. serial->tx_buffer = serial->tx_data;
  1087. serial->tx_data = temp;
  1088. serial->tx_data_count = serial->tx_buffer_count;
  1089. serial->tx_buffer_count = 0;
  1090. /* If temp is set, it means we switched buffers */
  1091. if (temp && serial->write_data) {
  1092. res = serial->write_data(serial);
  1093. if (res >= 0)
  1094. serial->tx_urb_used = 1;
  1095. }
  1096. out:
  1097. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1098. }
  1099. /* make a request (for reading and writing data to muxed serial port) */
  1100. static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
  1101. struct urb *ctrl_urb,
  1102. struct usb_ctrlrequest *ctrl_req,
  1103. u8 *ctrl_urb_data, u32 size)
  1104. {
  1105. int result;
  1106. int pipe;
  1107. /* Sanity check */
  1108. if (!serial || !ctrl_urb || !ctrl_req) {
  1109. printk(KERN_ERR "%s: Wrong arguments\n", __func__);
  1110. return -EINVAL;
  1111. }
  1112. /* initialize */
  1113. ctrl_req->wValue = 0;
  1114. ctrl_req->wIndex = hso_port_to_mux(port);
  1115. ctrl_req->wLength = size;
  1116. if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
  1117. /* Reading command */
  1118. ctrl_req->bRequestType = USB_DIR_IN |
  1119. USB_TYPE_OPTION_VENDOR |
  1120. USB_RECIP_INTERFACE;
  1121. ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
  1122. pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
  1123. } else {
  1124. /* Writing command */
  1125. ctrl_req->bRequestType = USB_DIR_OUT |
  1126. USB_TYPE_OPTION_VENDOR |
  1127. USB_RECIP_INTERFACE;
  1128. ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
  1129. pipe = usb_sndctrlpipe(serial->parent->usb, 0);
  1130. }
  1131. /* syslog */
  1132. D2("%s command (%02x) len: %d, port: %d",
  1133. type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
  1134. ctrl_req->bRequestType, ctrl_req->wLength, port);
  1135. /* Load ctrl urb */
  1136. ctrl_urb->transfer_flags = 0;
  1137. usb_fill_control_urb(ctrl_urb,
  1138. serial->parent->usb,
  1139. pipe,
  1140. (u8 *) ctrl_req,
  1141. ctrl_urb_data, size, ctrl_callback, serial);
  1142. /* Send it on merry way */
  1143. result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
  1144. if (result) {
  1145. dev_err(&ctrl_urb->dev->dev,
  1146. "%s failed submit ctrl_urb %d type %d", __func__,
  1147. result, type);
  1148. return result;
  1149. }
  1150. /* done */
  1151. return size;
  1152. }
  1153. /* called by intr_callback when read occurs */
  1154. static int hso_mux_serial_read(struct hso_serial *serial)
  1155. {
  1156. if (!serial)
  1157. return -EINVAL;
  1158. /* clean data */
  1159. memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
  1160. /* make the request */
  1161. if (serial->num_rx_urbs != 1) {
  1162. dev_err(&serial->parent->interface->dev,
  1163. "ERROR: mux'd reads with multiple buffers "
  1164. "not possible\n");
  1165. return 0;
  1166. }
  1167. return mux_device_request(serial,
  1168. USB_CDC_GET_ENCAPSULATED_RESPONSE,
  1169. serial->parent->port_spec & HSO_PORT_MASK,
  1170. serial->rx_urb[0],
  1171. &serial->ctrl_req_rx,
  1172. serial->rx_data[0], serial->rx_data_length);
  1173. }
  1174. /* used for muxed serial port callback (muxed serial read) */
  1175. static void intr_callback(struct urb *urb)
  1176. {
  1177. struct hso_shared_int *shared_int = urb->context;
  1178. struct hso_serial *serial;
  1179. unsigned char *port_req;
  1180. int status = urb->status;
  1181. int i;
  1182. usb_mark_last_busy(urb->dev);
  1183. /* sanity check */
  1184. if (!shared_int)
  1185. return;
  1186. /* status check */
  1187. if (status) {
  1188. log_usb_status(status, __func__);
  1189. return;
  1190. }
  1191. D4("\n--- Got intr callback 0x%02X ---", status);
  1192. /* what request? */
  1193. port_req = urb->transfer_buffer;
  1194. D4(" port_req = 0x%.2X\n", *port_req);
  1195. /* loop over all muxed ports to find the one sending this */
  1196. for (i = 0; i < 8; i++) {
  1197. /* max 8 channels on MUX */
  1198. if (*port_req & (1 << i)) {
  1199. serial = get_serial_by_shared_int_and_type(shared_int,
  1200. (1 << i));
  1201. if (serial != NULL) {
  1202. D1("Pending read interrupt on port %d\n", i);
  1203. if (!test_and_set_bit(HSO_SERIAL_FLAG_RX_SENT,
  1204. &serial->flags)) {
  1205. /* Setup and send a ctrl req read on
  1206. * port i */
  1207. hso_mux_serial_read(serial);
  1208. } else {
  1209. D1("Already pending a read on "
  1210. "port %d\n", i);
  1211. }
  1212. }
  1213. }
  1214. }
  1215. /* Resubmit interrupt urb */
  1216. hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
  1217. }
  1218. /* called for writing to muxed serial port */
  1219. static int hso_mux_serial_write_data(struct hso_serial *serial)
  1220. {
  1221. if (NULL == serial)
  1222. return -EINVAL;
  1223. return mux_device_request(serial,
  1224. USB_CDC_SEND_ENCAPSULATED_COMMAND,
  1225. serial->parent->port_spec & HSO_PORT_MASK,
  1226. serial->tx_urb,
  1227. &serial->ctrl_req_tx,
  1228. serial->tx_data, serial->tx_data_count);
  1229. }
  1230. /* write callback for Diag and CS port */
  1231. static void hso_std_serial_write_bulk_callback(struct urb *urb)
  1232. {
  1233. struct hso_serial *serial = urb->context;
  1234. int status = urb->status;
  1235. /* sanity check */
  1236. if (!serial) {
  1237. D1("serial == NULL");
  1238. return;
  1239. }
  1240. spin_lock(&serial->serial_lock);
  1241. serial->tx_urb_used = 0;
  1242. spin_unlock(&serial->serial_lock);
  1243. if (status) {
  1244. log_usb_status(status, __func__);
  1245. return;
  1246. }
  1247. hso_put_activity(serial->parent);
  1248. tty_wakeup(serial->tty);
  1249. hso_kick_transmit(serial);
  1250. D1(" ");
  1251. return;
  1252. }
  1253. /* called for writing diag or CS serial port */
  1254. static int hso_std_serial_write_data(struct hso_serial *serial)
  1255. {
  1256. int count = serial->tx_data_count;
  1257. int result;
  1258. usb_fill_bulk_urb(serial->tx_urb,
  1259. serial->parent->usb,
  1260. usb_sndbulkpipe(serial->parent->usb,
  1261. serial->out_endp->
  1262. bEndpointAddress & 0x7F),
  1263. serial->tx_data, serial->tx_data_count,
  1264. hso_std_serial_write_bulk_callback, serial);
  1265. result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
  1266. if (result) {
  1267. dev_warn(&serial->parent->usb->dev,
  1268. "Failed to submit urb - res %d\n", result);
  1269. return result;
  1270. }
  1271. return count;
  1272. }
  1273. /* callback after read or write on muxed serial port */
  1274. static void ctrl_callback(struct urb *urb)
  1275. {
  1276. struct hso_serial *serial = urb->context;
  1277. struct usb_ctrlrequest *req;
  1278. int status = urb->status;
  1279. /* sanity check */
  1280. if (!serial)
  1281. return;
  1282. spin_lock(&serial->serial_lock);
  1283. serial->tx_urb_used = 0;
  1284. spin_unlock(&serial->serial_lock);
  1285. if (status) {
  1286. log_usb_status(status, __func__);
  1287. return;
  1288. }
  1289. /* what request? */
  1290. req = (struct usb_ctrlrequest *)(urb->setup_packet);
  1291. D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
  1292. D4("Actual length of urb = %d\n", urb->actual_length);
  1293. DUMP1(urb->transfer_buffer, urb->actual_length);
  1294. if (req->bRequestType ==
  1295. (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
  1296. /* response to a read command */
  1297. if (serial->open_count > 0) {
  1298. /* handle RX data the normal way */
  1299. put_rxbuf_data(urb, serial);
  1300. }
  1301. /* Re issue a read as long as we receive data. */
  1302. if (urb->actual_length != 0)
  1303. hso_mux_serial_read(serial);
  1304. else
  1305. clear_bit(HSO_SERIAL_FLAG_RX_SENT, &serial->flags);
  1306. } else {
  1307. hso_put_activity(serial->parent);
  1308. tty_wakeup(serial->tty);
  1309. /* response to a write command */
  1310. hso_kick_transmit(serial);
  1311. }
  1312. }
  1313. /* handle RX data for serial port */
  1314. static void put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
  1315. {
  1316. struct tty_struct *tty = serial->tty;
  1317. /* Sanity check */
  1318. if (urb == NULL || serial == NULL) {
  1319. D1("serial = NULL");
  1320. return;
  1321. }
  1322. /* Push data to tty */
  1323. if (tty && urb->actual_length) {
  1324. D1("data to push to tty");
  1325. tty_insert_flip_string(tty, urb->transfer_buffer,
  1326. urb->actual_length);
  1327. tty_flip_buffer_push(tty);
  1328. }
  1329. }
  1330. /* read callback for Diag and CS port */
  1331. static void hso_std_serial_read_bulk_callback(struct urb *urb)
  1332. {
  1333. struct hso_serial *serial = urb->context;
  1334. int result;
  1335. int status = urb->status;
  1336. /* sanity check */
  1337. if (!serial) {
  1338. D1("serial == NULL");
  1339. return;
  1340. } else if (status) {
  1341. log_usb_status(status, __func__);
  1342. return;
  1343. }
  1344. D4("\n--- Got serial_read_bulk callback %02x ---", status);
  1345. D1("Actual length = %d\n", urb->actual_length);
  1346. DUMP1(urb->transfer_buffer, urb->actual_length);
  1347. /* Anyone listening? */
  1348. if (serial->open_count == 0)
  1349. return;
  1350. if (status == 0) {
  1351. if (serial->parent->port_spec & HSO_INFO_CRC_BUG) {
  1352. u32 rest;
  1353. u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  1354. rest =
  1355. urb->actual_length %
  1356. serial->in_endp->wMaxPacketSize;
  1357. if (((rest == 5) || (rest == 6))
  1358. && !memcmp(((u8 *) urb->transfer_buffer) +
  1359. urb->actual_length - 4, crc_check, 4)) {
  1360. urb->actual_length -= 4;
  1361. }
  1362. }
  1363. /* Valid data, handle RX data */
  1364. put_rxbuf_data(urb, serial);
  1365. } else if (status == -ENOENT || status == -ECONNRESET) {
  1366. /* Unlinked - check for throttled port. */
  1367. D2("Port %d, successfully unlinked urb", serial->minor);
  1368. } else {
  1369. D2("Port %d, status = %d for read urb", serial->minor, status);
  1370. return;
  1371. }
  1372. usb_mark_last_busy(urb->dev);
  1373. /* We are done with this URB, resubmit it. Prep the USB to wait for
  1374. * another frame */
  1375. usb_fill_bulk_urb(urb, serial->parent->usb,
  1376. usb_rcvbulkpipe(serial->parent->usb,
  1377. serial->in_endp->
  1378. bEndpointAddress & 0x7F),
  1379. urb->transfer_buffer, serial->rx_data_length,
  1380. hso_std_serial_read_bulk_callback, serial);
  1381. /* Give this to the USB subsystem so it can tell us when more data
  1382. * arrives. */
  1383. result = usb_submit_urb(urb, GFP_ATOMIC);
  1384. if (result) {
  1385. dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d",
  1386. __func__, result);
  1387. }
  1388. }
  1389. /* Base driver functions */
  1390. static void hso_log_port(struct hso_device *hso_dev)
  1391. {
  1392. char *port_type;
  1393. char port_dev[20];
  1394. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  1395. case HSO_PORT_CONTROL:
  1396. port_type = "Control";
  1397. break;
  1398. case HSO_PORT_APP:
  1399. port_type = "Application";
  1400. break;
  1401. case HSO_PORT_GPS:
  1402. port_type = "GPS";
  1403. break;
  1404. case HSO_PORT_GPS_CONTROL:
  1405. port_type = "GPS control";
  1406. break;
  1407. case HSO_PORT_APP2:
  1408. port_type = "Application2";
  1409. break;
  1410. case HSO_PORT_PCSC:
  1411. port_type = "PCSC";
  1412. break;
  1413. case HSO_PORT_DIAG:
  1414. port_type = "Diagnostic";
  1415. break;
  1416. case HSO_PORT_DIAG2:
  1417. port_type = "Diagnostic2";
  1418. break;
  1419. case HSO_PORT_MODEM:
  1420. port_type = "Modem";
  1421. break;
  1422. case HSO_PORT_NETWORK:
  1423. port_type = "Network";
  1424. break;
  1425. default:
  1426. port_type = "Unknown";
  1427. break;
  1428. }
  1429. if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  1430. sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
  1431. } else
  1432. sprintf(port_dev, "/dev/%s%d", tty_filename,
  1433. dev2ser(hso_dev)->minor);
  1434. dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
  1435. port_type, port_dev);
  1436. }
  1437. static int hso_start_net_device(struct hso_device *hso_dev)
  1438. {
  1439. int i, result = 0;
  1440. struct hso_net *hso_net = dev2net(hso_dev);
  1441. if (!hso_net)
  1442. return -ENODEV;
  1443. /* send URBs for all read buffers */
  1444. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1445. /* Prep a receive URB */
  1446. usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1447. hso_dev->usb,
  1448. usb_rcvbulkpipe(hso_dev->usb,
  1449. hso_net->in_endp->
  1450. bEndpointAddress & 0x7F),
  1451. hso_net->mux_bulk_rx_buf_pool[i],
  1452. MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
  1453. hso_net);
  1454. /* Put it out there so the device can send us stuff */
  1455. result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1456. GFP_NOIO);
  1457. if (result)
  1458. dev_warn(&hso_dev->usb->dev,
  1459. "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
  1460. i, result);
  1461. }
  1462. return result;
  1463. }
  1464. static int hso_stop_net_device(struct hso_device *hso_dev)
  1465. {
  1466. int i;
  1467. struct hso_net *hso_net = dev2net(hso_dev);
  1468. if (!hso_net)
  1469. return -ENODEV;
  1470. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1471. if (hso_net->mux_bulk_rx_urb_pool[i])
  1472. usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  1473. }
  1474. if (hso_net->mux_bulk_tx_urb)
  1475. usb_kill_urb(hso_net->mux_bulk_tx_urb);
  1476. return 0;
  1477. }
  1478. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
  1479. {
  1480. int i, result = 0;
  1481. struct hso_serial *serial = dev2ser(hso_dev);
  1482. if (!serial)
  1483. return -ENODEV;
  1484. /* If it is not the MUX port fill in and submit a bulk urb (already
  1485. * allocated in hso_serial_start) */
  1486. if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
  1487. for (i = 0; i < serial->num_rx_urbs; i++) {
  1488. usb_fill_bulk_urb(serial->rx_urb[i],
  1489. serial->parent->usb,
  1490. usb_rcvbulkpipe(serial->parent->usb,
  1491. serial->in_endp->
  1492. bEndpointAddress &
  1493. 0x7F),
  1494. serial->rx_data[i],
  1495. serial->rx_data_length,
  1496. hso_std_serial_read_bulk_callback,
  1497. serial);
  1498. result = usb_submit_urb(serial->rx_urb[i], flags);
  1499. if (result) {
  1500. dev_warn(&serial->parent->usb->dev,
  1501. "Failed to submit urb - res %d\n",
  1502. result);
  1503. break;
  1504. }
  1505. }
  1506. } else {
  1507. mutex_lock(&serial->shared_int->shared_int_lock);
  1508. if (!serial->shared_int->use_count) {
  1509. result =
  1510. hso_mux_submit_intr_urb(serial->shared_int,
  1511. hso_dev->usb, flags);
  1512. }
  1513. serial->shared_int->use_count++;
  1514. mutex_unlock(&serial->shared_int->shared_int_lock);
  1515. }
  1516. return result;
  1517. }
  1518. static int hso_stop_serial_device(struct hso_device *hso_dev)
  1519. {
  1520. int i;
  1521. struct hso_serial *serial = dev2ser(hso_dev);
  1522. if (!serial)
  1523. return -ENODEV;
  1524. for (i = 0; i < serial->num_rx_urbs; i++) {
  1525. if (serial->rx_urb[i])
  1526. usb_kill_urb(serial->rx_urb[i]);
  1527. }
  1528. if (serial->tx_urb)
  1529. usb_kill_urb(serial->tx_urb);
  1530. if (serial->shared_int) {
  1531. mutex_lock(&serial->shared_int->shared_int_lock);
  1532. if (serial->shared_int->use_count &&
  1533. (--serial->shared_int->use_count == 0)) {
  1534. struct urb *urb;
  1535. urb = serial->shared_int->shared_intr_urb;
  1536. if (urb)
  1537. usb_kill_urb(urb);
  1538. }
  1539. mutex_unlock(&serial->shared_int->shared_int_lock);
  1540. }
  1541. return 0;
  1542. }
  1543. static void hso_serial_common_free(struct hso_serial *serial)
  1544. {
  1545. int i;
  1546. if (serial->parent->dev)
  1547. device_remove_file(serial->parent->dev, &dev_attr_hsotype);
  1548. tty_unregister_device(tty_drv, serial->minor);
  1549. for (i = 0; i < serial->num_rx_urbs; i++) {
  1550. /* unlink and free RX URB */
  1551. usb_free_urb(serial->rx_urb[i]);
  1552. /* free the RX buffer */
  1553. kfree(serial->rx_data[i]);
  1554. }
  1555. /* unlink and free TX URB */
  1556. usb_free_urb(serial->tx_urb);
  1557. kfree(serial->tx_data);
  1558. }
  1559. static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
  1560. int rx_size, int tx_size)
  1561. {
  1562. struct device *dev;
  1563. int minor;
  1564. int i;
  1565. minor = get_free_serial_index();
  1566. if (minor < 0)
  1567. goto exit;
  1568. /* register our minor number */
  1569. serial->parent->dev = tty_register_device(tty_drv, minor,
  1570. &serial->parent->interface->dev);
  1571. dev = serial->parent->dev;
  1572. dev->driver_data = serial->parent;
  1573. i = device_create_file(dev, &dev_attr_hsotype);
  1574. /* fill in specific data for later use */
  1575. serial->minor = minor;
  1576. serial->magic = HSO_SERIAL_MAGIC;
  1577. spin_lock_init(&serial->serial_lock);
  1578. serial->num_rx_urbs = num_urbs;
  1579. /* RX, allocate urb and initialize */
  1580. /* prepare our RX buffer */
  1581. serial->rx_data_length = rx_size;
  1582. for (i = 0; i < serial->num_rx_urbs; i++) {
  1583. serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
  1584. if (!serial->rx_urb[i]) {
  1585. dev_err(dev, "Could not allocate urb?\n");
  1586. goto exit;
  1587. }
  1588. serial->rx_urb[i]->transfer_buffer = NULL;
  1589. serial->rx_urb[i]->transfer_buffer_length = 0;
  1590. serial->rx_data[i] = kzalloc(serial->rx_data_length,
  1591. GFP_KERNEL);
  1592. if (!serial->rx_data[i]) {
  1593. dev_err(dev, "%s - Out of memory\n", __func__);
  1594. goto exit;
  1595. }
  1596. }
  1597. /* TX, allocate urb and initialize */
  1598. serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  1599. if (!serial->tx_urb) {
  1600. dev_err(dev, "Could not allocate urb?\n");
  1601. goto exit;
  1602. }
  1603. serial->tx_urb->transfer_buffer = NULL;
  1604. serial->tx_urb->transfer_buffer_length = 0;
  1605. /* prepare our TX buffer */
  1606. serial->tx_data_count = 0;
  1607. serial->tx_buffer_count = 0;
  1608. serial->tx_data_length = tx_size;
  1609. serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
  1610. if (!serial->tx_data) {
  1611. dev_err(dev, "%s - Out of memory", __func__);
  1612. goto exit;
  1613. }
  1614. serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
  1615. if (!serial->tx_buffer) {
  1616. dev_err(dev, "%s - Out of memory", __func__);
  1617. goto exit;
  1618. }
  1619. return 0;
  1620. exit:
  1621. hso_serial_common_free(serial);
  1622. return -1;
  1623. }
  1624. /* Frees a general hso device */
  1625. static void hso_free_device(struct hso_device *hso_dev)
  1626. {
  1627. kfree(hso_dev);
  1628. }
  1629. /* Creates a general hso device */
  1630. static struct hso_device *hso_create_device(struct usb_interface *intf,
  1631. int port_spec)
  1632. {
  1633. struct hso_device *hso_dev;
  1634. hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
  1635. if (!hso_dev)
  1636. return NULL;
  1637. hso_dev->port_spec = port_spec;
  1638. hso_dev->usb = interface_to_usbdev(intf);
  1639. hso_dev->interface = intf;
  1640. kref_init(&hso_dev->ref);
  1641. mutex_init(&hso_dev->mutex);
  1642. INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
  1643. INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
  1644. return hso_dev;
  1645. }
  1646. /* Removes a network device in the network device table */
  1647. static int remove_net_device(struct hso_device *hso_dev)
  1648. {
  1649. int i;
  1650. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  1651. if (network_table[i] == hso_dev) {
  1652. network_table[i] = NULL;
  1653. break;
  1654. }
  1655. }
  1656. if (i == HSO_MAX_NET_DEVICES)
  1657. return -1;
  1658. return 0;
  1659. }
  1660. /* Frees our network device */
  1661. static void hso_free_net_device(struct hso_device *hso_dev)
  1662. {
  1663. int i;
  1664. struct hso_net *hso_net = dev2net(hso_dev);
  1665. if (!hso_net)
  1666. return;
  1667. /* start freeing */
  1668. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1669. usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  1670. kfree(hso_net->mux_bulk_rx_buf_pool[i]);
  1671. }
  1672. usb_free_urb(hso_net->mux_bulk_tx_urb);
  1673. kfree(hso_net->mux_bulk_tx_buf);
  1674. remove_net_device(hso_net->parent);
  1675. if (hso_net->net) {
  1676. unregister_netdev(hso_net->net);
  1677. free_netdev(hso_net->net);
  1678. }
  1679. hso_free_device(hso_dev);
  1680. }
  1681. /* initialize the network interface */
  1682. static void hso_net_init(struct net_device *net)
  1683. {
  1684. struct hso_net *hso_net = netdev_priv(net);
  1685. D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
  1686. /* fill in the other fields */
  1687. net->open = hso_net_open;
  1688. net->stop = hso_net_close;
  1689. net->hard_start_xmit = hso_net_start_xmit;
  1690. net->tx_timeout = hso_net_tx_timeout;
  1691. net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
  1692. net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  1693. net->type = ARPHRD_NONE;
  1694. net->mtu = DEFAULT_MTU - 14;
  1695. net->tx_queue_len = 10;
  1696. SET_ETHTOOL_OPS(net, &ops);
  1697. /* and initialize the semaphore */
  1698. spin_lock_init(&hso_net->net_lock);
  1699. }
  1700. /* Adds a network device in the network device table */
  1701. static int add_net_device(struct hso_device *hso_dev)
  1702. {
  1703. int i;
  1704. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  1705. if (network_table[i] == NULL) {
  1706. network_table[i] = hso_dev;
  1707. break;
  1708. }
  1709. }
  1710. if (i == HSO_MAX_NET_DEVICES)
  1711. return -1;
  1712. return 0;
  1713. }
  1714. static int hso_radio_toggle(void *data, enum rfkill_state state)
  1715. {
  1716. struct hso_device *hso_dev = data;
  1717. int enabled = (state == RFKILL_STATE_ON);
  1718. int rv;
  1719. mutex_lock(&hso_dev->mutex);
  1720. if (hso_dev->usb_gone)
  1721. rv = 0;
  1722. else
  1723. rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
  1724. enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
  1725. USB_CTRL_SET_TIMEOUT);
  1726. mutex_unlock(&hso_dev->mutex);
  1727. return rv;
  1728. }
  1729. /* Creates and sets up everything for rfkill */
  1730. static void hso_create_rfkill(struct hso_device *hso_dev,
  1731. struct usb_interface *interface)
  1732. {
  1733. struct hso_net *hso_net = dev2net(hso_dev);
  1734. struct device *dev = hso_dev->dev;
  1735. char *rfkn;
  1736. hso_net->rfkill = rfkill_allocate(&interface_to_usbdev(interface)->dev,
  1737. RFKILL_TYPE_WLAN);
  1738. if (!hso_net->rfkill) {
  1739. dev_err(dev, "%s - Out of memory", __func__);
  1740. return;
  1741. }
  1742. rfkn = kzalloc(20, GFP_KERNEL);
  1743. if (!rfkn) {
  1744. rfkill_free(hso_net->rfkill);
  1745. dev_err(dev, "%s - Out of memory", __func__);
  1746. return;
  1747. }
  1748. snprintf(rfkn, 20, "hso-%d",
  1749. interface->altsetting->desc.bInterfaceNumber);
  1750. hso_net->rfkill->name = rfkn;
  1751. hso_net->rfkill->state = RFKILL_STATE_ON;
  1752. hso_net->rfkill->data = hso_dev;
  1753. hso_net->rfkill->toggle_radio = hso_radio_toggle;
  1754. if (rfkill_register(hso_net->rfkill) < 0) {
  1755. kfree(rfkn);
  1756. hso_net->rfkill->name = NULL;
  1757. rfkill_free(hso_net->rfkill);
  1758. dev_err(dev, "%s - Failed to register rfkill", __func__);
  1759. return;
  1760. }
  1761. }
  1762. /* Creates our network device */
  1763. static struct hso_device *hso_create_net_device(struct usb_interface *interface)
  1764. {
  1765. int result, i;
  1766. struct net_device *net;
  1767. struct hso_net *hso_net;
  1768. struct hso_device *hso_dev;
  1769. hso_dev = hso_create_device(interface, HSO_INTF_MUX | HSO_PORT_NETWORK);
  1770. if (!hso_dev)
  1771. return NULL;
  1772. /* allocate our network device, then we can put in our private data */
  1773. /* call hso_net_init to do the basic initialization */
  1774. net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
  1775. if (!net) {
  1776. dev_err(&interface->dev, "Unable to create ethernet device\n");
  1777. goto exit;
  1778. }
  1779. hso_net = netdev_priv(net);
  1780. hso_dev->port_data.dev_net = hso_net;
  1781. hso_net->net = net;
  1782. hso_net->parent = hso_dev;
  1783. hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  1784. USB_DIR_IN);
  1785. if (!hso_net->in_endp) {
  1786. dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
  1787. goto exit;
  1788. }
  1789. hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  1790. USB_DIR_OUT);
  1791. if (!hso_net->out_endp) {
  1792. dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
  1793. goto exit;
  1794. }
  1795. SET_NETDEV_DEV(net, &interface->dev);
  1796. /* registering our net device */
  1797. result = register_netdev(net);
  1798. if (result) {
  1799. dev_err(&interface->dev, "Failed to register device\n");
  1800. goto exit;
  1801. }
  1802. /* start allocating */
  1803. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1804. hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
  1805. if (!hso_net->mux_bulk_rx_urb_pool[i]) {
  1806. dev_err(&interface->dev, "Could not allocate rx urb\n");
  1807. goto exit;
  1808. }
  1809. hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
  1810. GFP_KERNEL);
  1811. if (!hso_net->mux_bulk_rx_buf_pool[i]) {
  1812. dev_err(&interface->dev, "Could not allocate rx buf\n");
  1813. goto exit;
  1814. }
  1815. }
  1816. hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  1817. if (!hso_net->mux_bulk_tx_urb) {
  1818. dev_err(&interface->dev, "Could not allocate tx urb\n");
  1819. goto exit;
  1820. }
  1821. hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
  1822. if (!hso_net->mux_bulk_tx_buf) {
  1823. dev_err(&interface->dev, "Could not allocate tx buf\n");
  1824. goto exit;
  1825. }
  1826. add_net_device(hso_dev);
  1827. hso_log_port(hso_dev);
  1828. hso_create_rfkill(hso_dev, interface);
  1829. return hso_dev;
  1830. exit:
  1831. hso_free_net_device(hso_dev);
  1832. return NULL;
  1833. }
  1834. /* Frees an AT channel ( goes for both mux and non-mux ) */
  1835. static void hso_free_serial_device(struct hso_device *hso_dev)
  1836. {
  1837. struct hso_serial *serial = dev2ser(hso_dev);
  1838. if (!serial)
  1839. return;
  1840. set_serial_by_index(serial->minor, NULL);
  1841. hso_serial_common_free(serial);
  1842. if (serial->shared_int) {
  1843. mutex_lock(&serial->shared_int->shared_int_lock);
  1844. if (--serial->shared_int->ref_count == 0)
  1845. hso_free_shared_int(serial->shared_int);
  1846. else
  1847. mutex_unlock(&serial->shared_int->shared_int_lock);
  1848. }
  1849. kfree(serial);
  1850. hso_free_device(hso_dev);
  1851. }
  1852. /* Creates a bulk AT channel */
  1853. static struct hso_device *hso_create_bulk_serial_device(
  1854. struct usb_interface *interface, int port)
  1855. {
  1856. struct hso_device *hso_dev;
  1857. struct hso_serial *serial;
  1858. int num_urbs;
  1859. hso_dev = hso_create_device(interface, port);
  1860. if (!hso_dev)
  1861. return NULL;
  1862. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  1863. if (!serial)
  1864. goto exit;
  1865. serial->parent = hso_dev;
  1866. hso_dev->port_data.dev_serial = serial;
  1867. if (port & HSO_PORT_MODEM)
  1868. num_urbs = 2;
  1869. else
  1870. num_urbs = 1;
  1871. if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
  1872. BULK_URB_TX_SIZE))
  1873. goto exit;
  1874. serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  1875. USB_DIR_IN);
  1876. if (!serial->in_endp) {
  1877. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  1878. goto exit;
  1879. }
  1880. if (!
  1881. (serial->out_endp =
  1882. hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
  1883. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  1884. goto exit;
  1885. }
  1886. serial->write_data = hso_std_serial_write_data;
  1887. /* and record this serial */
  1888. set_serial_by_index(serial->minor, serial);
  1889. /* setup the proc dirs and files if needed */
  1890. hso_log_port(hso_dev);
  1891. /* done, return it */
  1892. return hso_dev;
  1893. exit:
  1894. if (hso_dev && serial)
  1895. hso_serial_common_free(serial);
  1896. kfree(serial);
  1897. hso_free_device(hso_dev);
  1898. return NULL;
  1899. }
  1900. /* Creates a multiplexed AT channel */
  1901. static
  1902. struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
  1903. int port,
  1904. struct hso_shared_int *mux)
  1905. {
  1906. struct hso_device *hso_dev;
  1907. struct hso_serial *serial;
  1908. int port_spec;
  1909. port_spec = HSO_INTF_MUX;
  1910. port_spec &= ~HSO_PORT_MASK;
  1911. port_spec |= hso_mux_to_port(port);
  1912. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
  1913. return NULL;
  1914. hso_dev = hso_create_device(interface, port_spec);
  1915. if (!hso_dev)
  1916. return NULL;
  1917. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  1918. if (!serial)
  1919. goto exit;
  1920. hso_dev->port_data.dev_serial = serial;
  1921. serial->parent = hso_dev;
  1922. if (hso_serial_common_create
  1923. (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
  1924. goto exit;
  1925. serial->tx_data_length--;
  1926. serial->write_data = hso_mux_serial_write_data;
  1927. serial->shared_int = mux;
  1928. mutex_lock(&serial->shared_int->shared_int_lock);
  1929. serial->shared_int->ref_count++;
  1930. mutex_unlock(&serial->shared_int->shared_int_lock);
  1931. /* and record this serial */
  1932. set_serial_by_index(serial->minor, serial);
  1933. /* setup the proc dirs and files if needed */
  1934. hso_log_port(hso_dev);
  1935. /* done, return it */
  1936. return hso_dev;
  1937. exit:
  1938. if (serial) {
  1939. tty_unregister_device(tty_drv, serial->minor);
  1940. kfree(serial);
  1941. }
  1942. if (hso_dev)
  1943. hso_free_device(hso_dev);
  1944. return NULL;
  1945. }
  1946. static void hso_free_shared_int(struct hso_shared_int *mux)
  1947. {
  1948. usb_free_urb(mux->shared_intr_urb);
  1949. kfree(mux->shared_intr_buf);
  1950. mutex_unlock(&mux->shared_int_lock);
  1951. kfree(mux);
  1952. }
  1953. static
  1954. struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
  1955. {
  1956. struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  1957. if (!mux)
  1958. return NULL;
  1959. mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
  1960. USB_DIR_IN);
  1961. if (!mux->intr_endp) {
  1962. dev_err(&interface->dev, "Can't find INT IN endpoint\n");
  1963. goto exit;
  1964. }
  1965. mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  1966. if (!mux->shared_intr_urb) {
  1967. dev_err(&interface->dev, "Could not allocate intr urb?");
  1968. goto exit;
  1969. }
  1970. mux->shared_intr_buf = kzalloc(mux->intr_endp->wMaxPacketSize,
  1971. GFP_KERNEL);
  1972. if (!mux->shared_intr_buf) {
  1973. dev_err(&interface->dev, "Could not allocate intr buf?");
  1974. goto exit;
  1975. }
  1976. mutex_init(&mux->shared_int_lock);
  1977. return mux;
  1978. exit:
  1979. kfree(mux->shared_intr_buf);
  1980. usb_free_urb(mux->shared_intr_urb);
  1981. kfree(mux);
  1982. return NULL;
  1983. }
  1984. /* Gets the port spec for a certain interface */
  1985. static int hso_get_config_data(struct usb_interface *interface)
  1986. {
  1987. struct usb_device *usbdev = interface_to_usbdev(interface);
  1988. u8 config_data[17];
  1989. u32 if_num = interface->altsetting->desc.bInterfaceNumber;
  1990. s32 result;
  1991. if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
  1992. 0x86, 0xC0, 0, 0, config_data, 17,
  1993. USB_CTRL_SET_TIMEOUT) != 0x11) {
  1994. return -EIO;
  1995. }
  1996. switch (config_data[if_num]) {
  1997. case 0x0:
  1998. result = 0;
  1999. break;
  2000. case 0x1:
  2001. result = HSO_PORT_DIAG;
  2002. break;
  2003. case 0x2:
  2004. result = HSO_PORT_GPS;
  2005. break;
  2006. case 0x3:
  2007. result = HSO_PORT_GPS_CONTROL;
  2008. break;
  2009. case 0x4:
  2010. result = HSO_PORT_APP;
  2011. break;
  2012. case 0x5:
  2013. result = HSO_PORT_APP2;
  2014. break;
  2015. case 0x6:
  2016. result = HSO_PORT_CONTROL;
  2017. break;
  2018. case 0x7:
  2019. result = HSO_PORT_NETWORK;
  2020. break;
  2021. case 0x8:
  2022. result = HSO_PORT_MODEM;
  2023. break;
  2024. case 0x9:
  2025. result = HSO_PORT_MSD;
  2026. break;
  2027. case 0xa:
  2028. result = HSO_PORT_PCSC;
  2029. break;
  2030. case 0xb:
  2031. result = HSO_PORT_VOICE;
  2032. break;
  2033. default:
  2034. result = 0;
  2035. }
  2036. if (result)
  2037. result |= HSO_INTF_BULK;
  2038. if (config_data[16] & 0x1)
  2039. result |= HSO_INFO_CRC_BUG;
  2040. return result;
  2041. }
  2042. /* called once for each interface upon device insertion */
  2043. static int hso_probe(struct usb_interface *interface,
  2044. const struct usb_device_id *id)
  2045. {
  2046. int mux, i, if_num, port_spec;
  2047. unsigned char port_mask;
  2048. struct hso_device *hso_dev = NULL;
  2049. struct hso_shared_int *shared_int;
  2050. struct hso_device *tmp_dev = NULL;
  2051. if_num = interface->altsetting->desc.bInterfaceNumber;
  2052. /* Get the interface/port specification from either driver_info or from
  2053. * the device itself */
  2054. if (id->driver_info)
  2055. port_spec = ((u32 *)(id->driver_info))[if_num];
  2056. else
  2057. port_spec = hso_get_config_data(interface);
  2058. if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
  2059. dev_err(&interface->dev, "Not our interface\n");
  2060. return -ENODEV;
  2061. }
  2062. /* Check if we need to switch to alt interfaces prior to port
  2063. * configuration */
  2064. if (interface->num_altsetting > 1)
  2065. usb_set_interface(interface_to_usbdev(interface), if_num, 1);
  2066. interface->needs_remote_wakeup = 1;
  2067. /* Allocate new hso device(s) */
  2068. switch (port_spec & HSO_INTF_MASK) {
  2069. case HSO_INTF_MUX:
  2070. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  2071. /* Create the network device */
  2072. if (!disable_net) {
  2073. hso_dev = hso_create_net_device(interface);
  2074. if (!hso_dev)
  2075. goto exit;
  2076. tmp_dev = hso_dev;
  2077. }
  2078. }
  2079. if (hso_get_mux_ports(interface, &port_mask))
  2080. /* TODO: de-allocate everything */
  2081. goto exit;
  2082. shared_int = hso_create_shared_int(interface);
  2083. if (!shared_int)
  2084. goto exit;
  2085. for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
  2086. if (port_mask & i) {
  2087. hso_dev = hso_create_mux_serial_device(
  2088. interface, i, shared_int);
  2089. if (!hso_dev)
  2090. goto exit;
  2091. }
  2092. }
  2093. if (tmp_dev)
  2094. hso_dev = tmp_dev;
  2095. break;
  2096. case HSO_INTF_BULK:
  2097. /* It's a regular bulk interface */
  2098. if (((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK)
  2099. && !disable_net)
  2100. hso_dev = hso_create_net_device(interface);
  2101. else
  2102. hso_dev =
  2103. hso_create_bulk_serial_device(interface, port_spec);
  2104. if (!hso_dev)
  2105. goto exit;
  2106. break;
  2107. default:
  2108. goto exit;
  2109. }
  2110. usb_driver_claim_interface(&hso_driver, interface, hso_dev);
  2111. /* save our data pointer in this device */
  2112. usb_set_intfdata(interface, hso_dev);
  2113. /* done */
  2114. return 0;
  2115. exit:
  2116. hso_free_interface(interface);
  2117. return -ENODEV;
  2118. }
  2119. /* device removed, cleaning up */
  2120. static void hso_disconnect(struct usb_interface *interface)
  2121. {
  2122. hso_free_interface(interface);
  2123. /* remove reference of our private data */
  2124. usb_set_intfdata(interface, NULL);
  2125. usb_driver_release_interface(&hso_driver, interface);
  2126. }
  2127. static void async_get_intf(struct work_struct *data)
  2128. {
  2129. struct hso_device *hso_dev =
  2130. container_of(data, struct hso_device, async_get_intf);
  2131. usb_autopm_get_interface(hso_dev->interface);
  2132. }
  2133. static void async_put_intf(struct work_struct *data)
  2134. {
  2135. struct hso_device *hso_dev =
  2136. container_of(data, struct hso_device, async_put_intf);
  2137. usb_autopm_put_interface(hso_dev->interface);
  2138. }
  2139. static int hso_get_activity(struct hso_device *hso_dev)
  2140. {
  2141. if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
  2142. if (!hso_dev->is_active) {
  2143. hso_dev->is_active = 1;
  2144. schedule_work(&hso_dev->async_get_intf);
  2145. }
  2146. }
  2147. if (hso_dev->usb->state != USB_STATE_CONFIGURED)
  2148. return -EAGAIN;
  2149. usb_mark_last_busy(hso_dev->usb);
  2150. return 0;
  2151. }
  2152. static int hso_put_activity(struct hso_device *hso_dev)
  2153. {
  2154. if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
  2155. if (hso_dev->is_active) {
  2156. hso_dev->is_active = 0;
  2157. schedule_work(&hso_dev->async_put_intf);
  2158. return -EAGAIN;
  2159. }
  2160. }
  2161. hso_dev->is_active = 0;
  2162. return 0;
  2163. }
  2164. /* called by kernel when we need to suspend device */
  2165. static int hso_suspend(struct usb_interface *iface, pm_message_t message)
  2166. {
  2167. int i, result;
  2168. /* Stop all serial ports */
  2169. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2170. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2171. result = hso_stop_serial_device(serial_table[i]);
  2172. if (result)
  2173. goto out;
  2174. }
  2175. }
  2176. /* Stop all network ports */
  2177. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2178. if (network_table[i] &&
  2179. (network_table[i]->interface == iface)) {
  2180. result = hso_stop_net_device(network_table[i]);
  2181. if (result)
  2182. goto out;
  2183. }
  2184. }
  2185. out:
  2186. return 0;
  2187. }
  2188. /* called by kernel when we need to resume device */
  2189. static int hso_resume(struct usb_interface *iface)
  2190. {
  2191. int i, result = 0;
  2192. struct hso_net *hso_net;
  2193. /* Start all serial ports */
  2194. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2195. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2196. if (dev2ser(serial_table[i])->open_count) {
  2197. result =
  2198. hso_start_serial_device(serial_table[i], GFP_NOIO);
  2199. hso_kick_transmit(dev2ser(serial_table[i]));
  2200. if (result)
  2201. goto out;
  2202. }
  2203. }
  2204. }
  2205. /* Start all network ports */
  2206. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2207. if (network_table[i] &&
  2208. (network_table[i]->interface == iface)) {
  2209. hso_net = dev2net(network_table[i]);
  2210. /* First transmit any lingering data, then restart the
  2211. * device. */
  2212. if (hso_net->skb_tx_buf) {
  2213. dev_dbg(&iface->dev,
  2214. "Transmitting lingering data\n");
  2215. hso_net_start_xmit(hso_net->skb_tx_buf,
  2216. hso_net->net);
  2217. }
  2218. result = hso_start_net_device(network_table[i]);
  2219. if (result)
  2220. goto out;
  2221. }
  2222. }
  2223. out:
  2224. return result;
  2225. }
  2226. static void hso_serial_ref_free(struct kref *ref)
  2227. {
  2228. struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
  2229. hso_free_serial_device(hso_dev);
  2230. }
  2231. static void hso_free_interface(struct usb_interface *interface)
  2232. {
  2233. struct hso_serial *hso_dev;
  2234. int i;
  2235. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2236. if (serial_table[i]
  2237. && (serial_table[i]->interface == interface)) {
  2238. hso_dev = dev2ser(serial_table[i]);
  2239. if (hso_dev->tty)
  2240. tty_hangup(hso_dev->tty);
  2241. mutex_lock(&hso_dev->parent->mutex);
  2242. hso_dev->parent->usb_gone = 1;
  2243. mutex_unlock(&hso_dev->parent->mutex);
  2244. kref_put(&serial_table[i]->ref, hso_serial_ref_free);
  2245. }
  2246. }
  2247. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2248. if (network_table[i]
  2249. && (network_table[i]->interface == interface)) {
  2250. struct rfkill *rfk = dev2net(network_table[i])->rfkill;
  2251. /* hso_stop_net_device doesn't stop the net queue since
  2252. * traffic needs to start it again when suspended */
  2253. netif_stop_queue(dev2net(network_table[i])->net);
  2254. hso_stop_net_device(network_table[i]);
  2255. cancel_work_sync(&network_table[i]->async_put_intf);
  2256. cancel_work_sync(&network_table[i]->async_get_intf);
  2257. if(rfk)
  2258. rfkill_unregister(rfk);
  2259. hso_free_net_device(network_table[i]);
  2260. }
  2261. }
  2262. }
  2263. /* Helper functions */
  2264. /* Get the endpoint ! */
  2265. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  2266. int type, int dir)
  2267. {
  2268. int i;
  2269. struct usb_host_interface *iface = intf->cur_altsetting;
  2270. struct usb_endpoint_descriptor *endp;
  2271. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2272. endp = &iface->endpoint[i].desc;
  2273. if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
  2274. ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == type))
  2275. return endp;
  2276. }
  2277. return NULL;
  2278. }
  2279. /* Get the byte that describes which ports are enabled */
  2280. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
  2281. {
  2282. int i;
  2283. struct usb_host_interface *iface = intf->cur_altsetting;
  2284. if (iface->extralen == 3) {
  2285. *ports = iface->extra[2];
  2286. return 0;
  2287. }
  2288. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2289. if (iface->endpoint[i].extralen == 3) {
  2290. *ports = iface->endpoint[i].extra[2];
  2291. return 0;
  2292. }
  2293. }
  2294. return -1;
  2295. }
  2296. /* interrupt urb needs to be submitted, used for serial read of muxed port */
  2297. static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
  2298. struct usb_device *usb, gfp_t gfp)
  2299. {
  2300. int result;
  2301. usb_fill_int_urb(shared_int->shared_intr_urb, usb,
  2302. usb_rcvintpipe(usb,
  2303. shared_int->intr_endp->bEndpointAddress & 0x7F),
  2304. shared_int->shared_intr_buf,
  2305. shared_int->intr_endp->wMaxPacketSize,
  2306. intr_callback, shared_int,
  2307. shared_int->intr_endp->bInterval);
  2308. result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
  2309. if (result)
  2310. dev_warn(&usb->dev, "%s failed mux_intr_urb %d", __func__,
  2311. result);
  2312. return result;
  2313. }
  2314. /* operations setup of the serial interface */
  2315. static struct tty_operations hso_serial_ops = {
  2316. .open = hso_serial_open,
  2317. .close = hso_serial_close,
  2318. .write = hso_serial_write,
  2319. .write_room = hso_serial_write_room,
  2320. .set_termios = hso_serial_set_termios,
  2321. .chars_in_buffer = hso_serial_chars_in_buffer,
  2322. .tiocmget = hso_serial_tiocmget,
  2323. .tiocmset = hso_serial_tiocmset,
  2324. };
  2325. static struct usb_driver hso_driver = {
  2326. .name = driver_name,
  2327. .probe = hso_probe,
  2328. .disconnect = hso_disconnect,
  2329. .id_table = hso_ids,
  2330. .suspend = hso_suspend,
  2331. .resume = hso_resume,
  2332. .supports_autosuspend = 1,
  2333. };
  2334. static int __init hso_init(void)
  2335. {
  2336. int i;
  2337. int result;
  2338. /* put it in the log */
  2339. printk(KERN_INFO "hso: %s\n", version);
  2340. /* Initialise the serial table semaphore and table */
  2341. spin_lock_init(&serial_table_lock);
  2342. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
  2343. serial_table[i] = NULL;
  2344. /* allocate our driver using the proper amount of supported minors */
  2345. tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
  2346. if (!tty_drv)
  2347. return -ENOMEM;
  2348. /* fill in all needed values */
  2349. tty_drv->magic = TTY_DRIVER_MAGIC;
  2350. tty_drv->owner = THIS_MODULE;
  2351. tty_drv->driver_name = driver_name;
  2352. tty_drv->name = tty_filename;
  2353. /* if major number is provided as parameter, use that one */
  2354. if (tty_major)
  2355. tty_drv->major = tty_major;
  2356. tty_drv->minor_start = 0;
  2357. tty_drv->num = HSO_SERIAL_TTY_MINORS;
  2358. tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
  2359. tty_drv->subtype = SERIAL_TYPE_NORMAL;
  2360. tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  2361. tty_drv->init_termios = tty_std_termios;
  2362. tty_drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  2363. tty_drv->termios = hso_serial_termios;
  2364. tty_drv->termios_locked = hso_serial_termios_locked;
  2365. tty_set_operations(tty_drv, &hso_serial_ops);
  2366. /* register the tty driver */
  2367. result = tty_register_driver(tty_drv);
  2368. if (result) {
  2369. printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
  2370. __func__, result);
  2371. return result;
  2372. }
  2373. /* register this module as an usb driver */
  2374. result = usb_register(&hso_driver);
  2375. if (result) {
  2376. printk(KERN_ERR "Could not register hso driver? error: %d\n",
  2377. result);
  2378. /* cleanup serial interface */
  2379. tty_unregister_driver(tty_drv);
  2380. return result;
  2381. }
  2382. /* done */
  2383. return 0;
  2384. }
  2385. static void __exit hso_exit(void)
  2386. {
  2387. printk(KERN_INFO "hso: unloaded\n");
  2388. tty_unregister_driver(tty_drv);
  2389. /* deregister the usb driver */
  2390. usb_deregister(&hso_driver);
  2391. }
  2392. /* Module definitions */
  2393. module_init(hso_init);
  2394. module_exit(hso_exit);
  2395. MODULE_AUTHOR(MOD_AUTHOR);
  2396. MODULE_DESCRIPTION(MOD_DESCRIPTION);
  2397. MODULE_LICENSE(MOD_LICENSE);
  2398. MODULE_INFO(Version, DRIVER_VERSION);
  2399. /* change the debug level (eg: insmod hso.ko debug=0x04) */
  2400. MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
  2401. module_param(debug, int, S_IRUGO | S_IWUSR);
  2402. /* set the major tty number (eg: insmod hso.ko tty_major=245) */
  2403. MODULE_PARM_DESC(tty_major, "Set the major tty number");
  2404. module_param(tty_major, int, S_IRUGO | S_IWUSR);
  2405. /* disable network interface (eg: insmod hso.ko disable_net=1) */
  2406. MODULE_PARM_DESC(disable_net, "Disable the network interface");
  2407. module_param(disable_net, int, S_IRUGO | S_IWUSR);